blob: 7e960d0a5b929bb6c5dff6ac544b643063bd7b7a [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.
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030017 */
Thierry Reding3d1a51d2011-08-04 04:14:01 -030018
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030019#include <linux/module.h>
20#include <linux/delay.h>
21#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/mm.h>
26#include <linux/ioport.h>
27#include <linux/init.h>
28#include <linux/sched.h>
29#include <linux/random.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030030#include <linux/usb.h>
31#include <linux/videodev2.h>
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -030032#include <media/v4l2-ioctl.h>
Hans Verkuil770056c2012-09-11 11:50:37 -030033#include <media/v4l2-event.h>
Stefan Ringel886a3c02011-05-09 16:53:50 -030034#include <media/tuner.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030035#include <linux/interrupt.h>
36#include <linux/kthread.h>
37#include <linux/highmem.h>
38#include <linux/freezer.h>
39
40#include "tm6000-regs.h"
41#include "tm6000.h"
42
43#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
44
Michel Ludwig95a83822007-07-24 08:06:45 -030045/* Limits minimum and default number of buffers */
46#define TM6000_MIN_BUF 4
47#define TM6000_DEF_BUF 8
48
Julian Scheel16427fa2012-10-04 10:04:28 -030049#define TM6000_NUM_URB_BUF 8
50
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030051#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030052
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030053/* Declare static vars that will be used as parameters */
54static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
55static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030056static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
Mauro Carvalho Chehabb391b0e2012-12-20 15:21:23 -020057static bool keep_urb; /* keep urb buffers allocated */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030058
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030059/* Debug level */
60int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030061EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030062
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030063static struct tm6000_fmt format[] = {
64 {
65 .name = "4:2:2, packed, YVY2",
66 .fourcc = V4L2_PIX_FMT_YUYV,
67 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030068 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030069 .name = "4:2:2, packed, UYVY",
70 .fourcc = V4L2_PIX_FMT_UYVY,
71 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030072 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030073 .name = "A/V + VBI mux packet",
74 .fourcc = V4L2_PIX_FMT_TM6000,
75 .depth = 16,
76 }
77};
78
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030079/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -030080 * DMA and thread functions
81 * ------------------------------------------------------------------
82 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030083
84#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -030085#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030086
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030087#define norm_minw(a) norm_maxw(a)
88#define norm_minh(a) norm_maxh(a)
89
90/*
91 * video-buf generic routine to get the next available buffer
92 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -030093static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030094 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030095{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030096 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030097
98 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030099 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300100 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300101 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300102 }
103
104 *buf = list_entry(dma_q->active.next,
105 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300106}
107
108/*
109 * Announces that a buffer were filled and request the next
110 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300111static inline void buffer_filled(struct tm6000_core *dev,
112 struct tm6000_dmaqueue *dma_q,
113 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300114{
115 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300116 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300117 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300118 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300119 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300120
121 list_del(&buf->vb.queue);
122 wake_up(&buf->vb.done);
123}
124
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300125/*
126 * Identify the tm5600/6000 buffer header type and properly handles
127 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300128static int copy_streams(u8 *data, unsigned long len,
129 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300130{
131 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300132 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300133 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300134 unsigned long header = 0;
135 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300136 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300137 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300138 char *voutp = NULL;
139 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300140
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300141 if (!dev->radio) {
142 /* get video buffer */
143 get_next_buf(dma_q, &vbuf);
144
145 if (!vbuf)
146 return rc;
147 voutp = videobuf_to_vmalloc(&vbuf->vb);
148
149 if (!voutp)
150 return 0;
151 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300152
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300153 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300154 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300155 /* Header */
156 if (dev->isoc_ctl.tmp_buf_len > 0) {
157 /* from last urb or packet */
158 header = dev->isoc_ctl.tmp_buf;
159 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300160 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300161 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300162 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300163 4 - dev->isoc_ctl.tmp_buf_len);
164 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300165 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300166 dev->isoc_ctl.tmp_buf_len = 0;
167 } else {
168 if (ptr + 3 >= endp) {
169 /* have incomplete header */
170 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300171 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300172 dev->isoc_ctl.tmp_buf_len);
173 return rc;
174 }
175 /* Seek for sync */
176 for (; ptr < endp - 3; ptr++) {
177 if (*(ptr + 3) == 0x47)
178 break;
179 }
180 /* Get message header */
181 header = *(unsigned long *)ptr;
182 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300183 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300184
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300185 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300186 size = ((header & 0x7e) << 1);
187 if (size > 0)
188 size -= 4;
189 block = (header >> 7) & 0xf;
190 field = (header >> 11) & 0x1;
191 line = (header >> 12) & 0x1ff;
192 cmd = (header >> 21) & 0x7;
193 /* Validates haeder fields */
194 if (size > TM6000_URB_MSG_LEN)
195 size = TM6000_URB_MSG_LEN;
196 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300197 /*
198 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300199 */
200 switch (cmd) {
201 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300202 if (!dev->radio) {
203 if ((dev->isoc_ctl.vfield != field) &&
204 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300205 /*
206 * Announces that a new buffer
207 * were filled
208 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300209 buffer_filled(dev, dma_q, vbuf);
210 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300211 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300212 get_next_buf(dma_q, &vbuf);
213 if (!vbuf)
214 return rc;
215 voutp = videobuf_to_vmalloc(&vbuf->vb);
216 if (!voutp)
217 return rc;
218 memset(voutp, 0, vbuf->vb.size);
219 }
220 linewidth = vbuf->vb.width << 1;
221 pos = ((line << 1) - field - 1) *
222 linewidth + block * TM6000_URB_MSG_LEN;
223 /* Don't allow to write out of the buffer */
224 if (pos + size > vbuf->vb.size)
225 cmd = TM6000_URB_MSG_ERR;
226 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300227 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300228 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300229 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300230 break;
231 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300232 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300233 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300234 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300235 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300236 } else {
237 /* Continue the last copy */
238 cmd = dev->isoc_ctl.cmd;
239 size = dev->isoc_ctl.size;
240 pos = dev->isoc_ctl.pos;
241 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300242 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300243 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300244 cpysize = (endp - ptr > size) ? size : endp - ptr;
245 if (cpysize) {
246 /* copy data in different buffers */
247 switch (cmd) {
248 case TM6000_URB_MSG_VIDEO:
249 /* Fills video buffer */
250 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300251 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300252 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300253 case TM6000_URB_MSG_AUDIO: {
254 int i;
255 for (i = 0; i < cpysize; i += 2)
256 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300257
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300258 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300259 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300260 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300261 case TM6000_URB_MSG_VBI:
262 /* Need some code to copy vbi buffer */
263 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300264 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300265 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300266 u32 pts;
267 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300268 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
269 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300270 break;
271 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300272 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300273 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300274 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300275 /*
276 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300277 * complete. Preserve the state for a next packet
278 */
279 dev->isoc_ctl.pos = pos + cpysize;
280 dev->isoc_ctl.size = size - cpysize;
281 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300282 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300283 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300284 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300285 } else {
286 dev->isoc_ctl.cmd = 0;
287 ptr += pktsize;
288 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300289 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300290 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300291}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300292
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300293/*
294 * Identify the tm5600/6000 buffer header type and properly handles
295 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300296static int copy_multiplexed(u8 *ptr, unsigned long len,
297 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300298{
299 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300300 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
301 unsigned int pos = dev->isoc_ctl.pos, cpysize;
302 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300303 struct tm6000_buffer *buf;
304 char *outp = NULL;
305
306 get_next_buf(dma_q, &buf);
307 if (buf)
308 outp = videobuf_to_vmalloc(&buf->vb);
309
310 if (!outp)
311 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300312
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300313 while (len > 0) {
314 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300315 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300316 pos += cpysize;
317 ptr += cpysize;
318 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300319 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300320 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300321 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300322 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300323 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300324 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300325 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300326 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300327 outp = videobuf_to_vmalloc(&(buf->vb));
328 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300329 return rc;
330 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300331 }
332 }
333
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300334 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300335 return rc;
336}
337
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300338static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300339 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300340{
341 char *errmsg = "Unknown";
342
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300343 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300344 case -ENOENT:
345 errmsg = "unlinked synchronuously";
346 break;
347 case -ECONNRESET:
348 errmsg = "unlinked asynchronuously";
349 break;
350 case -ENOSR:
351 errmsg = "Buffer error (overrun)";
352 break;
353 case -EPIPE:
354 errmsg = "Stalled (device not responding)";
355 break;
356 case -EOVERFLOW:
357 errmsg = "Babble (bad cable?)";
358 break;
359 case -EPROTO:
360 errmsg = "Bit-stuff error (bad cable?)";
361 break;
362 case -EILSEQ:
363 errmsg = "CRC/Timeout (could be anything)";
364 break;
365 case -ETIME:
366 errmsg = "Device does not respond";
367 break;
368 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300369 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300370 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
371 status, errmsg);
372 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300373 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300374 packet, status, errmsg);
375 }
376}
377
378
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300379/*
380 * Controls the isoc copy of each urb packet
381 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300382static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300383{
384 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300385 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
386 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300387 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300388
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300389 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300390 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300391 return 0;
392 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300393
394 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300395 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300396
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300397 if (status < 0) {
398 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300399 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300400 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300401
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300402 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300403
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300404 if (len > 0) {
405 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300406 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300407 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
408 rc = copy_multiplexed(p, len, urb);
409 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300410 return rc;
411 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300412 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300413 }
414 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300415 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300416 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300417 return rc;
418}
419
420/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300421 * URB control
422 * ------------------------------------------------------------------
423 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300424
425/*
426 * IRQ callback, called by URB callback
427 */
428static void tm6000_irq_callback(struct urb *urb)
429{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300430 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300431 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300432 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300433
Thierry Reding14f09152011-08-04 04:14:06 -0300434 switch (urb->status) {
435 case 0:
436 case -ETIMEDOUT:
437 break;
438
439 case -ECONNRESET:
440 case -ENOENT:
441 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300442 return;
443
Thierry Reding14f09152011-08-04 04:14:06 -0300444 default:
445 tm6000_err("urb completion error %d.\n", urb->status);
446 break;
447 }
448
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300449 spin_lock(&dev->slock);
450 tm6000_isoc_copy(urb);
451 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300452
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300453 /* Reset urb buffers */
454 for (i = 0; i < urb->number_of_packets; i++) {
455 urb->iso_frame_desc[i].status = 0;
456 urb->iso_frame_desc[i].actual_length = 0;
457 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300458
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300459 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
460 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300461 tm6000_err("urb resubmit failed (error=%i)\n",
462 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300463}
464
465/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300466 * Allocate URB buffers
467 */
468static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
469{
470 int num_bufs = TM6000_NUM_URB_BUF;
471 int i;
472
473 if (dev->urb_buffer != NULL)
474 return 0;
475
476 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
477 if (!dev->urb_buffer) {
478 tm6000_err("cannot allocate memory for urb buffers\n");
479 return -ENOMEM;
480 }
481
482 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
483 if (!dev->urb_dma) {
484 tm6000_err("cannot allocate memory for urb dma pointers\n");
485 return -ENOMEM;
486 }
487
488 for (i = 0; i < num_bufs; i++) {
489 dev->urb_buffer[i] = usb_alloc_coherent(
490 dev->udev, dev->urb_size,
491 GFP_KERNEL, &dev->urb_dma[i]);
492 if (!dev->urb_buffer[i]) {
493 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
494 dev->urb_size, i);
495 return -ENOMEM;
496 }
497 memset(dev->urb_buffer[i], 0, dev->urb_size);
498 }
499
500 return 0;
501}
502
503/*
504 * Free URB buffers
505 */
506static int tm6000_free_urb_buffers(struct tm6000_core *dev)
507{
508 int i;
509
510 if (dev->urb_buffer == NULL)
511 return 0;
512
513 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
514 if (dev->urb_buffer[i]) {
515 usb_free_coherent(dev->udev,
516 dev->urb_size,
517 dev->urb_buffer[i],
518 dev->urb_dma[i]);
519 dev->urb_buffer[i] = NULL;
520 }
521 }
522 kfree(dev->urb_buffer);
523 kfree(dev->urb_dma);
524 dev->urb_buffer = NULL;
525 dev->urb_dma = NULL;
526
527 return 0;
528}
529
530/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300531 * Stop and Deallocate URBs
532 */
533static void tm6000_uninit_isoc(struct tm6000_core *dev)
534{
535 struct urb *urb;
536 int i;
537
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300538 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300539 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300540 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300541 if (urb) {
542 usb_kill_urb(urb);
543 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300544 usb_free_urb(urb);
545 dev->isoc_ctl.urb[i] = NULL;
546 }
547 dev->isoc_ctl.transfer_buffer[i] = NULL;
548 }
549
Julian Scheel16427fa2012-10-04 10:04:28 -0300550 if (!keep_urb)
551 tm6000_free_urb_buffers(dev);
552
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300553 kfree(dev->isoc_ctl.urb);
554 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300555
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300556 dev->isoc_ctl.urb = NULL;
557 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300558 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300559}
560
561/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300562 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300563 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300564static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300565{
566 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300567 int i, j, sb_size, pipe, size, max_packets;
568 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300569 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300570
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300571 /* De-allocates all pending stuff */
572 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300573 /* Stop interrupt USB pipe */
574 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300575
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300576 usb_set_interface(dev->udev,
577 dev->isoc_in.bInterfaceNumber,
578 dev->isoc_in.bAlternateSetting);
579
Dmitri Belimov641d2112010-12-22 05:57:46 -0300580 /* Start interrupt USB pipe */
581 tm6000_ir_int_start(dev);
582
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300583 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300584 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300585 USB_ENDPOINT_NUMBER_MASK);
586
587 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
588
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300589 if (size > dev->isoc_in.maxsize)
590 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300591
592 dev->isoc_ctl.max_pkt_size = size;
593
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300594 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300595 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300596 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300597
598 dev->isoc_ctl.num_bufs = num_bufs;
599
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300600 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300601 if (!dev->isoc_ctl.urb) {
602 tm6000_err("cannot alloc memory for usb buffers\n");
603 return -ENOMEM;
604 }
605
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300606 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300607 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300608 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300609 tm6000_err("cannot allocate memory for usbtransfer\n");
610 kfree(dev->isoc_ctl.urb);
611 return -ENOMEM;
612 }
613
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200614 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300615 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300616 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300617
Julian Scheel16427fa2012-10-04 10:04:28 -0300618
Mauro Carvalho Chehab485bdbb2015-06-05 09:33:44 -0300619 if (tm6000_alloc_urb_buffers(dev) < 0) {
Julian Scheel16427fa2012-10-04 10:04:28 -0300620 tm6000_err("cannot allocate memory for urb buffers\n");
621
622 /* call free, as some buffers might have been allocated */
623 tm6000_free_urb_buffers(dev);
624 kfree(dev->isoc_ctl.urb);
625 kfree(dev->isoc_ctl.transfer_buffer);
626 return -ENOMEM;
627 }
628
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300629 /* allocate urbs and transfer buffers */
630 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
631 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
632 if (!urb) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300633 tm6000_uninit_isoc(dev);
Christophe JAILLETf81a18d2017-02-22 19:32:19 -0300634 tm6000_free_urb_buffers(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300635 return -ENOMEM;
636 }
637 dev->isoc_ctl.urb[i] = urb;
638
Julian Scheel16427fa2012-10-04 10:04:28 -0300639 urb->transfer_dma = dev->urb_dma[i];
640 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300641
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300642 usb_fill_bulk_urb(urb, dev->udev, pipe,
643 dev->isoc_ctl.transfer_buffer[i], sb_size,
644 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300645 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300646 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300647 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300648
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300649 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300650 urb->iso_frame_desc[j].offset = size * j;
651 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300652 }
653 }
654
655 return 0;
656}
657
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300658static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300659{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300660 struct tm6000_dmaqueue *dma_q = &dev->vidq;
661 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300662
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300663 dma_q->frame = 0;
664 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300665
666 init_waitqueue_head(&dma_q->wq);
667
668 /* submit urbs and enables IRQ */
669 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300670 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300671 if (rc) {
672 tm6000_err("submit of urb %i failed (error=%i)\n", i,
673 rc);
674 tm6000_uninit_isoc(dev);
675 return rc;
676 }
677 }
678
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300679 return 0;
680}
681
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300682/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300683 * Videobuf operations
684 * ------------------------------------------------------------------
685 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300686
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300687static int
688buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
689{
690 struct tm6000_fh *fh = vq->priv_data;
691
692 *size = fh->fmt->depth * fh->width * fh->height >> 3;
693 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300694 *count = TM6000_DEF_BUF;
695
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300696 if (*count < TM6000_MIN_BUF)
697 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300698
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300699 while (*size * *count > vid_limit * 1024 * 1024)
700 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300701
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300702 return 0;
703}
704
705static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
706{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300707 struct tm6000_fh *fh = vq->priv_data;
708 struct tm6000_core *dev = fh->dev;
709 unsigned long flags;
710
Mauro Carvalho Chehab09f20822015-05-19 08:00:56 -0300711 BUG_ON(in_interrupt());
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300712
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300713 /* We used to wait for the buffer to finish here, but this didn't work
714 because, as we were keeping the state as VIDEOBUF_QUEUED,
715 videobuf_queue_cancel marked it as finished for us.
716 (Also, it could wedge forever if the hardware was misconfigured.)
717
718 This should be safe; by the time we get here, the buffer isn't
719 queued anymore. If we ever start marking the buffers as
720 VIDEOBUF_ACTIVE, it won't be, though.
721 */
722 spin_lock_irqsave(&dev->slock, flags);
723 if (dev->isoc_ctl.buf == buf)
724 dev->isoc_ctl.buf = NULL;
725 spin_unlock_irqrestore(&dev->slock, flags);
726
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300727 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300728 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300729}
730
731static int
732buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
733 enum v4l2_field field)
734{
735 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300736 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300737 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300738 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300739
740 BUG_ON(NULL == fh->fmt);
741
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300742
743 /* FIXME: It assumes depth=2 */
744 /* The only currently supported format is 16 bits/pixel */
745 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
746 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
747 return -EINVAL;
748
749 if (buf->fmt != fh->fmt ||
750 buf->vb.width != fh->width ||
751 buf->vb.height != fh->height ||
752 buf->vb.field != field) {
753 buf->fmt = fh->fmt;
754 buf->vb.width = fh->width;
755 buf->vb.height = fh->height;
756 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300757 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300758 }
759
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300760 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400761 rc = videobuf_iolock(vq, &buf->vb, NULL);
762 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300763 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300764 }
765
Thierry Reding88e834a2011-08-04 04:14:12 -0300766 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300767 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300768 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300769 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300770
771 rc = tm6000_start_thread(dev);
772 if (rc < 0)
773 goto fail;
774
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300775 }
776
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300777 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300778 return 0;
779
780fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300781 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300782 return rc;
783}
784
785static void
786buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
787{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300788 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300789 struct tm6000_fh *fh = vq->priv_data;
790 struct tm6000_core *dev = fh->dev;
791 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300792
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300793 buf->vb.state = VIDEOBUF_QUEUED;
794 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300795}
796
797static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
798{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300799 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300800
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300801 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300802}
803
804static struct videobuf_queue_ops tm6000_video_qops = {
805 .buf_setup = buffer_setup,
806 .buf_prepare = buffer_prepare,
807 .buf_queue = buffer_queue,
808 .buf_release = buffer_release,
809};
810
811/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300812 * IOCTL handling
813 * ------------------------------------------------------------------
814 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300815
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300816static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300817{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300818 /* Is the current fh handling it? if so, that's OK */
819 if (dev->resources == fh && dev->is_res_read)
820 return true;
821
822 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300823}
824
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300825static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300826{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300827 /* Is the current fh handling it? if so, that's OK */
828 if (dev->resources == fh)
829 return true;
830
831 return false;
832}
833
834static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
835 bool is_res_read)
836{
837 /* Is the current fh handling it? if so, that's OK */
838 if (dev->resources == fh && dev->is_res_read == is_res_read)
839 return true;
840
841 /* is it free? */
842 if (dev->resources)
843 return false;
844
845 /* grab it */
846 dev->resources = fh;
847 dev->is_res_read = is_res_read;
848 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
849 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300850}
851
852static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
853{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300854 /* Is the current fh handling it? if so, that's OK */
855 if (dev->resources != fh)
856 return;
857
858 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300859 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300860}
861
862/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300863 * IOCTL vidioc handling
864 * ------------------------------------------------------------------
865 */
866static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300867 struct v4l2_capability *cap)
868{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300869 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300870 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300871
872 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300873 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300874 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Stefan Ringel886a3c02011-05-09 16:53:50 -0300875 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300876 cap->device_caps |= V4L2_CAP_TUNER;
877 if (vdev->vfl_type == VFL_TYPE_GRABBER)
878 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
879 V4L2_CAP_STREAMING |
880 V4L2_CAP_READWRITE;
881 else
882 cap->device_caps |= V4L2_CAP_RADIO;
883 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
884 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300885
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300886 return 0;
887}
888
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300889static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300890 struct v4l2_fmtdesc *f)
891{
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300892 if (f->index >= ARRAY_SIZE(format))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300893 return -EINVAL;
894
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300895 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896 f->pixelformat = format[f->index].fourcc;
897 return 0;
898}
899
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300900static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300901 struct v4l2_format *f)
902{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300903 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300904
905 f->fmt.pix.width = fh->width;
906 f->fmt.pix.height = fh->height;
907 f->fmt.pix.field = fh->vb_vidq.field;
908 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuile6185782012-09-11 11:51:02 -0300909 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300910 f->fmt.pix.bytesperline =
911 (f->fmt.pix.width * fh->fmt->depth) >> 3;
912 f->fmt.pix.sizeimage =
913 f->fmt.pix.height * f->fmt.pix.bytesperline;
914
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300915 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300916}
917
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300918static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300919{
920 unsigned int i;
921
922 for (i = 0; i < ARRAY_SIZE(format); i++)
923 if (format[i].fourcc == fourcc)
924 return format+i;
925 return NULL;
926}
927
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300928static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300929 struct v4l2_format *f)
930{
931 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
932 struct tm6000_fmt *fmt;
933 enum v4l2_field field;
934
935 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
936 if (NULL == fmt) {
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200937 dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
938 f->fmt.pix.pixelformat);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300939 return -EINVAL;
940 }
941
942 field = f->fmt.pix.field;
943
Hans Verkuiled572562013-02-01 09:08:46 -0300944 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300945
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300946 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300947
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300948 f->fmt.pix.width = dev->width;
949 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300950
951 f->fmt.pix.width &= ~0x01;
952
953 f->fmt.pix.field = field;
954
955 f->fmt.pix.bytesperline =
956 (f->fmt.pix.width * fmt->depth) >> 3;
957 f->fmt.pix.sizeimage =
958 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuile6185782012-09-11 11:51:02 -0300959 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300960
961 return 0;
962}
963
964/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300965static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300966 struct v4l2_format *f)
967{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300968 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300969 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300970 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300971 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300972 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300973
974 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
975 fh->width = f->fmt.pix.width;
976 fh->height = f->fmt.pix.height;
977 fh->vb_vidq.field = f->fmt.pix.field;
978 fh->type = f->type;
979
980 dev->fourcc = f->fmt.pix.pixelformat;
981
982 tm6000_set_fourcc_format(dev);
983
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300984 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300985}
986
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300987static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300988 struct v4l2_requestbuffers *p)
989{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300990 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300991
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300992 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300993}
994
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300995static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300996 struct v4l2_buffer *p)
997{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300998 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300999
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001000 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001001}
1002
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001003static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001004{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001005 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001006
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001007 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001008}
1009
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001010static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001011{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001012 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001013
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001014 return videobuf_dqbuf(&fh->vb_vidq, p,
1015 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001016}
1017
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001018static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1019{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001020 struct tm6000_fh *fh = priv;
1021 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001022
1023 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 return -EINVAL;
1025 if (i != fh->type)
1026 return -EINVAL;
1027
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001028 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001029 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001030 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001031}
1032
1033static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1034{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001035 struct tm6000_fh *fh = priv;
1036 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001037
1038 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1039 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001040
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001041 if (i != fh->type)
1042 return -EINVAL;
1043
1044 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001045 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001046
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001047 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001048}
1049
Hans Verkuil314527a2013-03-15 06:10:40 -03001050static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001051{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001052 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001053 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001054 struct tm6000_core *dev = fh->dev;
1055
Hans Verkuil314527a2013-03-15 06:10:40 -03001056 dev->norm = norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001057 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001058
1059 fh->width = dev->width;
1060 fh->height = dev->height;
1061
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001062 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001063 return rc;
1064
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001065 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001066
1067 return 0;
1068}
1069
Hans Verkuil804be2d2013-06-03 05:36:44 -03001070static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1071{
1072 struct tm6000_fh *fh = priv;
1073 struct tm6000_core *dev = fh->dev;
1074
1075 *norm = dev->norm;
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
Hans Verkuil804be2d2013-06-03 05:36:44 -03001137 rc = vidioc_s_std(file, priv, dev->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,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001218 const struct v4l2_tuner *t)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001219{
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,
Hans Verkuilb530a442013-03-19 04:09:26 -03001258 const struct v4l2_frequency *f)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001259{
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,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001296 const struct v4l2_tuner *t)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001297{
1298 struct tm6000_fh *fh = file->private_data;
1299 struct tm6000_core *dev = fh->dev;
1300
1301 if (0 != t->index)
1302 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001303 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001304 return 0;
1305}
1306
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001307/* ------------------------------------------------------------------
1308 File operations for the device
1309 ------------------------------------------------------------------*/
1310
Hans Verkuil14a09da2012-06-24 07:26:46 -03001311static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001312{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001313 struct video_device *vdev = video_devdata(file);
1314 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001315 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001316 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil9f747352013-01-31 08:23:01 -03001317 int rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001318 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001319
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001320 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1321 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001322
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001323 switch (vdev->vfl_type) {
1324 case VFL_TYPE_GRABBER:
1325 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1326 break;
1327 case VFL_TYPE_VBI:
1328 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1329 break;
1330 case VFL_TYPE_RADIO:
1331 radio = 1;
1332 break;
1333 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001334
1335 /* If more than one user, mutex should be added */
1336 dev->users++;
1337
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001338 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1339 video_device_node_name(vdev), v4l2_type_names[type],
1340 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001341
1342 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001343 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001344 if (NULL == fh) {
1345 dev->users--;
1346 return -ENOMEM;
1347 }
1348
Hans Verkuil770056c2012-09-11 11:50:37 -03001349 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001350 file->private_data = fh;
1351 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001352 fh->radio = radio;
1353 dev->radio = radio;
1354 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001355 dev->fourcc = format[0].fourcc;
1356
1357 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001358
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001359 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001360
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001361 fh->width = dev->width;
1362 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001363
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -02001364 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001365 (unsigned long)fh, (unsigned long)dev,
1366 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -02001367 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1368 list_empty(&dev->vidq.queued));
1369 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1370 list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001371
1372 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001373 rc = tm6000_init_analog_mode(dev);
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001374 if (rc < 0) {
1375 v4l2_fh_exit(&fh->fh);
1376 kfree(fh);
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001377 return rc;
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001378 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001379
Hans Verkuil9f747352013-01-31 08:23:01 -03001380 dev->mode = TM6000_MODE_ANALOG;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001381
Thierry Redingaa4a5832011-08-04 04:14:15 -03001382 if (!fh->radio) {
1383 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1384 NULL, &dev->slock,
1385 fh->type,
1386 V4L2_FIELD_INTERLACED,
1387 sizeof(struct tm6000_buffer), fh, &dev->lock);
1388 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001389 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001390 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001391 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1392 tm6000_prepare_isoc(dev);
1393 tm6000_start_thread(dev);
1394 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001395 v4l2_fh_add(&fh->fh);
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{
Hans Verkuil770056c2012-09-11 11:50:37 -03001436 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001437 struct tm6000_fh *fh = file->private_data;
1438 struct tm6000_buffer *buf;
Hans Verkuil770056c2012-09-11 11:50:37 -03001439 int res = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001440
Hans Verkuil770056c2012-09-11 11:50:37 -03001441 if (v4l2_event_pending(&fh->fh))
1442 res = POLLPRI;
1443 else if (req_events & POLLPRI)
1444 poll_wait(file, &fh->fh.wait, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001445 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
Hans Verkuil770056c2012-09-11 11:50:37 -03001446 return res | POLLERR;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001447
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001448 if (!!is_res_streaming(fh->dev, fh))
Hans Verkuil770056c2012-09-11 11:50:37 -03001449 return res | POLLERR;
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001450
1451 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001452 /* streaming capture */
1453 if (list_empty(&fh->vb_vidq.stream))
Hans Verkuil770056c2012-09-11 11:50:37 -03001454 return res | POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001455 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Hans Verkuil82f0efb2013-02-07 07:05:43 -03001456 poll_wait(file, &buf->vb.done, wait);
1457 if (buf->vb.state == VIDEOBUF_DONE ||
1458 buf->vb.state == VIDEOBUF_ERROR)
1459 return res | POLLIN | POLLRDNORM;
Hans Verkuil770056c2012-09-11 11:50:37 -03001460 } else if (req_events & (POLLIN | POLLRDNORM)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001461 /* read() capture */
Hans Verkuil770056c2012-09-11 11:50:37 -03001462 return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001463 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001464 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001465}
1466
Hans Verkuil14a09da2012-06-24 07:26:46 -03001467static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1468{
1469 struct tm6000_fh *fh = file->private_data;
1470 struct tm6000_core *dev = fh->dev;
1471 unsigned int res;
1472
1473 mutex_lock(&dev->lock);
1474 res = __tm6000_poll(file, wait);
1475 mutex_unlock(&dev->lock);
1476 return res;
1477}
1478
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001479static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001480{
1481 struct tm6000_fh *fh = file->private_data;
1482 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001483 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001484
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001485 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1486 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001487
Hans Verkuil14a09da2012-06-24 07:26:46 -03001488 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001489 dev->users--;
1490
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001491 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001492
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001493 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001494 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001495
Stefan Ringel8159c182011-11-28 15:46:18 -03001496 /* Stop interrupt USB pipe */
1497 tm6000_ir_int_stop(dev);
1498
1499 usb_reset_configuration(dev->udev);
1500
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001501 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001502 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001503 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001504 else
1505 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001506 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001507
1508 /* Start interrupt USB pipe */
1509 tm6000_ir_int_start(dev);
1510
Thierry Redingaa4a5832011-08-04 04:14:15 -03001511 if (!fh->radio)
1512 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001513 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001514 v4l2_fh_del(&fh->fh);
1515 v4l2_fh_exit(&fh->fh);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001516 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001517 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001518
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001519 return 0;
1520}
1521
1522static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1523{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001524 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001525 struct tm6000_core *dev = fh->dev;
1526 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001527
Hans Verkuil14a09da2012-06-24 07:26:46 -03001528 if (mutex_lock_interruptible(&dev->lock))
1529 return -ERESTARTSYS;
1530 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1531 mutex_unlock(&dev->lock);
1532 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001533}
1534
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001535static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001536 .owner = THIS_MODULE,
1537 .open = tm6000_open,
1538 .release = tm6000_release,
1539 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1540 .read = tm6000_read,
1541 .poll = tm6000_poll,
1542 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001543};
1544
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001545static const struct v4l2_ioctl_ops video_ioctl_ops = {
1546 .vidioc_querycap = vidioc_querycap,
1547 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1548 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1549 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1550 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1551 .vidioc_s_std = vidioc_s_std,
Hans Verkuil804be2d2013-06-03 05:36:44 -03001552 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001553 .vidioc_enum_input = vidioc_enum_input,
1554 .vidioc_g_input = vidioc_g_input,
1555 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001556 .vidioc_g_tuner = vidioc_g_tuner,
1557 .vidioc_s_tuner = vidioc_s_tuner,
1558 .vidioc_g_frequency = vidioc_g_frequency,
1559 .vidioc_s_frequency = vidioc_s_frequency,
1560 .vidioc_streamon = vidioc_streamon,
1561 .vidioc_streamoff = vidioc_streamoff,
1562 .vidioc_reqbufs = vidioc_reqbufs,
1563 .vidioc_querybuf = vidioc_querybuf,
1564 .vidioc_qbuf = vidioc_qbuf,
1565 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil770056c2012-09-11 11:50:37 -03001566 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1567 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001568};
1569
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001570static struct video_device tm6000_template = {
1571 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001572 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001573 .ioctl_ops = &video_ioctl_ops,
Hans Verkuil65b88c02015-03-09 13:34:10 -03001574 .release = video_device_release_empty,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001575 .tvnorms = TM6000_STD,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001576};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001577
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001578static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001579 .owner = THIS_MODULE,
1580 .open = tm6000_open,
Hans Verkuil52dec542012-09-11 11:53:51 -03001581 .poll = v4l2_ctrl_poll,
Stefan Ringel1f385712011-05-09 16:54:01 -03001582 .release = tm6000_release,
1583 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001584};
1585
1586static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001587 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001588 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001589 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001590 .vidioc_g_frequency = vidioc_g_frequency,
1591 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil770056c2012-09-11 11:50:37 -03001592 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1593 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001594};
1595
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001596static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001597 .name = "tm6000",
1598 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001599 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001600};
1601
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001602/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001603 * Initialization and module stuff
1604 * ------------------------------------------------------------------
1605 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001606
Hans Verkuil65b88c02015-03-09 13:34:10 -03001607static void vdev_init(struct tm6000_core *dev,
1608 struct video_device *vfd,
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001609 const struct video_device
1610 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001611{
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001612 *vfd = *template;
1613 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil65b88c02015-03-09 13:34:10 -03001614 vfd->release = video_device_release_empty;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001615 vfd->lock = &dev->lock;
1616
1617 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1618
1619 video_set_drvdata(vfd, dev);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001620}
1621
1622int tm6000_v4l2_register(struct tm6000_core *dev)
1623{
Hans Verkuil9f747352013-01-31 08:23:01 -03001624 int ret = 0;
1625
1626 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1627 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1628 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1629 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1630 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1631 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1632 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1633 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1634 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1635 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1636 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1637 V4L2_CID_SATURATION, 0, 255, 1, 112);
1638 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1639 V4L2_CID_HUE, -128, 127, 1, 0);
1640 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1641 &dev->radio_ctrl_handler, NULL);
1642
1643 if (dev->radio_ctrl_handler.error)
1644 ret = dev->radio_ctrl_handler.error;
1645 if (!ret && dev->ctrl_handler.error)
1646 ret = dev->ctrl_handler.error;
1647 if (ret)
1648 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001649
Hans Verkuil65b88c02015-03-09 13:34:10 -03001650 vdev_init(dev, &dev->vfd, &tm6000_template, "video");
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001651
Hans Verkuil65b88c02015-03-09 13:34:10 -03001652 dev->vfd.ctrl_handler = &dev->ctrl_handler;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001653
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001654 /* init video dma queues */
1655 INIT_LIST_HEAD(&dev->vidq.active);
1656 INIT_LIST_HEAD(&dev->vidq.queued);
1657
Hans Verkuil65b88c02015-03-09 13:34:10 -03001658 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001659
1660 if (ret < 0) {
1661 printk(KERN_INFO "%s: can't register video device\n",
1662 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001663 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001664 }
1665
1666 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001667 dev->name, video_device_node_name(&dev->vfd));
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001668
Stefan Ringel14169102011-05-09 16:53:49 -03001669 if (dev->caps.has_radio) {
Hans Verkuil65b88c02015-03-09 13:34:10 -03001670 vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
Stefan Ringel14169102011-05-09 16:53:49 -03001671 "radio");
Hans Verkuil65b88c02015-03-09 13:34:10 -03001672 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1673 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Stefan Ringel14169102011-05-09 16:53:49 -03001674 radio_nr);
1675 if (ret < 0) {
1676 printk(KERN_INFO "%s: can't register radio device\n",
1677 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001678 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001679 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001680
Stefan Ringel14169102011-05-09 16:53:49 -03001681 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001682 dev->name, video_device_node_name(&dev->radio_dev));
Stefan Ringel14169102011-05-09 16:53:49 -03001683 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001684
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001685 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001686 return ret;
Hans Verkuil9f747352013-01-31 08:23:01 -03001687
1688unreg_video:
Hans Verkuil65b88c02015-03-09 13:34:10 -03001689 video_unregister_device(&dev->vfd);
Hans Verkuil9f747352013-01-31 08:23:01 -03001690free_ctrl:
1691 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1692 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1693 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001694}
1695
1696int tm6000_v4l2_unregister(struct tm6000_core *dev)
1697{
Hans Verkuil65b88c02015-03-09 13:34:10 -03001698 video_unregister_device(&dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001699
Julian Scheel16427fa2012-10-04 10:04:28 -03001700 /* if URB buffers are still allocated free them now */
1701 tm6000_free_urb_buffers(dev);
1702
Hans Verkuil65b88c02015-03-09 13:34:10 -03001703 video_unregister_device(&dev->radio_dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001704 return 0;
1705}
1706
1707int tm6000_v4l2_exit(void)
1708{
1709 return 0;
1710}
1711
1712module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001713MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001714
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001715module_param_named(debug, tm6000_debug, int, 0444);
1716MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001717
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001718module_param(vid_limit, int, 0644);
1719MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001720
Julian Scheel16427fa2012-10-04 10:04:28 -03001721module_param(keep_urb, bool, 0);
1722MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");