blob: 0d45f35e169706e6c4f9194e08d5f8c49f51fe6a [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
Markus Elfring7e11d502017-09-14 14:34:39 +0200473 if (dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300474 return 0;
475
476 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200477 if (!dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300478 return -ENOMEM;
Julian Scheel16427fa2012-10-04 10:04:28 -0300479
480 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200481 if (!dev->urb_dma)
Julian Scheel16427fa2012-10-04 10:04:28 -0300482 return -ENOMEM;
Julian Scheel16427fa2012-10-04 10:04:28 -0300483
484 for (i = 0; i < num_bufs; i++) {
485 dev->urb_buffer[i] = usb_alloc_coherent(
486 dev->udev, dev->urb_size,
487 GFP_KERNEL, &dev->urb_dma[i]);
488 if (!dev->urb_buffer[i]) {
489 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
490 dev->urb_size, i);
491 return -ENOMEM;
492 }
493 memset(dev->urb_buffer[i], 0, dev->urb_size);
494 }
495
496 return 0;
497}
498
499/*
500 * Free URB buffers
501 */
502static int tm6000_free_urb_buffers(struct tm6000_core *dev)
503{
504 int i;
505
Markus Elfring7e11d502017-09-14 14:34:39 +0200506 if (!dev->urb_buffer)
Julian Scheel16427fa2012-10-04 10:04:28 -0300507 return 0;
508
509 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
510 if (dev->urb_buffer[i]) {
511 usb_free_coherent(dev->udev,
512 dev->urb_size,
513 dev->urb_buffer[i],
514 dev->urb_dma[i]);
515 dev->urb_buffer[i] = NULL;
516 }
517 }
518 kfree(dev->urb_buffer);
519 kfree(dev->urb_dma);
520 dev->urb_buffer = NULL;
521 dev->urb_dma = NULL;
522
523 return 0;
524}
525
526/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300527 * Stop and Deallocate URBs
528 */
529static void tm6000_uninit_isoc(struct tm6000_core *dev)
530{
531 struct urb *urb;
532 int i;
533
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300534 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300535 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300536 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300537 if (urb) {
538 usb_kill_urb(urb);
539 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300540 usb_free_urb(urb);
541 dev->isoc_ctl.urb[i] = NULL;
542 }
543 dev->isoc_ctl.transfer_buffer[i] = NULL;
544 }
545
Julian Scheel16427fa2012-10-04 10:04:28 -0300546 if (!keep_urb)
547 tm6000_free_urb_buffers(dev);
548
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300549 kfree(dev->isoc_ctl.urb);
550 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300551
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300552 dev->isoc_ctl.urb = NULL;
553 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300554 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300555}
556
557/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300558 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300559 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300560static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300561{
562 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300563 int i, j, sb_size, pipe, size, max_packets;
564 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300565 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300566
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300567 /* De-allocates all pending stuff */
568 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300569 /* Stop interrupt USB pipe */
570 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300571
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300572 usb_set_interface(dev->udev,
573 dev->isoc_in.bInterfaceNumber,
574 dev->isoc_in.bAlternateSetting);
575
Dmitri Belimov641d2112010-12-22 05:57:46 -0300576 /* Start interrupt USB pipe */
577 tm6000_ir_int_start(dev);
578
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300579 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300580 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300581 USB_ENDPOINT_NUMBER_MASK);
582
583 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
584
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300585 if (size > dev->isoc_in.maxsize)
586 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300587
588 dev->isoc_ctl.max_pkt_size = size;
589
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300590 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300591 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300592 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300593
594 dev->isoc_ctl.num_bufs = num_bufs;
595
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300596 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Markus Elfring7e11d502017-09-14 14:34:39 +0200597 if (!dev->isoc_ctl.urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300598 return -ENOMEM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300599
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300600 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300601 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300602 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300603 kfree(dev->isoc_ctl.urb);
604 return -ENOMEM;
605 }
606
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200607 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 -0300608 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300609 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300610
Julian Scheel16427fa2012-10-04 10:04:28 -0300611
Mauro Carvalho Chehab485bdbb2015-06-05 09:33:44 -0300612 if (tm6000_alloc_urb_buffers(dev) < 0) {
Julian Scheel16427fa2012-10-04 10:04:28 -0300613 tm6000_err("cannot allocate memory for urb buffers\n");
614
615 /* call free, as some buffers might have been allocated */
616 tm6000_free_urb_buffers(dev);
617 kfree(dev->isoc_ctl.urb);
618 kfree(dev->isoc_ctl.transfer_buffer);
619 return -ENOMEM;
620 }
621
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300622 /* allocate urbs and transfer buffers */
623 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
624 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
625 if (!urb) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300626 tm6000_uninit_isoc(dev);
Christophe JAILLETf81a18d2017-02-22 19:32:19 -0300627 tm6000_free_urb_buffers(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300628 return -ENOMEM;
629 }
630 dev->isoc_ctl.urb[i] = urb;
631
Julian Scheel16427fa2012-10-04 10:04:28 -0300632 urb->transfer_dma = dev->urb_dma[i];
633 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300634
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300635 usb_fill_bulk_urb(urb, dev->udev, pipe,
636 dev->isoc_ctl.transfer_buffer[i], sb_size,
637 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300638 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300639 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300640 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300641
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300642 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300643 urb->iso_frame_desc[j].offset = size * j;
644 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300645 }
646 }
647
648 return 0;
649}
650
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300651static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300652{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300653 struct tm6000_dmaqueue *dma_q = &dev->vidq;
654 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300655
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300656 dma_q->frame = 0;
657 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300658
659 init_waitqueue_head(&dma_q->wq);
660
661 /* submit urbs and enables IRQ */
662 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300663 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300664 if (rc) {
665 tm6000_err("submit of urb %i failed (error=%i)\n", i,
666 rc);
667 tm6000_uninit_isoc(dev);
668 return rc;
669 }
670 }
671
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300672 return 0;
673}
674
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300675/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300676 * Videobuf operations
677 * ------------------------------------------------------------------
678 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300679
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300680static int
681buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
682{
683 struct tm6000_fh *fh = vq->priv_data;
684
685 *size = fh->fmt->depth * fh->width * fh->height >> 3;
686 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300687 *count = TM6000_DEF_BUF;
688
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300689 if (*count < TM6000_MIN_BUF)
690 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300691
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300692 while (*size * *count > vid_limit * 1024 * 1024)
693 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300694
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300695 return 0;
696}
697
698static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
699{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300700 struct tm6000_fh *fh = vq->priv_data;
701 struct tm6000_core *dev = fh->dev;
702 unsigned long flags;
703
Mauro Carvalho Chehab09f20822015-05-19 08:00:56 -0300704 BUG_ON(in_interrupt());
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300705
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300706 /* We used to wait for the buffer to finish here, but this didn't work
707 because, as we were keeping the state as VIDEOBUF_QUEUED,
708 videobuf_queue_cancel marked it as finished for us.
709 (Also, it could wedge forever if the hardware was misconfigured.)
710
711 This should be safe; by the time we get here, the buffer isn't
712 queued anymore. If we ever start marking the buffers as
713 VIDEOBUF_ACTIVE, it won't be, though.
714 */
715 spin_lock_irqsave(&dev->slock, flags);
716 if (dev->isoc_ctl.buf == buf)
717 dev->isoc_ctl.buf = NULL;
718 spin_unlock_irqrestore(&dev->slock, flags);
719
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300720 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300721 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300722}
723
724static int
725buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
726 enum v4l2_field field)
727{
728 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300729 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300730 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300731 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300732
733 BUG_ON(NULL == fh->fmt);
734
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300735
736 /* FIXME: It assumes depth=2 */
737 /* The only currently supported format is 16 bits/pixel */
738 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
739 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
740 return -EINVAL;
741
742 if (buf->fmt != fh->fmt ||
743 buf->vb.width != fh->width ||
744 buf->vb.height != fh->height ||
745 buf->vb.field != field) {
746 buf->fmt = fh->fmt;
747 buf->vb.width = fh->width;
748 buf->vb.height = fh->height;
749 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300750 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300751 }
752
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300753 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400754 rc = videobuf_iolock(vq, &buf->vb, NULL);
755 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300756 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300757 }
758
Thierry Reding88e834a2011-08-04 04:14:12 -0300759 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300760 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300761 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300762 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300763
764 rc = tm6000_start_thread(dev);
765 if (rc < 0)
766 goto fail;
767
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300768 }
769
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300770 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300771 return 0;
772
773fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300774 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300775 return rc;
776}
777
778static void
779buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
780{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300781 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300782 struct tm6000_fh *fh = vq->priv_data;
783 struct tm6000_core *dev = fh->dev;
784 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300785
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300786 buf->vb.state = VIDEOBUF_QUEUED;
787 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300788}
789
790static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
791{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300792 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300793
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300794 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300795}
796
Julia Lawall63f2ec62017-08-04 08:09:47 -0400797static const struct videobuf_queue_ops tm6000_video_qops = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300798 .buf_setup = buffer_setup,
799 .buf_prepare = buffer_prepare,
800 .buf_queue = buffer_queue,
801 .buf_release = buffer_release,
802};
803
804/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300805 * IOCTL handling
806 * ------------------------------------------------------------------
807 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300808
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300809static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300810{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300811 /* Is the current fh handling it? if so, that's OK */
812 if (dev->resources == fh && dev->is_res_read)
813 return true;
814
815 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300816}
817
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300818static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300819{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300820 /* Is the current fh handling it? if so, that's OK */
821 if (dev->resources == fh)
822 return true;
823
824 return false;
825}
826
827static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
828 bool is_res_read)
829{
830 /* Is the current fh handling it? if so, that's OK */
831 if (dev->resources == fh && dev->is_res_read == is_res_read)
832 return true;
833
834 /* is it free? */
835 if (dev->resources)
836 return false;
837
838 /* grab it */
839 dev->resources = fh;
840 dev->is_res_read = is_res_read;
841 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
842 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300843}
844
845static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
846{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300847 /* Is the current fh handling it? if so, that's OK */
848 if (dev->resources != fh)
849 return;
850
851 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300852 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300853}
854
855/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300856 * IOCTL vidioc handling
857 * ------------------------------------------------------------------
858 */
859static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300860 struct v4l2_capability *cap)
861{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300862 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300863 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300864
865 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300866 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300867 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Stefan Ringel886a3c02011-05-09 16:53:50 -0300868 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300869 cap->device_caps |= V4L2_CAP_TUNER;
870 if (vdev->vfl_type == VFL_TYPE_GRABBER)
871 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
872 V4L2_CAP_STREAMING |
873 V4L2_CAP_READWRITE;
874 else
875 cap->device_caps |= V4L2_CAP_RADIO;
876 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
877 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300878
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300879 return 0;
880}
881
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300882static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300883 struct v4l2_fmtdesc *f)
884{
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300885 if (f->index >= ARRAY_SIZE(format))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300886 return -EINVAL;
887
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300888 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300889 f->pixelformat = format[f->index].fourcc;
890 return 0;
891}
892
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300893static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300894 struct v4l2_format *f)
895{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300896 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300897
898 f->fmt.pix.width = fh->width;
899 f->fmt.pix.height = fh->height;
900 f->fmt.pix.field = fh->vb_vidq.field;
901 f->fmt.pix.pixelformat = fh->fmt->fourcc;
Hans Verkuile6185782012-09-11 11:51:02 -0300902 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300903 f->fmt.pix.bytesperline =
904 (f->fmt.pix.width * fh->fmt->depth) >> 3;
905 f->fmt.pix.sizeimage =
906 f->fmt.pix.height * f->fmt.pix.bytesperline;
907
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300908 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300909}
910
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300911static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300912{
913 unsigned int i;
914
915 for (i = 0; i < ARRAY_SIZE(format); i++)
916 if (format[i].fourcc == fourcc)
917 return format+i;
918 return NULL;
919}
920
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300921static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300922 struct v4l2_format *f)
923{
924 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
925 struct tm6000_fmt *fmt;
926 enum v4l2_field field;
927
928 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
929 if (NULL == fmt) {
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -0200930 dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
931 f->fmt.pix.pixelformat);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300932 return -EINVAL;
933 }
934
935 field = f->fmt.pix.field;
936
Hans Verkuiled572562013-02-01 09:08:46 -0300937 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300938
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300939 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300940
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300941 f->fmt.pix.width = dev->width;
942 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300943
944 f->fmt.pix.width &= ~0x01;
945
946 f->fmt.pix.field = field;
947
948 f->fmt.pix.bytesperline =
949 (f->fmt.pix.width * fmt->depth) >> 3;
950 f->fmt.pix.sizeimage =
951 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuile6185782012-09-11 11:51:02 -0300952 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300953
954 return 0;
955}
956
957/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300958static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300959 struct v4l2_format *f)
960{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300961 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300962 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300963 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300964 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300965 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300966
967 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
968 fh->width = f->fmt.pix.width;
969 fh->height = f->fmt.pix.height;
970 fh->vb_vidq.field = f->fmt.pix.field;
971 fh->type = f->type;
972
973 dev->fourcc = f->fmt.pix.pixelformat;
974
975 tm6000_set_fourcc_format(dev);
976
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300977 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300978}
979
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300980static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300981 struct v4l2_requestbuffers *p)
982{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300983 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300984
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300985 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300986}
987
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300988static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300989 struct v4l2_buffer *p)
990{
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_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300994}
995
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300996static int vidioc_qbuf(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_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001001}
1002
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001003static int vidioc_dqbuf(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_dqbuf(&fh->vb_vidq, p,
1008 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001009}
1010
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001011static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1012{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001013 struct tm6000_fh *fh = priv;
1014 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001015
1016 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1017 return -EINVAL;
1018 if (i != fh->type)
1019 return -EINVAL;
1020
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001021 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001022 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001023 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001024}
1025
1026static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1027{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001028 struct tm6000_fh *fh = priv;
1029 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001030
1031 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1032 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001033
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001034 if (i != fh->type)
1035 return -EINVAL;
1036
1037 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001038 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001039
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001040 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001041}
1042
Hans Verkuil314527a2013-03-15 06:10:40 -03001043static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001044{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001045 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001046 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001047 struct tm6000_core *dev = fh->dev;
1048
Hans Verkuil314527a2013-03-15 06:10:40 -03001049 dev->norm = norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001050 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001051
1052 fh->width = dev->width;
1053 fh->height = dev->height;
1054
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001055 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001056 return rc;
1057
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001058 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001059
1060 return 0;
1061}
1062
Hans Verkuil804be2d2013-06-03 05:36:44 -03001063static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1064{
1065 struct tm6000_fh *fh = priv;
1066 struct tm6000_core *dev = fh->dev;
1067
1068 *norm = dev->norm;
1069 return 0;
1070}
1071
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001072static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001073 [TM6000_INPUT_TV] = "Television",
1074 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1075 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1076 [TM6000_INPUT_SVIDEO] = "S-Video",
1077};
1078
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001079static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001080 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001081{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001082 struct tm6000_fh *fh = priv;
1083 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001084 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001085
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001086 n = i->index;
1087 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001088 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001089
1090 if (!dev->vinput[n].type)
1091 return -EINVAL;
1092
1093 i->index = n;
1094
1095 if (dev->vinput[n].type == TM6000_INPUT_TV)
1096 i->type = V4L2_INPUT_TYPE_TUNER;
1097 else
1098 i->type = V4L2_INPUT_TYPE_CAMERA;
1099
1100 strcpy(i->name, iname[dev->vinput[n].type]);
1101
1102 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001103
1104 return 0;
1105}
1106
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001107static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001108{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001109 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001110 struct tm6000_core *dev = fh->dev;
1111
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001112 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001113
1114 return 0;
1115}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001116
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001117static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001118{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001119 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001120 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001121 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001122
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001123 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001124 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001125 if (!dev->vinput[i].type)
1126 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001127
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001128 dev->input = i;
1129
Hans Verkuil804be2d2013-06-03 05:36:44 -03001130 rc = vidioc_s_std(file, priv, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001131
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001132 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001133}
1134
Stefan Ringel886a3c02011-05-09 16:53:50 -03001135/* --- controls ---------------------------------------------- */
Hans Verkuil9f747352013-01-31 08:23:01 -03001136
1137static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001138{
Hans Verkuil9f747352013-01-31 08:23:01 -03001139 struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1140 u8 val = ctrl->val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001141
1142 switch (ctrl->id) {
1143 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001144 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001145 return 0;
1146 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001147 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001148 return 0;
1149 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001150 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001151 return 0;
1152 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001153 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001154 return 0;
Hans Verkuil9f747352013-01-31 08:23:01 -03001155 }
1156 return -EINVAL;
1157}
1158
1159static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1160 .s_ctrl = tm6000_s_ctrl,
1161};
1162
1163static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1164{
1165 struct tm6000_core *dev = container_of(ctrl->handler,
1166 struct tm6000_core, radio_ctrl_handler);
1167 u8 val = ctrl->val;
1168
1169 switch (ctrl->id) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001170 case V4L2_CID_AUDIO_MUTE:
1171 dev->ctl_mute = val;
1172 tm6000_tvaudio_set_mute(dev, val);
1173 return 0;
1174 case V4L2_CID_AUDIO_VOLUME:
1175 dev->ctl_volume = val;
1176 tm6000_set_volume(dev, val);
1177 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001178 }
1179 return -EINVAL;
1180}
1181
Hans Verkuil9f747352013-01-31 08:23:01 -03001182static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1183 .s_ctrl = tm6000_radio_s_ctrl,
1184};
1185
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001186static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001187 struct v4l2_tuner *t)
1188{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001189 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001190 struct tm6000_core *dev = fh->dev;
1191
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001192 if (UNSET == dev->tuner_type)
1193 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001194 if (0 != t->index)
1195 return -EINVAL;
1196
1197 strcpy(t->name, "Television");
1198 t->type = V4L2_TUNER_ANALOG_TV;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001199 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001200 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001201 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1202
1203 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1204
1205 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001206
1207 return 0;
1208}
1209
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001210static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001211 const struct v4l2_tuner *t)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001212{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001213 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001214 struct tm6000_core *dev = fh->dev;
1215
1216 if (UNSET == dev->tuner_type)
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001217 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001218 if (0 != t->index)
1219 return -EINVAL;
1220
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001221 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1222 dev->amode = V4L2_TUNER_MODE_STEREO;
1223 else
1224 dev->amode = t->audmode;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001225 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1226
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001228
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001229 return 0;
1230}
1231
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001232static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001233 struct v4l2_frequency *f)
1234{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001235 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001236 struct tm6000_core *dev = fh->dev;
1237
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001238 if (UNSET == dev->tuner_type)
1239 return -ENOTTY;
1240 if (f->tuner)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001241 return -EINVAL;
1242
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001243 f->frequency = dev->freq;
1244
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001245 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001246
1247 return 0;
1248}
1249
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001250static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001251 const struct v4l2_frequency *f)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001252{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001253 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001254 struct tm6000_core *dev = fh->dev;
1255
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001256 if (UNSET == dev->tuner_type)
1257 return -ENOTTY;
1258 if (f->tuner != 0)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001259 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001260
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001261 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001262 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001263
1264 return 0;
1265}
1266
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001267static int radio_g_tuner(struct file *file, void *priv,
1268 struct v4l2_tuner *t)
1269{
1270 struct tm6000_fh *fh = file->private_data;
1271 struct tm6000_core *dev = fh->dev;
1272
1273 if (0 != t->index)
1274 return -EINVAL;
1275
1276 memset(t, 0, sizeof(*t));
1277 strcpy(t->name, "Radio");
1278 t->type = V4L2_TUNER_RADIO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001279 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001280 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001281 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001282
1283 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1284
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001285 return 0;
1286}
1287
1288static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001289 const struct v4l2_tuner *t)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001290{
1291 struct tm6000_fh *fh = file->private_data;
1292 struct tm6000_core *dev = fh->dev;
1293
1294 if (0 != t->index)
1295 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001296 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001297 return 0;
1298}
1299
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001300/* ------------------------------------------------------------------
1301 File operations for the device
1302 ------------------------------------------------------------------*/
1303
Hans Verkuil14a09da2012-06-24 07:26:46 -03001304static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001305{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001306 struct video_device *vdev = video_devdata(file);
1307 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001308 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001309 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil9f747352013-01-31 08:23:01 -03001310 int rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001311 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001312
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001313 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1314 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001315
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001316 switch (vdev->vfl_type) {
1317 case VFL_TYPE_GRABBER:
1318 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1319 break;
1320 case VFL_TYPE_VBI:
1321 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1322 break;
1323 case VFL_TYPE_RADIO:
1324 radio = 1;
1325 break;
1326 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001327
1328 /* If more than one user, mutex should be added */
1329 dev->users++;
1330
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001331 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1332 video_device_node_name(vdev), v4l2_type_names[type],
1333 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001334
1335 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001336 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001337 if (NULL == fh) {
1338 dev->users--;
1339 return -ENOMEM;
1340 }
1341
Hans Verkuil770056c2012-09-11 11:50:37 -03001342 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001343 file->private_data = fh;
1344 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001345 fh->radio = radio;
1346 dev->radio = radio;
1347 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001348 dev->fourcc = format[0].fourcc;
1349
1350 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001351
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001352 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001353
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001354 fh->width = dev->width;
1355 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001356
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -02001357 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001358 (unsigned long)fh, (unsigned long)dev,
1359 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab68616502016-10-18 17:44:19 -02001360 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1361 list_empty(&dev->vidq.queued));
1362 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1363 list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001364
1365 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001366 rc = tm6000_init_analog_mode(dev);
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001367 if (rc < 0) {
1368 v4l2_fh_exit(&fh->fh);
1369 kfree(fh);
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001370 return rc;
Santosh Kumar Singh1e071032016-12-19 15:10:58 -02001371 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001372
Hans Verkuil9f747352013-01-31 08:23:01 -03001373 dev->mode = TM6000_MODE_ANALOG;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001374
Thierry Redingaa4a5832011-08-04 04:14:15 -03001375 if (!fh->radio) {
1376 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1377 NULL, &dev->slock,
1378 fh->type,
1379 V4L2_FIELD_INTERLACED,
1380 sizeof(struct tm6000_buffer), fh, &dev->lock);
1381 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001382 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001383 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001384 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1385 tm6000_prepare_isoc(dev);
1386 tm6000_start_thread(dev);
1387 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001388 v4l2_fh_add(&fh->fh);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001389
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001390 return 0;
1391}
1392
Hans Verkuil14a09da2012-06-24 07:26:46 -03001393static int tm6000_open(struct file *file)
1394{
1395 struct video_device *vdev = video_devdata(file);
1396 int res;
1397
1398 mutex_lock(vdev->lock);
1399 res = __tm6000_open(file);
1400 mutex_unlock(vdev->lock);
1401 return res;
1402}
1403
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001404static ssize_t
1405tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1406{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001407 struct tm6000_fh *fh = file->private_data;
1408 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001409
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001410 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001411 int res;
1412
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001413 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001414 return -EBUSY;
1415
Hans Verkuil14a09da2012-06-24 07:26:46 -03001416 if (mutex_lock_interruptible(&dev->lock))
1417 return -ERESTARTSYS;
1418 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001419 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001420 mutex_unlock(&dev->lock);
1421 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001422 }
1423 return 0;
1424}
1425
1426static unsigned int
Hans Verkuil14a09da2012-06-24 07:26:46 -03001427__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001428{
Hans Verkuil770056c2012-09-11 11:50:37 -03001429 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001430 struct tm6000_fh *fh = file->private_data;
1431 struct tm6000_buffer *buf;
Hans Verkuil770056c2012-09-11 11:50:37 -03001432 int res = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001433
Hans Verkuil770056c2012-09-11 11:50:37 -03001434 if (v4l2_event_pending(&fh->fh))
1435 res = POLLPRI;
1436 else if (req_events & POLLPRI)
1437 poll_wait(file, &fh->fh.wait, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001438 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
Hans Verkuil770056c2012-09-11 11:50:37 -03001439 return res | POLLERR;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001440
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001441 if (!!is_res_streaming(fh->dev, fh))
Hans Verkuil770056c2012-09-11 11:50:37 -03001442 return res | POLLERR;
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001443
1444 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001445 /* streaming capture */
1446 if (list_empty(&fh->vb_vidq.stream))
Hans Verkuil770056c2012-09-11 11:50:37 -03001447 return res | POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001448 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Hans Verkuil82f0efb2013-02-07 07:05:43 -03001449 poll_wait(file, &buf->vb.done, wait);
1450 if (buf->vb.state == VIDEOBUF_DONE ||
1451 buf->vb.state == VIDEOBUF_ERROR)
1452 return res | POLLIN | POLLRDNORM;
Hans Verkuil770056c2012-09-11 11:50:37 -03001453 } else if (req_events & (POLLIN | POLLRDNORM)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001454 /* read() capture */
Hans Verkuil770056c2012-09-11 11:50:37 -03001455 return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001456 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001457 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001458}
1459
Hans Verkuil14a09da2012-06-24 07:26:46 -03001460static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1461{
1462 struct tm6000_fh *fh = file->private_data;
1463 struct tm6000_core *dev = fh->dev;
1464 unsigned int res;
1465
1466 mutex_lock(&dev->lock);
1467 res = __tm6000_poll(file, wait);
1468 mutex_unlock(&dev->lock);
1469 return res;
1470}
1471
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001472static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001473{
1474 struct tm6000_fh *fh = file->private_data;
1475 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001476 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001477
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001478 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1479 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001480
Hans Verkuil14a09da2012-06-24 07:26:46 -03001481 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001482 dev->users--;
1483
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001484 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001485
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001486 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001487 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001488
Stefan Ringel8159c182011-11-28 15:46:18 -03001489 /* Stop interrupt USB pipe */
1490 tm6000_ir_int_stop(dev);
1491
1492 usb_reset_configuration(dev->udev);
1493
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001494 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001495 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001496 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001497 else
1498 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001499 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001500
1501 /* Start interrupt USB pipe */
1502 tm6000_ir_int_start(dev);
1503
Thierry Redingaa4a5832011-08-04 04:14:15 -03001504 if (!fh->radio)
1505 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001506 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001507 v4l2_fh_del(&fh->fh);
1508 v4l2_fh_exit(&fh->fh);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001509 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001510 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001511
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001512 return 0;
1513}
1514
1515static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1516{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001517 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001518 struct tm6000_core *dev = fh->dev;
1519 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001520
Hans Verkuil14a09da2012-06-24 07:26:46 -03001521 if (mutex_lock_interruptible(&dev->lock))
1522 return -ERESTARTSYS;
1523 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1524 mutex_unlock(&dev->lock);
1525 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001526}
1527
Bhumika Goyalff05c982017-06-29 02:55:23 -04001528static const struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001529 .owner = THIS_MODULE,
1530 .open = tm6000_open,
1531 .release = tm6000_release,
1532 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1533 .read = tm6000_read,
1534 .poll = tm6000_poll,
1535 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001536};
1537
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001538static const struct v4l2_ioctl_ops video_ioctl_ops = {
1539 .vidioc_querycap = vidioc_querycap,
1540 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1541 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1542 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1543 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1544 .vidioc_s_std = vidioc_s_std,
Hans Verkuil804be2d2013-06-03 05:36:44 -03001545 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001546 .vidioc_enum_input = vidioc_enum_input,
1547 .vidioc_g_input = vidioc_g_input,
1548 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001549 .vidioc_g_tuner = vidioc_g_tuner,
1550 .vidioc_s_tuner = vidioc_s_tuner,
1551 .vidioc_g_frequency = vidioc_g_frequency,
1552 .vidioc_s_frequency = vidioc_s_frequency,
1553 .vidioc_streamon = vidioc_streamon,
1554 .vidioc_streamoff = vidioc_streamoff,
1555 .vidioc_reqbufs = vidioc_reqbufs,
1556 .vidioc_querybuf = vidioc_querybuf,
1557 .vidioc_qbuf = vidioc_qbuf,
1558 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil770056c2012-09-11 11:50:37 -03001559 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1560 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001561};
1562
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001563static struct video_device tm6000_template = {
1564 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001565 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001566 .ioctl_ops = &video_ioctl_ops,
Hans Verkuil65b88c02015-03-09 13:34:10 -03001567 .release = video_device_release_empty,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001568 .tvnorms = TM6000_STD,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001569};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001570
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001571static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001572 .owner = THIS_MODULE,
1573 .open = tm6000_open,
Hans Verkuil52dec542012-09-11 11:53:51 -03001574 .poll = v4l2_ctrl_poll,
Stefan Ringel1f385712011-05-09 16:54:01 -03001575 .release = tm6000_release,
1576 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001577};
1578
1579static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001580 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001581 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001582 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001583 .vidioc_g_frequency = vidioc_g_frequency,
1584 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil770056c2012-09-11 11:50:37 -03001585 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1586 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001587};
1588
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001589static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001590 .name = "tm6000",
1591 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001592 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001593};
1594
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001595/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001596 * Initialization and module stuff
1597 * ------------------------------------------------------------------
1598 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001599
Hans Verkuil65b88c02015-03-09 13:34:10 -03001600static void vdev_init(struct tm6000_core *dev,
1601 struct video_device *vfd,
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001602 const struct video_device
1603 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001604{
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001605 *vfd = *template;
1606 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil65b88c02015-03-09 13:34:10 -03001607 vfd->release = video_device_release_empty;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001608 vfd->lock = &dev->lock;
1609
1610 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1611
1612 video_set_drvdata(vfd, dev);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001613}
1614
1615int tm6000_v4l2_register(struct tm6000_core *dev)
1616{
Hans Verkuil9f747352013-01-31 08:23:01 -03001617 int ret = 0;
1618
1619 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1620 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1621 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1622 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1623 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1624 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1625 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1626 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1627 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1628 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1629 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1630 V4L2_CID_SATURATION, 0, 255, 1, 112);
1631 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1632 V4L2_CID_HUE, -128, 127, 1, 0);
1633 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1634 &dev->radio_ctrl_handler, NULL);
1635
1636 if (dev->radio_ctrl_handler.error)
1637 ret = dev->radio_ctrl_handler.error;
1638 if (!ret && dev->ctrl_handler.error)
1639 ret = dev->ctrl_handler.error;
1640 if (ret)
1641 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001642
Hans Verkuil65b88c02015-03-09 13:34:10 -03001643 vdev_init(dev, &dev->vfd, &tm6000_template, "video");
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001644
Hans Verkuil65b88c02015-03-09 13:34:10 -03001645 dev->vfd.ctrl_handler = &dev->ctrl_handler;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001646
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001647 /* init video dma queues */
1648 INIT_LIST_HEAD(&dev->vidq.active);
1649 INIT_LIST_HEAD(&dev->vidq.queued);
1650
Hans Verkuil65b88c02015-03-09 13:34:10 -03001651 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001652
1653 if (ret < 0) {
1654 printk(KERN_INFO "%s: can't register video device\n",
1655 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001656 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001657 }
1658
1659 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001660 dev->name, video_device_node_name(&dev->vfd));
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001661
Stefan Ringel14169102011-05-09 16:53:49 -03001662 if (dev->caps.has_radio) {
Hans Verkuil65b88c02015-03-09 13:34:10 -03001663 vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
Stefan Ringel14169102011-05-09 16:53:49 -03001664 "radio");
Hans Verkuil65b88c02015-03-09 13:34:10 -03001665 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1666 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Stefan Ringel14169102011-05-09 16:53:49 -03001667 radio_nr);
1668 if (ret < 0) {
1669 printk(KERN_INFO "%s: can't register radio device\n",
1670 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001671 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001672 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001673
Stefan Ringel14169102011-05-09 16:53:49 -03001674 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001675 dev->name, video_device_node_name(&dev->radio_dev));
Stefan Ringel14169102011-05-09 16:53:49 -03001676 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001677
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001678 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001679 return ret;
Hans Verkuil9f747352013-01-31 08:23:01 -03001680
1681unreg_video:
Hans Verkuil65b88c02015-03-09 13:34:10 -03001682 video_unregister_device(&dev->vfd);
Hans Verkuil9f747352013-01-31 08:23:01 -03001683free_ctrl:
1684 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1685 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1686 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001687}
1688
1689int tm6000_v4l2_unregister(struct tm6000_core *dev)
1690{
Hans Verkuil65b88c02015-03-09 13:34:10 -03001691 video_unregister_device(&dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001692
Julian Scheel16427fa2012-10-04 10:04:28 -03001693 /* if URB buffers are still allocated free them now */
1694 tm6000_free_urb_buffers(dev);
1695
Hans Verkuil65b88c02015-03-09 13:34:10 -03001696 video_unregister_device(&dev->radio_dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001697 return 0;
1698}
1699
1700int tm6000_v4l2_exit(void)
1701{
1702 return 0;
1703}
1704
1705module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001706MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001707
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001708module_param_named(debug, tm6000_debug, int, 0444);
1709MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001710
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001711module_param(vid_limit, int, 0644);
1712MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001713
Julian Scheel16427fa2012-10-04 10:04:28 -03001714module_param(keep_urb, bool, 0);
1715MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");