blob: c397aa213601eca124dce1dc529974cedba002fb [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
54#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
86MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080088
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030089static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030090module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080092
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030093/* supported video standards */
94static struct em28xx_fmt format[] = {
95 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030096 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030099 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300102 .fourcc = V4L2_PIX_FMT_RGB565,
103 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300104 .reg = EM28XX_OUTFMT_RGB_16_656,
105 }, {
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .depth = 8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
110 }, {
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
113 .depth = 8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
115 }, {
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
120 }, {
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
123 .depth = 12,
124 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300125 },
126};
127
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300130static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800131 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200132 .id = V4L2_CID_AUDIO_VOLUME,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Volume",
135 .minimum = 0x0,
136 .maximum = 0x1f,
137 .step = 0x1,
138 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300139 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300140 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200141 .id = V4L2_CID_AUDIO_MUTE,
142 .type = V4L2_CTRL_TYPE_BOOLEAN,
143 .name = "Mute",
144 .minimum = 0,
145 .maximum = 1,
146 .step = 1,
147 .default_value = 1,
148 .flags = 0,
149 }
150};
151
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300152/* ------------------------------------------------------------------
153 DMA and thread functions
154 ------------------------------------------------------------------*/
155
156/*
157 * Announces that a buffer were filled and request the next
158 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300159static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
162{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300167 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer74209dc2012-11-08 14:11:37 -0300169 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
175static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
178{
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300184 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300185
Frank Schaefer74209dc2012-11-08 14:11:37 -0300186 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
190}
191
192/*
193 * Identify the buffer header type and properly handles
194 */
195static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
198 unsigned char *p,
199 unsigned char *outp, unsigned long len)
200{
201 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300208 startread = p;
209 remain = len;
210
Frank Schaeferc02ec712012-11-08 14:11:33 -0300211 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300218
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
221 else
222 offset = linesdone * bytesperline * 2 + currlinedone;
223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = lencopy > remain ? remain : lencopy;
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300234 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300235 if (lencopy <= 0)
236 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300238
239 remain -= lencopy;
240
241 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300242 if (dev->progressive)
243 startwrite += lencopy;
244 else
245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300372 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
379
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300380 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300383
Frank Schaefer74209dc2012-11-08 14:11:37 -0300384 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385
386 return;
387}
388
389/*
390 * video-buf generic routine to get the next available VBI buffer
391 */
392static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
394{
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396 char *outp;
397
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300400 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300401 *buf = NULL;
402 return;
403 }
404
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
410
Frank Schaefer74209dc2012-11-08 14:11:37 -0300411 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414}
415
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300416/* Processes and copies the URB data content to a frame buffer queue */
417static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300418{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300419 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300420 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300421 int xfer_bulk, num_packets, i, rc = 1;
422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424
425 if (!dev)
426 return 0;
427
428 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
429 return 0;
430
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300431 if (urb->status < 0)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300433
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300434 xfer_bulk = usb_pipebulk(urb->pipe);
435
Frank Schaefer74209dc2012-11-08 14:11:37 -0300436 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300437 if (buf != NULL)
438 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300439
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300440 if (xfer_bulk) /* bulk */
441 num_packets = 1;
442 else /* isoc */
443 num_packets = urb->number_of_packets;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300445 for (i = 0; i < num_packets; i++) {
446 if (xfer_bulk) { /* bulk */
447 actual_length = urb->actual_length;
448
449 p = urb->transfer_buffer;
450 } else { /* isoc */
451 if (urb->iso_frame_desc[i].status < 0) {
452 print_err_status(dev, i,
453 urb->iso_frame_desc[i].status);
454 if (urb->iso_frame_desc[i].status != -EPROTO)
455 continue;
456 }
457
458 actual_length = urb->iso_frame_desc[i].actual_length;
459 if (actual_length > dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 continue;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300462 }
463
464 p = urb->transfer_buffer +
465 urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 }
467
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300468 if (actual_length <= 0) {
469 /* NOTE: happens very often with isoc transfers */
470 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300471 continue;
472 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473
474 /* FIXME: incomplete buffer checks where removed to make
475 logic simpler. Impacts of those changes should be evaluated
476 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300477 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478 em28xx_isocdbg("VBI HEADER!!!\n");
479 /* FIXME: Should add vbi copy */
480 continue;
481 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300482 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300483 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300484 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300486 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300487 if (buf != NULL)
488 buffer_filled(dev, dma_q, buf);
489 get_next_buf(dma_q, &buf);
490 if (buf == NULL)
491 outp = NULL;
492 else
493 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300494 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300495
496 if (buf != NULL) {
497 if (p[2] & 1)
498 buf->top_field = 0;
499 else
500 buf->top_field = 1;
501 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300502
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300505 if (buf != NULL) {
506 if (p[0] != 0x88 && p[0] != 0x22) {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300507 /* NOTE: no intermediate data packet header
508 * 88 88 88 88 when using bulk transfers */
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300509 em28xx_isocdbg("frame is not complete\n");
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300510 len = actual_length;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300511 } else {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300512 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300513 p += 4;
514 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300515 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300516 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300517 }
518 return rc;
519}
520
Frank Schaefer4601cc32012-11-08 14:11:46 -0300521/* Version of the urb data handler that takes into account a mixture of
522 video and VBI data */
523static inline int em28xx_urb_data_copy_vbi(struct em28xx *dev, struct urb *urb)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300524{
525 struct em28xx_buffer *buf, *vbi_buf;
526 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300527 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer79ff8692012-11-25 06:37:36 -0300528 int xfer_bulk, num_packets, i, rc = 1;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300529 unsigned int actual_length, len = 0;
530 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300531
532 if (!dev)
533 return 0;
534
535 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
536 return 0;
537
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300538 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300539 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300540
Frank Schaefer4601cc32012-11-08 14:11:46 -0300541 xfer_bulk = usb_pipebulk(urb->pipe);
542
Frank Schaefer74209dc2012-11-08 14:11:37 -0300543 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300544 if (buf != NULL)
545 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300546
Frank Schaefer74209dc2012-11-08 14:11:37 -0300547 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300548 if (vbi_buf != NULL)
549 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
550
Frank Schaefer4601cc32012-11-08 14:11:46 -0300551 if (xfer_bulk) /* bulk */
552 num_packets = 1;
553 else /* isoc */
554 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300555
Frank Schaefer4601cc32012-11-08 14:11:46 -0300556 for (i = 0; i < num_packets; i++) {
557 if (xfer_bulk) { /* bulk */
558 actual_length = urb->actual_length;
559
560 p = urb->transfer_buffer;
561 } else { /* isoc */
562 if (urb->iso_frame_desc[i].status < 0) {
563 print_err_status(dev, i,
564 urb->iso_frame_desc[i].status);
565 if (urb->iso_frame_desc[i].status != -EPROTO)
566 continue;
567 }
568
569 actual_length = urb->iso_frame_desc[i].actual_length;
570 if (actual_length > dev->max_pkt_size) {
571 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300572 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300573 }
574
575 p = urb->transfer_buffer +
576 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300577 }
578
Frank Schaefer3610f582012-11-25 06:37:33 -0300579 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300580 /* NOTE: happens very often with isoc transfers */
581 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300582 continue;
583 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300584
585 /* capture type 0 = vbi start
586 capture type 1 = video start
587 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300588 len = actual_length;
589 if (len >= 4) {
590 /* NOTE: headers are always 4 bytes and
591 * never split across packets */
592 if (p[0] == 0x33 && p[1] == 0x95) {
593 dev->capture_type = 0;
594 dev->vbi_read = 0;
595 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300596 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300597 p += 4;
598 len -= 4;
599 } else if (p[0] == 0x88 && p[1] == 0x88 &&
600 p[2] == 0x88 && p[3] == 0x88) {
601 /* continuation */
602 p += 4;
603 len -= 4;
604 } else if (p[0] == 0x22 && p[1] == 0x5a) {
605 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300606 dev->capture_type = 1;
607 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300608 p += 4;
609 len -= 4;
610 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300611 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300612 /* NOTE: with bulk transfers, intermediate data packets
613 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300614
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300615 if (dev->capture_type == 0) {
Frank Schaefer79ff8692012-11-25 06:37:36 -0300616 int vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300617 if (dev->vbi_read >= vbi_size) {
618 /* We've already read all the VBI data, so
619 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300620 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300621 } else if ((dev->vbi_read + len) < vbi_size) {
622 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300623 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300624 /* Brand new frame */
625 if (vbi_buf != NULL)
626 vbi_buffer_filled(dev,
627 vbi_dma_q,
628 vbi_buf);
629 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
630 if (vbi_buf == NULL)
631 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300632 else
633 vbioutp = videobuf_to_vmalloc(
634 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300635 }
636
637 if (dev->vbi_read == 0) {
638 vbi_dma_q->pos = 0;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300639 if (vbi_buf != NULL)
640 vbi_buf->top_field = dev->top_field;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300641 }
642
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300643 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300644 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
645 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300646 } else {
647 /* Some of this frame is VBI data and some is
648 video data */
649 int vbi_data_len = vbi_size - dev->vbi_read;
650 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300651 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
652 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300653 dev->capture_type = 1;
654 p += vbi_data_len;
655 len -= vbi_data_len;
656 }
657 }
658
659 if (dev->capture_type == 1) {
660 dev->capture_type = 2;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300661 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300662 if (buf != NULL)
663 buffer_filled(dev, dma_q, buf);
664 get_next_buf(dma_q, &buf);
665 if (buf == NULL)
666 outp = NULL;
667 else
668 outp = videobuf_to_vmalloc(&buf->vb);
669 }
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300670 if (buf != NULL)
671 buf->top_field = dev->top_field;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300672
673 dma_q->pos = 0;
674 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300675
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300676 if (buf != NULL && dev->capture_type == 2 && len > 0)
677 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300678 }
679 return rc;
680}
681
682
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300683/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300684 Videobuf operations
685 ------------------------------------------------------------------*/
686
687static int
688buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
689{
690 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300691 struct em28xx *dev = fh->dev;
692 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300693
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300694 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
695 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300696
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300697 if (0 == *count)
698 *count = EM28XX_DEF_BUF;
699
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300700 if (*count < EM28XX_MIN_BUF)
701 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300702
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300703 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300704 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300705 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300706 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300707
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300708 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300709
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300710 return 0;
711}
712
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300713/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300714static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
715{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300716 struct em28xx_fh *fh = vq->priv_data;
717 struct em28xx *dev = fh->dev;
718 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300719 if (in_interrupt())
720 BUG();
721
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300722 /* We used to wait for the buffer to finish here, but this didn't work
723 because, as we were keeping the state as VIDEOBUF_QUEUED,
724 videobuf_queue_cancel marked it as finished for us.
725 (Also, it could wedge forever if the hardware was misconfigured.)
726
727 This should be safe; by the time we get here, the buffer isn't
728 queued anymore. If we ever start marking the buffers as
729 VIDEOBUF_ACTIVE, it won't be, though.
730 */
731 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300732 if (dev->usb_ctl.vid_buf == buf)
733 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300734 spin_unlock_irqrestore(&dev->slock, flags);
735
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300736 videobuf_vmalloc_free(&buf->vb);
737 buf->vb.state = VIDEOBUF_NEEDS_INIT;
738}
739
740static int
741buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
742 enum v4l2_field field)
743{
744 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300745 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746 struct em28xx *dev = fh->dev;
747 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300748
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300749 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
750 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300751
752 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
753 return -EINVAL;
754
Brandon Philips05612972008-04-13 14:57:01 -0300755 buf->vb.width = dev->width;
756 buf->vb.height = dev->height;
757 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300758
759 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300760 rc = videobuf_iolock(vq, &buf->vb, NULL);
761 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300762 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763 }
764
Frank Schaefer74209dc2012-11-08 14:11:37 -0300765 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300766 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767
768 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300769 dev->capture_type = -1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300770 if (em28xx_vbi_supported(dev) == 1)
Frank Schaeferc647a912012-11-08 14:11:52 -0300771 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
772 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300773 EM28XX_NUM_BUFS,
774 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300775 dev->packet_multiplier,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300776 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300777 else
Frank Schaeferc647a912012-11-08 14:11:52 -0300778 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
779 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300780 EM28XX_NUM_BUFS,
781 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300782 dev->packet_multiplier,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300783 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300784 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300785 goto fail;
786 }
787
788 buf->vb.state = VIDEOBUF_PREPARED;
789 return 0;
790
791fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300792 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300793 return rc;
794}
795
796static void
797buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
798{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300799 struct em28xx_buffer *buf = container_of(vb,
800 struct em28xx_buffer,
801 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300802 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300803 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300804 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300805
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300806 buf->vb.state = VIDEOBUF_QUEUED;
807 list_add_tail(&buf->vb.queue, &vidq->active);
808
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300809}
810
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300811static void buffer_release(struct videobuf_queue *vq,
812 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300813{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300814 struct em28xx_buffer *buf = container_of(vb,
815 struct em28xx_buffer,
816 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300817 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300818 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300819
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300820 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300821
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300822 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300823}
824
825static struct videobuf_queue_ops em28xx_video_qops = {
826 .buf_setup = buffer_setup,
827 .buf_prepare = buffer_prepare,
828 .buf_queue = buffer_queue,
829 .buf_release = buffer_release,
830};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800831
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300832/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800833
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800834static void video_mux(struct em28xx *dev, int index)
835{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800836 dev->ctl_input = index;
837 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300838 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800839
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300840 if (!dev->ctl_aoutput)
841 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
842
Hans Verkuil5325b422009-04-02 11:26:22 -0300843 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
844 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800845
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300846 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300847 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300848 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
849 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300850 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300851 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300852 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
853 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800854 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300855
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300856 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300857 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
858 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300859 }
860
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300861 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800862}
863
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300864/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300865static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300866{
867 struct em28xx *dev = fh->dev;
868
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869 if (fh->resources & bit)
870 /* have it already allocated */
871 return 1;
872
873 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300874 if (dev->resources & bit) {
875 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300876 return 0;
877 }
878 /* it's free, grab it */
879 fh->resources |= bit;
880 dev->resources |= bit;
881 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300882 return 1;
883}
884
885static int res_check(struct em28xx_fh *fh, unsigned int bit)
886{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300887 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300888}
889
890static int res_locked(struct em28xx *dev, unsigned int bit)
891{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300892 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300893}
894
895static void res_free(struct em28xx_fh *fh, unsigned int bits)
896{
897 struct em28xx *dev = fh->dev;
898
899 BUG_ON((fh->resources & bits) != bits);
900
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300901 fh->resources &= ~bits;
902 dev->resources &= ~bits;
903 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300904}
905
906static int get_ressource(struct em28xx_fh *fh)
907{
908 switch (fh->type) {
909 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
910 return EM28XX_RESOURCE_VIDEO;
911 case V4L2_BUF_TYPE_VBI_CAPTURE:
912 return EM28XX_RESOURCE_VBI;
913 default:
914 BUG();
915 return 0;
916 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300917}
918
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800919/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300920 * ac97_queryctrl()
921 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300922 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300923static int ac97_queryctrl(struct v4l2_queryctrl *qc)
924{
925 int i;
926
927 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
928 if (qc->id && qc->id == ac97_qctrl[i].id) {
929 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
930 return 0;
931 }
932 }
933
934 /* Control is not ac97 related */
935 return 1;
936}
937
938/*
939 * ac97_get_ctrl()
940 * return the current values for ac97 mute and volume
941 */
942static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300943{
944 switch (ctrl->id) {
945 case V4L2_CID_AUDIO_MUTE:
946 ctrl->value = dev->mute;
947 return 0;
948 case V4L2_CID_AUDIO_VOLUME:
949 ctrl->value = dev->volume;
950 return 0;
951 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300952 /* Control is not ac97 related */
953 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300954 }
955}
956
957/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300958 * ac97_set_ctrl()
959 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300961static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300963 int i;
964
965 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
966 if (ctrl->id == ac97_qctrl[i].id)
967 goto handle;
968
969 /* Announce that hasn't handle it */
970 return 1;
971
972handle:
973 if (ctrl->value < ac97_qctrl[i].minimum ||
974 ctrl->value > ac97_qctrl[i].maximum)
975 return -ERANGE;
976
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300977 switch (ctrl->id) {
978 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300979 dev->mute = ctrl->value;
980 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300981 case V4L2_CID_AUDIO_VOLUME:
982 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300983 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300985
986 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300987}
988
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300989static int check_dev(struct em28xx *dev)
990{
991 if (dev->state & DEV_DISCONNECTED) {
992 em28xx_errdev("v4l2 ioctl: device not present\n");
993 return -ENODEV;
994 }
995
996 if (dev->state & DEV_MISCONFIGURED) {
997 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
998 "close and open it again\n");
999 return -EIO;
1000 }
1001 return 0;
1002}
1003
1004static void get_scale(struct em28xx *dev,
1005 unsigned int width, unsigned int height,
1006 unsigned int *hscale, unsigned int *vscale)
1007{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001008 unsigned int maxw = norm_maxw(dev);
1009 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001010
1011 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1012 if (*hscale >= 0x4000)
1013 *hscale = 0x3fff;
1014
1015 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1016 if (*vscale >= 0x4000)
1017 *vscale = 0x3fff;
1018}
1019
1020/* ------------------------------------------------------------------
1021 IOCTL vidioc handling
1022 ------------------------------------------------------------------*/
1023
Hans Verkuil78b526a2008-05-28 12:16:41 -03001024static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001025 struct v4l2_format *f)
1026{
1027 struct em28xx_fh *fh = priv;
1028 struct em28xx *dev = fh->dev;
1029
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030 f->fmt.pix.width = dev->width;
1031 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001032 f->fmt.pix.pixelformat = dev->format->fourcc;
1033 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001034 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1036
1037 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001038 if (dev->progressive)
1039 f->fmt.pix.field = V4L2_FIELD_NONE;
1040 else
1041 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001042 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043 return 0;
1044}
1045
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001046static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1047{
1048 unsigned int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(format); i++)
1051 if (format[i].fourcc == fourcc)
1052 return &format[i];
1053
1054 return NULL;
1055}
1056
Hans Verkuil78b526a2008-05-28 12:16:41 -03001057static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001058 struct v4l2_format *f)
1059{
1060 struct em28xx_fh *fh = priv;
1061 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001062 unsigned int width = f->fmt.pix.width;
1063 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064 unsigned int maxw = norm_maxw(dev);
1065 unsigned int maxh = norm_maxh(dev);
1066 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001067 struct em28xx_fmt *fmt;
1068
1069 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1070 if (!fmt) {
1071 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1072 f->fmt.pix.pixelformat);
1073 return -EINVAL;
1074 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001075
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001076 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001078 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1079 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001080 /* MaxPacketSize for em2800 is too small to capture at full resolution
1081 * use half of maxw as the scaler can only scale to 50% */
1082 if (width == maxw && height == maxh)
1083 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001084 } else {
1085 /* width must even because of the YUYV format
1086 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001087 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1088 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001089 }
1090
1091 get_scale(dev, width, height, &hscale, &vscale);
1092
1093 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1094 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1095
1096 f->fmt.pix.width = width;
1097 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001098 f->fmt.pix.pixelformat = fmt->fourcc;
1099 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1100 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001101 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001102 if (dev->progressive)
1103 f->fmt.pix.field = V4L2_FIELD_NONE;
1104 else
1105 f->fmt.pix.field = dev->interlaced ?
1106 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001107
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001108 return 0;
1109}
1110
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001111static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1112 unsigned width, unsigned height)
1113{
1114 struct em28xx_fmt *fmt;
1115
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001116 fmt = format_by_fourcc(fourcc);
1117 if (!fmt)
1118 return -EINVAL;
1119
1120 dev->format = fmt;
1121 dev->width = width;
1122 dev->height = height;
1123
1124 /* set new image size */
1125 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1126
1127 em28xx_set_alternate(dev);
1128 em28xx_resolution_set(dev);
1129
1130 return 0;
1131}
1132
Hans Verkuil78b526a2008-05-28 12:16:41 -03001133static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134 struct v4l2_format *f)
1135{
1136 struct em28xx_fh *fh = priv;
1137 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001138 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001139
1140 rc = check_dev(dev);
1141 if (rc < 0)
1142 return rc;
1143
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001144 vidioc_try_fmt_vid_cap(file, priv, f);
1145
Brandon Philips05612972008-04-13 14:57:01 -03001146 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1147 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001148 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001149 }
1150
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001151 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001152 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001153}
1154
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001155static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1156{
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001159 int rc;
1160
1161 rc = check_dev(dev);
1162 if (rc < 0)
1163 return rc;
1164
1165 *norm = dev->norm;
1166
1167 return 0;
1168}
1169
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001170static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1171{
1172 struct em28xx_fh *fh = priv;
1173 struct em28xx *dev = fh->dev;
1174 int rc;
1175
1176 rc = check_dev(dev);
1177 if (rc < 0)
1178 return rc;
1179
1180 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1181
1182 return 0;
1183}
1184
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001185static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001186{
1187 struct em28xx_fh *fh = priv;
1188 struct em28xx *dev = fh->dev;
1189 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001190 int rc;
1191
1192 rc = check_dev(dev);
1193 if (rc < 0)
1194 return rc;
1195
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001196 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197
1198 /* Adjusts width/height, if needed */
1199 f.fmt.pix.width = dev->width;
1200 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001201 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001202
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203 /* set new image size */
1204 dev->width = f.fmt.pix.width;
1205 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1207
1208 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001209 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001210
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001211 return 0;
1212}
1213
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001214static int vidioc_g_parm(struct file *file, void *priv,
1215 struct v4l2_streamparm *p)
1216{
1217 struct em28xx_fh *fh = priv;
1218 struct em28xx *dev = fh->dev;
1219 int rc = 0;
1220
1221 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1222 return -EINVAL;
1223
1224 if (dev->board.is_webcam)
1225 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1226 video, g_parm, p);
1227 else
1228 v4l2_video_std_frame_period(dev->norm,
1229 &p->parm.capture.timeperframe);
1230
1231 return rc;
1232}
1233
1234static int vidioc_s_parm(struct file *file, void *priv,
1235 struct v4l2_streamparm *p)
1236{
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1239
1240 if (!dev->board.is_webcam)
1241 return -EINVAL;
1242
1243 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1244 return -EINVAL;
1245
1246 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1247}
1248
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001249static const char *iname[] = {
1250 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1251 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1252 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1253 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1254 [EM28XX_VMUX_SVIDEO] = "S-Video",
1255 [EM28XX_VMUX_TELEVISION] = "Television",
1256 [EM28XX_VMUX_CABLE] = "Cable TV",
1257 [EM28XX_VMUX_DVB] = "DVB",
1258 [EM28XX_VMUX_DEBUG] = "for debug only",
1259};
1260
1261static int vidioc_enum_input(struct file *file, void *priv,
1262 struct v4l2_input *i)
1263{
1264 struct em28xx_fh *fh = priv;
1265 struct em28xx *dev = fh->dev;
1266 unsigned int n;
1267
1268 n = i->index;
1269 if (n >= MAX_EM28XX_INPUT)
1270 return -EINVAL;
1271 if (0 == INPUT(n)->type)
1272 return -EINVAL;
1273
1274 i->index = n;
1275 i->type = V4L2_INPUT_TYPE_CAMERA;
1276
1277 strcpy(i->name, iname[INPUT(n)->type]);
1278
1279 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1280 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1281 i->type = V4L2_INPUT_TYPE_TUNER;
1282
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001283 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001284
1285 return 0;
1286}
1287
1288static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1289{
1290 struct em28xx_fh *fh = priv;
1291 struct em28xx *dev = fh->dev;
1292
1293 *i = dev->ctl_input;
1294
1295 return 0;
1296}
1297
1298static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1299{
1300 struct em28xx_fh *fh = priv;
1301 struct em28xx *dev = fh->dev;
1302 int rc;
1303
1304 rc = check_dev(dev);
1305 if (rc < 0)
1306 return rc;
1307
1308 if (i >= MAX_EM28XX_INPUT)
1309 return -EINVAL;
1310 if (0 == INPUT(i)->type)
1311 return -EINVAL;
1312
Ezequiel García96371fc2012-03-23 18:09:34 -03001313 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001314 return 0;
1315}
1316
1317static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1318{
1319 struct em28xx_fh *fh = priv;
1320 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001322 if (!dev->audio_mode.has_audio)
1323 return -EINVAL;
1324
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001325 switch (a->index) {
1326 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001327 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001328 break;
1329 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001331 break;
1332 case EM28XX_AMUX_VIDEO2:
1333 strcpy(a->name, "Television alt");
1334 break;
1335 case EM28XX_AMUX_PHONE:
1336 strcpy(a->name, "Phone");
1337 break;
1338 case EM28XX_AMUX_MIC:
1339 strcpy(a->name, "Mic");
1340 break;
1341 case EM28XX_AMUX_CD:
1342 strcpy(a->name, "CD");
1343 break;
1344 case EM28XX_AMUX_AUX:
1345 strcpy(a->name, "Aux");
1346 break;
1347 case EM28XX_AMUX_PCM_OUT:
1348 strcpy(a->name, "PCM");
1349 break;
1350 default:
1351 return -EINVAL;
1352 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001353
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001354 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356
1357 return 0;
1358}
1359
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001360static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001361{
1362 struct em28xx_fh *fh = priv;
1363 struct em28xx *dev = fh->dev;
1364
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001365
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001366 if (!dev->audio_mode.has_audio)
1367 return -EINVAL;
1368
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001369 if (a->index >= MAX_EM28XX_INPUT)
1370 return -EINVAL;
1371 if (0 == INPUT(a->index)->type)
1372 return -EINVAL;
1373
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001374 dev->ctl_ainput = INPUT(a->index)->amux;
1375 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001376
1377 if (!dev->ctl_aoutput)
1378 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001379
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001380 return 0;
1381}
1382
1383static int vidioc_queryctrl(struct file *file, void *priv,
1384 struct v4l2_queryctrl *qc)
1385{
1386 struct em28xx_fh *fh = priv;
1387 struct em28xx *dev = fh->dev;
1388 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389 int rc;
1390
1391 rc = check_dev(dev);
1392 if (rc < 0)
1393 return rc;
1394
1395 memset(qc, 0, sizeof(*qc));
1396
1397 qc->id = id;
1398
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001399 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001400 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1401 rc = ac97_queryctrl(qc);
1402 if (!rc)
1403 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001404 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001405
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001406 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001407 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408
1409 if (qc->type)
1410 return 0;
1411 else
1412 return -EINVAL;
1413}
1414
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001415/*
1416 * FIXME: This is an indirect way to check if a control exists at a
1417 * subdev. Instead of that hack, maybe the better would be to change all
1418 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1419 */
1420static int check_subdev_ctrl(struct em28xx *dev, int id)
1421{
1422 struct v4l2_queryctrl qc;
1423
1424 memset(&qc, 0, sizeof(qc));
1425 qc.id = id;
1426
1427 /* enumerate V4L2 device controls */
1428 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1429
1430 if (qc.type)
1431 return 0;
1432 else
1433 return -EINVAL;
1434}
1435
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436static int vidioc_g_ctrl(struct file *file, void *priv,
1437 struct v4l2_control *ctrl)
1438{
1439 struct em28xx_fh *fh = priv;
1440 struct em28xx *dev = fh->dev;
1441 int rc;
1442
1443 rc = check_dev(dev);
1444 if (rc < 0)
1445 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001446 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001447
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001448 /* Set an AC97 control */
1449 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1450 rc = ac97_get_ctrl(dev, ctrl);
1451 else
1452 rc = 1;
1453
1454 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1455 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001456 if (check_subdev_ctrl(dev, ctrl->id))
1457 return -EINVAL;
1458
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001459 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001460 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001461 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001463 return rc;
1464}
1465
1466static int vidioc_s_ctrl(struct file *file, void *priv,
1467 struct v4l2_control *ctrl)
1468{
1469 struct em28xx_fh *fh = priv;
1470 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001471 int rc;
1472
1473 rc = check_dev(dev);
1474 if (rc < 0)
1475 return rc;
1476
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001477 /* Set an AC97 control */
1478 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1479 rc = ac97_set_ctrl(dev, ctrl);
1480 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001481 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001483 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001484 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001485 rc = check_subdev_ctrl(dev, ctrl->id);
1486 if (!rc)
1487 v4l2_device_call_all(&dev->v4l2_dev, 0,
1488 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001489 /*
1490 * In the case of non-AC97 volume controls, we still need
1491 * to do some setups at em28xx, in order to mute/unmute
1492 * and to adjust audio volume. However, the value ranges
1493 * should be checked by the corresponding V4L subdriver.
1494 */
1495 switch (ctrl->id) {
1496 case V4L2_CID_AUDIO_MUTE:
1497 dev->mute = ctrl->value;
1498 rc = em28xx_audio_analog_set(dev);
1499 break;
1500 case V4L2_CID_AUDIO_VOLUME:
1501 dev->volume = ctrl->value;
1502 rc = em28xx_audio_analog_set(dev);
1503 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001504 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001505 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001506}
1507
1508static int vidioc_g_tuner(struct file *file, void *priv,
1509 struct v4l2_tuner *t)
1510{
1511 struct em28xx_fh *fh = priv;
1512 struct em28xx *dev = fh->dev;
1513 int rc;
1514
1515 rc = check_dev(dev);
1516 if (rc < 0)
1517 return rc;
1518
1519 if (0 != t->index)
1520 return -EINVAL;
1521
1522 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001523 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001524
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001525 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001526 return 0;
1527}
1528
1529static int vidioc_s_tuner(struct file *file, void *priv,
1530 struct v4l2_tuner *t)
1531{
1532 struct em28xx_fh *fh = priv;
1533 struct em28xx *dev = fh->dev;
1534 int rc;
1535
1536 rc = check_dev(dev);
1537 if (rc < 0)
1538 return rc;
1539
1540 if (0 != t->index)
1541 return -EINVAL;
1542
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001543 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001544 return 0;
1545}
1546
1547static int vidioc_g_frequency(struct file *file, void *priv,
1548 struct v4l2_frequency *f)
1549{
1550 struct em28xx_fh *fh = priv;
1551 struct em28xx *dev = fh->dev;
1552
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001553 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001554 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555 return 0;
1556}
1557
1558static int vidioc_s_frequency(struct file *file, void *priv,
1559 struct v4l2_frequency *f)
1560{
1561 struct em28xx_fh *fh = priv;
1562 struct em28xx *dev = fh->dev;
1563 int rc;
1564
1565 rc = check_dev(dev);
1566 if (rc < 0)
1567 return rc;
1568
1569 if (0 != f->tuner)
1570 return -EINVAL;
1571
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001572 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1573 return -EINVAL;
1574 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575 return -EINVAL;
1576
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001577 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001578 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001579
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001580 return 0;
1581}
1582
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001583#ifdef CONFIG_VIDEO_ADV_DEBUG
1584static int em28xx_reg_len(int reg)
1585{
1586 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001587 case EM28XX_R40_AC97LSB:
1588 case EM28XX_R30_HSCALELOW:
1589 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001590 return 2;
1591 default:
1592 return 1;
1593 }
1594}
1595
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001596static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001597 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001598{
1599 struct em28xx_fh *fh = priv;
1600 struct em28xx *dev = fh->dev;
1601
1602 chip->ident = V4L2_IDENT_NONE;
1603 chip->revision = 0;
1604
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001605 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001606
1607 return 0;
1608}
1609
1610
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001611static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001612 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001613{
1614 struct em28xx_fh *fh = priv;
1615 struct em28xx *dev = fh->dev;
1616 int ret;
1617
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001618 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001619 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001620 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001621 if (ret < 0)
1622 return ret;
1623
1624 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001625 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001626 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001627 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001628 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001629 return 0;
1630 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001631 /* TODO: is this correct? */
1632 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1633 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001634 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001635 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001636 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001637 }
1638
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001639 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001640 reg->size = em28xx_reg_len(reg->reg);
1641 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001642 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001643
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001644 if (ret < 0)
1645 return ret;
1646
1647 reg->val = ret;
1648 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001649 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001650 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1651 reg->reg, (char *)&val, 2);
1652 if (ret < 0)
1653 return ret;
1654
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001655 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001656 }
1657
1658 return 0;
1659}
1660
1661static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001662 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001663{
1664 struct em28xx_fh *fh = priv;
1665 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001666 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001667
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001668 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001669 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001670 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001671 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001672 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001673 return 0;
1674 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001675 /* TODO: is this correct? */
1676 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1677 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001678 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001679 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001680 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001681 }
1682
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001683 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001684 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001685
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001686 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001687 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001688}
1689#endif
1690
1691
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001692static int vidioc_cropcap(struct file *file, void *priv,
1693 struct v4l2_cropcap *cc)
1694{
1695 struct em28xx_fh *fh = priv;
1696 struct em28xx *dev = fh->dev;
1697
1698 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1699 return -EINVAL;
1700
1701 cc->bounds.left = 0;
1702 cc->bounds.top = 0;
1703 cc->bounds.width = dev->width;
1704 cc->bounds.height = dev->height;
1705 cc->defrect = cc->bounds;
1706 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1707 cc->pixelaspect.denominator = 59;
1708
1709 return 0;
1710}
1711
1712static int vidioc_streamon(struct file *file, void *priv,
1713 enum v4l2_buf_type type)
1714{
1715 struct em28xx_fh *fh = priv;
1716 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001717 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001718
1719 rc = check_dev(dev);
1720 if (rc < 0)
1721 return rc;
1722
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001723 if (unlikely(type != fh->type))
1724 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001725
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001726 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1727 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001728
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001729 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001730 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001731
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001732 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1733 rc = videobuf_streamon(&fh->vb_vidq);
1734 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1735 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001736
1737 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001738}
1739
1740static int vidioc_streamoff(struct file *file, void *priv,
1741 enum v4l2_buf_type type)
1742{
1743 struct em28xx_fh *fh = priv;
1744 struct em28xx *dev = fh->dev;
1745 int rc;
1746
1747 rc = check_dev(dev);
1748 if (rc < 0)
1749 return rc;
1750
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001751 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1752 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001753 return -EINVAL;
1754 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001755 return -EINVAL;
1756
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001757 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1758 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001759
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001760 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001761 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1762 videobuf_streamoff(&fh->vb_vidq);
1763 res_free(fh, EM28XX_RESOURCE_VIDEO);
1764 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001765 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001766 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1767 videobuf_streamoff(&fh->vb_vbiq);
1768 res_free(fh, EM28XX_RESOURCE_VBI);
1769 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001770 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001771
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001772 return 0;
1773}
1774
1775static int vidioc_querycap(struct file *file, void *priv,
1776 struct v4l2_capability *cap)
1777{
1778 struct em28xx_fh *fh = priv;
1779 struct em28xx *dev = fh->dev;
1780
1781 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1782 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001783 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785 cap->capabilities =
1786 V4L2_CAP_SLICED_VBI_CAPTURE |
1787 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001788 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1789
Devin Heitmueller04146142009-09-11 00:08:44 -03001790 if (dev->vbi_dev)
1791 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1792
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001793 if (dev->audio_mode.has_audio)
1794 cap->capabilities |= V4L2_CAP_AUDIO;
1795
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001796 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001797 cap->capabilities |= V4L2_CAP_TUNER;
1798
1799 return 0;
1800}
1801
Hans Verkuil78b526a2008-05-28 12:16:41 -03001802static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001803 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001805 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001806 return -EINVAL;
1807
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001808 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1809 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001810
1811 return 0;
1812}
1813
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001814static int vidioc_enum_framesizes(struct file *file, void *priv,
1815 struct v4l2_frmsizeenum *fsize)
1816{
1817 struct em28xx_fh *fh = priv;
1818 struct em28xx *dev = fh->dev;
1819 struct em28xx_fmt *fmt;
1820 unsigned int maxw = norm_maxw(dev);
1821 unsigned int maxh = norm_maxh(dev);
1822
1823 fmt = format_by_fourcc(fsize->pixel_format);
1824 if (!fmt) {
1825 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1826 fsize->pixel_format);
1827 return -EINVAL;
1828 }
1829
1830 if (dev->board.is_em2800) {
1831 if (fsize->index > 1)
1832 return -EINVAL;
1833 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1834 fsize->discrete.width = maxw / (1 + fsize->index);
1835 fsize->discrete.height = maxh / (1 + fsize->index);
1836 return 0;
1837 }
1838
1839 if (fsize->index != 0)
1840 return -EINVAL;
1841
1842 /* Report a continuous range */
1843 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1844 fsize->stepwise.min_width = 48;
1845 fsize->stepwise.min_height = 32;
1846 fsize->stepwise.max_width = maxw;
1847 fsize->stepwise.max_height = maxh;
1848 fsize->stepwise.step_width = 1;
1849 fsize->stepwise.step_height = 1;
1850 return 0;
1851}
1852
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001853/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001854static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001855 struct v4l2_format *f)
1856{
1857 struct em28xx_fh *fh = priv;
1858 struct em28xx *dev = fh->dev;
1859 int rc;
1860
1861 rc = check_dev(dev);
1862 if (rc < 0)
1863 return rc;
1864
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001865 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001866 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001867
1868 if (f->fmt.sliced.service_set == 0)
1869 rc = -EINVAL;
1870
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001871 return rc;
1872}
1873
Hans Verkuil78b526a2008-05-28 12:16:41 -03001874static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001875 struct v4l2_format *f)
1876{
1877 struct em28xx_fh *fh = priv;
1878 struct em28xx *dev = fh->dev;
1879 int rc;
1880
1881 rc = check_dev(dev);
1882 if (rc < 0)
1883 return rc;
1884
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001885 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001886
1887 if (f->fmt.sliced.service_set == 0)
1888 return -EINVAL;
1889
1890 return 0;
1891}
1892
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001893/* RAW VBI ioctls */
1894
1895static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1896 struct v4l2_format *format)
1897{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001898 struct em28xx_fh *fh = priv;
1899 struct em28xx *dev = fh->dev;
1900
1901 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001902 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1903 format->fmt.vbi.offset = 0;
1904 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001905 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1906 format->fmt.vbi.count[0] = dev->vbi_height;
1907 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001908
1909 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001910 if (dev->norm & V4L2_STD_525_60) {
1911 /* NTSC */
1912 format->fmt.vbi.start[0] = 10;
1913 format->fmt.vbi.start[1] = 273;
1914 } else if (dev->norm & V4L2_STD_625_50) {
1915 /* PAL */
1916 format->fmt.vbi.start[0] = 6;
1917 format->fmt.vbi.start[1] = 318;
1918 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001919
1920 return 0;
1921}
1922
1923static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1924 struct v4l2_format *format)
1925{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001926 struct em28xx_fh *fh = priv;
1927 struct em28xx *dev = fh->dev;
1928
1929 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001930 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1931 format->fmt.vbi.offset = 0;
1932 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001933 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1934 format->fmt.vbi.count[0] = dev->vbi_height;
1935 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001936
1937 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001938 if (dev->norm & V4L2_STD_525_60) {
1939 /* NTSC */
1940 format->fmt.vbi.start[0] = 10;
1941 format->fmt.vbi.start[1] = 273;
1942 } else if (dev->norm & V4L2_STD_625_50) {
1943 /* PAL */
1944 format->fmt.vbi.start[0] = 6;
1945 format->fmt.vbi.start[1] = 318;
1946 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001947
1948 return 0;
1949}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001950
1951static int vidioc_reqbufs(struct file *file, void *priv,
1952 struct v4l2_requestbuffers *rb)
1953{
1954 struct em28xx_fh *fh = priv;
1955 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001956 int rc;
1957
1958 rc = check_dev(dev);
1959 if (rc < 0)
1960 return rc;
1961
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001962 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1963 return videobuf_reqbufs(&fh->vb_vidq, rb);
1964 else
1965 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001966}
1967
1968static int vidioc_querybuf(struct file *file, void *priv,
1969 struct v4l2_buffer *b)
1970{
1971 struct em28xx_fh *fh = priv;
1972 struct em28xx *dev = fh->dev;
1973 int rc;
1974
1975 rc = check_dev(dev);
1976 if (rc < 0)
1977 return rc;
1978
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001979 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1980 return videobuf_querybuf(&fh->vb_vidq, b);
1981 else {
1982 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1983 the videobuf framework, but we probably shouldn't be
1984 returning a buffer larger than that which was asked for.
1985 At a minimum, it causes a crash in zvbi since it does
1986 a memcpy based on the source buffer length */
1987 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001988 b->length = dev->vbi_width * dev->vbi_height * 2;
1989
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001990 return result;
1991 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001992}
1993
1994static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1995{
1996 struct em28xx_fh *fh = priv;
1997 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001998 int rc;
1999
2000 rc = check_dev(dev);
2001 if (rc < 0)
2002 return rc;
2003
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002004 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2005 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002006 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002007 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002008}
2009
2010static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2011{
2012 struct em28xx_fh *fh = priv;
2013 struct em28xx *dev = fh->dev;
2014 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002015
2016 rc = check_dev(dev);
2017 if (rc < 0)
2018 return rc;
2019
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002020 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2021 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2022 O_NONBLOCK);
2023 else
2024 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2025 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002026}
2027
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002028/* ----------------------------------------------------------- */
2029/* RADIO ESPECIFIC IOCTLS */
2030/* ----------------------------------------------------------- */
2031
2032static int radio_querycap(struct file *file, void *priv,
2033 struct v4l2_capability *cap)
2034{
2035 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2036
2037 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2038 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002039 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002040
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002041 cap->capabilities = V4L2_CAP_TUNER;
2042 return 0;
2043}
2044
2045static int radio_g_tuner(struct file *file, void *priv,
2046 struct v4l2_tuner *t)
2047{
2048 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2049
2050 if (unlikely(t->index > 0))
2051 return -EINVAL;
2052
2053 strcpy(t->name, "Radio");
2054 t->type = V4L2_TUNER_RADIO;
2055
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002056 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002057
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002058 return 0;
2059}
2060
2061static int radio_enum_input(struct file *file, void *priv,
2062 struct v4l2_input *i)
2063{
2064 if (i->index != 0)
2065 return -EINVAL;
2066 strcpy(i->name, "Radio");
2067 i->type = V4L2_INPUT_TYPE_TUNER;
2068
2069 return 0;
2070}
2071
2072static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2073{
2074 if (unlikely(a->index))
2075 return -EINVAL;
2076
2077 strcpy(a->name, "Radio");
2078 return 0;
2079}
2080
2081static int radio_s_tuner(struct file *file, void *priv,
2082 struct v4l2_tuner *t)
2083{
2084 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2085
2086 if (0 != t->index)
2087 return -EINVAL;
2088
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002089 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002090
2091 return 0;
2092}
2093
2094static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b2012-09-04 11:59:31 -03002095 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002096{
2097 return 0;
2098}
2099
2100static int radio_s_input(struct file *file, void *fh, unsigned int i)
2101{
2102 return 0;
2103}
2104
2105static int radio_queryctrl(struct file *file, void *priv,
2106 struct v4l2_queryctrl *qc)
2107{
2108 int i;
2109
2110 if (qc->id < V4L2_CID_BASE ||
2111 qc->id >= V4L2_CID_LASTP1)
2112 return -EINVAL;
2113
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002114 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2115 if (qc->id && qc->id == ac97_qctrl[i].id) {
2116 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002117 return 0;
2118 }
2119 }
2120
2121 return -EINVAL;
2122}
2123
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002124/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002125 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126 * inits the device and starts isoc transfer
2127 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002128static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002129{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002130 int errCode = 0, radio = 0;
2131 struct video_device *vdev = video_devdata(filp);
2132 struct em28xx *dev = video_drvdata(filp);
2133 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002134 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002135 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002136
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002137 switch (vdev->vfl_type) {
2138 case VFL_TYPE_GRABBER:
2139 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2140 break;
2141 case VFL_TYPE_VBI:
2142 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2143 break;
2144 case VFL_TYPE_RADIO:
2145 radio = 1;
2146 break;
2147 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002148
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002149 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2150 video_device_node_name(vdev), v4l2_type_names[fh_type],
2151 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002152
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153
Hans Verkuil876cb142012-06-23 08:12:47 -03002154 if (mutex_lock_interruptible(&dev->lock))
2155 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002156 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002157 if (!fh) {
2158 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002159 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002160 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002161 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002162 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002163 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002164 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002165 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002166
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002167 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002168 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002169 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002170 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002172 /* Needed, since GPIO might have disabled power of
2173 some i2c device
2174 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002175 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002176
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002177 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002178 if (fh->radio) {
2179 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002180 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002181 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002182
2183 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002185 if (dev->progressive)
2186 field = V4L2_FIELD_NONE;
2187 else
2188 field = V4L2_FIELD_INTERLACED;
2189
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002190 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2191 NULL, &dev->slock,
2192 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002193 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002194
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002195 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2196 NULL, &dev->slock,
2197 V4L2_BUF_TYPE_VBI_CAPTURE,
2198 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002199 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002200 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002201
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202 return errCode;
2203}
2204
2205/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002206 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207 * unregisters the v4l2,i2c and usb devices
2208 * called when the device gets disconected or at module unload
2209*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002210void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002211{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002213 /*FIXME: I2C IR should be disconnected */
2214
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002215 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002216 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002217 video_unregister_device(dev->radio_dev);
2218 else
2219 video_device_release(dev->radio_dev);
2220 dev->radio_dev = NULL;
2221 }
2222 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002223 em28xx_info("V4L2 device %s deregistered\n",
2224 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002225 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002226 video_unregister_device(dev->vbi_dev);
2227 else
2228 video_device_release(dev->vbi_dev);
2229 dev->vbi_dev = NULL;
2230 }
2231 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002232 em28xx_info("V4L2 device %s deregistered\n",
2233 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002234 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002235 video_unregister_device(dev->vdev);
2236 else
2237 video_device_release(dev->vdev);
2238 dev->vdev = NULL;
2239 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240}
2241
2242/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002243 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002244 * stops streaming and deallocates all resources allocated by the v4l2
2245 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002247static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002248{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002249 struct em28xx_fh *fh = filp->private_data;
2250 struct em28xx *dev = fh->dev;
2251 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002252
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002253 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254
Hans Verkuil876cb142012-06-23 08:12:47 -03002255 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002256 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002257 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002258 res_free(fh, EM28XX_RESOURCE_VIDEO);
2259 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002260
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002261 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2262 videobuf_stop(&fh->vb_vbiq);
2263 res_free(fh, EM28XX_RESOURCE_VBI);
2264 }
2265
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002266 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002267 /* the device is already disconnect,
2268 free the remaining resources */
2269 if (dev->state & DEV_DISCONNECTED) {
2270 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002271 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002272 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002273 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002274 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002275 return 0;
2276 }
2277
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002278 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002279 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002280
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002281 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002282 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002283 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002284
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002285 /* set alternate 0 */
2286 dev->alt = 0;
2287 em28xx_videodbg("setting alternate 0\n");
2288 errCode = usb_set_interface(dev->udev, 0, 0);
2289 if (errCode < 0) {
2290 em28xx_errdev("cannot change alternate number to "
2291 "0 (error=%i)\n", errCode);
2292 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002294
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002295 videobuf_mmap_free(&fh->vb_vidq);
2296 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002297 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002298 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002299 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002300 return 0;
2301}
2302
2303/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002304 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305 * will allocate buffers when called for the first time
2306 */
2307static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002308em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002309 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002311 struct em28xx_fh *fh = filp->private_data;
2312 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002313 int rc;
2314
2315 rc = check_dev(dev);
2316 if (rc < 0)
2317 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002318
Hans Verkuil876cb142012-06-23 08:12:47 -03002319 if (mutex_lock_interruptible(&dev->lock))
2320 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002321 /* FIXME: read() is not prepared to allow changing the video
2322 resolution while streaming. Seems a bug at em28xx_set_fmt
2323 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002324
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002325 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002326 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002327 rc = -EBUSY;
2328 else
2329 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002330 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002331 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002332 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002333 rc = -EBUSY;
2334 else
2335 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002336 filp->f_flags & O_NONBLOCK);
2337 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002338 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002339
Hans Verkuil876cb142012-06-23 08:12:47 -03002340 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002341}
2342
2343/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002344 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002345 * will allocate buffers when called for the first time
2346 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002347static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002348{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002349 struct em28xx_fh *fh = filp->private_data;
2350 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002351 int rc;
2352
2353 rc = check_dev(dev);
2354 if (rc < 0)
2355 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002356
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002357 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2358 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2359 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002360 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002361 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2362 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2363 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002364 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002365 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002366 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002367 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002368}
2369
Hans Verkuil876cb142012-06-23 08:12:47 -03002370static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2371{
2372 struct em28xx_fh *fh = filp->private_data;
2373 struct em28xx *dev = fh->dev;
2374 unsigned int res;
2375
2376 mutex_lock(&dev->lock);
2377 res = em28xx_poll(filp, wait);
2378 mutex_unlock(&dev->lock);
2379 return res;
2380}
2381
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002382/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002383 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002384 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002385static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002387 struct em28xx_fh *fh = filp->private_data;
2388 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002389 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002390
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002391 rc = check_dev(dev);
2392 if (rc < 0)
2393 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002394
Hans Verkuil876cb142012-06-23 08:12:47 -03002395 if (mutex_lock_interruptible(&dev->lock))
2396 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002397 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2398 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2399 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2400 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002401 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002403 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2404 (unsigned long)vma->vm_start,
2405 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2406 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002407
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002408 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002409}
2410
Hans Verkuilbec43662008-12-30 06:58:20 -03002411static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002412 .owner = THIS_MODULE,
2413 .open = em28xx_v4l2_open,
2414 .release = em28xx_v4l2_close,
2415 .read = em28xx_v4l2_read,
2416 .poll = em28xx_v4l2_poll,
2417 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002418 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002419};
2420
Hans Verkuila3998102008-07-21 02:57:38 -03002421static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002422 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002423 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2424 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2425 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2426 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002427 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2428 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002429 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002430 .vidioc_g_audio = vidioc_g_audio,
2431 .vidioc_s_audio = vidioc_s_audio,
2432 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002433 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2434 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2435 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002436
2437 .vidioc_reqbufs = vidioc_reqbufs,
2438 .vidioc_querybuf = vidioc_querybuf,
2439 .vidioc_qbuf = vidioc_qbuf,
2440 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002441 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002442 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002443 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002444 .vidioc_g_parm = vidioc_g_parm,
2445 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002446 .vidioc_enum_input = vidioc_enum_input,
2447 .vidioc_g_input = vidioc_g_input,
2448 .vidioc_s_input = vidioc_s_input,
2449 .vidioc_queryctrl = vidioc_queryctrl,
2450 .vidioc_g_ctrl = vidioc_g_ctrl,
2451 .vidioc_s_ctrl = vidioc_s_ctrl,
2452 .vidioc_streamon = vidioc_streamon,
2453 .vidioc_streamoff = vidioc_streamoff,
2454 .vidioc_g_tuner = vidioc_g_tuner,
2455 .vidioc_s_tuner = vidioc_s_tuner,
2456 .vidioc_g_frequency = vidioc_g_frequency,
2457 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002458#ifdef CONFIG_VIDEO_ADV_DEBUG
2459 .vidioc_g_register = vidioc_g_register,
2460 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002461 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002462#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002463};
2464
2465static const struct video_device em28xx_video_template = {
2466 .fops = &em28xx_v4l_fops,
2467 .release = video_device_release,
2468 .ioctl_ops = &video_ioctl_ops,
2469
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002470 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002471 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002472};
2473
Hans Verkuilbec43662008-12-30 06:58:20 -03002474static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002475 .owner = THIS_MODULE,
2476 .open = em28xx_v4l2_open,
2477 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002478 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002479};
2480
2481static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002482 .vidioc_querycap = radio_querycap,
2483 .vidioc_g_tuner = radio_g_tuner,
2484 .vidioc_enum_input = radio_enum_input,
2485 .vidioc_g_audio = radio_g_audio,
2486 .vidioc_s_tuner = radio_s_tuner,
2487 .vidioc_s_audio = radio_s_audio,
2488 .vidioc_s_input = radio_s_input,
2489 .vidioc_queryctrl = radio_queryctrl,
2490 .vidioc_g_ctrl = vidioc_g_ctrl,
2491 .vidioc_s_ctrl = vidioc_s_ctrl,
2492 .vidioc_g_frequency = vidioc_g_frequency,
2493 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002494#ifdef CONFIG_VIDEO_ADV_DEBUG
2495 .vidioc_g_register = vidioc_g_register,
2496 .vidioc_s_register = vidioc_s_register,
2497#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002498};
2499
Hans Verkuila3998102008-07-21 02:57:38 -03002500static struct video_device em28xx_radio_template = {
2501 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002502 .fops = &radio_fops,
2503 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002504};
2505
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002506/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002507
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002508
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002509
Adrian Bunk532fe652008-01-28 22:10:48 -03002510static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002511 const struct video_device *template,
2512 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002513{
2514 struct video_device *vfd;
2515
2516 vfd = video_device_alloc();
2517 if (NULL == vfd)
2518 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002519
2520 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002521 vfd->v4l2_dev = &dev->v4l2_dev;
2522 vfd->release = video_device_release;
2523 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002524 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002525
2526 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2527 dev->name, type_name);
2528
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002529 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002530 return vfd;
2531}
2532
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002533int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002534{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002535 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002536 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002537 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002538
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002539 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2540 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002541
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002542 /* set default norm */
2543 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002544 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002545 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002546
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002547 /* Analog specific initialization */
2548 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002549
2550 maxw = norm_maxw(dev);
2551 /* MaxPacketSize for em2800 is too small to capture at full resolution
2552 * use half of maxw as the scaler can only scale to 50% */
2553 if (dev->board.is_em2800)
2554 maxw /= 2;
2555
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002556 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002557 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002558
Ezequiel García96371fc2012-03-23 18:09:34 -03002559 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002560
2561 /* Audio defaults */
2562 dev->mute = 1;
2563 dev->volume = 0x1f;
2564
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002565/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002566 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2567 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2568 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002569
2570 em28xx_set_outfmt(dev);
2571 em28xx_colorlevels_set_default(dev);
2572 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002573
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002574 /* allocate and fill video video_device struct */
2575 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2576 if (!dev->vdev) {
2577 em28xx_errdev("cannot allocate video_device.\n");
2578 return -ENODEV;
2579 }
2580
2581 /* register v4l2 video video_device */
2582 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2583 video_nr[dev->devno]);
2584 if (ret) {
2585 em28xx_errdev("unable to register video device (error=%i).\n",
2586 ret);
2587 return ret;
2588 }
2589
2590 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002591 if (em28xx_vbi_supported(dev) == 1) {
2592 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2593 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002594
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002595 /* register v4l2 vbi video_device */
2596 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2597 vbi_nr[dev->devno]);
2598 if (ret < 0) {
2599 em28xx_errdev("unable to register vbi device\n");
2600 return ret;
2601 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002602 }
2603
2604 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002605 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2606 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002607 if (!dev->radio_dev) {
2608 em28xx_errdev("cannot allocate video_device.\n");
2609 return -ENODEV;
2610 }
2611 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2612 radio_nr[dev->devno]);
2613 if (ret < 0) {
2614 em28xx_errdev("can't register radio device\n");
2615 return ret;
2616 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002617 em28xx_info("Registered radio device as %s\n",
2618 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002619 }
2620
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002621 em28xx_info("V4L2 video device registered as %s\n",
2622 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002623
2624 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002625 em28xx_info("V4L2 VBI device registered as %s\n",
2626 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002627
2628 return 0;
2629}