blob: 133e6b0a5c7d06b686772f6402a015b3ab19201d [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/*
171 * Identify the buffer header type and properly handles
172 */
173static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300174 struct em28xx_buffer *buf,
175 unsigned char *p,
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;
Frank Schaefer4078d622012-12-08 11:31:29 -0300181 unsigned char *outp = buf->vb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182
Frank Schaefer87325332012-12-08 11:31:27 -0300183 if (buf->pos + len > buf->vb.size)
184 len = buf->vb.size - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300185
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 startread = p;
187 remain = len;
188
Frank Schaeferc02ec712012-11-08 14:11:33 -0300189 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300190 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300191 else /* interlaced mode, even nr. of lines */
192 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300193
Frank Schaefer87325332012-12-08 11:31:27 -0300194 linesdone = buf->pos / bytesperline;
195 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300196
197 if (dev->progressive)
198 offset = linesdone * bytesperline + currlinedone;
199 else
200 offset = linesdone * bytesperline * 2 + currlinedone;
201
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204 lencopy = lencopy > remain ? remain : lencopy;
205
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300210 remain = (char *)outp + buf->vb.size - (char *)startwrite;
211 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300213 if (lencopy <= 0)
214 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218
219 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300220 if (dev->progressive)
221 startwrite += lencopy;
222 else
223 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = remain;
227 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300228 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300230 if ((char *)startwrite + lencopy > (char *)outp +
231 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) -
235 ((char *)outp + buf->vb.size));
236 lencopy = remain = (char *)outp + buf->vb.size -
237 (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
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300250static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300251 struct em28xx_buffer *buf,
252 unsigned char *p,
Frank Schaefer4078d622012-12-08 11:31:29 -0300253 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300254{
255 void *startwrite, *startread;
256 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300257 int bytesperline;
Frank Schaefer4078d622012-12-08 11:31:29 -0300258 unsigned char *outp;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300259
260 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300261 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300262 return;
263 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300264 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300265
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300266 if (buf == NULL) {
267 return;
268 }
269 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300270 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300271 return;
272 }
Frank Schaefer4078d622012-12-08 11:31:29 -0300273 outp = buf->vb_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300274 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300275 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300276 return;
277 }
278
Frank Schaefer87325332012-12-08 11:31:27 -0300279 if (buf->pos + len > buf->vb.size)
280 len = buf->vb.size - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300282 startread = p;
283
Frank Schaefer87325332012-12-08 11:31:27 -0300284 startwrite = outp + buf->pos;
285 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300286
287 /* Make sure the bottom field populates the second half of the frame */
288 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300289 startwrite += bytesperline * dev->vbi_height;
290 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300291 }
292
293 memcpy(startwrite, startread, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300294 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300295}
296
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300297static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300298 int packet, int status)
299{
300 char *errmsg = "Unknown";
301
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300302 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300303 case -ENOENT:
304 errmsg = "unlinked synchronuously";
305 break;
306 case -ECONNRESET:
307 errmsg = "unlinked asynchronuously";
308 break;
309 case -ENOSR:
310 errmsg = "Buffer error (overrun)";
311 break;
312 case -EPIPE:
313 errmsg = "Stalled (device not responding)";
314 break;
315 case -EOVERFLOW:
316 errmsg = "Babble (bad cable?)";
317 break;
318 case -EPROTO:
319 errmsg = "Bit-stuff error (bad cable?)";
320 break;
321 case -EILSEQ:
322 errmsg = "CRC/Timeout (could be anything)";
323 break;
324 case -ETIME:
325 errmsg = "Device does not respond";
326 break;
327 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300328 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
330 } else {
331 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
332 packet, status, errmsg);
333 }
334}
335
336/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300337 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300338 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300339static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
340 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300342 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300343 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300344
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300345 if (list_empty(&dma_q->active)) {
346 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300347 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300348 }
349
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300350 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300351 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300352 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300353 outp = videobuf_to_vmalloc(&buf->vb);
354 memset(outp, 0, buf->vb.size);
Frank Schaefer87325332012-12-08 11:31:27 -0300355 buf->pos = 0;
Frank Schaefer4078d622012-12-08 11:31:29 -0300356 buf->vb_buf = outp;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300357
Frank Schaefer24a6d842012-12-08 11:31:24 -0300358 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300359}
360
Frank Schaefere04c00d2012-12-08 11:31:30 -0300361/*
362 * Finish the current buffer if completed and prepare for the next field
363 */
364static struct em28xx_buffer *
365finish_field_prepare_next(struct em28xx *dev,
366 struct em28xx_buffer *buf,
367 struct em28xx_dmaqueue *dma_q)
368{
369 if (dev->progressive || dev->top_field) { /* Brand new frame */
370 if (buf != NULL)
371 finish_buffer(dev, buf);
372 buf = get_next_buf(dev, dma_q);
373 }
374 if (buf != NULL) {
375 buf->top_field = dev->top_field;
376 buf->pos = 0;
377 }
378
379 return buf;
380}
381
Frank Schaefer227b7c92012-12-08 11:31:31 -0300382/*
383 * Process data packet according to the em2710/em2750/em28xx frame data format
384 */
385static inline void process_frame_data_em28xx(struct em28xx *dev,
386 unsigned char *data_pkt,
387 unsigned int data_len)
388{
389 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
390 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
391 struct em28xx_dmaqueue *dma_q = &dev->vidq;
392 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
393
394 /* capture type 0 = vbi start
395 capture type 1 = vbi in progress
396 capture type 2 = video start
397 capture type 3 = video in progress */
398 if (data_len >= 4) {
399 /* NOTE: Headers are always 4 bytes and
400 * never split across packets */
401 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
402 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
403 /* Continuation */
404 data_pkt += 4;
405 data_len -= 4;
406 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
407 /* Field start (VBI mode) */
408 dev->capture_type = 0;
409 dev->vbi_read = 0;
410 em28xx_isocdbg("VBI START HEADER !!!\n");
411 dev->top_field = !(data_pkt[2] & 1);
412 data_pkt += 4;
413 data_len -= 4;
414 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
415 /* Field start (VBI disabled) */
416 dev->capture_type = 2;
417 em28xx_isocdbg("VIDEO START HEADER !!!\n");
418 dev->top_field = !(data_pkt[2] & 1);
419 data_pkt += 4;
420 data_len -= 4;
421 }
422 }
423 /* NOTE: With bulk transfers, intermediate data packets
424 * have no continuation header */
425
426 if (dev->capture_type == 0) {
427 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
428 dev->usb_ctl.vbi_buf = vbi_buf;
429 dev->capture_type = 1;
430 }
431
432 if (dev->capture_type == 1) {
433 int vbi_size = dev->vbi_width * dev->vbi_height;
434 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
435 (vbi_size - dev->vbi_read) : data_len;
436
437 /* Copy VBI data */
438 if (vbi_buf != NULL)
439 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
440 dev->vbi_read += vbi_data_len;
441
442 if (vbi_data_len < data_len) {
443 /* Continue with copying video data */
444 dev->capture_type = 2;
445 data_pkt += vbi_data_len;
446 data_len -= vbi_data_len;
447 }
448 }
449
450 if (dev->capture_type == 2) {
451 buf = finish_field_prepare_next(dev, buf, dma_q);
452 dev->usb_ctl.vid_buf = buf;
453 dev->capture_type = 3;
454 }
455
456 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
457 em28xx_copy_video(dev, buf, data_pkt, data_len);
458}
459
Frank Schaefer960da932012-11-25 06:37:37 -0300460/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300461static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300462{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300463 int xfer_bulk, num_packets, i;
464 unsigned char *usb_data_pkt;
465 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300466
467 if (!dev)
468 return 0;
469
470 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
471 return 0;
472
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300473 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300474 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300475
Frank Schaefer4601cc32012-11-08 14:11:46 -0300476 xfer_bulk = usb_pipebulk(urb->pipe);
477
Frank Schaefer4601cc32012-11-08 14:11:46 -0300478 if (xfer_bulk) /* bulk */
479 num_packets = 1;
480 else /* isoc */
481 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300482
Frank Schaefer4601cc32012-11-08 14:11:46 -0300483 for (i = 0; i < num_packets; i++) {
484 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300485 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300486
Frank Schaefer227b7c92012-12-08 11:31:31 -0300487 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300488 } else { /* isoc */
489 if (urb->iso_frame_desc[i].status < 0) {
490 print_err_status(dev, i,
491 urb->iso_frame_desc[i].status);
492 if (urb->iso_frame_desc[i].status != -EPROTO)
493 continue;
494 }
495
Frank Schaefer227b7c92012-12-08 11:31:31 -0300496 usb_data_len = urb->iso_frame_desc[i].actual_length;
497 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300498 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300499 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300500 }
501
Frank Schaefer227b7c92012-12-08 11:31:31 -0300502 usb_data_pkt = urb->transfer_buffer +
503 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300504 }
505
Frank Schaefer227b7c92012-12-08 11:31:31 -0300506 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300507 /* NOTE: happens very often with isoc transfers */
508 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300509 continue;
510 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300511
Frank Schaefer227b7c92012-12-08 11:31:31 -0300512 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300513 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300514 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300515}
516
517
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300518/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300519 Videobuf operations
520 ------------------------------------------------------------------*/
521
522static int
523buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
524{
525 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300526 struct em28xx *dev = fh->dev;
527 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300528
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300529 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
530 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300531
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300532 if (0 == *count)
533 *count = EM28XX_DEF_BUF;
534
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300535 if (*count < EM28XX_MIN_BUF)
536 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300537
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300538 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300539 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300540 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300541 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300542
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300543 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300544
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300545 return 0;
546}
547
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300548/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300549static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
550{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300551 struct em28xx_fh *fh = vq->priv_data;
552 struct em28xx *dev = fh->dev;
553 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300554 if (in_interrupt())
555 BUG();
556
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300557 /* We used to wait for the buffer to finish here, but this didn't work
558 because, as we were keeping the state as VIDEOBUF_QUEUED,
559 videobuf_queue_cancel marked it as finished for us.
560 (Also, it could wedge forever if the hardware was misconfigured.)
561
562 This should be safe; by the time we get here, the buffer isn't
563 queued anymore. If we ever start marking the buffers as
564 VIDEOBUF_ACTIVE, it won't be, though.
565 */
566 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300567 if (dev->usb_ctl.vid_buf == buf)
568 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300569 spin_unlock_irqrestore(&dev->slock, flags);
570
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300571 videobuf_vmalloc_free(&buf->vb);
572 buf->vb.state = VIDEOBUF_NEEDS_INIT;
573}
574
575static int
576buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
577 enum v4l2_field field)
578{
579 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300580 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300581 struct em28xx *dev = fh->dev;
582 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300583
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300584 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
585 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300586
587 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
588 return -EINVAL;
589
Brandon Philips05612972008-04-13 14:57:01 -0300590 buf->vb.width = dev->width;
591 buf->vb.height = dev->height;
592 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300593
594 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300595 rc = videobuf_iolock(vq, &buf->vb, NULL);
596 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300597 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300598 }
599
Frank Schaefer74209dc2012-11-08 14:11:37 -0300600 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300601 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300602
603 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300604 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300605 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
606 dev->analog_xfer_bulk,
607 EM28XX_NUM_BUFS,
608 dev->max_pkt_size,
609 dev->packet_multiplier,
610 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300611 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300612 goto fail;
613 }
614
615 buf->vb.state = VIDEOBUF_PREPARED;
616 return 0;
617
618fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300619 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300620 return rc;
621}
622
623static void
624buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
625{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300626 struct em28xx_buffer *buf = container_of(vb,
627 struct em28xx_buffer,
628 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300629 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300630 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300631 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300633 buf->vb.state = VIDEOBUF_QUEUED;
634 list_add_tail(&buf->vb.queue, &vidq->active);
635
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636}
637
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300638static void buffer_release(struct videobuf_queue *vq,
639 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300640{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300641 struct em28xx_buffer *buf = container_of(vb,
642 struct em28xx_buffer,
643 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300644 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300645 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300646
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300647 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300648
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300649 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650}
651
652static struct videobuf_queue_ops em28xx_video_qops = {
653 .buf_setup = buffer_setup,
654 .buf_prepare = buffer_prepare,
655 .buf_queue = buffer_queue,
656 .buf_release = buffer_release,
657};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800658
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300659/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800660
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800661static void video_mux(struct em28xx *dev, int index)
662{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800663 dev->ctl_input = index;
664 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300665 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800666
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300667 if (!dev->ctl_aoutput)
668 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
669
Hans Verkuil5325b422009-04-02 11:26:22 -0300670 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
671 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800672
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300673 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300674 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300675 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
676 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300677 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300678 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300679 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
680 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800681 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300682
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300683 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300684 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
685 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300686 }
687
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300688 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800689}
690
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300691/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300692static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300693{
694 struct em28xx *dev = fh->dev;
695
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300696 if (fh->resources & bit)
697 /* have it already allocated */
698 return 1;
699
700 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300701 if (dev->resources & bit) {
702 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300703 return 0;
704 }
705 /* it's free, grab it */
706 fh->resources |= bit;
707 dev->resources |= bit;
708 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300709 return 1;
710}
711
712static int res_check(struct em28xx_fh *fh, unsigned int bit)
713{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300714 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300715}
716
717static int res_locked(struct em28xx *dev, unsigned int bit)
718{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300719 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300720}
721
722static void res_free(struct em28xx_fh *fh, unsigned int bits)
723{
724 struct em28xx *dev = fh->dev;
725
726 BUG_ON((fh->resources & bits) != bits);
727
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300728 fh->resources &= ~bits;
729 dev->resources &= ~bits;
730 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300731}
732
733static int get_ressource(struct em28xx_fh *fh)
734{
735 switch (fh->type) {
736 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
737 return EM28XX_RESOURCE_VIDEO;
738 case V4L2_BUF_TYPE_VBI_CAPTURE:
739 return EM28XX_RESOURCE_VBI;
740 default:
741 BUG();
742 return 0;
743 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300744}
745
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800746/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300747 * ac97_queryctrl()
748 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300749 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300750static int ac97_queryctrl(struct v4l2_queryctrl *qc)
751{
752 int i;
753
754 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
755 if (qc->id && qc->id == ac97_qctrl[i].id) {
756 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
757 return 0;
758 }
759 }
760
761 /* Control is not ac97 related */
762 return 1;
763}
764
765/*
766 * ac97_get_ctrl()
767 * return the current values for ac97 mute and volume
768 */
769static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300770{
771 switch (ctrl->id) {
772 case V4L2_CID_AUDIO_MUTE:
773 ctrl->value = dev->mute;
774 return 0;
775 case V4L2_CID_AUDIO_VOLUME:
776 ctrl->value = dev->volume;
777 return 0;
778 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300779 /* Control is not ac97 related */
780 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300781 }
782}
783
784/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300785 * ac97_set_ctrl()
786 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300787 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300788static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300789{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300790 int i;
791
792 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
793 if (ctrl->id == ac97_qctrl[i].id)
794 goto handle;
795
796 /* Announce that hasn't handle it */
797 return 1;
798
799handle:
800 if (ctrl->value < ac97_qctrl[i].minimum ||
801 ctrl->value > ac97_qctrl[i].maximum)
802 return -ERANGE;
803
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804 switch (ctrl->id) {
805 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300806 dev->mute = ctrl->value;
807 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 case V4L2_CID_AUDIO_VOLUME:
809 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300810 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300812
813 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814}
815
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816static int check_dev(struct em28xx *dev)
817{
818 if (dev->state & DEV_DISCONNECTED) {
819 em28xx_errdev("v4l2 ioctl: device not present\n");
820 return -ENODEV;
821 }
822
823 if (dev->state & DEV_MISCONFIGURED) {
824 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
825 "close and open it again\n");
826 return -EIO;
827 }
828 return 0;
829}
830
831static void get_scale(struct em28xx *dev,
832 unsigned int width, unsigned int height,
833 unsigned int *hscale, unsigned int *vscale)
834{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300835 unsigned int maxw = norm_maxw(dev);
836 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837
838 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
839 if (*hscale >= 0x4000)
840 *hscale = 0x3fff;
841
842 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
843 if (*vscale >= 0x4000)
844 *vscale = 0x3fff;
845}
846
847/* ------------------------------------------------------------------
848 IOCTL vidioc handling
849 ------------------------------------------------------------------*/
850
Hans Verkuil78b526a2008-05-28 12:16:41 -0300851static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300852 struct v4l2_format *f)
853{
854 struct em28xx_fh *fh = priv;
855 struct em28xx *dev = fh->dev;
856
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300857 f->fmt.pix.width = dev->width;
858 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300859 f->fmt.pix.pixelformat = dev->format->fourcc;
860 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300861 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300862 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
863
864 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300865 if (dev->progressive)
866 f->fmt.pix.field = V4L2_FIELD_NONE;
867 else
868 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300869 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300870 return 0;
871}
872
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300873static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
874{
875 unsigned int i;
876
877 for (i = 0; i < ARRAY_SIZE(format); i++)
878 if (format[i].fourcc == fourcc)
879 return &format[i];
880
881 return NULL;
882}
883
Hans Verkuil78b526a2008-05-28 12:16:41 -0300884static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300885 struct v4l2_format *f)
886{
887 struct em28xx_fh *fh = priv;
888 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300889 unsigned int width = f->fmt.pix.width;
890 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891 unsigned int maxw = norm_maxw(dev);
892 unsigned int maxh = norm_maxh(dev);
893 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300894 struct em28xx_fmt *fmt;
895
896 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
897 if (!fmt) {
898 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
899 f->fmt.pix.pixelformat);
900 return -EINVAL;
901 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300902
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300903 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300904 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300905 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
906 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300907 /* MaxPacketSize for em2800 is too small to capture at full resolution
908 * use half of maxw as the scaler can only scale to 50% */
909 if (width == maxw && height == maxh)
910 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300911 } else {
912 /* width must even because of the YUYV format
913 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300914 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
915 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300916 }
917
918 get_scale(dev, width, height, &hscale, &vscale);
919
920 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
921 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
922
923 f->fmt.pix.width = width;
924 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300925 f->fmt.pix.pixelformat = fmt->fourcc;
926 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
927 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300928 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300929 if (dev->progressive)
930 f->fmt.pix.field = V4L2_FIELD_NONE;
931 else
932 f->fmt.pix.field = dev->interlaced ?
933 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935 return 0;
936}
937
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300938static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
939 unsigned width, unsigned height)
940{
941 struct em28xx_fmt *fmt;
942
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300943 fmt = format_by_fourcc(fourcc);
944 if (!fmt)
945 return -EINVAL;
946
947 dev->format = fmt;
948 dev->width = width;
949 dev->height = height;
950
951 /* set new image size */
952 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
953
954 em28xx_set_alternate(dev);
955 em28xx_resolution_set(dev);
956
957 return 0;
958}
959
Hans Verkuil78b526a2008-05-28 12:16:41 -0300960static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961 struct v4l2_format *f)
962{
963 struct em28xx_fh *fh = priv;
964 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300965 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966
967 rc = check_dev(dev);
968 if (rc < 0)
969 return rc;
970
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300971 vidioc_try_fmt_vid_cap(file, priv, f);
972
Brandon Philips05612972008-04-13 14:57:01 -0300973 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
974 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300975 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300976 }
977
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300978 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300979 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980}
981
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300982static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
983{
984 struct em28xx_fh *fh = priv;
985 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300986 int rc;
987
988 rc = check_dev(dev);
989 if (rc < 0)
990 return rc;
991
992 *norm = dev->norm;
993
994 return 0;
995}
996
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300997static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
998{
999 struct em28xx_fh *fh = priv;
1000 struct em28xx *dev = fh->dev;
1001 int rc;
1002
1003 rc = check_dev(dev);
1004 if (rc < 0)
1005 return rc;
1006
1007 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1008
1009 return 0;
1010}
1011
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001012static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013{
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
1016 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001017 int rc;
1018
1019 rc = check_dev(dev);
1020 if (rc < 0)
1021 return rc;
1022
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001023 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001024
1025 /* Adjusts width/height, if needed */
1026 f.fmt.pix.width = dev->width;
1027 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001028 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001029
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030 /* set new image size */
1031 dev->width = f.fmt.pix.width;
1032 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001033 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1034
1035 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001036 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001037
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038 return 0;
1039}
1040
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001041static int vidioc_g_parm(struct file *file, void *priv,
1042 struct v4l2_streamparm *p)
1043{
1044 struct em28xx_fh *fh = priv;
1045 struct em28xx *dev = fh->dev;
1046 int rc = 0;
1047
1048 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1049 return -EINVAL;
1050
1051 if (dev->board.is_webcam)
1052 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1053 video, g_parm, p);
1054 else
1055 v4l2_video_std_frame_period(dev->norm,
1056 &p->parm.capture.timeperframe);
1057
1058 return rc;
1059}
1060
1061static int vidioc_s_parm(struct file *file, void *priv,
1062 struct v4l2_streamparm *p)
1063{
1064 struct em28xx_fh *fh = priv;
1065 struct em28xx *dev = fh->dev;
1066
1067 if (!dev->board.is_webcam)
1068 return -EINVAL;
1069
1070 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1071 return -EINVAL;
1072
1073 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1074}
1075
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001076static const char *iname[] = {
1077 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1078 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1079 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1080 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1081 [EM28XX_VMUX_SVIDEO] = "S-Video",
1082 [EM28XX_VMUX_TELEVISION] = "Television",
1083 [EM28XX_VMUX_CABLE] = "Cable TV",
1084 [EM28XX_VMUX_DVB] = "DVB",
1085 [EM28XX_VMUX_DEBUG] = "for debug only",
1086};
1087
1088static int vidioc_enum_input(struct file *file, void *priv,
1089 struct v4l2_input *i)
1090{
1091 struct em28xx_fh *fh = priv;
1092 struct em28xx *dev = fh->dev;
1093 unsigned int n;
1094
1095 n = i->index;
1096 if (n >= MAX_EM28XX_INPUT)
1097 return -EINVAL;
1098 if (0 == INPUT(n)->type)
1099 return -EINVAL;
1100
1101 i->index = n;
1102 i->type = V4L2_INPUT_TYPE_CAMERA;
1103
1104 strcpy(i->name, iname[INPUT(n)->type]);
1105
1106 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1107 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1108 i->type = V4L2_INPUT_TYPE_TUNER;
1109
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001110 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001111
1112 return 0;
1113}
1114
1115static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1116{
1117 struct em28xx_fh *fh = priv;
1118 struct em28xx *dev = fh->dev;
1119
1120 *i = dev->ctl_input;
1121
1122 return 0;
1123}
1124
1125static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1126{
1127 struct em28xx_fh *fh = priv;
1128 struct em28xx *dev = fh->dev;
1129 int rc;
1130
1131 rc = check_dev(dev);
1132 if (rc < 0)
1133 return rc;
1134
1135 if (i >= MAX_EM28XX_INPUT)
1136 return -EINVAL;
1137 if (0 == INPUT(i)->type)
1138 return -EINVAL;
1139
Ezequiel García96371fc2012-03-23 18:09:34 -03001140 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141 return 0;
1142}
1143
1144static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1145{
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001148
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001149 if (!dev->audio_mode.has_audio)
1150 return -EINVAL;
1151
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001152 switch (a->index) {
1153 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001155 break;
1156 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001158 break;
1159 case EM28XX_AMUX_VIDEO2:
1160 strcpy(a->name, "Television alt");
1161 break;
1162 case EM28XX_AMUX_PHONE:
1163 strcpy(a->name, "Phone");
1164 break;
1165 case EM28XX_AMUX_MIC:
1166 strcpy(a->name, "Mic");
1167 break;
1168 case EM28XX_AMUX_CD:
1169 strcpy(a->name, "CD");
1170 break;
1171 case EM28XX_AMUX_AUX:
1172 strcpy(a->name, "Aux");
1173 break;
1174 case EM28XX_AMUX_PCM_OUT:
1175 strcpy(a->name, "PCM");
1176 break;
1177 default:
1178 return -EINVAL;
1179 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001180
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001181 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001182 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001183
1184 return 0;
1185}
1186
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001187static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001188{
1189 struct em28xx_fh *fh = priv;
1190 struct em28xx *dev = fh->dev;
1191
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001192
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001193 if (!dev->audio_mode.has_audio)
1194 return -EINVAL;
1195
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001196 if (a->index >= MAX_EM28XX_INPUT)
1197 return -EINVAL;
1198 if (0 == INPUT(a->index)->type)
1199 return -EINVAL;
1200
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001201 dev->ctl_ainput = INPUT(a->index)->amux;
1202 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001203
1204 if (!dev->ctl_aoutput)
1205 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001206
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001207 return 0;
1208}
1209
1210static int vidioc_queryctrl(struct file *file, void *priv,
1211 struct v4l2_queryctrl *qc)
1212{
1213 struct em28xx_fh *fh = priv;
1214 struct em28xx *dev = fh->dev;
1215 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001216 int rc;
1217
1218 rc = check_dev(dev);
1219 if (rc < 0)
1220 return rc;
1221
1222 memset(qc, 0, sizeof(*qc));
1223
1224 qc->id = id;
1225
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001226 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001227 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1228 rc = ac97_queryctrl(qc);
1229 if (!rc)
1230 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001232
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001233 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001234 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001235
1236 if (qc->type)
1237 return 0;
1238 else
1239 return -EINVAL;
1240}
1241
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001242/*
1243 * FIXME: This is an indirect way to check if a control exists at a
1244 * subdev. Instead of that hack, maybe the better would be to change all
1245 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1246 */
1247static int check_subdev_ctrl(struct em28xx *dev, int id)
1248{
1249 struct v4l2_queryctrl qc;
1250
1251 memset(&qc, 0, sizeof(qc));
1252 qc.id = id;
1253
1254 /* enumerate V4L2 device controls */
1255 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1256
1257 if (qc.type)
1258 return 0;
1259 else
1260 return -EINVAL;
1261}
1262
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001263static int vidioc_g_ctrl(struct file *file, void *priv,
1264 struct v4l2_control *ctrl)
1265{
1266 struct em28xx_fh *fh = priv;
1267 struct em28xx *dev = fh->dev;
1268 int rc;
1269
1270 rc = check_dev(dev);
1271 if (rc < 0)
1272 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001273 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001274
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001275 /* Set an AC97 control */
1276 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1277 rc = ac97_get_ctrl(dev, ctrl);
1278 else
1279 rc = 1;
1280
1281 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1282 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001283 if (check_subdev_ctrl(dev, ctrl->id))
1284 return -EINVAL;
1285
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001286 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001287 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001288 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001289
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 return rc;
1291}
1292
1293static int vidioc_s_ctrl(struct file *file, void *priv,
1294 struct v4l2_control *ctrl)
1295{
1296 struct em28xx_fh *fh = priv;
1297 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001298 int rc;
1299
1300 rc = check_dev(dev);
1301 if (rc < 0)
1302 return rc;
1303
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001304 /* Set an AC97 control */
1305 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1306 rc = ac97_set_ctrl(dev, ctrl);
1307 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001308 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001310 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001311 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001312 rc = check_subdev_ctrl(dev, ctrl->id);
1313 if (!rc)
1314 v4l2_device_call_all(&dev->v4l2_dev, 0,
1315 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001316 /*
1317 * In the case of non-AC97 volume controls, we still need
1318 * to do some setups at em28xx, in order to mute/unmute
1319 * and to adjust audio volume. However, the value ranges
1320 * should be checked by the corresponding V4L subdriver.
1321 */
1322 switch (ctrl->id) {
1323 case V4L2_CID_AUDIO_MUTE:
1324 dev->mute = ctrl->value;
1325 rc = em28xx_audio_analog_set(dev);
1326 break;
1327 case V4L2_CID_AUDIO_VOLUME:
1328 dev->volume = ctrl->value;
1329 rc = em28xx_audio_analog_set(dev);
1330 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001332 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001333}
1334
1335static int vidioc_g_tuner(struct file *file, void *priv,
1336 struct v4l2_tuner *t)
1337{
1338 struct em28xx_fh *fh = priv;
1339 struct em28xx *dev = fh->dev;
1340 int rc;
1341
1342 rc = check_dev(dev);
1343 if (rc < 0)
1344 return rc;
1345
1346 if (0 != t->index)
1347 return -EINVAL;
1348
1349 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001350 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001351
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001352 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001353 return 0;
1354}
1355
1356static int vidioc_s_tuner(struct file *file, void *priv,
1357 struct v4l2_tuner *t)
1358{
1359 struct em28xx_fh *fh = priv;
1360 struct em28xx *dev = fh->dev;
1361 int rc;
1362
1363 rc = check_dev(dev);
1364 if (rc < 0)
1365 return rc;
1366
1367 if (0 != t->index)
1368 return -EINVAL;
1369
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001370 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001371 return 0;
1372}
1373
1374static int vidioc_g_frequency(struct file *file, void *priv,
1375 struct v4l2_frequency *f)
1376{
1377 struct em28xx_fh *fh = priv;
1378 struct em28xx *dev = fh->dev;
1379
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001380 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 return 0;
1383}
1384
1385static int vidioc_s_frequency(struct file *file, void *priv,
1386 struct v4l2_frequency *f)
1387{
1388 struct em28xx_fh *fh = priv;
1389 struct em28xx *dev = fh->dev;
1390 int rc;
1391
1392 rc = check_dev(dev);
1393 if (rc < 0)
1394 return rc;
1395
1396 if (0 != f->tuner)
1397 return -EINVAL;
1398
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001399 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1400 return -EINVAL;
1401 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001402 return -EINVAL;
1403
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001404 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001405 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001406
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407 return 0;
1408}
1409
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001410#ifdef CONFIG_VIDEO_ADV_DEBUG
1411static int em28xx_reg_len(int reg)
1412{
1413 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001414 case EM28XX_R40_AC97LSB:
1415 case EM28XX_R30_HSCALELOW:
1416 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001417 return 2;
1418 default:
1419 return 1;
1420 }
1421}
1422
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001423static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001424 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001425{
1426 struct em28xx_fh *fh = priv;
1427 struct em28xx *dev = fh->dev;
1428
1429 chip->ident = V4L2_IDENT_NONE;
1430 chip->revision = 0;
1431
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001432 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001433
1434 return 0;
1435}
1436
1437
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001438static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001439 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001440{
1441 struct em28xx_fh *fh = priv;
1442 struct em28xx *dev = fh->dev;
1443 int ret;
1444
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001445 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001446 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001447 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001448 if (ret < 0)
1449 return ret;
1450
1451 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001452 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001453 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001454 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001455 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001456 return 0;
1457 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001458 /* TODO: is this correct? */
1459 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1460 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001461 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001462 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001463 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001464 }
1465
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001466 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001467 reg->size = em28xx_reg_len(reg->reg);
1468 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001469 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001470
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001471 if (ret < 0)
1472 return ret;
1473
1474 reg->val = ret;
1475 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001476 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001477 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1478 reg->reg, (char *)&val, 2);
1479 if (ret < 0)
1480 return ret;
1481
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001482 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001483 }
1484
1485 return 0;
1486}
1487
1488static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001489 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001490{
1491 struct em28xx_fh *fh = priv;
1492 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001493 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001494
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001495 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001496 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001497 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001498 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001499 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001500 return 0;
1501 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001502 /* TODO: is this correct? */
1503 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1504 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001505 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001506 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001507 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001508 }
1509
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001510 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001511 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001512
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001513 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001514 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001515}
1516#endif
1517
1518
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001519static int vidioc_cropcap(struct file *file, void *priv,
1520 struct v4l2_cropcap *cc)
1521{
1522 struct em28xx_fh *fh = priv;
1523 struct em28xx *dev = fh->dev;
1524
1525 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1526 return -EINVAL;
1527
1528 cc->bounds.left = 0;
1529 cc->bounds.top = 0;
1530 cc->bounds.width = dev->width;
1531 cc->bounds.height = dev->height;
1532 cc->defrect = cc->bounds;
1533 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1534 cc->pixelaspect.denominator = 59;
1535
1536 return 0;
1537}
1538
1539static int vidioc_streamon(struct file *file, void *priv,
1540 enum v4l2_buf_type type)
1541{
1542 struct em28xx_fh *fh = priv;
1543 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001544 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001545
1546 rc = check_dev(dev);
1547 if (rc < 0)
1548 return rc;
1549
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001550 if (unlikely(type != fh->type))
1551 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001552
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001553 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1554 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001555
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001556 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001557 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001558
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001559 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1560 rc = videobuf_streamon(&fh->vb_vidq);
1561 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1562 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001563
1564 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001565}
1566
1567static int vidioc_streamoff(struct file *file, void *priv,
1568 enum v4l2_buf_type type)
1569{
1570 struct em28xx_fh *fh = priv;
1571 struct em28xx *dev = fh->dev;
1572 int rc;
1573
1574 rc = check_dev(dev);
1575 if (rc < 0)
1576 return rc;
1577
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001578 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1579 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001580 return -EINVAL;
1581 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582 return -EINVAL;
1583
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001584 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1585 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001586
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001587 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001588 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1589 videobuf_streamoff(&fh->vb_vidq);
1590 res_free(fh, EM28XX_RESOURCE_VIDEO);
1591 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001592 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001593 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1594 videobuf_streamoff(&fh->vb_vbiq);
1595 res_free(fh, EM28XX_RESOURCE_VBI);
1596 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001597 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001598
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001599 return 0;
1600}
1601
1602static int vidioc_querycap(struct file *file, void *priv,
1603 struct v4l2_capability *cap)
1604{
1605 struct em28xx_fh *fh = priv;
1606 struct em28xx *dev = fh->dev;
1607
1608 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1609 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001610 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001611
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001612 cap->capabilities =
1613 V4L2_CAP_SLICED_VBI_CAPTURE |
1614 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001615 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1616
Devin Heitmueller04146142009-09-11 00:08:44 -03001617 if (dev->vbi_dev)
1618 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1619
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001620 if (dev->audio_mode.has_audio)
1621 cap->capabilities |= V4L2_CAP_AUDIO;
1622
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001623 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001624 cap->capabilities |= V4L2_CAP_TUNER;
1625
1626 return 0;
1627}
1628
Hans Verkuil78b526a2008-05-28 12:16:41 -03001629static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001630 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001631{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001632 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001633 return -EINVAL;
1634
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001635 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1636 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001637
1638 return 0;
1639}
1640
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001641static int vidioc_enum_framesizes(struct file *file, void *priv,
1642 struct v4l2_frmsizeenum *fsize)
1643{
1644 struct em28xx_fh *fh = priv;
1645 struct em28xx *dev = fh->dev;
1646 struct em28xx_fmt *fmt;
1647 unsigned int maxw = norm_maxw(dev);
1648 unsigned int maxh = norm_maxh(dev);
1649
1650 fmt = format_by_fourcc(fsize->pixel_format);
1651 if (!fmt) {
1652 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1653 fsize->pixel_format);
1654 return -EINVAL;
1655 }
1656
1657 if (dev->board.is_em2800) {
1658 if (fsize->index > 1)
1659 return -EINVAL;
1660 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1661 fsize->discrete.width = maxw / (1 + fsize->index);
1662 fsize->discrete.height = maxh / (1 + fsize->index);
1663 return 0;
1664 }
1665
1666 if (fsize->index != 0)
1667 return -EINVAL;
1668
1669 /* Report a continuous range */
1670 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1671 fsize->stepwise.min_width = 48;
1672 fsize->stepwise.min_height = 32;
1673 fsize->stepwise.max_width = maxw;
1674 fsize->stepwise.max_height = maxh;
1675 fsize->stepwise.step_width = 1;
1676 fsize->stepwise.step_height = 1;
1677 return 0;
1678}
1679
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001680/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001681static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001682 struct v4l2_format *f)
1683{
1684 struct em28xx_fh *fh = priv;
1685 struct em28xx *dev = fh->dev;
1686 int rc;
1687
1688 rc = check_dev(dev);
1689 if (rc < 0)
1690 return rc;
1691
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001692 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001693 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001694
1695 if (f->fmt.sliced.service_set == 0)
1696 rc = -EINVAL;
1697
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001698 return rc;
1699}
1700
Hans Verkuil78b526a2008-05-28 12:16:41 -03001701static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001702 struct v4l2_format *f)
1703{
1704 struct em28xx_fh *fh = priv;
1705 struct em28xx *dev = fh->dev;
1706 int rc;
1707
1708 rc = check_dev(dev);
1709 if (rc < 0)
1710 return rc;
1711
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001712 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001713
1714 if (f->fmt.sliced.service_set == 0)
1715 return -EINVAL;
1716
1717 return 0;
1718}
1719
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001720/* RAW VBI ioctls */
1721
1722static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1723 struct v4l2_format *format)
1724{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001725 struct em28xx_fh *fh = priv;
1726 struct em28xx *dev = fh->dev;
1727
1728 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001729 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1730 format->fmt.vbi.offset = 0;
1731 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001732 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1733 format->fmt.vbi.count[0] = dev->vbi_height;
1734 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001735
1736 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001737 if (dev->norm & V4L2_STD_525_60) {
1738 /* NTSC */
1739 format->fmt.vbi.start[0] = 10;
1740 format->fmt.vbi.start[1] = 273;
1741 } else if (dev->norm & V4L2_STD_625_50) {
1742 /* PAL */
1743 format->fmt.vbi.start[0] = 6;
1744 format->fmt.vbi.start[1] = 318;
1745 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001746
1747 return 0;
1748}
1749
1750static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1751 struct v4l2_format *format)
1752{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001753 struct em28xx_fh *fh = priv;
1754 struct em28xx *dev = fh->dev;
1755
1756 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001757 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1758 format->fmt.vbi.offset = 0;
1759 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001760 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1761 format->fmt.vbi.count[0] = dev->vbi_height;
1762 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001763
1764 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001765 if (dev->norm & V4L2_STD_525_60) {
1766 /* NTSC */
1767 format->fmt.vbi.start[0] = 10;
1768 format->fmt.vbi.start[1] = 273;
1769 } else if (dev->norm & V4L2_STD_625_50) {
1770 /* PAL */
1771 format->fmt.vbi.start[0] = 6;
1772 format->fmt.vbi.start[1] = 318;
1773 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001774
1775 return 0;
1776}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001777
1778static int vidioc_reqbufs(struct file *file, void *priv,
1779 struct v4l2_requestbuffers *rb)
1780{
1781 struct em28xx_fh *fh = priv;
1782 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001783 int rc;
1784
1785 rc = check_dev(dev);
1786 if (rc < 0)
1787 return rc;
1788
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001789 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1790 return videobuf_reqbufs(&fh->vb_vidq, rb);
1791 else
1792 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793}
1794
1795static int vidioc_querybuf(struct file *file, void *priv,
1796 struct v4l2_buffer *b)
1797{
1798 struct em28xx_fh *fh = priv;
1799 struct em28xx *dev = fh->dev;
1800 int rc;
1801
1802 rc = check_dev(dev);
1803 if (rc < 0)
1804 return rc;
1805
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001806 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1807 return videobuf_querybuf(&fh->vb_vidq, b);
1808 else {
1809 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1810 the videobuf framework, but we probably shouldn't be
1811 returning a buffer larger than that which was asked for.
1812 At a minimum, it causes a crash in zvbi since it does
1813 a memcpy based on the source buffer length */
1814 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001815 b->length = dev->vbi_width * dev->vbi_height * 2;
1816
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001817 return result;
1818 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001819}
1820
1821static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1822{
1823 struct em28xx_fh *fh = priv;
1824 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001825 int rc;
1826
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_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001833 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001834 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001835}
1836
1837static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1838{
1839 struct em28xx_fh *fh = priv;
1840 struct em28xx *dev = fh->dev;
1841 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001842
1843 rc = check_dev(dev);
1844 if (rc < 0)
1845 return rc;
1846
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001847 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1848 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1849 O_NONBLOCK);
1850 else
1851 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1852 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001853}
1854
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001855/* ----------------------------------------------------------- */
1856/* RADIO ESPECIFIC IOCTLS */
1857/* ----------------------------------------------------------- */
1858
1859static int radio_querycap(struct file *file, void *priv,
1860 struct v4l2_capability *cap)
1861{
1862 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1863
1864 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1865 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001866 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001867
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001868 cap->capabilities = V4L2_CAP_TUNER;
1869 return 0;
1870}
1871
1872static int radio_g_tuner(struct file *file, void *priv,
1873 struct v4l2_tuner *t)
1874{
1875 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1876
1877 if (unlikely(t->index > 0))
1878 return -EINVAL;
1879
1880 strcpy(t->name, "Radio");
1881 t->type = V4L2_TUNER_RADIO;
1882
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001883 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001884
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001885 return 0;
1886}
1887
1888static int radio_enum_input(struct file *file, void *priv,
1889 struct v4l2_input *i)
1890{
1891 if (i->index != 0)
1892 return -EINVAL;
1893 strcpy(i->name, "Radio");
1894 i->type = V4L2_INPUT_TYPE_TUNER;
1895
1896 return 0;
1897}
1898
1899static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1900{
1901 if (unlikely(a->index))
1902 return -EINVAL;
1903
1904 strcpy(a->name, "Radio");
1905 return 0;
1906}
1907
1908static int radio_s_tuner(struct file *file, void *priv,
1909 struct v4l2_tuner *t)
1910{
1911 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1912
1913 if (0 != t->index)
1914 return -EINVAL;
1915
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001916 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001917
1918 return 0;
1919}
1920
1921static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001922 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001923{
1924 return 0;
1925}
1926
1927static int radio_s_input(struct file *file, void *fh, unsigned int i)
1928{
1929 return 0;
1930}
1931
1932static int radio_queryctrl(struct file *file, void *priv,
1933 struct v4l2_queryctrl *qc)
1934{
1935 int i;
1936
1937 if (qc->id < V4L2_CID_BASE ||
1938 qc->id >= V4L2_CID_LASTP1)
1939 return -EINVAL;
1940
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001941 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1942 if (qc->id && qc->id == ac97_qctrl[i].id) {
1943 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001944 return 0;
1945 }
1946 }
1947
1948 return -EINVAL;
1949}
1950
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001951/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001952 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001953 * inits the device and starts isoc transfer
1954 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001955static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001956{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001957 int errCode = 0, radio = 0;
1958 struct video_device *vdev = video_devdata(filp);
1959 struct em28xx *dev = video_drvdata(filp);
1960 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001961 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001962 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001963
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001964 switch (vdev->vfl_type) {
1965 case VFL_TYPE_GRABBER:
1966 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1967 break;
1968 case VFL_TYPE_VBI:
1969 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1970 break;
1971 case VFL_TYPE_RADIO:
1972 radio = 1;
1973 break;
1974 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001975
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001976 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1977 video_device_node_name(vdev), v4l2_type_names[fh_type],
1978 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001979
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001980
Hans Verkuil876cb142012-06-23 08:12:47 -03001981 if (mutex_lock_interruptible(&dev->lock))
1982 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001983 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001984 if (!fh) {
1985 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001986 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001987 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001988 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001989 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001990 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001991 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001992 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001993
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001994 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001995 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001996 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001997 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001998
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001999 /* Needed, since GPIO might have disabled power of
2000 some i2c device
2001 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002002 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002003
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002004 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002005 if (fh->radio) {
2006 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002007 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002008 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002009
2010 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002011
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002012 if (dev->progressive)
2013 field = V4L2_FIELD_NONE;
2014 else
2015 field = V4L2_FIELD_INTERLACED;
2016
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002017 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2018 NULL, &dev->slock,
2019 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002020 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002021
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002022 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2023 NULL, &dev->slock,
2024 V4L2_BUF_TYPE_VBI_CAPTURE,
2025 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002026 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002027 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002028
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002029 return errCode;
2030}
2031
2032/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002033 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002034 * unregisters the v4l2,i2c and usb devices
2035 * called when the device gets disconected or at module unload
2036*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002037void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002038{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002039
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002040 /*FIXME: I2C IR should be disconnected */
2041
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002042 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002043 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002044 video_unregister_device(dev->radio_dev);
2045 else
2046 video_device_release(dev->radio_dev);
2047 dev->radio_dev = NULL;
2048 }
2049 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002050 em28xx_info("V4L2 device %s deregistered\n",
2051 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002052 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002053 video_unregister_device(dev->vbi_dev);
2054 else
2055 video_device_release(dev->vbi_dev);
2056 dev->vbi_dev = NULL;
2057 }
2058 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002059 em28xx_info("V4L2 device %s deregistered\n",
2060 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002061 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002062 video_unregister_device(dev->vdev);
2063 else
2064 video_device_release(dev->vdev);
2065 dev->vdev = NULL;
2066 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002067}
2068
2069/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002070 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002071 * stops streaming and deallocates all resources allocated by the v4l2
2072 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002074static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002075{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002076 struct em28xx_fh *fh = filp->private_data;
2077 struct em28xx *dev = fh->dev;
2078 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002079
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002080 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002081
Hans Verkuil876cb142012-06-23 08:12:47 -03002082 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002083 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002084 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002085 res_free(fh, EM28XX_RESOURCE_VIDEO);
2086 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002088 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2089 videobuf_stop(&fh->vb_vbiq);
2090 res_free(fh, EM28XX_RESOURCE_VBI);
2091 }
2092
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002093 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002094 /* the device is already disconnect,
2095 free the remaining resources */
2096 if (dev->state & DEV_DISCONNECTED) {
2097 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002098 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002099 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002100 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002101 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002102 return 0;
2103 }
2104
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002105 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002106 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002107
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002108 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002109 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002110 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002111
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002112 /* set alternate 0 */
2113 dev->alt = 0;
2114 em28xx_videodbg("setting alternate 0\n");
2115 errCode = usb_set_interface(dev->udev, 0, 0);
2116 if (errCode < 0) {
2117 em28xx_errdev("cannot change alternate number to "
2118 "0 (error=%i)\n", errCode);
2119 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002120 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002121
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002122 videobuf_mmap_free(&fh->vb_vidq);
2123 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002124 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002125 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002126 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127 return 0;
2128}
2129
2130/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002131 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002132 * will allocate buffers when called for the first time
2133 */
2134static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002135em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002136 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002137{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002138 struct em28xx_fh *fh = filp->private_data;
2139 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002140 int rc;
2141
2142 rc = check_dev(dev);
2143 if (rc < 0)
2144 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145
Hans Verkuil876cb142012-06-23 08:12:47 -03002146 if (mutex_lock_interruptible(&dev->lock))
2147 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002148 /* FIXME: read() is not prepared to allow changing the video
2149 resolution while streaming. Seems a bug at em28xx_set_fmt
2150 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002151
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002152 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002153 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002154 rc = -EBUSY;
2155 else
2156 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002157 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002158 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002159 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002160 rc = -EBUSY;
2161 else
2162 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002163 filp->f_flags & O_NONBLOCK);
2164 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002165 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002166
Hans Verkuil876cb142012-06-23 08:12:47 -03002167 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002168}
2169
2170/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002171 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172 * will allocate buffers when called for the first time
2173 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002174static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002175{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002176 struct em28xx_fh *fh = filp->private_data;
2177 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002178 int rc;
2179
2180 rc = check_dev(dev);
2181 if (rc < 0)
2182 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002184 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2185 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2186 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002187 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002188 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2189 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2190 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002191 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002192 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002193 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002194 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195}
2196
Hans Verkuil876cb142012-06-23 08:12:47 -03002197static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2198{
2199 struct em28xx_fh *fh = filp->private_data;
2200 struct em28xx *dev = fh->dev;
2201 unsigned int res;
2202
2203 mutex_lock(&dev->lock);
2204 res = em28xx_poll(filp, wait);
2205 mutex_unlock(&dev->lock);
2206 return res;
2207}
2208
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002210 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002211 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002212static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002214 struct em28xx_fh *fh = filp->private_data;
2215 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002216 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002217
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002218 rc = check_dev(dev);
2219 if (rc < 0)
2220 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002221
Hans Verkuil876cb142012-06-23 08:12:47 -03002222 if (mutex_lock_interruptible(&dev->lock))
2223 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002224 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2225 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2226 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2227 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002228 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002229
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002230 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2231 (unsigned long)vma->vm_start,
2232 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2233 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002234
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002235 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002236}
2237
Hans Verkuilbec43662008-12-30 06:58:20 -03002238static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002239 .owner = THIS_MODULE,
2240 .open = em28xx_v4l2_open,
2241 .release = em28xx_v4l2_close,
2242 .read = em28xx_v4l2_read,
2243 .poll = em28xx_v4l2_poll,
2244 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002245 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246};
2247
Hans Verkuila3998102008-07-21 02:57:38 -03002248static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002249 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002250 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2251 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2252 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2253 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002254 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2255 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002256 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002257 .vidioc_g_audio = vidioc_g_audio,
2258 .vidioc_s_audio = vidioc_s_audio,
2259 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002260 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2261 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2262 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002263
2264 .vidioc_reqbufs = vidioc_reqbufs,
2265 .vidioc_querybuf = vidioc_querybuf,
2266 .vidioc_qbuf = vidioc_qbuf,
2267 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002268 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002269 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002270 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002271 .vidioc_g_parm = vidioc_g_parm,
2272 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002273 .vidioc_enum_input = vidioc_enum_input,
2274 .vidioc_g_input = vidioc_g_input,
2275 .vidioc_s_input = vidioc_s_input,
2276 .vidioc_queryctrl = vidioc_queryctrl,
2277 .vidioc_g_ctrl = vidioc_g_ctrl,
2278 .vidioc_s_ctrl = vidioc_s_ctrl,
2279 .vidioc_streamon = vidioc_streamon,
2280 .vidioc_streamoff = vidioc_streamoff,
2281 .vidioc_g_tuner = vidioc_g_tuner,
2282 .vidioc_s_tuner = vidioc_s_tuner,
2283 .vidioc_g_frequency = vidioc_g_frequency,
2284 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002285#ifdef CONFIG_VIDEO_ADV_DEBUG
2286 .vidioc_g_register = vidioc_g_register,
2287 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002288 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002289#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002290};
2291
2292static const struct video_device em28xx_video_template = {
2293 .fops = &em28xx_v4l_fops,
2294 .release = video_device_release,
2295 .ioctl_ops = &video_ioctl_ops,
2296
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002297 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002298 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002299};
2300
Hans Verkuilbec43662008-12-30 06:58:20 -03002301static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002302 .owner = THIS_MODULE,
2303 .open = em28xx_v4l2_open,
2304 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002305 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002306};
2307
2308static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002309 .vidioc_querycap = radio_querycap,
2310 .vidioc_g_tuner = radio_g_tuner,
2311 .vidioc_enum_input = radio_enum_input,
2312 .vidioc_g_audio = radio_g_audio,
2313 .vidioc_s_tuner = radio_s_tuner,
2314 .vidioc_s_audio = radio_s_audio,
2315 .vidioc_s_input = radio_s_input,
2316 .vidioc_queryctrl = radio_queryctrl,
2317 .vidioc_g_ctrl = vidioc_g_ctrl,
2318 .vidioc_s_ctrl = vidioc_s_ctrl,
2319 .vidioc_g_frequency = vidioc_g_frequency,
2320 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002321#ifdef CONFIG_VIDEO_ADV_DEBUG
2322 .vidioc_g_register = vidioc_g_register,
2323 .vidioc_s_register = vidioc_s_register,
2324#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002325};
2326
Hans Verkuila3998102008-07-21 02:57:38 -03002327static struct video_device em28xx_radio_template = {
2328 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002329 .fops = &radio_fops,
2330 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002331};
2332
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002333/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002334
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002335
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002336
Adrian Bunk532fe652008-01-28 22:10:48 -03002337static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002338 const struct video_device *template,
2339 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002340{
2341 struct video_device *vfd;
2342
2343 vfd = video_device_alloc();
2344 if (NULL == vfd)
2345 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002346
2347 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002348 vfd->v4l2_dev = &dev->v4l2_dev;
2349 vfd->release = video_device_release;
2350 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002351 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002352
2353 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2354 dev->name, type_name);
2355
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002356 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002357 return vfd;
2358}
2359
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002360int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002361{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002362 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002363 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002364 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002365
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002366 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2367 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002368
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002369 /* set default norm */
2370 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002371 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002372 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002373
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002374 /* Analog specific initialization */
2375 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002376
2377 maxw = norm_maxw(dev);
2378 /* MaxPacketSize for em2800 is too small to capture at full resolution
2379 * use half of maxw as the scaler can only scale to 50% */
2380 if (dev->board.is_em2800)
2381 maxw /= 2;
2382
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002383 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002384 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002385
Ezequiel García96371fc2012-03-23 18:09:34 -03002386 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002387
2388 /* Audio defaults */
2389 dev->mute = 1;
2390 dev->volume = 0x1f;
2391
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002392/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002393 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2394 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2395 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002396
2397 em28xx_set_outfmt(dev);
2398 em28xx_colorlevels_set_default(dev);
2399 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002400
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002401 /* allocate and fill video video_device struct */
2402 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2403 if (!dev->vdev) {
2404 em28xx_errdev("cannot allocate video_device.\n");
2405 return -ENODEV;
2406 }
2407
2408 /* register v4l2 video video_device */
2409 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2410 video_nr[dev->devno]);
2411 if (ret) {
2412 em28xx_errdev("unable to register video device (error=%i).\n",
2413 ret);
2414 return ret;
2415 }
2416
2417 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002418 if (em28xx_vbi_supported(dev) == 1) {
2419 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2420 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002421
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002422 /* register v4l2 vbi video_device */
2423 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2424 vbi_nr[dev->devno]);
2425 if (ret < 0) {
2426 em28xx_errdev("unable to register vbi device\n");
2427 return ret;
2428 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002429 }
2430
2431 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002432 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2433 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002434 if (!dev->radio_dev) {
2435 em28xx_errdev("cannot allocate video_device.\n");
2436 return -ENODEV;
2437 }
2438 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2439 radio_nr[dev->devno]);
2440 if (ret < 0) {
2441 em28xx_errdev("can't register radio device\n");
2442 return ret;
2443 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002444 em28xx_info("Registered radio device as %s\n",
2445 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002446 }
2447
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002448 em28xx_info("V4L2 video device registered as %s\n",
2449 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002450
2451 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002452 em28xx_info("V4L2 VBI device registered as %s\n",
2453 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002454
2455 return 0;
2456}