blob: c7e23dd73b8846cd1d98917da4ad27235be2486c [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 Thorntone0fadfd342008-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 Thorntone0fadfd342008-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 Schaefer4601cc32012-11-08 14:11:46 -0300528 int xfer_bulk, vbi_size, num_packets, i, rc = 1;
529 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 Schaefer4601cc32012-11-08 14:11:46 -0300579 if (actual_length <= 0) {
580 /* 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 */
588 if (p[0] == 0x33 && p[1] == 0x95) {
589 dev->capture_type = 0;
590 dev->vbi_read = 0;
591 em28xx_isocdbg("VBI START HEADER!!!\n");
592 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300593 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300594 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300595 } else if (p[0] == 0x88 && p[1] == 0x88 &&
596 p[2] == 0x88 && p[3] == 0x88) {
597 /* continuation */
598 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300599 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300600 } else if (p[0] == 0x22 && p[1] == 0x5a) {
601 /* start video */
602 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300603 len = actual_length - 4;
604 } else {
605 /* NOTE: With bulk transfers, intermediate data packets
606 * have no continuation header */
607 len = actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300608 }
609
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300610 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300611
612 if (dev->capture_type == 0) {
613 if (dev->vbi_read >= vbi_size) {
614 /* We've already read all the VBI data, so
615 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300616 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300617 } else if ((dev->vbi_read + len) < vbi_size) {
618 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300619 if (dev->vbi_read == 0 &&
620 (!(dev->cur_field & 1))) {
621 /* Brand new frame */
622 if (vbi_buf != NULL)
623 vbi_buffer_filled(dev,
624 vbi_dma_q,
625 vbi_buf);
626 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
627 if (vbi_buf == NULL)
628 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300629 else
630 vbioutp = videobuf_to_vmalloc(
631 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300632 }
633
634 if (dev->vbi_read == 0) {
635 vbi_dma_q->pos = 0;
636 if (vbi_buf != NULL) {
637 if (dev->cur_field & 1)
638 vbi_buf->top_field = 0;
639 else
640 vbi_buf->top_field = 1;
641 }
642 }
643
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300644 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300645 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300647 } else {
648 /* Some of this frame is VBI data and some is
649 video data */
650 int vbi_data_len = vbi_size - dev->vbi_read;
651 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300652 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
653 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300654 dev->capture_type = 1;
655 p += vbi_data_len;
656 len -= vbi_data_len;
657 }
658 }
659
660 if (dev->capture_type == 1) {
661 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300662 if (dev->progressive || !(dev->cur_field & 1)) {
663 if (buf != NULL)
664 buffer_filled(dev, dma_q, buf);
665 get_next_buf(dma_q, &buf);
666 if (buf == NULL)
667 outp = NULL;
668 else
669 outp = videobuf_to_vmalloc(&buf->vb);
670 }
671 if (buf != NULL) {
672 if (dev->cur_field & 1)
673 buf->top_field = 0;
674 else
675 buf->top_field = 1;
676 }
677
678 dma_q->pos = 0;
679 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300680
681 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300682 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300683 p[2] == 0x88 && p[3] == 0x88) {
684 p += 4;
685 len -= 4;
686 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300687 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300688 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
689 p[2], len, (p[2] & 1) ?
690 "odd" : "even");
691 p += 4;
692 len -= 4;
693 }
694
695 if (len > 0)
696 em28xx_copy_video(dev, dma_q, buf, p, outp,
697 len);
698 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300699 }
700 return rc;
701}
702
703
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300704/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300705 Videobuf operations
706 ------------------------------------------------------------------*/
707
708static int
709buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
710{
711 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300712 struct em28xx *dev = fh->dev;
713 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300714
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300715 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
716 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300717
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300718 if (0 == *count)
719 *count = EM28XX_DEF_BUF;
720
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300721 if (*count < EM28XX_MIN_BUF)
722 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300723
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300724 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300725 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300726 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300727 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300728
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300729 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300730
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300731 return 0;
732}
733
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300734/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300735static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
736{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300737 struct em28xx_fh *fh = vq->priv_data;
738 struct em28xx *dev = fh->dev;
739 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300740 if (in_interrupt())
741 BUG();
742
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300743 /* We used to wait for the buffer to finish here, but this didn't work
744 because, as we were keeping the state as VIDEOBUF_QUEUED,
745 videobuf_queue_cancel marked it as finished for us.
746 (Also, it could wedge forever if the hardware was misconfigured.)
747
748 This should be safe; by the time we get here, the buffer isn't
749 queued anymore. If we ever start marking the buffers as
750 VIDEOBUF_ACTIVE, it won't be, though.
751 */
752 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300753 if (dev->usb_ctl.vid_buf == buf)
754 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300755 spin_unlock_irqrestore(&dev->slock, flags);
756
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300757 videobuf_vmalloc_free(&buf->vb);
758 buf->vb.state = VIDEOBUF_NEEDS_INIT;
759}
760
761static int
762buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
763 enum v4l2_field field)
764{
765 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300766 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 struct em28xx *dev = fh->dev;
768 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300769
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300770 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
771 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300772
773 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
774 return -EINVAL;
775
Brandon Philips05612972008-04-13 14:57:01 -0300776 buf->vb.width = dev->width;
777 buf->vb.height = dev->height;
778 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300779
780 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300781 rc = videobuf_iolock(vq, &buf->vb, NULL);
782 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300784 }
785
Frank Schaefer74209dc2012-11-08 14:11:37 -0300786 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300787 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300788
789 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300790 if (em28xx_vbi_supported(dev) == 1)
Frank Schaeferc647a912012-11-08 14:11:52 -0300791 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
792 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300793 EM28XX_NUM_BUFS,
794 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300795 dev->packet_multiplier,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300796 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300797 else
Frank Schaeferc647a912012-11-08 14:11:52 -0300798 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
799 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300800 EM28XX_NUM_BUFS,
801 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300802 dev->packet_multiplier,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300803 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300804 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300805 goto fail;
806 }
807
808 buf->vb.state = VIDEOBUF_PREPARED;
809 return 0;
810
811fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300812 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300813 return rc;
814}
815
816static void
817buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
818{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300819 struct em28xx_buffer *buf = container_of(vb,
820 struct em28xx_buffer,
821 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300822 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300823 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300824 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300825
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300826 buf->vb.state = VIDEOBUF_QUEUED;
827 list_add_tail(&buf->vb.queue, &vidq->active);
828
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300829}
830
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300831static void buffer_release(struct videobuf_queue *vq,
832 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300833{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300834 struct em28xx_buffer *buf = container_of(vb,
835 struct em28xx_buffer,
836 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300837 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300838 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300839
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300840 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300841
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300842 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300843}
844
845static struct videobuf_queue_ops em28xx_video_qops = {
846 .buf_setup = buffer_setup,
847 .buf_prepare = buffer_prepare,
848 .buf_queue = buffer_queue,
849 .buf_release = buffer_release,
850};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800851
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300852/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800853
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800854static void video_mux(struct em28xx *dev, int index)
855{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800856 dev->ctl_input = index;
857 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300858 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800859
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300860 if (!dev->ctl_aoutput)
861 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
862
Hans Verkuil5325b422009-04-02 11:26:22 -0300863 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
864 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800865
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300866 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300867 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300868 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
869 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300870 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300871 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300872 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
873 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800874 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300875
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300876 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300877 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
878 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300879 }
880
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300881 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800882}
883
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300884/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300885static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300886{
887 struct em28xx *dev = fh->dev;
888
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300889 if (fh->resources & bit)
890 /* have it already allocated */
891 return 1;
892
893 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300894 if (dev->resources & bit) {
895 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300896 return 0;
897 }
898 /* it's free, grab it */
899 fh->resources |= bit;
900 dev->resources |= bit;
901 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300902 return 1;
903}
904
905static int res_check(struct em28xx_fh *fh, unsigned int bit)
906{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300907 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300908}
909
910static int res_locked(struct em28xx *dev, unsigned int bit)
911{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300912 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300913}
914
915static void res_free(struct em28xx_fh *fh, unsigned int bits)
916{
917 struct em28xx *dev = fh->dev;
918
919 BUG_ON((fh->resources & bits) != bits);
920
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300921 fh->resources &= ~bits;
922 dev->resources &= ~bits;
923 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300924}
925
926static int get_ressource(struct em28xx_fh *fh)
927{
928 switch (fh->type) {
929 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
930 return EM28XX_RESOURCE_VIDEO;
931 case V4L2_BUF_TYPE_VBI_CAPTURE:
932 return EM28XX_RESOURCE_VBI;
933 default:
934 BUG();
935 return 0;
936 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300937}
938
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800939/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300940 * ac97_queryctrl()
941 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300942 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300943static int ac97_queryctrl(struct v4l2_queryctrl *qc)
944{
945 int i;
946
947 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
948 if (qc->id && qc->id == ac97_qctrl[i].id) {
949 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
950 return 0;
951 }
952 }
953
954 /* Control is not ac97 related */
955 return 1;
956}
957
958/*
959 * ac97_get_ctrl()
960 * return the current values for ac97 mute and volume
961 */
962static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963{
964 switch (ctrl->id) {
965 case V4L2_CID_AUDIO_MUTE:
966 ctrl->value = dev->mute;
967 return 0;
968 case V4L2_CID_AUDIO_VOLUME:
969 ctrl->value = dev->volume;
970 return 0;
971 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300972 /* Control is not ac97 related */
973 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974 }
975}
976
977/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300978 * ac97_set_ctrl()
979 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300981static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300982{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300983 int i;
984
985 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
986 if (ctrl->id == ac97_qctrl[i].id)
987 goto handle;
988
989 /* Announce that hasn't handle it */
990 return 1;
991
992handle:
993 if (ctrl->value < ac97_qctrl[i].minimum ||
994 ctrl->value > ac97_qctrl[i].maximum)
995 return -ERANGE;
996
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 switch (ctrl->id) {
998 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300999 dev->mute = ctrl->value;
1000 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001001 case V4L2_CID_AUDIO_VOLUME:
1002 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001003 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001005
1006 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007}
1008
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001009static int check_dev(struct em28xx *dev)
1010{
1011 if (dev->state & DEV_DISCONNECTED) {
1012 em28xx_errdev("v4l2 ioctl: device not present\n");
1013 return -ENODEV;
1014 }
1015
1016 if (dev->state & DEV_MISCONFIGURED) {
1017 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1018 "close and open it again\n");
1019 return -EIO;
1020 }
1021 return 0;
1022}
1023
1024static void get_scale(struct em28xx *dev,
1025 unsigned int width, unsigned int height,
1026 unsigned int *hscale, unsigned int *vscale)
1027{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001028 unsigned int maxw = norm_maxw(dev);
1029 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030
1031 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1032 if (*hscale >= 0x4000)
1033 *hscale = 0x3fff;
1034
1035 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1036 if (*vscale >= 0x4000)
1037 *vscale = 0x3fff;
1038}
1039
1040/* ------------------------------------------------------------------
1041 IOCTL vidioc handling
1042 ------------------------------------------------------------------*/
1043
Hans Verkuil78b526a2008-05-28 12:16:41 -03001044static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001045 struct v4l2_format *f)
1046{
1047 struct em28xx_fh *fh = priv;
1048 struct em28xx *dev = fh->dev;
1049
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001050 f->fmt.pix.width = dev->width;
1051 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001052 f->fmt.pix.pixelformat = dev->format->fourcc;
1053 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001054 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001055 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1056
1057 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001058 if (dev->progressive)
1059 f->fmt.pix.field = V4L2_FIELD_NONE;
1060 else
1061 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001062 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001063 return 0;
1064}
1065
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001066static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1067{
1068 unsigned int i;
1069
1070 for (i = 0; i < ARRAY_SIZE(format); i++)
1071 if (format[i].fourcc == fourcc)
1072 return &format[i];
1073
1074 return NULL;
1075}
1076
Hans Verkuil78b526a2008-05-28 12:16:41 -03001077static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 struct v4l2_format *f)
1079{
1080 struct em28xx_fh *fh = priv;
1081 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001082 unsigned int width = f->fmt.pix.width;
1083 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001084 unsigned int maxw = norm_maxw(dev);
1085 unsigned int maxh = norm_maxh(dev);
1086 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001087 struct em28xx_fmt *fmt;
1088
1089 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1090 if (!fmt) {
1091 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1092 f->fmt.pix.pixelformat);
1093 return -EINVAL;
1094 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001096 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001097 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001098 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1099 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001100 /* MaxPacketSize for em2800 is too small to capture at full resolution
1101 * use half of maxw as the scaler can only scale to 50% */
1102 if (width == maxw && height == maxh)
1103 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001104 } else {
1105 /* width must even because of the YUYV format
1106 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001107 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1108 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001109 }
1110
1111 get_scale(dev, width, height, &hscale, &vscale);
1112
1113 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1114 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1115
1116 f->fmt.pix.width = width;
1117 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001118 f->fmt.pix.pixelformat = fmt->fourcc;
1119 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1120 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001121 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001122 if (dev->progressive)
1123 f->fmt.pix.field = V4L2_FIELD_NONE;
1124 else
1125 f->fmt.pix.field = dev->interlaced ?
1126 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001127
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001128 return 0;
1129}
1130
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001131static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1132 unsigned width, unsigned height)
1133{
1134 struct em28xx_fmt *fmt;
1135
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001136 fmt = format_by_fourcc(fourcc);
1137 if (!fmt)
1138 return -EINVAL;
1139
1140 dev->format = fmt;
1141 dev->width = width;
1142 dev->height = height;
1143
1144 /* set new image size */
1145 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1146
1147 em28xx_set_alternate(dev);
1148 em28xx_resolution_set(dev);
1149
1150 return 0;
1151}
1152
Hans Verkuil78b526a2008-05-28 12:16:41 -03001153static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154 struct v4l2_format *f)
1155{
1156 struct em28xx_fh *fh = priv;
1157 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001158 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001159
1160 rc = check_dev(dev);
1161 if (rc < 0)
1162 return rc;
1163
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001164 vidioc_try_fmt_vid_cap(file, priv, f);
1165
Brandon Philips05612972008-04-13 14:57:01 -03001166 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1167 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001168 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001169 }
1170
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001171 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001172 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001173}
1174
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001175static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1176{
1177 struct em28xx_fh *fh = priv;
1178 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001179 int rc;
1180
1181 rc = check_dev(dev);
1182 if (rc < 0)
1183 return rc;
1184
1185 *norm = dev->norm;
1186
1187 return 0;
1188}
1189
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001190static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1191{
1192 struct em28xx_fh *fh = priv;
1193 struct em28xx *dev = fh->dev;
1194 int rc;
1195
1196 rc = check_dev(dev);
1197 if (rc < 0)
1198 return rc;
1199
1200 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1201
1202 return 0;
1203}
1204
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001205static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206{
1207 struct em28xx_fh *fh = priv;
1208 struct em28xx *dev = fh->dev;
1209 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001210 int rc;
1211
1212 rc = check_dev(dev);
1213 if (rc < 0)
1214 return rc;
1215
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001216 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001217
1218 /* Adjusts width/height, if needed */
1219 f.fmt.pix.width = dev->width;
1220 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001221 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001222
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001223 /* set new image size */
1224 dev->width = f.fmt.pix.width;
1225 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001226 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1227
1228 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001229 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231 return 0;
1232}
1233
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001234static int vidioc_g_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 int rc = 0;
1240
1241 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1242 return -EINVAL;
1243
1244 if (dev->board.is_webcam)
1245 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1246 video, g_parm, p);
1247 else
1248 v4l2_video_std_frame_period(dev->norm,
1249 &p->parm.capture.timeperframe);
1250
1251 return rc;
1252}
1253
1254static int vidioc_s_parm(struct file *file, void *priv,
1255 struct v4l2_streamparm *p)
1256{
1257 struct em28xx_fh *fh = priv;
1258 struct em28xx *dev = fh->dev;
1259
1260 if (!dev->board.is_webcam)
1261 return -EINVAL;
1262
1263 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1264 return -EINVAL;
1265
1266 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1267}
1268
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001269static const char *iname[] = {
1270 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1271 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1272 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1273 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1274 [EM28XX_VMUX_SVIDEO] = "S-Video",
1275 [EM28XX_VMUX_TELEVISION] = "Television",
1276 [EM28XX_VMUX_CABLE] = "Cable TV",
1277 [EM28XX_VMUX_DVB] = "DVB",
1278 [EM28XX_VMUX_DEBUG] = "for debug only",
1279};
1280
1281static int vidioc_enum_input(struct file *file, void *priv,
1282 struct v4l2_input *i)
1283{
1284 struct em28xx_fh *fh = priv;
1285 struct em28xx *dev = fh->dev;
1286 unsigned int n;
1287
1288 n = i->index;
1289 if (n >= MAX_EM28XX_INPUT)
1290 return -EINVAL;
1291 if (0 == INPUT(n)->type)
1292 return -EINVAL;
1293
1294 i->index = n;
1295 i->type = V4L2_INPUT_TYPE_CAMERA;
1296
1297 strcpy(i->name, iname[INPUT(n)->type]);
1298
1299 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1300 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1301 i->type = V4L2_INPUT_TYPE_TUNER;
1302
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001303 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001304
1305 return 0;
1306}
1307
1308static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1309{
1310 struct em28xx_fh *fh = priv;
1311 struct em28xx *dev = fh->dev;
1312
1313 *i = dev->ctl_input;
1314
1315 return 0;
1316}
1317
1318static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1319{
1320 struct em28xx_fh *fh = priv;
1321 struct em28xx *dev = fh->dev;
1322 int rc;
1323
1324 rc = check_dev(dev);
1325 if (rc < 0)
1326 return rc;
1327
1328 if (i >= MAX_EM28XX_INPUT)
1329 return -EINVAL;
1330 if (0 == INPUT(i)->type)
1331 return -EINVAL;
1332
Ezequiel García96371fc2012-03-23 18:09:34 -03001333 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001334 return 0;
1335}
1336
1337static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1338{
1339 struct em28xx_fh *fh = priv;
1340 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001341
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001342 if (!dev->audio_mode.has_audio)
1343 return -EINVAL;
1344
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001345 switch (a->index) {
1346 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001348 break;
1349 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001350 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001351 break;
1352 case EM28XX_AMUX_VIDEO2:
1353 strcpy(a->name, "Television alt");
1354 break;
1355 case EM28XX_AMUX_PHONE:
1356 strcpy(a->name, "Phone");
1357 break;
1358 case EM28XX_AMUX_MIC:
1359 strcpy(a->name, "Mic");
1360 break;
1361 case EM28XX_AMUX_CD:
1362 strcpy(a->name, "CD");
1363 break;
1364 case EM28XX_AMUX_AUX:
1365 strcpy(a->name, "Aux");
1366 break;
1367 case EM28XX_AMUX_PCM_OUT:
1368 strcpy(a->name, "PCM");
1369 break;
1370 default:
1371 return -EINVAL;
1372 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001373
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001374 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001375 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001376
1377 return 0;
1378}
1379
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001380static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381{
1382 struct em28xx_fh *fh = priv;
1383 struct em28xx *dev = fh->dev;
1384
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001385
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001386 if (!dev->audio_mode.has_audio)
1387 return -EINVAL;
1388
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001389 if (a->index >= MAX_EM28XX_INPUT)
1390 return -EINVAL;
1391 if (0 == INPUT(a->index)->type)
1392 return -EINVAL;
1393
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001394 dev->ctl_ainput = INPUT(a->index)->amux;
1395 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001396
1397 if (!dev->ctl_aoutput)
1398 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001399
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001400 return 0;
1401}
1402
1403static int vidioc_queryctrl(struct file *file, void *priv,
1404 struct v4l2_queryctrl *qc)
1405{
1406 struct em28xx_fh *fh = priv;
1407 struct em28xx *dev = fh->dev;
1408 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001409 int rc;
1410
1411 rc = check_dev(dev);
1412 if (rc < 0)
1413 return rc;
1414
1415 memset(qc, 0, sizeof(*qc));
1416
1417 qc->id = id;
1418
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001419 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001420 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1421 rc = ac97_queryctrl(qc);
1422 if (!rc)
1423 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001424 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001425
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001426 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001427 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001428
1429 if (qc->type)
1430 return 0;
1431 else
1432 return -EINVAL;
1433}
1434
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001435/*
1436 * FIXME: This is an indirect way to check if a control exists at a
1437 * subdev. Instead of that hack, maybe the better would be to change all
1438 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1439 */
1440static int check_subdev_ctrl(struct em28xx *dev, int id)
1441{
1442 struct v4l2_queryctrl qc;
1443
1444 memset(&qc, 0, sizeof(qc));
1445 qc.id = id;
1446
1447 /* enumerate V4L2 device controls */
1448 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1449
1450 if (qc.type)
1451 return 0;
1452 else
1453 return -EINVAL;
1454}
1455
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001456static int vidioc_g_ctrl(struct file *file, void *priv,
1457 struct v4l2_control *ctrl)
1458{
1459 struct em28xx_fh *fh = priv;
1460 struct em28xx *dev = fh->dev;
1461 int rc;
1462
1463 rc = check_dev(dev);
1464 if (rc < 0)
1465 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001466 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001467
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001468 /* Set an AC97 control */
1469 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1470 rc = ac97_get_ctrl(dev, ctrl);
1471 else
1472 rc = 1;
1473
1474 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1475 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001476 if (check_subdev_ctrl(dev, ctrl->id))
1477 return -EINVAL;
1478
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001479 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001480 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001481 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001483 return rc;
1484}
1485
1486static int vidioc_s_ctrl(struct file *file, void *priv,
1487 struct v4l2_control *ctrl)
1488{
1489 struct em28xx_fh *fh = priv;
1490 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001491 int rc;
1492
1493 rc = check_dev(dev);
1494 if (rc < 0)
1495 return rc;
1496
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001497 /* Set an AC97 control */
1498 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1499 rc = ac97_set_ctrl(dev, ctrl);
1500 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001501 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001502
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001503 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001504 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001505 rc = check_subdev_ctrl(dev, ctrl->id);
1506 if (!rc)
1507 v4l2_device_call_all(&dev->v4l2_dev, 0,
1508 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001509 /*
1510 * In the case of non-AC97 volume controls, we still need
1511 * to do some setups at em28xx, in order to mute/unmute
1512 * and to adjust audio volume. However, the value ranges
1513 * should be checked by the corresponding V4L subdriver.
1514 */
1515 switch (ctrl->id) {
1516 case V4L2_CID_AUDIO_MUTE:
1517 dev->mute = ctrl->value;
1518 rc = em28xx_audio_analog_set(dev);
1519 break;
1520 case V4L2_CID_AUDIO_VOLUME:
1521 dev->volume = ctrl->value;
1522 rc = em28xx_audio_analog_set(dev);
1523 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001524 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001525 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001526}
1527
1528static int vidioc_g_tuner(struct file *file, void *priv,
1529 struct v4l2_tuner *t)
1530{
1531 struct em28xx_fh *fh = priv;
1532 struct em28xx *dev = fh->dev;
1533 int rc;
1534
1535 rc = check_dev(dev);
1536 if (rc < 0)
1537 return rc;
1538
1539 if (0 != t->index)
1540 return -EINVAL;
1541
1542 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001543 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001544
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001545 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546 return 0;
1547}
1548
1549static int vidioc_s_tuner(struct file *file, void *priv,
1550 struct v4l2_tuner *t)
1551{
1552 struct em28xx_fh *fh = priv;
1553 struct em28xx *dev = fh->dev;
1554 int rc;
1555
1556 rc = check_dev(dev);
1557 if (rc < 0)
1558 return rc;
1559
1560 if (0 != t->index)
1561 return -EINVAL;
1562
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001563 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001564 return 0;
1565}
1566
1567static int vidioc_g_frequency(struct file *file, void *priv,
1568 struct v4l2_frequency *f)
1569{
1570 struct em28xx_fh *fh = priv;
1571 struct em28xx *dev = fh->dev;
1572
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001573 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001574 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575 return 0;
1576}
1577
1578static int vidioc_s_frequency(struct file *file, void *priv,
1579 struct v4l2_frequency *f)
1580{
1581 struct em28xx_fh *fh = priv;
1582 struct em28xx *dev = fh->dev;
1583 int rc;
1584
1585 rc = check_dev(dev);
1586 if (rc < 0)
1587 return rc;
1588
1589 if (0 != f->tuner)
1590 return -EINVAL;
1591
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001592 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1593 return -EINVAL;
1594 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595 return -EINVAL;
1596
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001597 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001598 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001599
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001600 return 0;
1601}
1602
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001603#ifdef CONFIG_VIDEO_ADV_DEBUG
1604static int em28xx_reg_len(int reg)
1605{
1606 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001607 case EM28XX_R40_AC97LSB:
1608 case EM28XX_R30_HSCALELOW:
1609 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001610 return 2;
1611 default:
1612 return 1;
1613 }
1614}
1615
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001616static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001617 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001618{
1619 struct em28xx_fh *fh = priv;
1620 struct em28xx *dev = fh->dev;
1621
1622 chip->ident = V4L2_IDENT_NONE;
1623 chip->revision = 0;
1624
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001625 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001626
1627 return 0;
1628}
1629
1630
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001631static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001632 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001633{
1634 struct em28xx_fh *fh = priv;
1635 struct em28xx *dev = fh->dev;
1636 int ret;
1637
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001638 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001639 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001640 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001641 if (ret < 0)
1642 return ret;
1643
1644 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001645 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001646 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001647 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001648 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001649 return 0;
1650 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001651 /* TODO: is this correct? */
1652 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1653 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001654 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001655 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001656 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001657 }
1658
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001659 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001660 reg->size = em28xx_reg_len(reg->reg);
1661 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001662 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001663
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001664 if (ret < 0)
1665 return ret;
1666
1667 reg->val = ret;
1668 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001669 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001670 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1671 reg->reg, (char *)&val, 2);
1672 if (ret < 0)
1673 return ret;
1674
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001675 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001676 }
1677
1678 return 0;
1679}
1680
1681static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001682 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001683{
1684 struct em28xx_fh *fh = priv;
1685 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001686 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001687
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001688 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001689 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001690 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001691 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001692 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001693 return 0;
1694 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001695 /* TODO: is this correct? */
1696 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1697 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001698 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001699 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001700 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001701 }
1702
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001703 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001704 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001705
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001706 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001707 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001708}
1709#endif
1710
1711
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001712static int vidioc_cropcap(struct file *file, void *priv,
1713 struct v4l2_cropcap *cc)
1714{
1715 struct em28xx_fh *fh = priv;
1716 struct em28xx *dev = fh->dev;
1717
1718 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1719 return -EINVAL;
1720
1721 cc->bounds.left = 0;
1722 cc->bounds.top = 0;
1723 cc->bounds.width = dev->width;
1724 cc->bounds.height = dev->height;
1725 cc->defrect = cc->bounds;
1726 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1727 cc->pixelaspect.denominator = 59;
1728
1729 return 0;
1730}
1731
1732static int vidioc_streamon(struct file *file, void *priv,
1733 enum v4l2_buf_type type)
1734{
1735 struct em28xx_fh *fh = priv;
1736 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001737 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001738
1739 rc = check_dev(dev);
1740 if (rc < 0)
1741 return rc;
1742
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001743 if (unlikely(type != fh->type))
1744 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001745
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001746 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1747 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001748
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001749 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001750 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001751
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001752 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1753 rc = videobuf_streamon(&fh->vb_vidq);
1754 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1755 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001756
1757 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001758}
1759
1760static int vidioc_streamoff(struct file *file, void *priv,
1761 enum v4l2_buf_type type)
1762{
1763 struct em28xx_fh *fh = priv;
1764 struct em28xx *dev = fh->dev;
1765 int rc;
1766
1767 rc = check_dev(dev);
1768 if (rc < 0)
1769 return rc;
1770
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001771 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1772 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001773 return -EINVAL;
1774 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 return -EINVAL;
1776
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001777 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1778 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001779
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001780 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001781 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1782 videobuf_streamoff(&fh->vb_vidq);
1783 res_free(fh, EM28XX_RESOURCE_VIDEO);
1784 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001785 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001786 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1787 videobuf_streamoff(&fh->vb_vbiq);
1788 res_free(fh, EM28XX_RESOURCE_VBI);
1789 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001790 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 return 0;
1793}
1794
1795static int vidioc_querycap(struct file *file, void *priv,
1796 struct v4l2_capability *cap)
1797{
1798 struct em28xx_fh *fh = priv;
1799 struct em28xx *dev = fh->dev;
1800
1801 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1802 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001803 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001805 cap->capabilities =
1806 V4L2_CAP_SLICED_VBI_CAPTURE |
1807 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001808 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1809
Devin Heitmueller04146142009-09-11 00:08:44 -03001810 if (dev->vbi_dev)
1811 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1812
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001813 if (dev->audio_mode.has_audio)
1814 cap->capabilities |= V4L2_CAP_AUDIO;
1815
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001816 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817 cap->capabilities |= V4L2_CAP_TUNER;
1818
1819 return 0;
1820}
1821
Hans Verkuil78b526a2008-05-28 12:16:41 -03001822static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001823 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001825 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001826 return -EINVAL;
1827
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001828 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1829 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001830
1831 return 0;
1832}
1833
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001834static int vidioc_enum_framesizes(struct file *file, void *priv,
1835 struct v4l2_frmsizeenum *fsize)
1836{
1837 struct em28xx_fh *fh = priv;
1838 struct em28xx *dev = fh->dev;
1839 struct em28xx_fmt *fmt;
1840 unsigned int maxw = norm_maxw(dev);
1841 unsigned int maxh = norm_maxh(dev);
1842
1843 fmt = format_by_fourcc(fsize->pixel_format);
1844 if (!fmt) {
1845 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1846 fsize->pixel_format);
1847 return -EINVAL;
1848 }
1849
1850 if (dev->board.is_em2800) {
1851 if (fsize->index > 1)
1852 return -EINVAL;
1853 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1854 fsize->discrete.width = maxw / (1 + fsize->index);
1855 fsize->discrete.height = maxh / (1 + fsize->index);
1856 return 0;
1857 }
1858
1859 if (fsize->index != 0)
1860 return -EINVAL;
1861
1862 /* Report a continuous range */
1863 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1864 fsize->stepwise.min_width = 48;
1865 fsize->stepwise.min_height = 32;
1866 fsize->stepwise.max_width = maxw;
1867 fsize->stepwise.max_height = maxh;
1868 fsize->stepwise.step_width = 1;
1869 fsize->stepwise.step_height = 1;
1870 return 0;
1871}
1872
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001873/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001874static int vidioc_g_fmt_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
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001885 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001886 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001887
1888 if (f->fmt.sliced.service_set == 0)
1889 rc = -EINVAL;
1890
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001891 return rc;
1892}
1893
Hans Verkuil78b526a2008-05-28 12:16:41 -03001894static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001895 struct v4l2_format *f)
1896{
1897 struct em28xx_fh *fh = priv;
1898 struct em28xx *dev = fh->dev;
1899 int rc;
1900
1901 rc = check_dev(dev);
1902 if (rc < 0)
1903 return rc;
1904
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001905 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001906
1907 if (f->fmt.sliced.service_set == 0)
1908 return -EINVAL;
1909
1910 return 0;
1911}
1912
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001913/* RAW VBI ioctls */
1914
1915static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1916 struct v4l2_format *format)
1917{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001918 struct em28xx_fh *fh = priv;
1919 struct em28xx *dev = fh->dev;
1920
1921 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001922 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1923 format->fmt.vbi.offset = 0;
1924 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001925 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1926 format->fmt.vbi.count[0] = dev->vbi_height;
1927 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001928
1929 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001930 if (dev->norm & V4L2_STD_525_60) {
1931 /* NTSC */
1932 format->fmt.vbi.start[0] = 10;
1933 format->fmt.vbi.start[1] = 273;
1934 } else if (dev->norm & V4L2_STD_625_50) {
1935 /* PAL */
1936 format->fmt.vbi.start[0] = 6;
1937 format->fmt.vbi.start[1] = 318;
1938 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001939
1940 return 0;
1941}
1942
1943static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1944 struct v4l2_format *format)
1945{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001946 struct em28xx_fh *fh = priv;
1947 struct em28xx *dev = fh->dev;
1948
1949 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001950 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1951 format->fmt.vbi.offset = 0;
1952 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001953 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1954 format->fmt.vbi.count[0] = dev->vbi_height;
1955 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001956
1957 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001958 if (dev->norm & V4L2_STD_525_60) {
1959 /* NTSC */
1960 format->fmt.vbi.start[0] = 10;
1961 format->fmt.vbi.start[1] = 273;
1962 } else if (dev->norm & V4L2_STD_625_50) {
1963 /* PAL */
1964 format->fmt.vbi.start[0] = 6;
1965 format->fmt.vbi.start[1] = 318;
1966 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001967
1968 return 0;
1969}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001970
1971static int vidioc_reqbufs(struct file *file, void *priv,
1972 struct v4l2_requestbuffers *rb)
1973{
1974 struct em28xx_fh *fh = priv;
1975 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001976 int rc;
1977
1978 rc = check_dev(dev);
1979 if (rc < 0)
1980 return rc;
1981
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001982 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1983 return videobuf_reqbufs(&fh->vb_vidq, rb);
1984 else
1985 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001986}
1987
1988static int vidioc_querybuf(struct file *file, void *priv,
1989 struct v4l2_buffer *b)
1990{
1991 struct em28xx_fh *fh = priv;
1992 struct em28xx *dev = fh->dev;
1993 int rc;
1994
1995 rc = check_dev(dev);
1996 if (rc < 0)
1997 return rc;
1998
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001999 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2000 return videobuf_querybuf(&fh->vb_vidq, b);
2001 else {
2002 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
2003 the videobuf framework, but we probably shouldn't be
2004 returning a buffer larger than that which was asked for.
2005 At a minimum, it causes a crash in zvbi since it does
2006 a memcpy based on the source buffer length */
2007 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03002008 b->length = dev->vbi_width * dev->vbi_height * 2;
2009
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002010 return result;
2011 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012}
2013
2014static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2015{
2016 struct em28xx_fh *fh = priv;
2017 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002018 int rc;
2019
2020 rc = check_dev(dev);
2021 if (rc < 0)
2022 return rc;
2023
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002024 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2025 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002026 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002027 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002028}
2029
2030static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2031{
2032 struct em28xx_fh *fh = priv;
2033 struct em28xx *dev = fh->dev;
2034 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002035
2036 rc = check_dev(dev);
2037 if (rc < 0)
2038 return rc;
2039
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002040 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2041 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2042 O_NONBLOCK);
2043 else
2044 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2045 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002046}
2047
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002048/* ----------------------------------------------------------- */
2049/* RADIO ESPECIFIC IOCTLS */
2050/* ----------------------------------------------------------- */
2051
2052static int radio_querycap(struct file *file, void *priv,
2053 struct v4l2_capability *cap)
2054{
2055 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2056
2057 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2058 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002059 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002060
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002061 cap->capabilities = V4L2_CAP_TUNER;
2062 return 0;
2063}
2064
2065static int radio_g_tuner(struct file *file, void *priv,
2066 struct v4l2_tuner *t)
2067{
2068 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2069
2070 if (unlikely(t->index > 0))
2071 return -EINVAL;
2072
2073 strcpy(t->name, "Radio");
2074 t->type = V4L2_TUNER_RADIO;
2075
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002076 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002077
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002078 return 0;
2079}
2080
2081static int radio_enum_input(struct file *file, void *priv,
2082 struct v4l2_input *i)
2083{
2084 if (i->index != 0)
2085 return -EINVAL;
2086 strcpy(i->name, "Radio");
2087 i->type = V4L2_INPUT_TYPE_TUNER;
2088
2089 return 0;
2090}
2091
2092static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2093{
2094 if (unlikely(a->index))
2095 return -EINVAL;
2096
2097 strcpy(a->name, "Radio");
2098 return 0;
2099}
2100
2101static int radio_s_tuner(struct file *file, void *priv,
2102 struct v4l2_tuner *t)
2103{
2104 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2105
2106 if (0 != t->index)
2107 return -EINVAL;
2108
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002109 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002110
2111 return 0;
2112}
2113
2114static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002115 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002116{
2117 return 0;
2118}
2119
2120static int radio_s_input(struct file *file, void *fh, unsigned int i)
2121{
2122 return 0;
2123}
2124
2125static int radio_queryctrl(struct file *file, void *priv,
2126 struct v4l2_queryctrl *qc)
2127{
2128 int i;
2129
2130 if (qc->id < V4L2_CID_BASE ||
2131 qc->id >= V4L2_CID_LASTP1)
2132 return -EINVAL;
2133
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002134 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2135 if (qc->id && qc->id == ac97_qctrl[i].id) {
2136 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002137 return 0;
2138 }
2139 }
2140
2141 return -EINVAL;
2142}
2143
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002144/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002145 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146 * inits the device and starts isoc transfer
2147 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002148static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002149{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002150 int errCode = 0, radio = 0;
2151 struct video_device *vdev = video_devdata(filp);
2152 struct em28xx *dev = video_drvdata(filp);
2153 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002154 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002155 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002156
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002157 switch (vdev->vfl_type) {
2158 case VFL_TYPE_GRABBER:
2159 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2160 break;
2161 case VFL_TYPE_VBI:
2162 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2163 break;
2164 case VFL_TYPE_RADIO:
2165 radio = 1;
2166 break;
2167 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002168
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002169 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2170 video_device_node_name(vdev), v4l2_type_names[fh_type],
2171 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173
Hans Verkuil876cb142012-06-23 08:12:47 -03002174 if (mutex_lock_interruptible(&dev->lock))
2175 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002176 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002177 if (!fh) {
2178 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002179 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002180 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002182 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002183 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002184 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002185 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002187 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002188 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002189 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002190 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002191
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002192 /* Needed, since GPIO might have disabled power of
2193 some i2c device
2194 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002195 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002196
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002197 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002198 if (fh->radio) {
2199 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002200 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002201 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202
2203 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002204
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002205 if (dev->progressive)
2206 field = V4L2_FIELD_NONE;
2207 else
2208 field = V4L2_FIELD_INTERLACED;
2209
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002210 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2211 NULL, &dev->slock,
2212 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002213 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002214
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002215 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2216 NULL, &dev->slock,
2217 V4L2_BUF_TYPE_VBI_CAPTURE,
2218 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002219 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002220 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002221
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222 return errCode;
2223}
2224
2225/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002226 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227 * unregisters the v4l2,i2c and usb devices
2228 * called when the device gets disconected or at module unload
2229*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002230void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002231{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002233 /*FIXME: I2C IR should be disconnected */
2234
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002235 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002236 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002237 video_unregister_device(dev->radio_dev);
2238 else
2239 video_device_release(dev->radio_dev);
2240 dev->radio_dev = NULL;
2241 }
2242 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002243 em28xx_info("V4L2 device %s deregistered\n",
2244 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002245 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002246 video_unregister_device(dev->vbi_dev);
2247 else
2248 video_device_release(dev->vbi_dev);
2249 dev->vbi_dev = NULL;
2250 }
2251 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002252 em28xx_info("V4L2 device %s deregistered\n",
2253 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002254 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002255 video_unregister_device(dev->vdev);
2256 else
2257 video_device_release(dev->vdev);
2258 dev->vdev = NULL;
2259 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002260}
2261
2262/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002263 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002264 * stops streaming and deallocates all resources allocated by the v4l2
2265 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002266 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002267static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002268{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002269 struct em28xx_fh *fh = filp->private_data;
2270 struct em28xx *dev = fh->dev;
2271 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002272
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002273 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002274
Hans Verkuil876cb142012-06-23 08:12:47 -03002275 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002276 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002277 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002278 res_free(fh, EM28XX_RESOURCE_VIDEO);
2279 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002281 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2282 videobuf_stop(&fh->vb_vbiq);
2283 res_free(fh, EM28XX_RESOURCE_VBI);
2284 }
2285
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002286 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002287 /* the device is already disconnect,
2288 free the remaining resources */
2289 if (dev->state & DEV_DISCONNECTED) {
2290 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002291 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002292 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002293 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002294 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002295 return 0;
2296 }
2297
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002298 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002299 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002300
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002301 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002302 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002303 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002304
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002305 /* set alternate 0 */
2306 dev->alt = 0;
2307 em28xx_videodbg("setting alternate 0\n");
2308 errCode = usb_set_interface(dev->udev, 0, 0);
2309 if (errCode < 0) {
2310 em28xx_errdev("cannot change alternate number to "
2311 "0 (error=%i)\n", errCode);
2312 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002313 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002314
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002315 videobuf_mmap_free(&fh->vb_vidq);
2316 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002317 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002318 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002319 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002320 return 0;
2321}
2322
2323/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002324 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002325 * will allocate buffers when called for the first time
2326 */
2327static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002328em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002329 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002330{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002331 struct em28xx_fh *fh = filp->private_data;
2332 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002333 int rc;
2334
2335 rc = check_dev(dev);
2336 if (rc < 0)
2337 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002338
Hans Verkuil876cb142012-06-23 08:12:47 -03002339 if (mutex_lock_interruptible(&dev->lock))
2340 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002341 /* FIXME: read() is not prepared to allow changing the video
2342 resolution while streaming. Seems a bug at em28xx_set_fmt
2343 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002344
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002345 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002346 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002347 rc = -EBUSY;
2348 else
2349 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002350 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002351 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002352 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002353 rc = -EBUSY;
2354 else
2355 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002356 filp->f_flags & O_NONBLOCK);
2357 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002358 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002359
Hans Verkuil876cb142012-06-23 08:12:47 -03002360 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002361}
2362
2363/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002364 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002365 * will allocate buffers when called for the first time
2366 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002367static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002368{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002369 struct em28xx_fh *fh = filp->private_data;
2370 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002371 int rc;
2372
2373 rc = check_dev(dev);
2374 if (rc < 0)
2375 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002376
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002377 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2378 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2379 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002380 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002381 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2382 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2383 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002384 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002385 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002386 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002387 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002388}
2389
Hans Verkuil876cb142012-06-23 08:12:47 -03002390static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2391{
2392 struct em28xx_fh *fh = filp->private_data;
2393 struct em28xx *dev = fh->dev;
2394 unsigned int res;
2395
2396 mutex_lock(&dev->lock);
2397 res = em28xx_poll(filp, wait);
2398 mutex_unlock(&dev->lock);
2399 return res;
2400}
2401
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002403 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002404 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002405static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002406{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002407 struct em28xx_fh *fh = filp->private_data;
2408 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002409 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002410
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002411 rc = check_dev(dev);
2412 if (rc < 0)
2413 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002414
Hans Verkuil876cb142012-06-23 08:12:47 -03002415 if (mutex_lock_interruptible(&dev->lock))
2416 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002417 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2418 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2419 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2420 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002421 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002422
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002423 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2424 (unsigned long)vma->vm_start,
2425 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2426 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002428 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002429}
2430
Hans Verkuilbec43662008-12-30 06:58:20 -03002431static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002432 .owner = THIS_MODULE,
2433 .open = em28xx_v4l2_open,
2434 .release = em28xx_v4l2_close,
2435 .read = em28xx_v4l2_read,
2436 .poll = em28xx_v4l2_poll,
2437 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002438 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002439};
2440
Hans Verkuila3998102008-07-21 02:57:38 -03002441static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002442 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002443 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2444 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2445 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2446 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002447 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2448 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002449 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002450 .vidioc_g_audio = vidioc_g_audio,
2451 .vidioc_s_audio = vidioc_s_audio,
2452 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002453 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2454 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2455 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002456
2457 .vidioc_reqbufs = vidioc_reqbufs,
2458 .vidioc_querybuf = vidioc_querybuf,
2459 .vidioc_qbuf = vidioc_qbuf,
2460 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002461 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002462 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002463 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002464 .vidioc_g_parm = vidioc_g_parm,
2465 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002466 .vidioc_enum_input = vidioc_enum_input,
2467 .vidioc_g_input = vidioc_g_input,
2468 .vidioc_s_input = vidioc_s_input,
2469 .vidioc_queryctrl = vidioc_queryctrl,
2470 .vidioc_g_ctrl = vidioc_g_ctrl,
2471 .vidioc_s_ctrl = vidioc_s_ctrl,
2472 .vidioc_streamon = vidioc_streamon,
2473 .vidioc_streamoff = vidioc_streamoff,
2474 .vidioc_g_tuner = vidioc_g_tuner,
2475 .vidioc_s_tuner = vidioc_s_tuner,
2476 .vidioc_g_frequency = vidioc_g_frequency,
2477 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002478#ifdef CONFIG_VIDEO_ADV_DEBUG
2479 .vidioc_g_register = vidioc_g_register,
2480 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002481 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002482#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002483};
2484
2485static const struct video_device em28xx_video_template = {
2486 .fops = &em28xx_v4l_fops,
2487 .release = video_device_release,
2488 .ioctl_ops = &video_ioctl_ops,
2489
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002490 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002491 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002492};
2493
Hans Verkuilbec43662008-12-30 06:58:20 -03002494static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002495 .owner = THIS_MODULE,
2496 .open = em28xx_v4l2_open,
2497 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002498 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002499};
2500
2501static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002502 .vidioc_querycap = radio_querycap,
2503 .vidioc_g_tuner = radio_g_tuner,
2504 .vidioc_enum_input = radio_enum_input,
2505 .vidioc_g_audio = radio_g_audio,
2506 .vidioc_s_tuner = radio_s_tuner,
2507 .vidioc_s_audio = radio_s_audio,
2508 .vidioc_s_input = radio_s_input,
2509 .vidioc_queryctrl = radio_queryctrl,
2510 .vidioc_g_ctrl = vidioc_g_ctrl,
2511 .vidioc_s_ctrl = vidioc_s_ctrl,
2512 .vidioc_g_frequency = vidioc_g_frequency,
2513 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002514#ifdef CONFIG_VIDEO_ADV_DEBUG
2515 .vidioc_g_register = vidioc_g_register,
2516 .vidioc_s_register = vidioc_s_register,
2517#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002518};
2519
Hans Verkuila3998102008-07-21 02:57:38 -03002520static struct video_device em28xx_radio_template = {
2521 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002522 .fops = &radio_fops,
2523 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002524};
2525
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002526/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002527
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002528
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002529
Adrian Bunk532fe652008-01-28 22:10:48 -03002530static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002531 const struct video_device *template,
2532 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002533{
2534 struct video_device *vfd;
2535
2536 vfd = video_device_alloc();
2537 if (NULL == vfd)
2538 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002539
2540 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002541 vfd->v4l2_dev = &dev->v4l2_dev;
2542 vfd->release = video_device_release;
2543 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002544 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002545
2546 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2547 dev->name, type_name);
2548
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002549 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002550 return vfd;
2551}
2552
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002553int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002554{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002555 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002556 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002557 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002558
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002559 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2560 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002561
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002562 /* set default norm */
2563 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002564 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002565 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002566
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002567 /* Analog specific initialization */
2568 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002569
2570 maxw = norm_maxw(dev);
2571 /* MaxPacketSize for em2800 is too small to capture at full resolution
2572 * use half of maxw as the scaler can only scale to 50% */
2573 if (dev->board.is_em2800)
2574 maxw /= 2;
2575
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002576 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002577 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002578
Ezequiel García96371fc2012-03-23 18:09:34 -03002579 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002580
2581 /* Audio defaults */
2582 dev->mute = 1;
2583 dev->volume = 0x1f;
2584
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002585/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002586 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2587 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2588 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002589
2590 em28xx_set_outfmt(dev);
2591 em28xx_colorlevels_set_default(dev);
2592 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002593
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002594 /* allocate and fill video video_device struct */
2595 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2596 if (!dev->vdev) {
2597 em28xx_errdev("cannot allocate video_device.\n");
2598 return -ENODEV;
2599 }
2600
2601 /* register v4l2 video video_device */
2602 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2603 video_nr[dev->devno]);
2604 if (ret) {
2605 em28xx_errdev("unable to register video device (error=%i).\n",
2606 ret);
2607 return ret;
2608 }
2609
2610 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002611 if (em28xx_vbi_supported(dev) == 1) {
2612 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2613 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002614
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002615 /* register v4l2 vbi video_device */
2616 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2617 vbi_nr[dev->devno]);
2618 if (ret < 0) {
2619 em28xx_errdev("unable to register vbi device\n");
2620 return ret;
2621 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002622 }
2623
2624 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002625 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2626 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002627 if (!dev->radio_dev) {
2628 em28xx_errdev("cannot allocate video_device.\n");
2629 return -ENODEV;
2630 }
2631 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2632 radio_nr[dev->devno]);
2633 if (ret < 0) {
2634 em28xx_errdev("can't register radio device\n");
2635 return ret;
2636 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002637 em28xx_info("Registered radio device as %s\n",
2638 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002639 }
2640
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002641 em28xx_info("V4L2 video device registered as %s\n",
2642 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002643
2644 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002645 em28xx_info("V4L2 VBI device registered as %s\n",
2646 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002647
2648 return 0;
2649}