blob: 96055de6e8ce2815cde4c0087c651f1a0857dd01 [file] [log] [blame]
Mauro Carvalho Chehab75d1e3e2017-12-01 08:47:10 -05001// SPDX-License-Identifier: GPL-2.0
2// tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3//
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04004// Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehab75d1e3e2017-12-01 08:47:10 -05005//
6// Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7// - Fixed module load/unload
Thierry Reding3d1a51d2011-08-04 04:14:01 -03008
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03009#include <linux/module.h>
10#include <linux/delay.h>
11#include <linux/errno.h>
12#include <linux/fs.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/mm.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/sched.h>
19#include <linux/random.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030020#include <linux/usb.h>
21#include <linux/videodev2.h>
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -030022#include <media/v4l2-ioctl.h>
Hans Verkuil770056c2012-09-11 11:50:37 -030023#include <media/v4l2-event.h>
Stefan Ringel886a3c02011-05-09 16:53:50 -030024#include <media/tuner.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030025#include <linux/interrupt.h>
26#include <linux/kthread.h>
27#include <linux/highmem.h>
28#include <linux/freezer.h>
29
30#include "tm6000-regs.h"
31#include "tm6000.h"
32
33#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
34
Michel Ludwig95a83822007-07-24 08:06:45 -030035/* Limits minimum and default number of buffers */
36#define TM6000_MIN_BUF 4
37#define TM6000_DEF_BUF 8
38
Julian Scheel16427fa2012-10-04 10:04:28 -030039#define TM6000_NUM_URB_BUF 8
40
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030041#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030042
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030043/* Declare static vars that will be used as parameters */
44static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
45static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030046static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
Mauro Carvalho Chehabb391b0e2012-12-20 15:21:23 -020047static bool keep_urb; /* keep urb buffers allocated */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030048
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030049/* Debug level */
50int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030051EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030052
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030053static struct tm6000_fmt format[] = {
54 {
55 .name = "4:2:2, packed, YVY2",
56 .fourcc = V4L2_PIX_FMT_YUYV,
57 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030058 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030059 .name = "4:2:2, packed, UYVY",
60 .fourcc = V4L2_PIX_FMT_UYVY,
61 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030062 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030063 .name = "A/V + VBI mux packet",
64 .fourcc = V4L2_PIX_FMT_TM6000,
65 .depth = 16,
66 }
67};
68
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030069/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -030070 * DMA and thread functions
71 * ------------------------------------------------------------------
72 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030073
74#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -030075#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030076
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030077#define norm_minw(a) norm_maxw(a)
78#define norm_minh(a) norm_maxh(a)
79
80/*
81 * video-buf generic routine to get the next available buffer
82 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -030083static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030084 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030085{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030086 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030087
88 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030089 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -030090 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -030091 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030092 }
93
94 *buf = list_entry(dma_q->active.next,
95 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030096}
97
98/*
99 * Announces that a buffer were filled and request the next
100 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300101static inline void buffer_filled(struct tm6000_core *dev,
102 struct tm6000_dmaqueue *dma_q,
103 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300104{
105 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300106 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300107 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300108 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300109 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300110
111 list_del(&buf->vb.queue);
112 wake_up(&buf->vb.done);
113}
114
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300115/*
116 * Identify the tm5600/6000 buffer header type and properly handles
117 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300118static int copy_streams(u8 *data, unsigned long len,
119 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300120{
121 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300122 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300123 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300124 unsigned long header = 0;
125 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300126 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300127 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300128 char *voutp = NULL;
129 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300130
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300131 if (!dev->radio) {
132 /* get video buffer */
133 get_next_buf(dma_q, &vbuf);
134
135 if (!vbuf)
136 return rc;
137 voutp = videobuf_to_vmalloc(&vbuf->vb);
138
139 if (!voutp)
140 return 0;
141 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300142
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300143 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300144 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300145 /* Header */
146 if (dev->isoc_ctl.tmp_buf_len > 0) {
147 /* from last urb or packet */
148 header = dev->isoc_ctl.tmp_buf;
149 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300150 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300151 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300152 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300153 4 - dev->isoc_ctl.tmp_buf_len);
154 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300155 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300156 dev->isoc_ctl.tmp_buf_len = 0;
157 } else {
158 if (ptr + 3 >= endp) {
159 /* have incomplete header */
160 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300161 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300162 dev->isoc_ctl.tmp_buf_len);
163 return rc;
164 }
165 /* Seek for sync */
166 for (; ptr < endp - 3; ptr++) {
167 if (*(ptr + 3) == 0x47)
168 break;
169 }
170 /* Get message header */
171 header = *(unsigned long *)ptr;
172 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300173 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300174
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300175 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300176 size = ((header & 0x7e) << 1);
177 if (size > 0)
178 size -= 4;
179 block = (header >> 7) & 0xf;
180 field = (header >> 11) & 0x1;
181 line = (header >> 12) & 0x1ff;
182 cmd = (header >> 21) & 0x7;
183 /* Validates haeder fields */
184 if (size > TM6000_URB_MSG_LEN)
185 size = TM6000_URB_MSG_LEN;
186 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300187 /*
188 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300189 */
190 switch (cmd) {
191 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300192 if (!dev->radio) {
193 if ((dev->isoc_ctl.vfield != field) &&
194 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300195 /*
196 * Announces that a new buffer
197 * were filled
198 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300199 buffer_filled(dev, dma_q, vbuf);
200 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300201 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300202 get_next_buf(dma_q, &vbuf);
203 if (!vbuf)
204 return rc;
205 voutp = videobuf_to_vmalloc(&vbuf->vb);
206 if (!voutp)
207 return rc;
208 memset(voutp, 0, vbuf->vb.size);
209 }
210 linewidth = vbuf->vb.width << 1;
211 pos = ((line << 1) - field - 1) *
212 linewidth + block * TM6000_URB_MSG_LEN;
213 /* Don't allow to write out of the buffer */
214 if (pos + size > vbuf->vb.size)
215 cmd = TM6000_URB_MSG_ERR;
216 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300217 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300218 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300219 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300220 break;
221 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300222 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300223 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300224 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300225 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300226 } else {
227 /* Continue the last copy */
228 cmd = dev->isoc_ctl.cmd;
229 size = dev->isoc_ctl.size;
230 pos = dev->isoc_ctl.pos;
231 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300232 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300233 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300234 cpysize = (endp - ptr > size) ? size : endp - ptr;
235 if (cpysize) {
236 /* copy data in different buffers */
237 switch (cmd) {
238 case TM6000_URB_MSG_VIDEO:
239 /* Fills video buffer */
240 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300241 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300242 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300243 case TM6000_URB_MSG_AUDIO: {
244 int i;
245 for (i = 0; i < cpysize; i += 2)
246 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300247
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300248 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300249 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300250 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300251 case TM6000_URB_MSG_VBI:
252 /* Need some code to copy vbi buffer */
253 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300254 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300255 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300256 u32 pts;
257 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300258 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
259 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300260 break;
261 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300262 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300263 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300264 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300265 /*
266 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300267 * complete. Preserve the state for a next packet
268 */
269 dev->isoc_ctl.pos = pos + cpysize;
270 dev->isoc_ctl.size = size - cpysize;
271 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300272 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300273 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300274 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300275 } else {
276 dev->isoc_ctl.cmd = 0;
277 ptr += pktsize;
278 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300279 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300280 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300281}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300282
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300283/*
284 * Identify the tm5600/6000 buffer header type and properly handles
285 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300286static int copy_multiplexed(u8 *ptr, unsigned long len,
287 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300288{
289 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300290 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
291 unsigned int pos = dev->isoc_ctl.pos, cpysize;
292 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300293 struct tm6000_buffer *buf;
294 char *outp = NULL;
295
296 get_next_buf(dma_q, &buf);
297 if (buf)
298 outp = videobuf_to_vmalloc(&buf->vb);
299
300 if (!outp)
301 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300302
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300303 while (len > 0) {
304 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300305 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300306 pos += cpysize;
307 ptr += cpysize;
308 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300309 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300310 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300311 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300312 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300313 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300314 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300315 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300316 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300317 outp = videobuf_to_vmalloc(&(buf->vb));
318 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300319 return rc;
320 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300321 }
322 }
323
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300324 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300325 return rc;
326}
327
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300328static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300329 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300330{
331 char *errmsg = "Unknown";
332
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300333 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300334 case -ENOENT:
Colin Ian Kingb436e262017-11-02 06:11:53 -0400335 errmsg = "unlinked synchronously";
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300336 break;
337 case -ECONNRESET:
Colin Ian Kingb436e262017-11-02 06:11:53 -0400338 errmsg = "unlinked asynchronously";
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300339 break;
340 case -ENOSR:
341 errmsg = "Buffer error (overrun)";
342 break;
343 case -EPIPE:
344 errmsg = "Stalled (device not responding)";
345 break;
346 case -EOVERFLOW:
347 errmsg = "Babble (bad cable?)";
348 break;
349 case -EPROTO:
350 errmsg = "Bit-stuff error (bad cable?)";
351 break;
352 case -EILSEQ:
353 errmsg = "CRC/Timeout (could be anything)";
354 break;
355 case -ETIME:
356 errmsg = "Device does not respond";
357 break;
358 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300359 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300360 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
361 status, errmsg);
362 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300363 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300364 packet, status, errmsg);
365 }
366}
367
368
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300369/*
370 * Controls the isoc copy of each urb packet
371 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300372static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300373{
374 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300375 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
376 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300377 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300378
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300379 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300380 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300381 return 0;
382 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300383
384 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300385 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300386
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300387 if (status < 0) {
388 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300389 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300390 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300391
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300392 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300393
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300394 if (len > 0) {
395 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300396 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300397 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
398 rc = copy_multiplexed(p, len, urb);
399 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300400 return rc;
401 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300402 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300403 }
404 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300405 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300406 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300407 return rc;
408}
409
410/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300411 * URB control
412 * ------------------------------------------------------------------
413 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300414
415/*
416 * IRQ callback, called by URB callback
417 */
418static void tm6000_irq_callback(struct urb *urb)
419{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300420 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300421 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300422 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300423
Thierry Reding14f09152011-08-04 04:14:06 -0300424 switch (urb->status) {
425 case 0:
426 case -ETIMEDOUT:
427 break;
428
429 case -ECONNRESET:
430 case -ENOENT:
431 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300432 return;
433
Thierry Reding14f09152011-08-04 04:14:06 -0300434 default:
435 tm6000_err("urb completion error %d.\n", urb->status);
436 break;
437 }
438
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300439 spin_lock(&dev->slock);
440 tm6000_isoc_copy(urb);
441 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300442
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300443 /* Reset urb buffers */
444 for (i = 0; i < urb->number_of_packets; i++) {
445 urb->iso_frame_desc[i].status = 0;
446 urb->iso_frame_desc[i].actual_length = 0;
447 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300448
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300449 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
450 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300451 tm6000_err("urb resubmit failed (error=%i)\n",
452 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300453}
454
455/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300456 * Allocate URB buffers
457 */
458static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
459{
460 int num_bufs = TM6000_NUM_URB_BUF;
461 int i;
462
Markus Elfring7e11d502017-09-14 14:34:39 +0200463 if (dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300464 return 0;
465
Kees Cook6da2ec52018-06-12 13:55:00 -0700466 dev->urb_buffer = kmalloc_array(num_bufs, sizeof(void *), GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200467 if (!dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300468 return -ENOMEM;
Julian Scheel16427fa2012-10-04 10:04:28 -0300469
Kees Cook6da2ec52018-06-12 13:55:00 -0700470 dev->urb_dma = kmalloc_array(num_bufs, sizeof(dma_addr_t *),
471 GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200472 if (!dev->urb_dma)
Julian Scheel16427fa2012-10-04 10:04:28 -0300473 return -ENOMEM;
Julian Scheel16427fa2012-10-04 10:04:28 -0300474
475 for (i = 0; i < num_bufs; i++) {
476 dev->urb_buffer[i] = usb_alloc_coherent(
477 dev->udev, dev->urb_size,
478 GFP_KERNEL, &dev->urb_dma[i]);
479 if (!dev->urb_buffer[i]) {
480 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
481 dev->urb_size, i);
482 return -ENOMEM;
483 }
484 memset(dev->urb_buffer[i], 0, dev->urb_size);
485 }
486
487 return 0;
488}
489
490/*
491 * Free URB buffers
492 */
493static int tm6000_free_urb_buffers(struct tm6000_core *dev)
494{
495 int i;
496
Markus Elfring7e11d502017-09-14 14:34:39 +0200497 if (!dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300498 return 0;
499
500 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
501 if (dev->urb_buffer[i]) {
502 usb_free_coherent(dev->udev,
503 dev->urb_size,
504 dev->urb_buffer[i],
505 dev->urb_dma[i]);
506 dev->urb_buffer[i] = NULL;
507 }
508 }
509 kfree(dev->urb_buffer);
510 kfree(dev->urb_dma);
511 dev->urb_buffer = NULL;
512 dev->urb_dma = NULL;
513
514 return 0;
515}
516
517/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300518 * Stop and Deallocate URBs
519 */
520static void tm6000_uninit_isoc(struct tm6000_core *dev)
521{
522 struct urb *urb;
523 int i;
524
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300525 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300526 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300527 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300528 if (urb) {
529 usb_kill_urb(urb);
530 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300531 usb_free_urb(urb);
532 dev->isoc_ctl.urb[i] = NULL;
533 }
534 dev->isoc_ctl.transfer_buffer[i] = NULL;
535 }
536
Julian Scheel16427fa2012-10-04 10:04:28 -0300537 if (!keep_urb)
538 tm6000_free_urb_buffers(dev);
539
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300540 kfree(dev->isoc_ctl.urb);
541 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300542
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300543 dev->isoc_ctl.urb = NULL;
544 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300545 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300546}
547
548/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300549 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300550 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300551static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300552{
553 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300554 int i, j, sb_size, pipe, size, max_packets;
555 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300556 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300557
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300558 /* De-allocates all pending stuff */
559 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300560 /* Stop interrupt USB pipe */
561 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300562
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300563 usb_set_interface(dev->udev,
564 dev->isoc_in.bInterfaceNumber,
565 dev->isoc_in.bAlternateSetting);
566
Dmitri Belimov641d2112010-12-22 05:57:46 -0300567 /* Start interrupt USB pipe */
568 tm6000_ir_int_start(dev);
569
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300570 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300571 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300572 USB_ENDPOINT_NUMBER_MASK);
573
574 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
575
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300576 if (size > dev->isoc_in.maxsize)
577 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300578
579 dev->isoc_ctl.max_pkt_size = size;
580
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300581 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300582 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300583 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300584
585 dev->isoc_ctl.num_bufs = num_bufs;
586
Kees Cook6da2ec52018-06-12 13:55:00 -0700587 dev->isoc_ctl.urb = kmalloc_array(num_bufs, sizeof(void *),
588 GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200589 if (!dev->isoc_ctl.urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300590 return -ENOMEM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300591
Kees Cook6da2ec52018-06-12 13:55:00 -0700592 dev->isoc_ctl.transfer_buffer = kmalloc_array(num_bufs,
593 sizeof(void *),
594 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300595 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300596 kfree(dev->isoc_ctl.urb);
597 return -ENOMEM;
598 }
599
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200600 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 -0300601 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300602 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300603
Julian Scheel16427fa2012-10-04 10:04:28 -0300604
Mauro Carvalho Chehab485bdbb2015-06-05 09:33:44 -0300605 if (tm6000_alloc_urb_buffers(dev) < 0) {
Julian Scheel16427fa2012-10-04 10:04:28 -0300606 tm6000_err("cannot allocate memory for urb buffers\n");
607
608 /* call free, as some buffers might have been allocated */
609 tm6000_free_urb_buffers(dev);
610 kfree(dev->isoc_ctl.urb);
611 kfree(dev->isoc_ctl.transfer_buffer);
612 return -ENOMEM;
613 }
614
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300615 /* allocate urbs and transfer buffers */
616 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
617 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
618 if (!urb) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300619 tm6000_uninit_isoc(dev);
Christophe JAILLETf81a18d2017-02-22 19:32:19 -0300620 tm6000_free_urb_buffers(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300621 return -ENOMEM;
622 }
623 dev->isoc_ctl.urb[i] = urb;
624
Julian Scheel16427fa2012-10-04 10:04:28 -0300625 urb->transfer_dma = dev->urb_dma[i];
626 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300627
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300628 usb_fill_bulk_urb(urb, dev->udev, pipe,
629 dev->isoc_ctl.transfer_buffer[i], sb_size,
630 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300631 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300632 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300633 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300634
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300635 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300636 urb->iso_frame_desc[j].offset = size * j;
637 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300638 }
639 }
640
641 return 0;
642}
643
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300644static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300645{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300646 struct tm6000_dmaqueue *dma_q = &dev->vidq;
647 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300648
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300649 dma_q->frame = 0;
650 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300651
652 init_waitqueue_head(&dma_q->wq);
653
654 /* submit urbs and enables IRQ */
655 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300656 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300657 if (rc) {
658 tm6000_err("submit of urb %i failed (error=%i)\n", i,
659 rc);
660 tm6000_uninit_isoc(dev);
661 return rc;
662 }
663 }
664
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300665 return 0;
666}
667
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300668/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300669 * Videobuf operations
670 * ------------------------------------------------------------------
671 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300672
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300673static int
674buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
675{
676 struct tm6000_fh *fh = vq->priv_data;
677
678 *size = fh->fmt->depth * fh->width * fh->height >> 3;
679 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300680 *count = TM6000_DEF_BUF;
681
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300682 if (*count < TM6000_MIN_BUF)
683 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300684
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300685 while (*size * *count > vid_limit * 1024 * 1024)
686 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300687
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300688 return 0;
689}
690
691static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
692{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300693 struct tm6000_fh *fh = vq->priv_data;
694 struct tm6000_core *dev = fh->dev;
695 unsigned long flags;
696
Mauro Carvalho Chehab09f20822015-05-19 08:00:56 -0300697 BUG_ON(in_interrupt());
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300698
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300699 /* We used to wait for the buffer to finish here, but this didn't work
700 because, as we were keeping the state as VIDEOBUF_QUEUED,
701 videobuf_queue_cancel marked it as finished for us.
702 (Also, it could wedge forever if the hardware was misconfigured.)
703
704 This should be safe; by the time we get here, the buffer isn't
705 queued anymore. If we ever start marking the buffers as
706 VIDEOBUF_ACTIVE, it won't be, though.
707 */
708 spin_lock_irqsave(&dev->slock, flags);
709 if (dev->isoc_ctl.buf == buf)
710 dev->isoc_ctl.buf = NULL;
711 spin_unlock_irqrestore(&dev->slock, flags);
712
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300713 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300714 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300715}
716
717static int
718buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
719 enum v4l2_field field)
720{
721 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300722 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300723 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300724 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300725
726 BUG_ON(NULL == fh->fmt);
727
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300728
729 /* FIXME: It assumes depth=2 */
730 /* The only currently supported format is 16 bits/pixel */
731 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
732 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
733 return -EINVAL;
734
735 if (buf->fmt != fh->fmt ||
736 buf->vb.width != fh->width ||
737 buf->vb.height != fh->height ||
738 buf->vb.field != field) {
739 buf->fmt = fh->fmt;
740 buf->vb.width = fh->width;
741 buf->vb.height = fh->height;
742 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300743 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300744 }
745
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300746 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400747 rc = videobuf_iolock(vq, &buf->vb, NULL);
748 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300749 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300750 }
751
Thierry Reding88e834a2011-08-04 04:14:12 -0300752 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300753 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300754 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300755 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300756
757 rc = tm6000_start_thread(dev);
758 if (rc < 0)
759 goto fail;
760
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300761 }
762
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300763 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300764 return 0;
765
766fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300767 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300768 return rc;
769}
770
771static void
772buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
773{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300774 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300775 struct tm6000_fh *fh = vq->priv_data;
776 struct tm6000_core *dev = fh->dev;
777 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300778
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300779 buf->vb.state = VIDEOBUF_QUEUED;
780 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300781}
782
783static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
784{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300785 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300786
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300787 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300788}
789
Julia Lawall63f2ec62017-08-04 08:09:47 -0400790static const struct videobuf_queue_ops tm6000_video_qops = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300791 .buf_setup = buffer_setup,
792 .buf_prepare = buffer_prepare,
793 .buf_queue = buffer_queue,
794 .buf_release = buffer_release,
795};
796
797/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300798 * IOCTL handling
799 * ------------------------------------------------------------------
800 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300801
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300802static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300803{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300804 /* Is the current fh handling it? if so, that's OK */
805 if (dev->resources == fh && dev->is_res_read)
806 return true;
807
808 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300809}
810
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300811static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300812{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300813 /* Is the current fh handling it? if so, that's OK */
814 if (dev->resources == fh)
815 return true;
816
817 return false;
818}
819
820static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
821 bool is_res_read)
822{
823 /* Is the current fh handling it? if so, that's OK */
824 if (dev->resources == fh && dev->is_res_read == is_res_read)
825 return true;
826
827 /* is it free? */
828 if (dev->resources)
829 return false;
830
831 /* grab it */
832 dev->resources = fh;
833 dev->is_res_read = is_res_read;
834 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
835 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300836}
837
838static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
839{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300840 /* Is the current fh handling it? if so, that's OK */
841 if (dev->resources != fh)
842 return;
843
844 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300845 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300846}
847
848/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300849 * IOCTL vidioc handling
850 * ------------------------------------------------------------------
851 */
852static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300853 struct v4l2_capability *cap)
854{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300855 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300856 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300857
858 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300859 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300860 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Stefan Ringel886a3c02011-05-09 16:53:50 -0300861 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300862 cap->device_caps |= V4L2_CAP_TUNER;
863 if (vdev->vfl_type == VFL_TYPE_GRABBER)
864 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
865 V4L2_CAP_STREAMING |
866 V4L2_CAP_READWRITE;
867 else
868 cap->device_caps |= V4L2_CAP_RADIO;
869 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
870 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300871
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300872 return 0;
873}
874
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300875static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300876 struct v4l2_fmtdesc *f)
877{
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300878 if (f->index >= ARRAY_SIZE(format))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300879 return -EINVAL;
880
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300881 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300882 f->pixelformat = format[f->index].fourcc;
883 return 0;
884}
885
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300886static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300887 struct v4l2_format *f)
888{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300889 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300890
891 f->fmt.pix.width = fh->width;
892 f->fmt.pix.height = fh->height;
893 f->fmt.pix.field = fh->vb_vidq.field;
894 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuile6185782012-09-11 11:51:02 -0300895 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896 f->fmt.pix.bytesperline =
897 (f->fmt.pix.width * fh->fmt->depth) >> 3;
898 f->fmt.pix.sizeimage =
899 f->fmt.pix.height * f->fmt.pix.bytesperline;
900
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300901 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300902}
903
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300904static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300905{
906 unsigned int i;
907
908 for (i = 0; i < ARRAY_SIZE(format); i++)
909 if (format[i].fourcc == fourcc)
910 return format+i;
911 return NULL;
912}
913
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300914static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300915 struct v4l2_format *f)
916{
917 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
918 struct tm6000_fmt *fmt;
919 enum v4l2_field field;
920
921 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
922 if (NULL == fmt) {
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200923 dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
924 f->fmt.pix.pixelformat);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300925 return -EINVAL;
926 }
927
928 field = f->fmt.pix.field;
929
Hans Verkuiled572562013-02-01 09:08:46 -0300930 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300931
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300932 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300933
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300934 f->fmt.pix.width = dev->width;
935 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300936
937 f->fmt.pix.width &= ~0x01;
938
939 f->fmt.pix.field = field;
940
941 f->fmt.pix.bytesperline =
942 (f->fmt.pix.width * fmt->depth) >> 3;
943 f->fmt.pix.sizeimage =
944 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuile6185782012-09-11 11:51:02 -0300945 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300946
947 return 0;
948}
949
950/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300951static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300952 struct v4l2_format *f)
953{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300954 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300955 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300956 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300957 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300958 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300959
960 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
961 fh->width = f->fmt.pix.width;
962 fh->height = f->fmt.pix.height;
963 fh->vb_vidq.field = f->fmt.pix.field;
964 fh->type = f->type;
965
966 dev->fourcc = f->fmt.pix.pixelformat;
967
968 tm6000_set_fourcc_format(dev);
969
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300970 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300971}
972
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300973static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300974 struct v4l2_requestbuffers *p)
975{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300976 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300977
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300978 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300979}
980
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300981static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300982 struct v4l2_buffer *p)
983{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300984 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300985
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300986 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300987}
988
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300989static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300990{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300991 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300992
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300993 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300994}
995
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300996static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300997{
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_dqbuf(&fh->vb_vidq, p,
1001 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001002}
1003
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001004static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1005{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001006 struct tm6000_fh *fh = priv;
1007 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001008
1009 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1010 return -EINVAL;
1011 if (i != fh->type)
1012 return -EINVAL;
1013
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001014 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001015 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001016 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001017}
1018
1019static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1020{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001021 struct tm6000_fh *fh = priv;
1022 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001023
1024 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1025 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001026
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001027 if (i != fh->type)
1028 return -EINVAL;
1029
1030 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001031 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001032
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001033 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001034}
1035
Hans Verkuil314527a2013-03-15 06:10:40 -03001036static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001037{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001038 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001039 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001040 struct tm6000_core *dev = fh->dev;
1041
Hans Verkuil314527a2013-03-15 06:10:40 -03001042 dev->norm = norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001043 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001044
1045 fh->width = dev->width;
1046 fh->height = dev->height;
1047
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001048 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001049 return rc;
1050
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001051 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001052
1053 return 0;
1054}
1055
Hans Verkuil804be2d2013-06-03 05:36:44 -03001056static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1057{
1058 struct tm6000_fh *fh = priv;
1059 struct tm6000_core *dev = fh->dev;
1060
1061 *norm = dev->norm;
1062 return 0;
1063}
1064
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001065static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001066 [TM6000_INPUT_TV] = "Television",
1067 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1068 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1069 [TM6000_INPUT_SVIDEO] = "S-Video",
1070};
1071
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001072static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001073 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001074{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001075 struct tm6000_fh *fh = priv;
1076 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001077 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001078
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001079 n = i->index;
1080 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001081 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001082
1083 if (!dev->vinput[n].type)
1084 return -EINVAL;
1085
1086 i->index = n;
1087
1088 if (dev->vinput[n].type == TM6000_INPUT_TV)
1089 i->type = V4L2_INPUT_TYPE_TUNER;
1090 else
1091 i->type = V4L2_INPUT_TYPE_CAMERA;
1092
1093 strcpy(i->name, iname[dev->vinput[n].type]);
1094
1095 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001096
1097 return 0;
1098}
1099
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001100static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001101{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001102 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001103 struct tm6000_core *dev = fh->dev;
1104
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001105 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001106
1107 return 0;
1108}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001109
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001110static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001111{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001112 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001113 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001114 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001115
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001116 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001117 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001118 if (!dev->vinput[i].type)
1119 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001120
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001121 dev->input = i;
1122
Hans Verkuil804be2d2013-06-03 05:36:44 -03001123 rc = vidioc_s_std(file, priv, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001124
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001125 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001126}
1127
Stefan Ringel886a3c02011-05-09 16:53:50 -03001128/* --- controls ---------------------------------------------- */
Hans Verkuil9f747352013-01-31 08:23:01 -03001129
1130static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001131{
Hans Verkuil9f747352013-01-31 08:23:01 -03001132 struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1133 u8 val = ctrl->val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001134
1135 switch (ctrl->id) {
1136 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001137 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001138 return 0;
1139 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001140 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001141 return 0;
1142 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001143 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001144 return 0;
1145 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001146 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001147 return 0;
Hans Verkuil9f747352013-01-31 08:23:01 -03001148 }
1149 return -EINVAL;
1150}
1151
1152static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1153 .s_ctrl = tm6000_s_ctrl,
1154};
1155
1156static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1157{
1158 struct tm6000_core *dev = container_of(ctrl->handler,
1159 struct tm6000_core, radio_ctrl_handler);
1160 u8 val = ctrl->val;
1161
1162 switch (ctrl->id) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001163 case V4L2_CID_AUDIO_MUTE:
1164 dev->ctl_mute = val;
1165 tm6000_tvaudio_set_mute(dev, val);
1166 return 0;
1167 case V4L2_CID_AUDIO_VOLUME:
1168 dev->ctl_volume = val;
1169 tm6000_set_volume(dev, val);
1170 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001171 }
1172 return -EINVAL;
1173}
1174
Hans Verkuil9f747352013-01-31 08:23:01 -03001175static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1176 .s_ctrl = tm6000_radio_s_ctrl,
1177};
1178
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001179static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001180 struct v4l2_tuner *t)
1181{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001182 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001183 struct tm6000_core *dev = fh->dev;
1184
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001185 if (UNSET == dev->tuner_type)
1186 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001187 if (0 != t->index)
1188 return -EINVAL;
1189
1190 strcpy(t->name, "Television");
1191 t->type = V4L2_TUNER_ANALOG_TV;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001192 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001193 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001194 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1195
1196 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1197
1198 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001199
1200 return 0;
1201}
1202
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001203static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001204 const struct v4l2_tuner *t)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001205{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001206 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001207 struct tm6000_core *dev = fh->dev;
1208
1209 if (UNSET == dev->tuner_type)
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001210 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001211 if (0 != t->index)
1212 return -EINVAL;
1213
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001214 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1215 dev->amode = V4L2_TUNER_MODE_STEREO;
1216 else
1217 dev->amode = t->audmode;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001218 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1219
1220 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001221
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001222 return 0;
1223}
1224
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001225static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001226 struct v4l2_frequency *f)
1227{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001228 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001229 struct tm6000_core *dev = fh->dev;
1230
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001231 if (UNSET == dev->tuner_type)
1232 return -ENOTTY;
1233 if (f->tuner)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001234 return -EINVAL;
1235
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001236 f->frequency = dev->freq;
1237
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001238 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001239
1240 return 0;
1241}
1242
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001243static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001244 const struct v4l2_frequency *f)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001245{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001246 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001247 struct tm6000_core *dev = fh->dev;
1248
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001249 if (UNSET == dev->tuner_type)
1250 return -ENOTTY;
1251 if (f->tuner != 0)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001252 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001253
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001254 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001255 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001256
1257 return 0;
1258}
1259
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001260static int radio_g_tuner(struct file *file, void *priv,
1261 struct v4l2_tuner *t)
1262{
1263 struct tm6000_fh *fh = file->private_data;
1264 struct tm6000_core *dev = fh->dev;
1265
1266 if (0 != t->index)
1267 return -EINVAL;
1268
1269 memset(t, 0, sizeof(*t));
1270 strcpy(t->name, "Radio");
1271 t->type = V4L2_TUNER_RADIO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001272 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001273 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001274 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001275
1276 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1277
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001278 return 0;
1279}
1280
1281static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001282 const struct v4l2_tuner *t)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001283{
1284 struct tm6000_fh *fh = file->private_data;
1285 struct tm6000_core *dev = fh->dev;
1286
1287 if (0 != t->index)
1288 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001289 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001290 return 0;
1291}
1292
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001293/* ------------------------------------------------------------------
1294 File operations for the device
1295 ------------------------------------------------------------------*/
1296
Hans Verkuil14a09da2012-06-24 07:26:46 -03001297static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001298{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001299 struct video_device *vdev = video_devdata(file);
1300 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001301 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001302 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil9f747352013-01-31 08:23:01 -03001303 int rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001304 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001305
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001306 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1307 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001308
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001309 switch (vdev->vfl_type) {
1310 case VFL_TYPE_GRABBER:
1311 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1312 break;
1313 case VFL_TYPE_VBI:
1314 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1315 break;
1316 case VFL_TYPE_RADIO:
1317 radio = 1;
1318 break;
Mauro Carvalho Chehab4839c582017-09-28 18:39:32 -04001319 default:
1320 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001321 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001322
1323 /* If more than one user, mutex should be added */
1324 dev->users++;
1325
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001326 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1327 video_device_node_name(vdev), v4l2_type_names[type],
1328 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001329
1330 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001331 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001332 if (NULL == fh) {
1333 dev->users--;
1334 return -ENOMEM;
1335 }
1336
Hans Verkuil770056c2012-09-11 11:50:37 -03001337 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001338 file->private_data = fh;
1339 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001340 fh->radio = radio;
1341 dev->radio = radio;
1342 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001343 dev->fourcc = format[0].fourcc;
1344
1345 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001346
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001347 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001348
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001349 fh->width = dev->width;
1350 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001351
Mauro Carvalho Chehabe771bdf2018-03-23 07:42:32 -04001352 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=%p, dev=%p, dev->vidq=%p\n",
1353 fh, dev, &dev->vidq);
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -02001354 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1355 list_empty(&dev->vidq.queued));
1356 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1357 list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001358
1359 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001360 rc = tm6000_init_analog_mode(dev);
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001361 if (rc < 0) {
1362 v4l2_fh_exit(&fh->fh);
1363 kfree(fh);
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001364 return rc;
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001365 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001366
Hans Verkuil9f747352013-01-31 08:23:01 -03001367 dev->mode = TM6000_MODE_ANALOG;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001368
Thierry Redingaa4a5832011-08-04 04:14:15 -03001369 if (!fh->radio) {
1370 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1371 NULL, &dev->slock,
1372 fh->type,
1373 V4L2_FIELD_INTERLACED,
1374 sizeof(struct tm6000_buffer), fh, &dev->lock);
1375 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001376 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001377 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001378 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1379 tm6000_prepare_isoc(dev);
1380 tm6000_start_thread(dev);
1381 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001382 v4l2_fh_add(&fh->fh);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001383
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001384 return 0;
1385}
1386
Hans Verkuil14a09da2012-06-24 07:26:46 -03001387static int tm6000_open(struct file *file)
1388{
1389 struct video_device *vdev = video_devdata(file);
1390 int res;
1391
1392 mutex_lock(vdev->lock);
1393 res = __tm6000_open(file);
1394 mutex_unlock(vdev->lock);
1395 return res;
1396}
1397
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001398static ssize_t
1399tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1400{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001401 struct tm6000_fh *fh = file->private_data;
1402 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001403
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001404 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001405 int res;
1406
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001407 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001408 return -EBUSY;
1409
Hans Verkuil14a09da2012-06-24 07:26:46 -03001410 if (mutex_lock_interruptible(&dev->lock))
1411 return -ERESTARTSYS;
1412 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001413 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001414 mutex_unlock(&dev->lock);
1415 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001416 }
1417 return 0;
1418}
1419
Al Viroc23e0cb2017-07-03 03:02:56 -04001420static __poll_t
Hans Verkuil14a09da2012-06-24 07:26:46 -03001421__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001422{
Al Viro01699432017-07-03 03:14:15 -04001423 __poll_t req_events = poll_requested_events(wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001424 struct tm6000_fh *fh = file->private_data;
1425 struct tm6000_buffer *buf;
Al Viroc23e0cb2017-07-03 03:02:56 -04001426 __poll_t res = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001427
Hans Verkuil770056c2012-09-11 11:50:37 -03001428 if (v4l2_event_pending(&fh->fh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001429 res = EPOLLPRI;
1430 else if (req_events & EPOLLPRI)
Hans Verkuil770056c2012-09-11 11:50:37 -03001431 poll_wait(file, &fh->fh.wait, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001432 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001433 return res | EPOLLERR;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001434
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001435 if (!!is_res_streaming(fh->dev, fh))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001436 return res | EPOLLERR;
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001437
1438 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001439 /* streaming capture */
1440 if (list_empty(&fh->vb_vidq.stream))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001441 return res | EPOLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001442 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Hans Verkuil82f0efb2013-02-07 07:05:43 -03001443 poll_wait(file, &buf->vb.done, wait);
1444 if (buf->vb.state == VIDEOBUF_DONE ||
1445 buf->vb.state == VIDEOBUF_ERROR)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001446 return res | EPOLLIN | EPOLLRDNORM;
1447 } else if (req_events & (EPOLLIN | EPOLLRDNORM)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001448 /* read() capture */
Hans Verkuil770056c2012-09-11 11:50:37 -03001449 return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001450 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001451 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001452}
1453
Al Viroc23e0cb2017-07-03 03:02:56 -04001454static __poll_t tm6000_poll(struct file *file, struct poll_table_struct *wait)
Hans Verkuil14a09da2012-06-24 07:26:46 -03001455{
1456 struct tm6000_fh *fh = file->private_data;
1457 struct tm6000_core *dev = fh->dev;
Al Viroc23e0cb2017-07-03 03:02:56 -04001458 __poll_t res;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001459
1460 mutex_lock(&dev->lock);
1461 res = __tm6000_poll(file, wait);
1462 mutex_unlock(&dev->lock);
1463 return res;
1464}
1465
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001466static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001467{
1468 struct tm6000_fh *fh = file->private_data;
1469 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001470 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001471
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001472 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1473 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001474
Hans Verkuil14a09da2012-06-24 07:26:46 -03001475 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001476 dev->users--;
1477
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001478 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001479
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001480 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001481 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001482
Stefan Ringel8159c182011-11-28 15:46:18 -03001483 /* Stop interrupt USB pipe */
1484 tm6000_ir_int_stop(dev);
1485
1486 usb_reset_configuration(dev->udev);
1487
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001488 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001489 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001490 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001491 else
1492 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001493 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001494
1495 /* Start interrupt USB pipe */
1496 tm6000_ir_int_start(dev);
1497
Thierry Redingaa4a5832011-08-04 04:14:15 -03001498 if (!fh->radio)
1499 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001500 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001501 v4l2_fh_del(&fh->fh);
1502 v4l2_fh_exit(&fh->fh);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001503 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001504 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001505
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001506 return 0;
1507}
1508
1509static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1510{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001511 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001512 struct tm6000_core *dev = fh->dev;
1513 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001514
Hans Verkuil14a09da2012-06-24 07:26:46 -03001515 if (mutex_lock_interruptible(&dev->lock))
1516 return -ERESTARTSYS;
1517 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1518 mutex_unlock(&dev->lock);
1519 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001520}
1521
Bhumika Goyalff05c982017-06-29 02:55:23 -04001522static const struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001523 .owner = THIS_MODULE,
1524 .open = tm6000_open,
1525 .release = tm6000_release,
1526 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1527 .read = tm6000_read,
1528 .poll = tm6000_poll,
1529 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001530};
1531
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001532static const struct v4l2_ioctl_ops video_ioctl_ops = {
1533 .vidioc_querycap = vidioc_querycap,
1534 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1535 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1536 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1537 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1538 .vidioc_s_std = vidioc_s_std,
Hans Verkuil804be2d2013-06-03 05:36:44 -03001539 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001540 .vidioc_enum_input = vidioc_enum_input,
1541 .vidioc_g_input = vidioc_g_input,
1542 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001543 .vidioc_g_tuner = vidioc_g_tuner,
1544 .vidioc_s_tuner = vidioc_s_tuner,
1545 .vidioc_g_frequency = vidioc_g_frequency,
1546 .vidioc_s_frequency = vidioc_s_frequency,
1547 .vidioc_streamon = vidioc_streamon,
1548 .vidioc_streamoff = vidioc_streamoff,
1549 .vidioc_reqbufs = vidioc_reqbufs,
1550 .vidioc_querybuf = vidioc_querybuf,
1551 .vidioc_qbuf = vidioc_qbuf,
1552 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil770056c2012-09-11 11:50:37 -03001553 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1554 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001555};
1556
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001557static struct video_device tm6000_template = {
1558 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001559 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001560 .ioctl_ops = &video_ioctl_ops,
Hans Verkuil65b88c02015-03-09 13:34:10 -03001561 .release = video_device_release_empty,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001562 .tvnorms = TM6000_STD,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001563};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001564
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001565static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001566 .owner = THIS_MODULE,
1567 .open = tm6000_open,
Hans Verkuil52dec542012-09-11 11:53:51 -03001568 .poll = v4l2_ctrl_poll,
Stefan Ringel1f385712011-05-09 16:54:01 -03001569 .release = tm6000_release,
1570 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001571};
1572
1573static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001574 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001575 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001576 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001577 .vidioc_g_frequency = vidioc_g_frequency,
1578 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil770056c2012-09-11 11:50:37 -03001579 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1580 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001581};
1582
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001583static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001584 .name = "tm6000",
1585 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001586 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001587};
1588
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001589/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001590 * Initialization and module stuff
1591 * ------------------------------------------------------------------
1592 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001593
Hans Verkuil65b88c02015-03-09 13:34:10 -03001594static void vdev_init(struct tm6000_core *dev,
1595 struct video_device *vfd,
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001596 const struct video_device
1597 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001598{
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001599 *vfd = *template;
1600 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil65b88c02015-03-09 13:34:10 -03001601 vfd->release = video_device_release_empty;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001602 vfd->lock = &dev->lock;
1603
1604 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1605
1606 video_set_drvdata(vfd, dev);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001607}
1608
1609int tm6000_v4l2_register(struct tm6000_core *dev)
1610{
Hans Verkuil9f747352013-01-31 08:23:01 -03001611 int ret = 0;
1612
1613 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1614 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1615 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1616 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1617 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1618 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1619 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1620 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1621 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1622 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1623 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1624 V4L2_CID_SATURATION, 0, 255, 1, 112);
1625 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1626 V4L2_CID_HUE, -128, 127, 1, 0);
1627 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1628 &dev->radio_ctrl_handler, NULL);
1629
1630 if (dev->radio_ctrl_handler.error)
1631 ret = dev->radio_ctrl_handler.error;
1632 if (!ret && dev->ctrl_handler.error)
1633 ret = dev->ctrl_handler.error;
1634 if (ret)
1635 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001636
Hans Verkuil65b88c02015-03-09 13:34:10 -03001637 vdev_init(dev, &dev->vfd, &tm6000_template, "video");
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001638
Hans Verkuil65b88c02015-03-09 13:34:10 -03001639 dev->vfd.ctrl_handler = &dev->ctrl_handler;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001640
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001641 /* init video dma queues */
1642 INIT_LIST_HEAD(&dev->vidq.active);
1643 INIT_LIST_HEAD(&dev->vidq.queued);
1644
Hans Verkuil65b88c02015-03-09 13:34:10 -03001645 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001646
1647 if (ret < 0) {
1648 printk(KERN_INFO "%s: can't register video device\n",
1649 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001650 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001651 }
1652
1653 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001654 dev->name, video_device_node_name(&dev->vfd));
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001655
Stefan Ringel14169102011-05-09 16:53:49 -03001656 if (dev->caps.has_radio) {
Hans Verkuil65b88c02015-03-09 13:34:10 -03001657 vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
Stefan Ringel14169102011-05-09 16:53:49 -03001658 "radio");
Hans Verkuil65b88c02015-03-09 13:34:10 -03001659 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1660 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Stefan Ringel14169102011-05-09 16:53:49 -03001661 radio_nr);
1662 if (ret < 0) {
1663 printk(KERN_INFO "%s: can't register radio device\n",
1664 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001665 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001666 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001667
Stefan Ringel14169102011-05-09 16:53:49 -03001668 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001669 dev->name, video_device_node_name(&dev->radio_dev));
Stefan Ringel14169102011-05-09 16:53:49 -03001670 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001671
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001672 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001673 return ret;
Hans Verkuil9f747352013-01-31 08:23:01 -03001674
1675unreg_video:
Hans Verkuil65b88c02015-03-09 13:34:10 -03001676 video_unregister_device(&dev->vfd);
Hans Verkuil9f747352013-01-31 08:23:01 -03001677free_ctrl:
1678 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1679 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1680 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001681}
1682
1683int tm6000_v4l2_unregister(struct tm6000_core *dev)
1684{
Hans Verkuil65b88c02015-03-09 13:34:10 -03001685 video_unregister_device(&dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001686
Julian Scheel16427fa2012-10-04 10:04:28 -03001687 /* if URB buffers are still allocated free them now */
1688 tm6000_free_urb_buffers(dev);
1689
Hans Verkuil65b88c02015-03-09 13:34:10 -03001690 video_unregister_device(&dev->radio_dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001691 return 0;
1692}
1693
1694int tm6000_v4l2_exit(void)
1695{
1696 return 0;
1697}
1698
1699module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001700MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001701
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001702module_param_named(debug, tm6000_debug, int, 0444);
1703MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001704
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001705module_param(vid_limit, int, 0644);
1706MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001707
Julian Scheel16427fa2012-10-04 10:04:28 -03001708module_param(keep_urb, bool, 0);
1709MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");