blob: fb9ee467713059be092072ef6293f7c4ed54b53d [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 Thorntone0fadfd342008-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 Verkuil0e8025b92012-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 Verkuilaecde8b52008-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;
1406
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001407 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001408
1409 return 0;
1410}
1411
1412
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001413static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001414 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001415{
1416 struct em28xx_fh *fh = priv;
1417 struct em28xx *dev = fh->dev;
1418 int ret;
1419
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001420 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001421 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001422 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001423 if (ret < 0)
1424 return ret;
1425
1426 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001427 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001428 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001429 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001430 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001431 return 0;
1432 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001433 /* TODO: is this correct? */
1434 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1435 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001436 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001437 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001438 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001439 }
1440
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001441 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001442 reg->size = em28xx_reg_len(reg->reg);
1443 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001444 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001445
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001446 if (ret < 0)
1447 return ret;
1448
1449 reg->val = ret;
1450 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001451 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001452 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1453 reg->reg, (char *)&val, 2);
1454 if (ret < 0)
1455 return ret;
1456
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001457 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001458 }
1459
1460 return 0;
1461}
1462
1463static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001464 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001465{
1466 struct em28xx_fh *fh = priv;
1467 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001468 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001469
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001470 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001471 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001472 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001473 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001474 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001475 return 0;
1476 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001477 /* TODO: is this correct? */
1478 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1479 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001480 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001481 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001482 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001483 }
1484
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001485 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001486 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001487
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001488 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001489 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001490}
1491#endif
1492
1493
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001494static int vidioc_cropcap(struct file *file, void *priv,
1495 struct v4l2_cropcap *cc)
1496{
1497 struct em28xx_fh *fh = priv;
1498 struct em28xx *dev = fh->dev;
1499
1500 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1501 return -EINVAL;
1502
1503 cc->bounds.left = 0;
1504 cc->bounds.top = 0;
1505 cc->bounds.width = dev->width;
1506 cc->bounds.height = dev->height;
1507 cc->defrect = cc->bounds;
1508 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1509 cc->pixelaspect.denominator = 59;
1510
1511 return 0;
1512}
1513
1514static int vidioc_streamon(struct file *file, void *priv,
1515 enum v4l2_buf_type type)
1516{
1517 struct em28xx_fh *fh = priv;
1518 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001519 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001520
1521 rc = check_dev(dev);
1522 if (rc < 0)
1523 return rc;
1524
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001525 if (unlikely(type != fh->type))
1526 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001527
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001528 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1529 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001530
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001531 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001532 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001533
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001534 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1535 rc = videobuf_streamon(&fh->vb_vidq);
1536 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1537 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001538
1539 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001540}
1541
1542static int vidioc_streamoff(struct file *file, void *priv,
1543 enum v4l2_buf_type type)
1544{
1545 struct em28xx_fh *fh = priv;
1546 struct em28xx *dev = fh->dev;
1547 int rc;
1548
1549 rc = check_dev(dev);
1550 if (rc < 0)
1551 return rc;
1552
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001553 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1554 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001555 return -EINVAL;
1556 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001557 return -EINVAL;
1558
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001559 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1560 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001561
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001562 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001563 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1564 videobuf_streamoff(&fh->vb_vidq);
1565 res_free(fh, EM28XX_RESOURCE_VIDEO);
1566 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001567 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001568 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1569 videobuf_streamoff(&fh->vb_vbiq);
1570 res_free(fh, EM28XX_RESOURCE_VBI);
1571 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001572 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001573
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001574 return 0;
1575}
1576
1577static int vidioc_querycap(struct file *file, void *priv,
1578 struct v4l2_capability *cap)
1579{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001580 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001581 struct em28xx_fh *fh = priv;
1582 struct em28xx *dev = fh->dev;
1583
1584 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1585 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001586 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001587
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001588 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1589 cap->device_caps = V4L2_CAP_READWRITE |
1590 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1591 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1592 cap->device_caps = V4L2_CAP_RADIO;
1593 else
1594 cap->device_caps = V4L2_CAP_READWRITE |
1595 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001596
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001597 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001598 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001599
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001600 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001601 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001602
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001603 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1604 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1605 if (dev->vbi_dev)
1606 cap->capabilities |=
1607 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
1608 if (dev->radio_dev)
1609 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001610 return 0;
1611}
1612
Hans Verkuil78b526a2008-05-28 12:16:41 -03001613static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001614 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001615{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001616 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001617 return -EINVAL;
1618
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001619 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1620 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001621
1622 return 0;
1623}
1624
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001625static int vidioc_enum_framesizes(struct file *file, void *priv,
1626 struct v4l2_frmsizeenum *fsize)
1627{
1628 struct em28xx_fh *fh = priv;
1629 struct em28xx *dev = fh->dev;
1630 struct em28xx_fmt *fmt;
1631 unsigned int maxw = norm_maxw(dev);
1632 unsigned int maxh = norm_maxh(dev);
1633
1634 fmt = format_by_fourcc(fsize->pixel_format);
1635 if (!fmt) {
1636 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1637 fsize->pixel_format);
1638 return -EINVAL;
1639 }
1640
1641 if (dev->board.is_em2800) {
1642 if (fsize->index > 1)
1643 return -EINVAL;
1644 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1645 fsize->discrete.width = maxw / (1 + fsize->index);
1646 fsize->discrete.height = maxh / (1 + fsize->index);
1647 return 0;
1648 }
1649
1650 if (fsize->index != 0)
1651 return -EINVAL;
1652
1653 /* Report a continuous range */
1654 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1655 fsize->stepwise.min_width = 48;
1656 fsize->stepwise.min_height = 32;
1657 fsize->stepwise.max_width = maxw;
1658 fsize->stepwise.max_height = maxh;
1659 fsize->stepwise.step_width = 1;
1660 fsize->stepwise.step_height = 1;
1661 return 0;
1662}
1663
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001664/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001665static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001666 struct v4l2_format *f)
1667{
1668 struct em28xx_fh *fh = priv;
1669 struct em28xx *dev = fh->dev;
1670 int rc;
1671
1672 rc = check_dev(dev);
1673 if (rc < 0)
1674 return rc;
1675
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001676 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001677 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001678
1679 if (f->fmt.sliced.service_set == 0)
1680 rc = -EINVAL;
1681
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001682 return rc;
1683}
1684
Hans Verkuil78b526a2008-05-28 12:16:41 -03001685static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001686 struct v4l2_format *f)
1687{
1688 struct em28xx_fh *fh = priv;
1689 struct em28xx *dev = fh->dev;
1690 int rc;
1691
1692 rc = check_dev(dev);
1693 if (rc < 0)
1694 return rc;
1695
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001696 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001697
1698 if (f->fmt.sliced.service_set == 0)
1699 return -EINVAL;
1700
1701 return 0;
1702}
1703
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001704/* RAW VBI ioctls */
1705
1706static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1707 struct v4l2_format *format)
1708{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001709 struct em28xx_fh *fh = priv;
1710 struct em28xx *dev = fh->dev;
1711
1712 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001713 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1714 format->fmt.vbi.offset = 0;
1715 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001716 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1717 format->fmt.vbi.count[0] = dev->vbi_height;
1718 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001719
1720 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001721 if (dev->norm & V4L2_STD_525_60) {
1722 /* NTSC */
1723 format->fmt.vbi.start[0] = 10;
1724 format->fmt.vbi.start[1] = 273;
1725 } else if (dev->norm & V4L2_STD_625_50) {
1726 /* PAL */
1727 format->fmt.vbi.start[0] = 6;
1728 format->fmt.vbi.start[1] = 318;
1729 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001730
1731 return 0;
1732}
1733
1734static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1735 struct v4l2_format *format)
1736{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001737 struct em28xx_fh *fh = priv;
1738 struct em28xx *dev = fh->dev;
1739
1740 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001741 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1742 format->fmt.vbi.offset = 0;
1743 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001744 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1745 format->fmt.vbi.count[0] = dev->vbi_height;
1746 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001747
1748 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001749 if (dev->norm & V4L2_STD_525_60) {
1750 /* NTSC */
1751 format->fmt.vbi.start[0] = 10;
1752 format->fmt.vbi.start[1] = 273;
1753 } else if (dev->norm & V4L2_STD_625_50) {
1754 /* PAL */
1755 format->fmt.vbi.start[0] = 6;
1756 format->fmt.vbi.start[1] = 318;
1757 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001758
1759 return 0;
1760}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001761
1762static int vidioc_reqbufs(struct file *file, void *priv,
1763 struct v4l2_requestbuffers *rb)
1764{
1765 struct em28xx_fh *fh = priv;
1766 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001767 int rc;
1768
1769 rc = check_dev(dev);
1770 if (rc < 0)
1771 return rc;
1772
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001773 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1774 return videobuf_reqbufs(&fh->vb_vidq, rb);
1775 else
1776 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001777}
1778
1779static int vidioc_querybuf(struct file *file, void *priv,
1780 struct v4l2_buffer *b)
1781{
1782 struct em28xx_fh *fh = priv;
1783 struct em28xx *dev = fh->dev;
1784 int rc;
1785
1786 rc = check_dev(dev);
1787 if (rc < 0)
1788 return rc;
1789
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001790 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1791 return videobuf_querybuf(&fh->vb_vidq, b);
1792 else {
1793 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1794 the videobuf framework, but we probably shouldn't be
1795 returning a buffer larger than that which was asked for.
1796 At a minimum, it causes a crash in zvbi since it does
1797 a memcpy based on the source buffer length */
1798 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001799 b->length = dev->vbi_width * dev->vbi_height * 2;
1800
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001801 return result;
1802 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001803}
1804
1805static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1806{
1807 struct em28xx_fh *fh = priv;
1808 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001809 int rc;
1810
1811 rc = check_dev(dev);
1812 if (rc < 0)
1813 return rc;
1814
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001815 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1816 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001817 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001818 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001819}
1820
1821static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1822{
1823 struct em28xx_fh *fh = priv;
1824 struct em28xx *dev = fh->dev;
1825 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001826
1827 rc = check_dev(dev);
1828 if (rc < 0)
1829 return rc;
1830
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001831 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1832 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1833 O_NONBLOCK);
1834 else
1835 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1836 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001837}
1838
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001839/* ----------------------------------------------------------- */
1840/* RADIO ESPECIFIC IOCTLS */
1841/* ----------------------------------------------------------- */
1842
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001843static int radio_g_tuner(struct file *file, void *priv,
1844 struct v4l2_tuner *t)
1845{
1846 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1847
1848 if (unlikely(t->index > 0))
1849 return -EINVAL;
1850
1851 strcpy(t->name, "Radio");
1852 t->type = V4L2_TUNER_RADIO;
1853
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001854 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001855
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001856 return 0;
1857}
1858
1859static int radio_enum_input(struct file *file, void *priv,
1860 struct v4l2_input *i)
1861{
1862 if (i->index != 0)
1863 return -EINVAL;
1864 strcpy(i->name, "Radio");
1865 i->type = V4L2_INPUT_TYPE_TUNER;
1866
1867 return 0;
1868}
1869
1870static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1871{
1872 if (unlikely(a->index))
1873 return -EINVAL;
1874
1875 strcpy(a->name, "Radio");
1876 return 0;
1877}
1878
1879static int radio_s_tuner(struct file *file, void *priv,
1880 struct v4l2_tuner *t)
1881{
1882 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1883
1884 if (0 != t->index)
1885 return -EINVAL;
1886
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001887 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001888
1889 return 0;
1890}
1891
1892static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001893 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001894{
1895 return 0;
1896}
1897
1898static int radio_s_input(struct file *file, void *fh, unsigned int i)
1899{
1900 return 0;
1901}
1902
1903static int radio_queryctrl(struct file *file, void *priv,
1904 struct v4l2_queryctrl *qc)
1905{
1906 int i;
1907
1908 if (qc->id < V4L2_CID_BASE ||
1909 qc->id >= V4L2_CID_LASTP1)
1910 return -EINVAL;
1911
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001912 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1913 if (qc->id && qc->id == ac97_qctrl[i].id) {
1914 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001915 return 0;
1916 }
1917 }
1918
1919 return -EINVAL;
1920}
1921
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001922/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001923 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001924 * inits the device and starts isoc transfer
1925 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001926static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001928 int errCode = 0, radio = 0;
1929 struct video_device *vdev = video_devdata(filp);
1930 struct em28xx *dev = video_drvdata(filp);
1931 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001932 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001933 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001934
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001935 switch (vdev->vfl_type) {
1936 case VFL_TYPE_GRABBER:
1937 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1938 break;
1939 case VFL_TYPE_VBI:
1940 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1941 break;
1942 case VFL_TYPE_RADIO:
1943 radio = 1;
1944 break;
1945 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001946
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001947 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1948 video_device_node_name(vdev), v4l2_type_names[fh_type],
1949 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001950
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001951
Hans Verkuil876cb142012-06-23 08:12:47 -03001952 if (mutex_lock_interruptible(&dev->lock))
1953 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001954 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001955 if (!fh) {
1956 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001957 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001958 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001959 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001960 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001961 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001962 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001963 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001964
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001965 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001966 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001967 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001968 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001969
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001970 /* Needed, since GPIO might have disabled power of
1971 some i2c device
1972 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001973 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001974
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001975 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001976 if (fh->radio) {
1977 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001978 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001979 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001980
1981 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001982
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001983 if (dev->progressive)
1984 field = V4L2_FIELD_NONE;
1985 else
1986 field = V4L2_FIELD_INTERLACED;
1987
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001988 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1989 NULL, &dev->slock,
1990 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001991 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001992
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001993 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1994 NULL, &dev->slock,
1995 V4L2_BUF_TYPE_VBI_CAPTURE,
1996 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001997 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03001998 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001999
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002000 return errCode;
2001}
2002
2003/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002004 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002005 * unregisters the v4l2,i2c and usb devices
2006 * called when the device gets disconected or at module unload
2007*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002008void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002009{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002010
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002011 /*FIXME: I2C IR should be disconnected */
2012
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002013 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002014 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002015 video_unregister_device(dev->radio_dev);
2016 else
2017 video_device_release(dev->radio_dev);
2018 dev->radio_dev = NULL;
2019 }
2020 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002021 em28xx_info("V4L2 device %s deregistered\n",
2022 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002023 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002024 video_unregister_device(dev->vbi_dev);
2025 else
2026 video_device_release(dev->vbi_dev);
2027 dev->vbi_dev = NULL;
2028 }
2029 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002030 em28xx_info("V4L2 device %s deregistered\n",
2031 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002032 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002033 video_unregister_device(dev->vdev);
2034 else
2035 video_device_release(dev->vdev);
2036 dev->vdev = NULL;
2037 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002038}
2039
2040/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002041 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002042 * stops streaming and deallocates all resources allocated by the v4l2
2043 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002044 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002045static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002046{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002047 struct em28xx_fh *fh = filp->private_data;
2048 struct em28xx *dev = fh->dev;
2049 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002050
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002051 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002052
Hans Verkuil876cb142012-06-23 08:12:47 -03002053 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002054 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002055 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002056 res_free(fh, EM28XX_RESOURCE_VIDEO);
2057 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002058
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002059 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2060 videobuf_stop(&fh->vb_vbiq);
2061 res_free(fh, EM28XX_RESOURCE_VBI);
2062 }
2063
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002064 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002065 /* the device is already disconnect,
2066 free the remaining resources */
2067 if (dev->state & DEV_DISCONNECTED) {
2068 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002069 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002070 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002071 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002072 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002073 return 0;
2074 }
2075
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002076 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002077 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002078
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002079 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002080 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002081 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002082
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002083 /* set alternate 0 */
2084 dev->alt = 0;
2085 em28xx_videodbg("setting alternate 0\n");
2086 errCode = usb_set_interface(dev->udev, 0, 0);
2087 if (errCode < 0) {
2088 em28xx_errdev("cannot change alternate number to "
2089 "0 (error=%i)\n", errCode);
2090 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002092
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002093 videobuf_mmap_free(&fh->vb_vidq);
2094 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002095 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002097 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002098 return 0;
2099}
2100
2101/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002102 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002103 * will allocate buffers when called for the first time
2104 */
2105static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002106em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002107 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002108{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002109 struct em28xx_fh *fh = filp->private_data;
2110 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002111 int rc;
2112
2113 rc = check_dev(dev);
2114 if (rc < 0)
2115 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116
Hans Verkuil876cb142012-06-23 08:12:47 -03002117 if (mutex_lock_interruptible(&dev->lock))
2118 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002119 /* FIXME: read() is not prepared to allow changing the video
2120 resolution while streaming. Seems a bug at em28xx_set_fmt
2121 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002122
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002123 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002124 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002125 rc = -EBUSY;
2126 else
2127 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002128 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002129 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002130 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002131 rc = -EBUSY;
2132 else
2133 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002134 filp->f_flags & O_NONBLOCK);
2135 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002136 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002137
Hans Verkuil876cb142012-06-23 08:12:47 -03002138 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139}
2140
2141/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002142 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002143 * will allocate buffers when called for the first time
2144 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002145static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002147 struct em28xx_fh *fh = filp->private_data;
2148 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002149 int rc;
2150
2151 rc = check_dev(dev);
2152 if (rc < 0)
2153 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002155 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2156 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2157 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002158 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002159 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2160 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2161 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002162 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002163 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002164 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002165 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002166}
2167
Hans Verkuil876cb142012-06-23 08:12:47 -03002168static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2169{
2170 struct em28xx_fh *fh = filp->private_data;
2171 struct em28xx *dev = fh->dev;
2172 unsigned int res;
2173
2174 mutex_lock(&dev->lock);
2175 res = em28xx_poll(filp, wait);
2176 mutex_unlock(&dev->lock);
2177 return res;
2178}
2179
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002180/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002181 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002182 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002183static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002185 struct em28xx_fh *fh = filp->private_data;
2186 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002187 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002188
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002189 rc = check_dev(dev);
2190 if (rc < 0)
2191 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002192
Hans Verkuil876cb142012-06-23 08:12:47 -03002193 if (mutex_lock_interruptible(&dev->lock))
2194 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002195 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2196 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2197 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2198 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002199 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002200
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002201 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2202 (unsigned long)vma->vm_start,
2203 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2204 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002206 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207}
2208
Hans Verkuilbec43662008-12-30 06:58:20 -03002209static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002210 .owner = THIS_MODULE,
2211 .open = em28xx_v4l2_open,
2212 .release = em28xx_v4l2_close,
2213 .read = em28xx_v4l2_read,
2214 .poll = em28xx_v4l2_poll,
2215 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002216 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002217};
2218
Hans Verkuila3998102008-07-21 02:57:38 -03002219static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002220 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002221 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2222 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2223 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2224 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002225 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2226 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002227 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002228 .vidioc_g_audio = vidioc_g_audio,
2229 .vidioc_s_audio = vidioc_s_audio,
2230 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002231 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2232 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2233 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002234
2235 .vidioc_reqbufs = vidioc_reqbufs,
2236 .vidioc_querybuf = vidioc_querybuf,
2237 .vidioc_qbuf = vidioc_qbuf,
2238 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002239 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002240 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002241 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002242 .vidioc_g_parm = vidioc_g_parm,
2243 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002244 .vidioc_enum_input = vidioc_enum_input,
2245 .vidioc_g_input = vidioc_g_input,
2246 .vidioc_s_input = vidioc_s_input,
2247 .vidioc_queryctrl = vidioc_queryctrl,
2248 .vidioc_g_ctrl = vidioc_g_ctrl,
2249 .vidioc_s_ctrl = vidioc_s_ctrl,
2250 .vidioc_streamon = vidioc_streamon,
2251 .vidioc_streamoff = vidioc_streamoff,
2252 .vidioc_g_tuner = vidioc_g_tuner,
2253 .vidioc_s_tuner = vidioc_s_tuner,
2254 .vidioc_g_frequency = vidioc_g_frequency,
2255 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002256#ifdef CONFIG_VIDEO_ADV_DEBUG
2257 .vidioc_g_register = vidioc_g_register,
2258 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002259 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002260#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002261};
2262
2263static const struct video_device em28xx_video_template = {
2264 .fops = &em28xx_v4l_fops,
2265 .release = video_device_release,
2266 .ioctl_ops = &video_ioctl_ops,
2267
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002268 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002269 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002270};
2271
Hans Verkuilbec43662008-12-30 06:58:20 -03002272static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002273 .owner = THIS_MODULE,
2274 .open = em28xx_v4l2_open,
2275 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002276 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002277};
2278
2279static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03002280 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002281 .vidioc_g_tuner = radio_g_tuner,
2282 .vidioc_enum_input = radio_enum_input,
2283 .vidioc_g_audio = radio_g_audio,
2284 .vidioc_s_tuner = radio_s_tuner,
2285 .vidioc_s_audio = radio_s_audio,
2286 .vidioc_s_input = radio_s_input,
2287 .vidioc_queryctrl = radio_queryctrl,
2288 .vidioc_g_ctrl = vidioc_g_ctrl,
2289 .vidioc_s_ctrl = vidioc_s_ctrl,
2290 .vidioc_g_frequency = vidioc_g_frequency,
2291 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002292#ifdef CONFIG_VIDEO_ADV_DEBUG
2293 .vidioc_g_register = vidioc_g_register,
2294 .vidioc_s_register = vidioc_s_register,
2295#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002296};
2297
Hans Verkuila3998102008-07-21 02:57:38 -03002298static struct video_device em28xx_radio_template = {
2299 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002300 .fops = &radio_fops,
2301 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002302};
2303
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002304/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002306
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002307
Adrian Bunk532fe652008-01-28 22:10:48 -03002308static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002309 const struct video_device *template,
2310 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002311{
2312 struct video_device *vfd;
2313
2314 vfd = video_device_alloc();
2315 if (NULL == vfd)
2316 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002317
2318 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002319 vfd->v4l2_dev = &dev->v4l2_dev;
2320 vfd->release = video_device_release;
2321 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002322 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002323
2324 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2325 dev->name, type_name);
2326
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002327 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002328 return vfd;
2329}
2330
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002331int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002332{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002333 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002334 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002335 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002336
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002337 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2338 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002339
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002340 /* set default norm */
2341 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002342 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002343 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002344
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002345 /* Analog specific initialization */
2346 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002347
2348 maxw = norm_maxw(dev);
2349 /* MaxPacketSize for em2800 is too small to capture at full resolution
2350 * use half of maxw as the scaler can only scale to 50% */
2351 if (dev->board.is_em2800)
2352 maxw /= 2;
2353
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002354 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002355 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002356
Ezequiel García96371fc2012-03-23 18:09:34 -03002357 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002358
2359 /* Audio defaults */
2360 dev->mute = 1;
2361 dev->volume = 0x1f;
2362
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002363/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002364 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2365 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2366 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002367
2368 em28xx_set_outfmt(dev);
2369 em28xx_colorlevels_set_default(dev);
2370 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002371
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002372 /* allocate and fill video video_device struct */
2373 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2374 if (!dev->vdev) {
2375 em28xx_errdev("cannot allocate video_device.\n");
2376 return -ENODEV;
2377 }
2378
2379 /* register v4l2 video video_device */
2380 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2381 video_nr[dev->devno]);
2382 if (ret) {
2383 em28xx_errdev("unable to register video device (error=%i).\n",
2384 ret);
2385 return ret;
2386 }
2387
2388 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002389 if (em28xx_vbi_supported(dev) == 1) {
2390 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2391 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002392
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002393 /* register v4l2 vbi video_device */
2394 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2395 vbi_nr[dev->devno]);
2396 if (ret < 0) {
2397 em28xx_errdev("unable to register vbi device\n");
2398 return ret;
2399 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002400 }
2401
2402 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002403 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2404 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002405 if (!dev->radio_dev) {
2406 em28xx_errdev("cannot allocate video_device.\n");
2407 return -ENODEV;
2408 }
2409 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2410 radio_nr[dev->devno]);
2411 if (ret < 0) {
2412 em28xx_errdev("can't register radio device\n");
2413 return ret;
2414 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002415 em28xx_info("Registered radio device as %s\n",
2416 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002417 }
2418
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002419 em28xx_info("V4L2 video device registered as %s\n",
2420 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002421
2422 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002423 em28xx_info("V4L2 VBI device registered as %s\n",
2424 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002425
2426 return 0;
2427}