blob: b71df42aa7bb9389234480a91f3f5048b7cf9533 [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/*
Frank Schaefer948a49a2012-12-08 11:31:25 -0300157 * Finish the current buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158 */
Frank Schaefer948a49a2012-12-08 11:31:25 -0300159static inline void finish_buffer(struct em28xx *dev,
160 struct em28xx_buffer *buf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
163 buf->vb.state = VIDEOBUF_DONE;
164 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300165 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300166 list_del(&buf->vb.queue);
167 wake_up(&buf->vb.done);
168}
169
170/*
Frank Schaefer36016a32012-12-08 11:31:32 -0300171 * Copy picture data from USB buffer to videobuf buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300172 */
173static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300174 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300175 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300176 unsigned long len)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300177{
178 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300179 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300180 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300181
Frank Schaefer87325332012-12-08 11:31:27 -0300182 if (buf->pos + len > buf->vb.size)
183 len = buf->vb.size - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184
Frank Schaefer36016a32012-12-08 11:31:32 -0300185 startread = usb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 remain = len;
187
Frank Schaeferc02ec712012-11-08 14:11:33 -0300188 if (dev->progressive || buf->top_field)
Frank Schaefer36016a32012-12-08 11:31:32 -0300189 fieldstart = buf->vb_buf;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300190 else /* interlaced mode, even nr. of lines */
Frank Schaefer36016a32012-12-08 11:31:32 -0300191 fieldstart = buf->vb_buf + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300192
Frank Schaefer87325332012-12-08 11:31:27 -0300193 linesdone = buf->pos / bytesperline;
194 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300195
196 if (dev->progressive)
197 offset = linesdone * bytesperline + currlinedone;
198 else
199 offset = linesdone * bytesperline * 2 + currlinedone;
200
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203 lencopy = lencopy > remain ? remain : lencopy;
204
Frank Schaefer36016a32012-12-08 11:31:32 -0300205 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Frank Schaefer36016a32012-12-08 11:31:32 -0300207 ((char *)startwrite + lencopy) -
208 ((char *)buf->vb_buf + buf->vb.size));
209 remain = (char *)buf->vb_buf + buf->vb.size -
210 (char *)startwrite;
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300211 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300213 if (lencopy <= 0)
214 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218
219 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300220 if (dev->progressive)
221 startwrite += lencopy;
222 else
223 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = remain;
227 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300228 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229
Frank Schaefer36016a32012-12-08 11:31:32 -0300230 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300231 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300232 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
233 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234 ((char *)startwrite + lencopy) -
Frank Schaefer36016a32012-12-08 11:31:32 -0300235 ((char *)buf->vb_buf + buf->vb.size));
236 lencopy = remain = (char *)buf->vb_buf + buf->vb.size -
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300237 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300238 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300239 if (lencopy <= 0)
240 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300241
242 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300243
244 remain -= lencopy;
245 }
246
Frank Schaefer87325332012-12-08 11:31:27 -0300247 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248}
249
Frank Schaefer36016a32012-12-08 11:31:32 -0300250/*
251 * Copy VBI data from USB buffer to videobuf buffer
252 */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300253static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300254 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300255 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300256 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300257{
Frank Schaefer36016a32012-12-08 11:31:32 -0300258 unsigned int offset;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300259
Frank Schaefer87325332012-12-08 11:31:27 -0300260 if (buf->pos + len > buf->vb.size)
261 len = buf->vb.size - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300262
Frank Schaefer87325332012-12-08 11:31:27 -0300263 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300264 /* Make sure the bottom field populates the second half of the frame */
Frank Schaefer36016a32012-12-08 11:31:32 -0300265 if (buf->top_field == 0)
266 offset += dev->vbi_width * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300267
Frank Schaefer36016a32012-12-08 11:31:32 -0300268 memcpy(buf->vb_buf + offset, usb_buf, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300269 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300270}
271
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300272static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300273 int packet, int status)
274{
275 char *errmsg = "Unknown";
276
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300277 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300278 case -ENOENT:
279 errmsg = "unlinked synchronuously";
280 break;
281 case -ECONNRESET:
282 errmsg = "unlinked asynchronuously";
283 break;
284 case -ENOSR:
285 errmsg = "Buffer error (overrun)";
286 break;
287 case -EPIPE:
288 errmsg = "Stalled (device not responding)";
289 break;
290 case -EOVERFLOW:
291 errmsg = "Babble (bad cable?)";
292 break;
293 case -EPROTO:
294 errmsg = "Bit-stuff error (bad cable?)";
295 break;
296 case -EILSEQ:
297 errmsg = "CRC/Timeout (could be anything)";
298 break;
299 case -ETIME:
300 errmsg = "Device does not respond";
301 break;
302 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300303 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300304 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
305 } else {
306 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
307 packet, status, errmsg);
308 }
309}
310
311/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300312 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300313 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300314static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
315 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300316{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300317 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300318 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300319
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300320 if (list_empty(&dma_q->active)) {
321 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300322 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300323 }
324
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300325 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300326 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300327 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300328 outp = videobuf_to_vmalloc(&buf->vb);
329 memset(outp, 0, buf->vb.size);
Frank Schaefer87325332012-12-08 11:31:27 -0300330 buf->pos = 0;
Frank Schaefer4078d622012-12-08 11:31:29 -0300331 buf->vb_buf = outp;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300332
Frank Schaefer24a6d842012-12-08 11:31:24 -0300333 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300334}
335
Frank Schaefere04c00d2012-12-08 11:31:30 -0300336/*
337 * Finish the current buffer if completed and prepare for the next field
338 */
339static struct em28xx_buffer *
340finish_field_prepare_next(struct em28xx *dev,
341 struct em28xx_buffer *buf,
342 struct em28xx_dmaqueue *dma_q)
343{
344 if (dev->progressive || dev->top_field) { /* Brand new frame */
345 if (buf != NULL)
346 finish_buffer(dev, buf);
347 buf = get_next_buf(dev, dma_q);
348 }
349 if (buf != NULL) {
350 buf->top_field = dev->top_field;
351 buf->pos = 0;
352 }
353
354 return buf;
355}
356
Frank Schaefer227b7c92012-12-08 11:31:31 -0300357/*
358 * Process data packet according to the em2710/em2750/em28xx frame data format
359 */
360static inline void process_frame_data_em28xx(struct em28xx *dev,
361 unsigned char *data_pkt,
362 unsigned int data_len)
363{
364 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
365 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
366 struct em28xx_dmaqueue *dma_q = &dev->vidq;
367 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
368
369 /* capture type 0 = vbi start
370 capture type 1 = vbi in progress
371 capture type 2 = video start
372 capture type 3 = video in progress */
373 if (data_len >= 4) {
374 /* NOTE: Headers are always 4 bytes and
375 * never split across packets */
376 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
377 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
378 /* Continuation */
379 data_pkt += 4;
380 data_len -= 4;
381 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
382 /* Field start (VBI mode) */
383 dev->capture_type = 0;
384 dev->vbi_read = 0;
385 em28xx_isocdbg("VBI START HEADER !!!\n");
386 dev->top_field = !(data_pkt[2] & 1);
387 data_pkt += 4;
388 data_len -= 4;
389 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
390 /* Field start (VBI disabled) */
391 dev->capture_type = 2;
392 em28xx_isocdbg("VIDEO START HEADER !!!\n");
393 dev->top_field = !(data_pkt[2] & 1);
394 data_pkt += 4;
395 data_len -= 4;
396 }
397 }
398 /* NOTE: With bulk transfers, intermediate data packets
399 * have no continuation header */
400
401 if (dev->capture_type == 0) {
402 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
403 dev->usb_ctl.vbi_buf = vbi_buf;
404 dev->capture_type = 1;
405 }
406
407 if (dev->capture_type == 1) {
408 int vbi_size = dev->vbi_width * dev->vbi_height;
409 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
410 (vbi_size - dev->vbi_read) : data_len;
411
412 /* Copy VBI data */
413 if (vbi_buf != NULL)
414 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
415 dev->vbi_read += vbi_data_len;
416
417 if (vbi_data_len < data_len) {
418 /* Continue with copying video data */
419 dev->capture_type = 2;
420 data_pkt += vbi_data_len;
421 data_len -= vbi_data_len;
422 }
423 }
424
425 if (dev->capture_type == 2) {
426 buf = finish_field_prepare_next(dev, buf, dma_q);
427 dev->usb_ctl.vid_buf = buf;
428 dev->capture_type = 3;
429 }
430
431 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
432 em28xx_copy_video(dev, buf, data_pkt, data_len);
433}
434
Frank Schaefer960da932012-11-25 06:37:37 -0300435/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300436static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300438 int xfer_bulk, num_packets, i;
439 unsigned char *usb_data_pkt;
440 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300441
442 if (!dev)
443 return 0;
444
445 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
446 return 0;
447
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300448 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300449 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300450
Frank Schaefer4601cc32012-11-08 14:11:46 -0300451 xfer_bulk = usb_pipebulk(urb->pipe);
452
Frank Schaefer4601cc32012-11-08 14:11:46 -0300453 if (xfer_bulk) /* bulk */
454 num_packets = 1;
455 else /* isoc */
456 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300457
Frank Schaefer4601cc32012-11-08 14:11:46 -0300458 for (i = 0; i < num_packets; i++) {
459 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300460 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300461
Frank Schaefer227b7c92012-12-08 11:31:31 -0300462 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300463 } else { /* isoc */
464 if (urb->iso_frame_desc[i].status < 0) {
465 print_err_status(dev, i,
466 urb->iso_frame_desc[i].status);
467 if (urb->iso_frame_desc[i].status != -EPROTO)
468 continue;
469 }
470
Frank Schaefer227b7c92012-12-08 11:31:31 -0300471 usb_data_len = urb->iso_frame_desc[i].actual_length;
472 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300473 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300474 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300475 }
476
Frank Schaefer227b7c92012-12-08 11:31:31 -0300477 usb_data_pkt = urb->transfer_buffer +
478 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300479 }
480
Frank Schaefer227b7c92012-12-08 11:31:31 -0300481 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300482 /* NOTE: happens very often with isoc transfers */
483 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300484 continue;
485 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300486
Frank Schaefer227b7c92012-12-08 11:31:31 -0300487 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300488 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300489 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300490}
491
492
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 Videobuf operations
495 ------------------------------------------------------------------*/
496
497static int
498buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
499{
500 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300501 struct em28xx *dev = fh->dev;
502 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300504 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
505 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300506
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 if (0 == *count)
508 *count = EM28XX_DEF_BUF;
509
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300510 if (*count < EM28XX_MIN_BUF)
511 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300512
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300513 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300514 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300515 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300516 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300517
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300518 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300519
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300520 return 0;
521}
522
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300523/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300524static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
525{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300526 struct em28xx_fh *fh = vq->priv_data;
527 struct em28xx *dev = fh->dev;
528 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300529 if (in_interrupt())
530 BUG();
531
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300532 /* We used to wait for the buffer to finish here, but this didn't work
533 because, as we were keeping the state as VIDEOBUF_QUEUED,
534 videobuf_queue_cancel marked it as finished for us.
535 (Also, it could wedge forever if the hardware was misconfigured.)
536
537 This should be safe; by the time we get here, the buffer isn't
538 queued anymore. If we ever start marking the buffers as
539 VIDEOBUF_ACTIVE, it won't be, though.
540 */
541 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300542 if (dev->usb_ctl.vid_buf == buf)
543 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300544 spin_unlock_irqrestore(&dev->slock, flags);
545
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300546 videobuf_vmalloc_free(&buf->vb);
547 buf->vb.state = VIDEOBUF_NEEDS_INIT;
548}
549
550static int
551buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
552 enum v4l2_field field)
553{
554 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300555 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300556 struct em28xx *dev = fh->dev;
557 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300558
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300559 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
560 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300561
562 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
563 return -EINVAL;
564
Brandon Philips05612972008-04-13 14:57:01 -0300565 buf->vb.width = dev->width;
566 buf->vb.height = dev->height;
567 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300568
569 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300570 rc = videobuf_iolock(vq, &buf->vb, NULL);
571 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300572 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300573 }
574
Frank Schaefer74209dc2012-11-08 14:11:37 -0300575 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300576 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300577
578 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300579 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300580 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
581 dev->analog_xfer_bulk,
582 EM28XX_NUM_BUFS,
583 dev->max_pkt_size,
584 dev->packet_multiplier,
585 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300586 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300587 goto fail;
588 }
589
590 buf->vb.state = VIDEOBUF_PREPARED;
591 return 0;
592
593fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300594 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595 return rc;
596}
597
598static void
599buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
600{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300601 struct em28xx_buffer *buf = container_of(vb,
602 struct em28xx_buffer,
603 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300604 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300605 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300606 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300607
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300608 buf->vb.state = VIDEOBUF_QUEUED;
609 list_add_tail(&buf->vb.queue, &vidq->active);
610
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300611}
612
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300613static void buffer_release(struct videobuf_queue *vq,
614 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300615{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300616 struct em28xx_buffer *buf = container_of(vb,
617 struct em28xx_buffer,
618 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300619 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300620 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300621
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300622 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300623
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300624 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300625}
626
627static struct videobuf_queue_ops em28xx_video_qops = {
628 .buf_setup = buffer_setup,
629 .buf_prepare = buffer_prepare,
630 .buf_queue = buffer_queue,
631 .buf_release = buffer_release,
632};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800633
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300634/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800635
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800636static void video_mux(struct em28xx *dev, int index)
637{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800638 dev->ctl_input = index;
639 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300640 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800641
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300642 if (!dev->ctl_aoutput)
643 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
644
Hans Verkuil5325b422009-04-02 11:26:22 -0300645 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
646 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800647
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300648 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300649 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300650 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
651 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300652 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300653 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300654 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
655 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800656 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300657
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300658 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300659 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
660 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300661 }
662
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300663 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800664}
665
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300666/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300667static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300668{
669 struct em28xx *dev = fh->dev;
670
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300671 if (fh->resources & bit)
672 /* have it already allocated */
673 return 1;
674
675 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300676 if (dev->resources & bit) {
677 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300678 return 0;
679 }
680 /* it's free, grab it */
681 fh->resources |= bit;
682 dev->resources |= bit;
683 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300684 return 1;
685}
686
687static int res_check(struct em28xx_fh *fh, unsigned int bit)
688{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300689 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300690}
691
692static int res_locked(struct em28xx *dev, unsigned int bit)
693{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300694 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300695}
696
697static void res_free(struct em28xx_fh *fh, unsigned int bits)
698{
699 struct em28xx *dev = fh->dev;
700
701 BUG_ON((fh->resources & bits) != bits);
702
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300703 fh->resources &= ~bits;
704 dev->resources &= ~bits;
705 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300706}
707
708static int get_ressource(struct em28xx_fh *fh)
709{
710 switch (fh->type) {
711 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
712 return EM28XX_RESOURCE_VIDEO;
713 case V4L2_BUF_TYPE_VBI_CAPTURE:
714 return EM28XX_RESOURCE_VBI;
715 default:
716 BUG();
717 return 0;
718 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300719}
720
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800721/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300722 * ac97_queryctrl()
723 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300724 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300725static int ac97_queryctrl(struct v4l2_queryctrl *qc)
726{
727 int i;
728
729 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
730 if (qc->id && qc->id == ac97_qctrl[i].id) {
731 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
732 return 0;
733 }
734 }
735
736 /* Control is not ac97 related */
737 return 1;
738}
739
740/*
741 * ac97_get_ctrl()
742 * return the current values for ac97 mute and volume
743 */
744static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300745{
746 switch (ctrl->id) {
747 case V4L2_CID_AUDIO_MUTE:
748 ctrl->value = dev->mute;
749 return 0;
750 case V4L2_CID_AUDIO_VOLUME:
751 ctrl->value = dev->volume;
752 return 0;
753 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300754 /* Control is not ac97 related */
755 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300756 }
757}
758
759/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300760 * ac97_set_ctrl()
761 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300762 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300763static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300764{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300765 int i;
766
767 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
768 if (ctrl->id == ac97_qctrl[i].id)
769 goto handle;
770
771 /* Announce that hasn't handle it */
772 return 1;
773
774handle:
775 if (ctrl->value < ac97_qctrl[i].minimum ||
776 ctrl->value > ac97_qctrl[i].maximum)
777 return -ERANGE;
778
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300779 switch (ctrl->id) {
780 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300781 dev->mute = ctrl->value;
782 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300783 case V4L2_CID_AUDIO_VOLUME:
784 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300785 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300786 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300787
788 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300789}
790
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300791static int check_dev(struct em28xx *dev)
792{
793 if (dev->state & DEV_DISCONNECTED) {
794 em28xx_errdev("v4l2 ioctl: device not present\n");
795 return -ENODEV;
796 }
797
798 if (dev->state & DEV_MISCONFIGURED) {
799 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
800 "close and open it again\n");
801 return -EIO;
802 }
803 return 0;
804}
805
806static void get_scale(struct em28xx *dev,
807 unsigned int width, unsigned int height,
808 unsigned int *hscale, unsigned int *vscale)
809{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300810 unsigned int maxw = norm_maxw(dev);
811 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812
813 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
814 if (*hscale >= 0x4000)
815 *hscale = 0x3fff;
816
817 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
818 if (*vscale >= 0x4000)
819 *vscale = 0x3fff;
820}
821
822/* ------------------------------------------------------------------
823 IOCTL vidioc handling
824 ------------------------------------------------------------------*/
825
Hans Verkuil78b526a2008-05-28 12:16:41 -0300826static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300827 struct v4l2_format *f)
828{
829 struct em28xx_fh *fh = priv;
830 struct em28xx *dev = fh->dev;
831
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300832 f->fmt.pix.width = dev->width;
833 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300834 f->fmt.pix.pixelformat = dev->format->fourcc;
835 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300836 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
838
839 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300840 if (dev->progressive)
841 f->fmt.pix.field = V4L2_FIELD_NONE;
842 else
843 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300844 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845 return 0;
846}
847
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300848static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
849{
850 unsigned int i;
851
852 for (i = 0; i < ARRAY_SIZE(format); i++)
853 if (format[i].fourcc == fourcc)
854 return &format[i];
855
856 return NULL;
857}
858
Hans Verkuil78b526a2008-05-28 12:16:41 -0300859static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300860 struct v4l2_format *f)
861{
862 struct em28xx_fh *fh = priv;
863 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300864 unsigned int width = f->fmt.pix.width;
865 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866 unsigned int maxw = norm_maxw(dev);
867 unsigned int maxh = norm_maxh(dev);
868 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300869 struct em28xx_fmt *fmt;
870
871 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
872 if (!fmt) {
873 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
874 f->fmt.pix.pixelformat);
875 return -EINVAL;
876 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300877
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300878 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300879 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300880 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
881 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300882 /* MaxPacketSize for em2800 is too small to capture at full resolution
883 * use half of maxw as the scaler can only scale to 50% */
884 if (width == maxw && height == maxh)
885 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300886 } else {
887 /* width must even because of the YUYV format
888 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300889 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
890 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891 }
892
893 get_scale(dev, width, height, &hscale, &vscale);
894
895 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
896 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
897
898 f->fmt.pix.width = width;
899 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300900 f->fmt.pix.pixelformat = fmt->fourcc;
901 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
902 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300903 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300904 if (dev->progressive)
905 f->fmt.pix.field = V4L2_FIELD_NONE;
906 else
907 f->fmt.pix.field = dev->interlaced ?
908 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300909
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910 return 0;
911}
912
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300913static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
914 unsigned width, unsigned height)
915{
916 struct em28xx_fmt *fmt;
917
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300918 fmt = format_by_fourcc(fourcc);
919 if (!fmt)
920 return -EINVAL;
921
922 dev->format = fmt;
923 dev->width = width;
924 dev->height = height;
925
926 /* set new image size */
927 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
928
929 em28xx_set_alternate(dev);
930 em28xx_resolution_set(dev);
931
932 return 0;
933}
934
Hans Verkuil78b526a2008-05-28 12:16:41 -0300935static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936 struct v4l2_format *f)
937{
938 struct em28xx_fh *fh = priv;
939 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300940 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941
942 rc = check_dev(dev);
943 if (rc < 0)
944 return rc;
945
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300946 vidioc_try_fmt_vid_cap(file, priv, f);
947
Brandon Philips05612972008-04-13 14:57:01 -0300948 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
949 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300950 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300951 }
952
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300953 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300954 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955}
956
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300957static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
958{
959 struct em28xx_fh *fh = priv;
960 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300961 int rc;
962
963 rc = check_dev(dev);
964 if (rc < 0)
965 return rc;
966
967 *norm = dev->norm;
968
969 return 0;
970}
971
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300972static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
973{
974 struct em28xx_fh *fh = priv;
975 struct em28xx *dev = fh->dev;
976 int rc;
977
978 rc = check_dev(dev);
979 if (rc < 0)
980 return rc;
981
982 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
983
984 return 0;
985}
986
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300987static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988{
989 struct em28xx_fh *fh = priv;
990 struct em28xx *dev = fh->dev;
991 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300992 int rc;
993
994 rc = check_dev(dev);
995 if (rc < 0)
996 return rc;
997
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300998 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300999
1000 /* Adjusts width/height, if needed */
1001 f.fmt.pix.width = dev->width;
1002 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001003 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005 /* set new image size */
1006 dev->width = f.fmt.pix.width;
1007 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001008 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1009
1010 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001011 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 return 0;
1014}
1015
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001016static int vidioc_g_parm(struct file *file, void *priv,
1017 struct v4l2_streamparm *p)
1018{
1019 struct em28xx_fh *fh = priv;
1020 struct em28xx *dev = fh->dev;
1021 int rc = 0;
1022
1023 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 return -EINVAL;
1025
1026 if (dev->board.is_webcam)
1027 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1028 video, g_parm, p);
1029 else
1030 v4l2_video_std_frame_period(dev->norm,
1031 &p->parm.capture.timeperframe);
1032
1033 return rc;
1034}
1035
1036static int vidioc_s_parm(struct file *file, void *priv,
1037 struct v4l2_streamparm *p)
1038{
1039 struct em28xx_fh *fh = priv;
1040 struct em28xx *dev = fh->dev;
1041
1042 if (!dev->board.is_webcam)
1043 return -EINVAL;
1044
1045 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1046 return -EINVAL;
1047
1048 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1049}
1050
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001051static const char *iname[] = {
1052 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1053 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1054 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1055 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1056 [EM28XX_VMUX_SVIDEO] = "S-Video",
1057 [EM28XX_VMUX_TELEVISION] = "Television",
1058 [EM28XX_VMUX_CABLE] = "Cable TV",
1059 [EM28XX_VMUX_DVB] = "DVB",
1060 [EM28XX_VMUX_DEBUG] = "for debug only",
1061};
1062
1063static int vidioc_enum_input(struct file *file, void *priv,
1064 struct v4l2_input *i)
1065{
1066 struct em28xx_fh *fh = priv;
1067 struct em28xx *dev = fh->dev;
1068 unsigned int n;
1069
1070 n = i->index;
1071 if (n >= MAX_EM28XX_INPUT)
1072 return -EINVAL;
1073 if (0 == INPUT(n)->type)
1074 return -EINVAL;
1075
1076 i->index = n;
1077 i->type = V4L2_INPUT_TYPE_CAMERA;
1078
1079 strcpy(i->name, iname[INPUT(n)->type]);
1080
1081 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1082 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1083 i->type = V4L2_INPUT_TYPE_TUNER;
1084
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001085 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001086
1087 return 0;
1088}
1089
1090static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1091{
1092 struct em28xx_fh *fh = priv;
1093 struct em28xx *dev = fh->dev;
1094
1095 *i = dev->ctl_input;
1096
1097 return 0;
1098}
1099
1100static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1101{
1102 struct em28xx_fh *fh = priv;
1103 struct em28xx *dev = fh->dev;
1104 int rc;
1105
1106 rc = check_dev(dev);
1107 if (rc < 0)
1108 return rc;
1109
1110 if (i >= MAX_EM28XX_INPUT)
1111 return -EINVAL;
1112 if (0 == INPUT(i)->type)
1113 return -EINVAL;
1114
Ezequiel García96371fc2012-03-23 18:09:34 -03001115 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001116 return 0;
1117}
1118
1119static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1120{
1121 struct em28xx_fh *fh = priv;
1122 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001123
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001124 if (!dev->audio_mode.has_audio)
1125 return -EINVAL;
1126
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001127 switch (a->index) {
1128 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001130 break;
1131 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001132 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001133 break;
1134 case EM28XX_AMUX_VIDEO2:
1135 strcpy(a->name, "Television alt");
1136 break;
1137 case EM28XX_AMUX_PHONE:
1138 strcpy(a->name, "Phone");
1139 break;
1140 case EM28XX_AMUX_MIC:
1141 strcpy(a->name, "Mic");
1142 break;
1143 case EM28XX_AMUX_CD:
1144 strcpy(a->name, "CD");
1145 break;
1146 case EM28XX_AMUX_AUX:
1147 strcpy(a->name, "Aux");
1148 break;
1149 case EM28XX_AMUX_PCM_OUT:
1150 strcpy(a->name, "PCM");
1151 break;
1152 default:
1153 return -EINVAL;
1154 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001155
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001156 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001158
1159 return 0;
1160}
1161
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001162static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001163{
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
1166
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001167
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001168 if (!dev->audio_mode.has_audio)
1169 return -EINVAL;
1170
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001171 if (a->index >= MAX_EM28XX_INPUT)
1172 return -EINVAL;
1173 if (0 == INPUT(a->index)->type)
1174 return -EINVAL;
1175
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001176 dev->ctl_ainput = INPUT(a->index)->amux;
1177 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001178
1179 if (!dev->ctl_aoutput)
1180 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001181
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001182 return 0;
1183}
1184
1185static int vidioc_queryctrl(struct file *file, void *priv,
1186 struct v4l2_queryctrl *qc)
1187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001191 int rc;
1192
1193 rc = check_dev(dev);
1194 if (rc < 0)
1195 return rc;
1196
1197 memset(qc, 0, sizeof(*qc));
1198
1199 qc->id = id;
1200
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001201 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001202 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1203 rc = ac97_queryctrl(qc);
1204 if (!rc)
1205 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001207
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001208 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001209 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001210
1211 if (qc->type)
1212 return 0;
1213 else
1214 return -EINVAL;
1215}
1216
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001217/*
1218 * FIXME: This is an indirect way to check if a control exists at a
1219 * subdev. Instead of that hack, maybe the better would be to change all
1220 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1221 */
1222static int check_subdev_ctrl(struct em28xx *dev, int id)
1223{
1224 struct v4l2_queryctrl qc;
1225
1226 memset(&qc, 0, sizeof(qc));
1227 qc.id = id;
1228
1229 /* enumerate V4L2 device controls */
1230 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1231
1232 if (qc.type)
1233 return 0;
1234 else
1235 return -EINVAL;
1236}
1237
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001238static int vidioc_g_ctrl(struct file *file, void *priv,
1239 struct v4l2_control *ctrl)
1240{
1241 struct em28xx_fh *fh = priv;
1242 struct em28xx *dev = fh->dev;
1243 int rc;
1244
1245 rc = check_dev(dev);
1246 if (rc < 0)
1247 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001248 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001249
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001250 /* Set an AC97 control */
1251 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1252 rc = ac97_get_ctrl(dev, ctrl);
1253 else
1254 rc = 1;
1255
1256 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1257 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001258 if (check_subdev_ctrl(dev, ctrl->id))
1259 return -EINVAL;
1260
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001261 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001262 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001263 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001264
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001265 return rc;
1266}
1267
1268static int vidioc_s_ctrl(struct file *file, void *priv,
1269 struct v4l2_control *ctrl)
1270{
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001273 int rc;
1274
1275 rc = check_dev(dev);
1276 if (rc < 0)
1277 return rc;
1278
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001279 /* Set an AC97 control */
1280 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1281 rc = ac97_set_ctrl(dev, ctrl);
1282 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001283 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001284
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001285 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001286 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001287 rc = check_subdev_ctrl(dev, ctrl->id);
1288 if (!rc)
1289 v4l2_device_call_all(&dev->v4l2_dev, 0,
1290 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001291 /*
1292 * In the case of non-AC97 volume controls, we still need
1293 * to do some setups at em28xx, in order to mute/unmute
1294 * and to adjust audio volume. However, the value ranges
1295 * should be checked by the corresponding V4L subdriver.
1296 */
1297 switch (ctrl->id) {
1298 case V4L2_CID_AUDIO_MUTE:
1299 dev->mute = ctrl->value;
1300 rc = em28xx_audio_analog_set(dev);
1301 break;
1302 case V4L2_CID_AUDIO_VOLUME:
1303 dev->volume = ctrl->value;
1304 rc = em28xx_audio_analog_set(dev);
1305 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001307 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001308}
1309
1310static int vidioc_g_tuner(struct file *file, void *priv,
1311 struct v4l2_tuner *t)
1312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
1315 int rc;
1316
1317 rc = check_dev(dev);
1318 if (rc < 0)
1319 return rc;
1320
1321 if (0 != t->index)
1322 return -EINVAL;
1323
1324 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001325 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001326
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001327 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328 return 0;
1329}
1330
1331static int vidioc_s_tuner(struct file *file, void *priv,
1332 struct v4l2_tuner *t)
1333{
1334 struct em28xx_fh *fh = priv;
1335 struct em28xx *dev = fh->dev;
1336 int rc;
1337
1338 rc = check_dev(dev);
1339 if (rc < 0)
1340 return rc;
1341
1342 if (0 != t->index)
1343 return -EINVAL;
1344
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001345 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001346 return 0;
1347}
1348
1349static int vidioc_g_frequency(struct file *file, void *priv,
1350 struct v4l2_frequency *f)
1351{
1352 struct em28xx_fh *fh = priv;
1353 struct em28xx *dev = fh->dev;
1354
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001355 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001357 return 0;
1358}
1359
1360static int vidioc_s_frequency(struct file *file, void *priv,
1361 struct v4l2_frequency *f)
1362{
1363 struct em28xx_fh *fh = priv;
1364 struct em28xx *dev = fh->dev;
1365 int rc;
1366
1367 rc = check_dev(dev);
1368 if (rc < 0)
1369 return rc;
1370
1371 if (0 != f->tuner)
1372 return -EINVAL;
1373
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001374 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1375 return -EINVAL;
1376 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377 return -EINVAL;
1378
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001379 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001380 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 return 0;
1383}
1384
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001385#ifdef CONFIG_VIDEO_ADV_DEBUG
1386static int em28xx_reg_len(int reg)
1387{
1388 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001389 case EM28XX_R40_AC97LSB:
1390 case EM28XX_R30_HSCALELOW:
1391 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001392 return 2;
1393 default:
1394 return 1;
1395 }
1396}
1397
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001398static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001399 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001400{
1401 struct em28xx_fh *fh = priv;
1402 struct em28xx *dev = fh->dev;
1403
1404 chip->ident = V4L2_IDENT_NONE;
1405 chip->revision = 0;
Hans Verkuil319a55f2012-09-06 09:53:08 -03001406 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1407 if (v4l2_chip_match_host(&chip->match))
1408 chip->ident = V4L2_IDENT_NONE;
1409 return 0;
1410 }
1411 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1412 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1413 return -EINVAL;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001414
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001415 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001416
1417 return 0;
1418}
1419
1420
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001421static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001422 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001423{
1424 struct em28xx_fh *fh = priv;
1425 struct em28xx *dev = fh->dev;
1426 int ret;
1427
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001428 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001429 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001430 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001431 if (ret < 0)
1432 return ret;
1433
1434 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001435 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001436 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001437 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001438 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001439 return 0;
1440 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001441 /* TODO: is this correct? */
1442 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1443 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001444 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001445 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001446 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001447 }
1448
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001449 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001450 reg->size = em28xx_reg_len(reg->reg);
1451 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001452 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001453
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001454 if (ret < 0)
1455 return ret;
1456
1457 reg->val = ret;
1458 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001459 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001460 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1461 reg->reg, (char *)&val, 2);
1462 if (ret < 0)
1463 return ret;
1464
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001465 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001466 }
1467
1468 return 0;
1469}
1470
1471static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001472 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001473{
1474 struct em28xx_fh *fh = priv;
1475 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001476 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001477
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001478 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001479 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001480 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001481 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001482 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001483 return 0;
1484 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001485 /* TODO: is this correct? */
1486 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1487 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001488 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001489 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001490 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001491 }
1492
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001493 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001494 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001495
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001496 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001497 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001498}
1499#endif
1500
1501
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001502static int vidioc_cropcap(struct file *file, void *priv,
1503 struct v4l2_cropcap *cc)
1504{
1505 struct em28xx_fh *fh = priv;
1506 struct em28xx *dev = fh->dev;
1507
1508 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1509 return -EINVAL;
1510
1511 cc->bounds.left = 0;
1512 cc->bounds.top = 0;
1513 cc->bounds.width = dev->width;
1514 cc->bounds.height = dev->height;
1515 cc->defrect = cc->bounds;
1516 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1517 cc->pixelaspect.denominator = 59;
1518
1519 return 0;
1520}
1521
1522static int vidioc_streamon(struct file *file, void *priv,
1523 enum v4l2_buf_type type)
1524{
1525 struct em28xx_fh *fh = priv;
1526 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001527 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001528
1529 rc = check_dev(dev);
1530 if (rc < 0)
1531 return rc;
1532
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001533 if (unlikely(type != fh->type))
1534 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001535
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001536 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1537 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001538
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001539 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001540 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001541
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001542 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1543 rc = videobuf_streamon(&fh->vb_vidq);
1544 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1545 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001546
1547 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001548}
1549
1550static int vidioc_streamoff(struct file *file, void *priv,
1551 enum v4l2_buf_type type)
1552{
1553 struct em28xx_fh *fh = priv;
1554 struct em28xx *dev = fh->dev;
1555 int rc;
1556
1557 rc = check_dev(dev);
1558 if (rc < 0)
1559 return rc;
1560
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001561 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1562 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001563 return -EINVAL;
1564 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001565 return -EINVAL;
1566
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001567 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1568 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001569
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001570 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001571 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1572 videobuf_streamoff(&fh->vb_vidq);
1573 res_free(fh, EM28XX_RESOURCE_VIDEO);
1574 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001575 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001576 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1577 videobuf_streamoff(&fh->vb_vbiq);
1578 res_free(fh, EM28XX_RESOURCE_VBI);
1579 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001580 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001581
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582 return 0;
1583}
1584
1585static int vidioc_querycap(struct file *file, void *priv,
1586 struct v4l2_capability *cap)
1587{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001588 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001589 struct em28xx_fh *fh = priv;
1590 struct em28xx *dev = fh->dev;
1591
1592 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1593 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001594 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001596 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1597 cap->device_caps = V4L2_CAP_READWRITE |
1598 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1599 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1600 cap->device_caps = V4L2_CAP_RADIO;
1601 else
1602 cap->device_caps = V4L2_CAP_READWRITE |
1603 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001604
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001605 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001606 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001607
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001608 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001609 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001610
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001611 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1612 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1613 if (dev->vbi_dev)
1614 cap->capabilities |=
1615 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
1616 if (dev->radio_dev)
1617 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001618 return 0;
1619}
1620
Hans Verkuil78b526a2008-05-28 12:16:41 -03001621static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001622 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001623{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001624 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001625 return -EINVAL;
1626
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001627 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1628 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001629
1630 return 0;
1631}
1632
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001633static int vidioc_enum_framesizes(struct file *file, void *priv,
1634 struct v4l2_frmsizeenum *fsize)
1635{
1636 struct em28xx_fh *fh = priv;
1637 struct em28xx *dev = fh->dev;
1638 struct em28xx_fmt *fmt;
1639 unsigned int maxw = norm_maxw(dev);
1640 unsigned int maxh = norm_maxh(dev);
1641
1642 fmt = format_by_fourcc(fsize->pixel_format);
1643 if (!fmt) {
1644 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1645 fsize->pixel_format);
1646 return -EINVAL;
1647 }
1648
1649 if (dev->board.is_em2800) {
1650 if (fsize->index > 1)
1651 return -EINVAL;
1652 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1653 fsize->discrete.width = maxw / (1 + fsize->index);
1654 fsize->discrete.height = maxh / (1 + fsize->index);
1655 return 0;
1656 }
1657
1658 if (fsize->index != 0)
1659 return -EINVAL;
1660
1661 /* Report a continuous range */
1662 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1663 fsize->stepwise.min_width = 48;
1664 fsize->stepwise.min_height = 32;
1665 fsize->stepwise.max_width = maxw;
1666 fsize->stepwise.max_height = maxh;
1667 fsize->stepwise.step_width = 1;
1668 fsize->stepwise.step_height = 1;
1669 return 0;
1670}
1671
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001672/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001673static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001674 struct v4l2_format *f)
1675{
1676 struct em28xx_fh *fh = priv;
1677 struct em28xx *dev = fh->dev;
1678 int rc;
1679
1680 rc = check_dev(dev);
1681 if (rc < 0)
1682 return rc;
1683
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001684 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001685 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001686
1687 if (f->fmt.sliced.service_set == 0)
1688 rc = -EINVAL;
1689
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001690 return rc;
1691}
1692
Hans Verkuil78b526a2008-05-28 12:16:41 -03001693static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001694 struct v4l2_format *f)
1695{
1696 struct em28xx_fh *fh = priv;
1697 struct em28xx *dev = fh->dev;
1698 int rc;
1699
1700 rc = check_dev(dev);
1701 if (rc < 0)
1702 return rc;
1703
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001704 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001705
1706 if (f->fmt.sliced.service_set == 0)
1707 return -EINVAL;
1708
1709 return 0;
1710}
1711
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001712/* RAW VBI ioctls */
1713
1714static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1715 struct v4l2_format *format)
1716{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001717 struct em28xx_fh *fh = priv;
1718 struct em28xx *dev = fh->dev;
1719
1720 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001721 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1722 format->fmt.vbi.offset = 0;
1723 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001724 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1725 format->fmt.vbi.count[0] = dev->vbi_height;
1726 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001727
1728 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001729 if (dev->norm & V4L2_STD_525_60) {
1730 /* NTSC */
1731 format->fmt.vbi.start[0] = 10;
1732 format->fmt.vbi.start[1] = 273;
1733 } else if (dev->norm & V4L2_STD_625_50) {
1734 /* PAL */
1735 format->fmt.vbi.start[0] = 6;
1736 format->fmt.vbi.start[1] = 318;
1737 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001738
1739 return 0;
1740}
1741
1742static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1743 struct v4l2_format *format)
1744{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001745 struct em28xx_fh *fh = priv;
1746 struct em28xx *dev = fh->dev;
1747
1748 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001749 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1750 format->fmt.vbi.offset = 0;
1751 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001752 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1753 format->fmt.vbi.count[0] = dev->vbi_height;
1754 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001755
1756 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001757 if (dev->norm & V4L2_STD_525_60) {
1758 /* NTSC */
1759 format->fmt.vbi.start[0] = 10;
1760 format->fmt.vbi.start[1] = 273;
1761 } else if (dev->norm & V4L2_STD_625_50) {
1762 /* PAL */
1763 format->fmt.vbi.start[0] = 6;
1764 format->fmt.vbi.start[1] = 318;
1765 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001766
1767 return 0;
1768}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001769
1770static int vidioc_reqbufs(struct file *file, void *priv,
1771 struct v4l2_requestbuffers *rb)
1772{
1773 struct em28xx_fh *fh = priv;
1774 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 int rc;
1776
1777 rc = check_dev(dev);
1778 if (rc < 0)
1779 return rc;
1780
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001781 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1782 return videobuf_reqbufs(&fh->vb_vidq, rb);
1783 else
1784 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785}
1786
1787static int vidioc_querybuf(struct file *file, void *priv,
1788 struct v4l2_buffer *b)
1789{
1790 struct em28xx_fh *fh = priv;
1791 struct em28xx *dev = fh->dev;
1792 int rc;
1793
1794 rc = check_dev(dev);
1795 if (rc < 0)
1796 return rc;
1797
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001798 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1799 return videobuf_querybuf(&fh->vb_vidq, b);
1800 else {
1801 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1802 the videobuf framework, but we probably shouldn't be
1803 returning a buffer larger than that which was asked for.
1804 At a minimum, it causes a crash in zvbi since it does
1805 a memcpy based on the source buffer length */
1806 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001807 b->length = dev->vbi_width * dev->vbi_height * 2;
1808
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001809 return result;
1810 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001811}
1812
1813static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1814{
1815 struct em28xx_fh *fh = priv;
1816 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817 int rc;
1818
1819 rc = check_dev(dev);
1820 if (rc < 0)
1821 return rc;
1822
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001823 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1824 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001825 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001826 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001827}
1828
1829static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1830{
1831 struct em28xx_fh *fh = priv;
1832 struct em28xx *dev = fh->dev;
1833 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001834
1835 rc = check_dev(dev);
1836 if (rc < 0)
1837 return rc;
1838
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001839 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1840 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1841 O_NONBLOCK);
1842 else
1843 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1844 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001845}
1846
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001847/* ----------------------------------------------------------- */
1848/* RADIO ESPECIFIC IOCTLS */
1849/* ----------------------------------------------------------- */
1850
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001851static int radio_g_tuner(struct file *file, void *priv,
1852 struct v4l2_tuner *t)
1853{
1854 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1855
1856 if (unlikely(t->index > 0))
1857 return -EINVAL;
1858
1859 strcpy(t->name, "Radio");
1860 t->type = V4L2_TUNER_RADIO;
1861
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001862 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001863
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001864 return 0;
1865}
1866
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001867static int radio_s_tuner(struct file *file, void *priv,
1868 struct v4l2_tuner *t)
1869{
1870 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1871
1872 if (0 != t->index)
1873 return -EINVAL;
1874
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001875 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001876
1877 return 0;
1878}
1879
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001880static int radio_queryctrl(struct file *file, void *priv,
1881 struct v4l2_queryctrl *qc)
1882{
1883 int i;
1884
1885 if (qc->id < V4L2_CID_BASE ||
1886 qc->id >= V4L2_CID_LASTP1)
1887 return -EINVAL;
1888
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001889 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1890 if (qc->id && qc->id == ac97_qctrl[i].id) {
1891 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001892 return 0;
1893 }
1894 }
1895
1896 return -EINVAL;
1897}
1898
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001899/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001900 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001901 * inits the device and starts isoc transfer
1902 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001903static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001904{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001905 int errCode = 0, radio = 0;
1906 struct video_device *vdev = video_devdata(filp);
1907 struct em28xx *dev = video_drvdata(filp);
1908 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001909 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001910 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001911
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001912 switch (vdev->vfl_type) {
1913 case VFL_TYPE_GRABBER:
1914 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1915 break;
1916 case VFL_TYPE_VBI:
1917 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1918 break;
1919 case VFL_TYPE_RADIO:
1920 radio = 1;
1921 break;
1922 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001923
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001924 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1925 video_device_node_name(vdev), v4l2_type_names[fh_type],
1926 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001928
Hans Verkuil876cb142012-06-23 08:12:47 -03001929 if (mutex_lock_interruptible(&dev->lock))
1930 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001931 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001932 if (!fh) {
1933 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001934 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001935 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001936 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001937 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001938 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001939 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001940 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001941
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001942 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001943 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001944 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001945 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001946
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001947 /* Needed, since GPIO might have disabled power of
1948 some i2c device
1949 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001950 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001951
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001952 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001953 if (fh->radio) {
1954 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001955 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001956 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001957
1958 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001959
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001960 if (dev->progressive)
1961 field = V4L2_FIELD_NONE;
1962 else
1963 field = V4L2_FIELD_INTERLACED;
1964
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001965 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1966 NULL, &dev->slock,
1967 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001968 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001969
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001970 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1971 NULL, &dev->slock,
1972 V4L2_BUF_TYPE_VBI_CAPTURE,
1973 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001974 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03001975 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001976
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001977 return errCode;
1978}
1979
1980/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001981 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001982 * unregisters the v4l2,i2c and usb devices
1983 * called when the device gets disconected or at module unload
1984*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001985void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001986{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001987
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001988 /*FIXME: I2C IR should be disconnected */
1989
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001990 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001991 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001992 video_unregister_device(dev->radio_dev);
1993 else
1994 video_device_release(dev->radio_dev);
1995 dev->radio_dev = NULL;
1996 }
1997 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001998 em28xx_info("V4L2 device %s deregistered\n",
1999 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002000 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002001 video_unregister_device(dev->vbi_dev);
2002 else
2003 video_device_release(dev->vbi_dev);
2004 dev->vbi_dev = NULL;
2005 }
2006 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002007 em28xx_info("V4L2 device %s deregistered\n",
2008 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002009 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002010 video_unregister_device(dev->vdev);
2011 else
2012 video_device_release(dev->vdev);
2013 dev->vdev = NULL;
2014 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002015}
2016
2017/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002018 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002019 * stops streaming and deallocates all resources allocated by the v4l2
2020 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002021 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002022static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002023{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002024 struct em28xx_fh *fh = filp->private_data;
2025 struct em28xx *dev = fh->dev;
2026 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002027
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002028 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002029
Hans Verkuil876cb142012-06-23 08:12:47 -03002030 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002031 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002032 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002033 res_free(fh, EM28XX_RESOURCE_VIDEO);
2034 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002035
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002036 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2037 videobuf_stop(&fh->vb_vbiq);
2038 res_free(fh, EM28XX_RESOURCE_VBI);
2039 }
2040
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002041 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002042 /* the device is already disconnect,
2043 free the remaining resources */
2044 if (dev->state & DEV_DISCONNECTED) {
2045 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002046 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002047 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002048 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002049 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002050 return 0;
2051 }
2052
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002053 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002054 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002055
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002056 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002057 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002058 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002059
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002060 /* set alternate 0 */
2061 dev->alt = 0;
2062 em28xx_videodbg("setting alternate 0\n");
2063 errCode = usb_set_interface(dev->udev, 0, 0);
2064 if (errCode < 0) {
2065 em28xx_errdev("cannot change alternate number to "
2066 "0 (error=%i)\n", errCode);
2067 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002068 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002069
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002070 videobuf_mmap_free(&fh->vb_vidq);
2071 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002072 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002074 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002075 return 0;
2076}
2077
2078/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002079 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002080 * will allocate buffers when called for the first time
2081 */
2082static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002083em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002084 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002085{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002086 struct em28xx_fh *fh = filp->private_data;
2087 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002088 int rc;
2089
2090 rc = check_dev(dev);
2091 if (rc < 0)
2092 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093
Hans Verkuil876cb142012-06-23 08:12:47 -03002094 if (mutex_lock_interruptible(&dev->lock))
2095 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002096 /* FIXME: read() is not prepared to allow changing the video
2097 resolution while streaming. Seems a bug at em28xx_set_fmt
2098 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002099
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002100 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002101 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002102 rc = -EBUSY;
2103 else
2104 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002105 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002106 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002107 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002108 rc = -EBUSY;
2109 else
2110 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002111 filp->f_flags & O_NONBLOCK);
2112 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002113 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002114
Hans Verkuil876cb142012-06-23 08:12:47 -03002115 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116}
2117
2118/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002119 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002120 * will allocate buffers when called for the first time
2121 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002122static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002123{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002124 struct em28xx_fh *fh = filp->private_data;
2125 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002126 int rc;
2127
2128 rc = check_dev(dev);
2129 if (rc < 0)
2130 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002131
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002132 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2133 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2134 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002135 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002136 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2137 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2138 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002139 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002140 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002141 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002142 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002143}
2144
Hans Verkuil876cb142012-06-23 08:12:47 -03002145static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2146{
2147 struct em28xx_fh *fh = filp->private_data;
2148 struct em28xx *dev = fh->dev;
2149 unsigned int res;
2150
2151 mutex_lock(&dev->lock);
2152 res = em28xx_poll(filp, wait);
2153 mutex_unlock(&dev->lock);
2154 return res;
2155}
2156
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002158 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002159 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002160static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002161{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002162 struct em28xx_fh *fh = filp->private_data;
2163 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002164 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002165
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002166 rc = check_dev(dev);
2167 if (rc < 0)
2168 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002169
Hans Verkuil876cb142012-06-23 08:12:47 -03002170 if (mutex_lock_interruptible(&dev->lock))
2171 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002172 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2173 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2174 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2175 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002176 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002177
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002178 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2179 (unsigned long)vma->vm_start,
2180 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2181 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002182
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002183 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184}
2185
Hans Verkuilbec43662008-12-30 06:58:20 -03002186static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002187 .owner = THIS_MODULE,
2188 .open = em28xx_v4l2_open,
2189 .release = em28xx_v4l2_close,
2190 .read = em28xx_v4l2_read,
2191 .poll = em28xx_v4l2_poll,
2192 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002193 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002194};
2195
Hans Verkuila3998102008-07-21 02:57:38 -03002196static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002197 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002198 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2199 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2200 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2201 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002202 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2203 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002204 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002205 .vidioc_g_audio = vidioc_g_audio,
2206 .vidioc_s_audio = vidioc_s_audio,
2207 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002208 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2209 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2210 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002211
2212 .vidioc_reqbufs = vidioc_reqbufs,
2213 .vidioc_querybuf = vidioc_querybuf,
2214 .vidioc_qbuf = vidioc_qbuf,
2215 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002216 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002217 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002218 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002219 .vidioc_g_parm = vidioc_g_parm,
2220 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002221 .vidioc_enum_input = vidioc_enum_input,
2222 .vidioc_g_input = vidioc_g_input,
2223 .vidioc_s_input = vidioc_s_input,
2224 .vidioc_queryctrl = vidioc_queryctrl,
2225 .vidioc_g_ctrl = vidioc_g_ctrl,
2226 .vidioc_s_ctrl = vidioc_s_ctrl,
2227 .vidioc_streamon = vidioc_streamon,
2228 .vidioc_streamoff = vidioc_streamoff,
2229 .vidioc_g_tuner = vidioc_g_tuner,
2230 .vidioc_s_tuner = vidioc_s_tuner,
2231 .vidioc_g_frequency = vidioc_g_frequency,
2232 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002233#ifdef CONFIG_VIDEO_ADV_DEBUG
2234 .vidioc_g_register = vidioc_g_register,
2235 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002236 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002237#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002238};
2239
2240static const struct video_device em28xx_video_template = {
2241 .fops = &em28xx_v4l_fops,
2242 .release = video_device_release,
2243 .ioctl_ops = &video_ioctl_ops,
2244
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002245 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002246 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002247};
2248
Hans Verkuilbec43662008-12-30 06:58:20 -03002249static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002250 .owner = THIS_MODULE,
2251 .open = em28xx_v4l2_open,
2252 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002253 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002254};
2255
2256static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03002257 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002258 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002259 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002260 .vidioc_queryctrl = radio_queryctrl,
2261 .vidioc_g_ctrl = vidioc_g_ctrl,
2262 .vidioc_s_ctrl = vidioc_s_ctrl,
2263 .vidioc_g_frequency = vidioc_g_frequency,
2264 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002265#ifdef CONFIG_VIDEO_ADV_DEBUG
2266 .vidioc_g_register = vidioc_g_register,
2267 .vidioc_s_register = vidioc_s_register,
2268#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002269};
2270
Hans Verkuila3998102008-07-21 02:57:38 -03002271static struct video_device em28xx_radio_template = {
2272 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002273 .fops = &radio_fops,
2274 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002275};
2276
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002277/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002278
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002279
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002280
Adrian Bunk532fe652008-01-28 22:10:48 -03002281static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002282 const struct video_device *template,
2283 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002284{
2285 struct video_device *vfd;
2286
2287 vfd = video_device_alloc();
2288 if (NULL == vfd)
2289 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002290
2291 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002292 vfd->v4l2_dev = &dev->v4l2_dev;
2293 vfd->release = video_device_release;
2294 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002295 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002296
2297 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2298 dev->name, type_name);
2299
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002300 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002301 return vfd;
2302}
2303
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002304int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002305{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002306 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002307 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002308 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002309
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002310 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2311 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002312
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002313 /* set default norm */
2314 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002315 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002316 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002317
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002318 /* Analog specific initialization */
2319 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002320
2321 maxw = norm_maxw(dev);
2322 /* MaxPacketSize for em2800 is too small to capture at full resolution
2323 * use half of maxw as the scaler can only scale to 50% */
2324 if (dev->board.is_em2800)
2325 maxw /= 2;
2326
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002327 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002328 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002329
Ezequiel García96371fc2012-03-23 18:09:34 -03002330 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002331
2332 /* Audio defaults */
2333 dev->mute = 1;
2334 dev->volume = 0x1f;
2335
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002336/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002337 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2338 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2339 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002340
2341 em28xx_set_outfmt(dev);
2342 em28xx_colorlevels_set_default(dev);
2343 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002344
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002345 /* allocate and fill video video_device struct */
2346 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2347 if (!dev->vdev) {
2348 em28xx_errdev("cannot allocate video_device.\n");
2349 return -ENODEV;
2350 }
2351
2352 /* register v4l2 video video_device */
2353 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2354 video_nr[dev->devno]);
2355 if (ret) {
2356 em28xx_errdev("unable to register video device (error=%i).\n",
2357 ret);
2358 return ret;
2359 }
2360
2361 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002362 if (em28xx_vbi_supported(dev) == 1) {
2363 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2364 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002365
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002366 /* register v4l2 vbi video_device */
2367 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2368 vbi_nr[dev->devno]);
2369 if (ret < 0) {
2370 em28xx_errdev("unable to register vbi device\n");
2371 return ret;
2372 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002373 }
2374
2375 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002376 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2377 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002378 if (!dev->radio_dev) {
2379 em28xx_errdev("cannot allocate video_device.\n");
2380 return -ENODEV;
2381 }
2382 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2383 radio_nr[dev->devno]);
2384 if (ret < 0) {
2385 em28xx_errdev("can't register radio device\n");
2386 return ret;
2387 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002388 em28xx_info("Registered radio device as %s\n",
2389 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002390 }
2391
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002392 em28xx_info("V4L2 video device registered as %s\n",
2393 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002394
2395 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002396 em28xx_info("V4L2 VBI device registered as %s\n",
2397 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002398
2399 return 0;
2400}