blob: 565b6462f152081cbe2967c106f1610a0ceba9f8 [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,
176 unsigned char *outp, unsigned long len)
177{
178 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300179 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300180 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300181
Frank Schaefer87325332012-12-08 11:31:27 -0300182 if (buf->pos + len > buf->vb.size)
183 len = buf->vb.size - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300185 startread = p;
186 remain = len;
187
Frank Schaeferc02ec712012-11-08 14:11:33 -0300188 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300189 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300190 else /* interlaced mode, even nr. of lines */
191 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300192
Frank Schaefer87325332012-12-08 11:31:27 -0300193 linesdone = buf->pos / bytesperline;
194 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300195
196 if (dev->progressive)
197 offset = linesdone * bytesperline + currlinedone;
198 else
199 offset = linesdone * bytesperline * 2 + currlinedone;
200
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203 lencopy = lencopy > remain ? remain : lencopy;
204
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300205 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 ((char *)startwrite + lencopy) -
208 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300209 remain = (char *)outp + buf->vb.size - (char *)startwrite;
210 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300211 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300212 if (lencopy <= 0)
213 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300214 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
216 remain -= lencopy;
217
218 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300219 if (dev->progressive)
220 startwrite += lencopy;
221 else
222 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300224 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 lencopy = remain;
226 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300227 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300228
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300229 if ((char *)startwrite + lencopy > (char *)outp +
230 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300231 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
232 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300233 ((char *)startwrite + lencopy) -
234 ((char *)outp + buf->vb.size));
235 lencopy = remain = (char *)outp + buf->vb.size -
236 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300238 if (lencopy <= 0)
239 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300240
241 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300242
243 remain -= lencopy;
244 }
245
Frank Schaefer87325332012-12-08 11:31:27 -0300246 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247}
248
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300249static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300250 struct em28xx_buffer *buf,
251 unsigned char *p,
252 unsigned char *outp, unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300253{
254 void *startwrite, *startread;
255 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300256 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300257
258 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300259 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300260 return;
261 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300262 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300263
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300264 if (buf == NULL) {
265 return;
266 }
267 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300268 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300269 return;
270 }
271 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300272 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300273 return;
274 }
275
Frank Schaefer87325332012-12-08 11:31:27 -0300276 if (buf->pos + len > buf->vb.size)
277 len = buf->vb.size - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300278
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300279 startread = p;
280
Frank Schaefer87325332012-12-08 11:31:27 -0300281 startwrite = outp + buf->pos;
282 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300283
284 /* Make sure the bottom field populates the second half of the frame */
285 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300286 startwrite += bytesperline * dev->vbi_height;
287 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300288 }
289
290 memcpy(startwrite, startread, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300291 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300292}
293
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300294static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295 int packet, int status)
296{
297 char *errmsg = "Unknown";
298
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300299 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300 case -ENOENT:
301 errmsg = "unlinked synchronuously";
302 break;
303 case -ECONNRESET:
304 errmsg = "unlinked asynchronuously";
305 break;
306 case -ENOSR:
307 errmsg = "Buffer error (overrun)";
308 break;
309 case -EPIPE:
310 errmsg = "Stalled (device not responding)";
311 break;
312 case -EOVERFLOW:
313 errmsg = "Babble (bad cable?)";
314 break;
315 case -EPROTO:
316 errmsg = "Bit-stuff error (bad cable?)";
317 break;
318 case -EILSEQ:
319 errmsg = "CRC/Timeout (could be anything)";
320 break;
321 case -ETIME:
322 errmsg = "Device does not respond";
323 break;
324 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300325 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300326 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
327 } else {
328 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
329 packet, status, errmsg);
330 }
331}
332
333/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300334 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300335 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300336static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
337 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300338{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300339 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300340 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300342 if (list_empty(&dma_q->active)) {
343 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300344 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300345 }
346
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300347 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300348 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300349 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300350 outp = videobuf_to_vmalloc(&buf->vb);
351 memset(outp, 0, buf->vb.size);
Frank Schaefer87325332012-12-08 11:31:27 -0300352 buf->pos = 0;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300353
Frank Schaefer24a6d842012-12-08 11:31:24 -0300354 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300355}
356
Frank Schaefer960da932012-11-25 06:37:37 -0300357/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300358static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300359{
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300360 struct em28xx_buffer *buf, *vbi_buf;
361 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300362 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer79ff8692012-11-25 06:37:36 -0300363 int xfer_bulk, num_packets, i, rc = 1;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300364 unsigned int actual_length, len = 0;
365 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300366
367 if (!dev)
368 return 0;
369
370 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
371 return 0;
372
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300373 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300374 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300375
Frank Schaefer4601cc32012-11-08 14:11:46 -0300376 xfer_bulk = usb_pipebulk(urb->pipe);
377
Frank Schaefer74209dc2012-11-08 14:11:37 -0300378 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300379 if (buf != NULL)
380 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300381
Frank Schaefer74209dc2012-11-08 14:11:37 -0300382 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300383 if (vbi_buf != NULL)
384 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
385
Frank Schaefer4601cc32012-11-08 14:11:46 -0300386 if (xfer_bulk) /* bulk */
387 num_packets = 1;
388 else /* isoc */
389 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300390
Frank Schaefer4601cc32012-11-08 14:11:46 -0300391 for (i = 0; i < num_packets; i++) {
392 if (xfer_bulk) { /* bulk */
393 actual_length = urb->actual_length;
394
395 p = urb->transfer_buffer;
396 } else { /* isoc */
397 if (urb->iso_frame_desc[i].status < 0) {
398 print_err_status(dev, i,
399 urb->iso_frame_desc[i].status);
400 if (urb->iso_frame_desc[i].status != -EPROTO)
401 continue;
402 }
403
404 actual_length = urb->iso_frame_desc[i].actual_length;
405 if (actual_length > dev->max_pkt_size) {
406 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300407 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300408 }
409
410 p = urb->transfer_buffer +
411 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300412 }
413
Frank Schaefer3610f582012-11-25 06:37:33 -0300414 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300415 /* NOTE: happens very often with isoc transfers */
416 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300417 continue;
418 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300419
420 /* capture type 0 = vbi start
421 capture type 1 = video start
422 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300423 len = actual_length;
424 if (len >= 4) {
425 /* NOTE: headers are always 4 bytes and
426 * never split across packets */
427 if (p[0] == 0x33 && p[1] == 0x95) {
428 dev->capture_type = 0;
429 dev->vbi_read = 0;
430 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300431 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300432 p += 4;
433 len -= 4;
434 } else if (p[0] == 0x88 && p[1] == 0x88 &&
435 p[2] == 0x88 && p[3] == 0x88) {
436 /* continuation */
437 p += 4;
438 len -= 4;
439 } else if (p[0] == 0x22 && p[1] == 0x5a) {
440 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300441 dev->capture_type = 1;
442 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300443 p += 4;
444 len -= 4;
445 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300446 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300447 /* NOTE: with bulk transfers, intermediate data packets
448 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300449
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300450 if (dev->capture_type == 0) {
Frank Schaefer79ff8692012-11-25 06:37:36 -0300451 int vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300452 if (dev->vbi_read >= vbi_size) {
453 /* We've already read all the VBI data, so
454 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300455 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300456 } else if ((dev->vbi_read + len) < vbi_size) {
457 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300458 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300459 /* Brand new frame */
460 if (vbi_buf != NULL)
Frank Schaefer948a49a2012-12-08 11:31:25 -0300461 finish_buffer(dev, vbi_buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300462 vbi_buf = get_next_buf(dev, vbi_dma_q);
463 dev->usb_ctl.vbi_buf = vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300464 if (vbi_buf == NULL)
465 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300466 else
467 vbioutp = videobuf_to_vmalloc(
468 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300469 }
470
471 if (dev->vbi_read == 0) {
Frank Schaefer87325332012-12-08 11:31:27 -0300472 if (vbi_buf != NULL) {
Frank Schaefer24a6d842012-12-08 11:31:24 -0300473 vbi_buf->top_field
474 = dev->top_field;
Frank Schaefer87325332012-12-08 11:31:27 -0300475 vbi_buf->pos = 0;
476 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300477 }
478
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300479 dev->vbi_read += len;
Frank Schaefer87325332012-12-08 11:31:27 -0300480 em28xx_copy_vbi(dev, vbi_buf, p, vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300481 } else {
482 /* Some of this frame is VBI data and some is
483 video data */
484 int vbi_data_len = vbi_size - dev->vbi_read;
485 dev->vbi_read += vbi_data_len;
Frank Schaefer87325332012-12-08 11:31:27 -0300486 em28xx_copy_vbi(dev, vbi_buf, p, vbioutp,
487 vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300488 dev->capture_type = 1;
489 p += vbi_data_len;
490 len -= vbi_data_len;
491 }
492 }
493
494 if (dev->capture_type == 1) {
495 dev->capture_type = 2;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300496 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300497 if (buf != NULL)
Frank Schaefer948a49a2012-12-08 11:31:25 -0300498 finish_buffer(dev, buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300499 buf = get_next_buf(dev, dma_q);
500 dev->usb_ctl.vid_buf = buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300501 if (buf == NULL)
502 outp = NULL;
503 else
504 outp = videobuf_to_vmalloc(&buf->vb);
505 }
Frank Schaefer87325332012-12-08 11:31:27 -0300506 if (buf != NULL) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300507 buf->top_field = dev->top_field;
Frank Schaefer87325332012-12-08 11:31:27 -0300508 buf->pos = 0;
509 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300510 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300511
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300512 if (buf != NULL && dev->capture_type == 2 && len > 0)
Frank Schaefer87325332012-12-08 11:31:27 -0300513 em28xx_copy_video(dev, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300514 }
515 return rc;
516}
517
518
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300519/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300520 Videobuf operations
521 ------------------------------------------------------------------*/
522
523static int
524buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
525{
526 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300527 struct em28xx *dev = fh->dev;
528 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300529
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300530 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
531 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300532
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300533 if (0 == *count)
534 *count = EM28XX_DEF_BUF;
535
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300536 if (*count < EM28XX_MIN_BUF)
537 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300538
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300539 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300540 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300541 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300542 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300543
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300544 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300545
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300546 return 0;
547}
548
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300549/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300550static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
551{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300552 struct em28xx_fh *fh = vq->priv_data;
553 struct em28xx *dev = fh->dev;
554 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300555 if (in_interrupt())
556 BUG();
557
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300558 /* We used to wait for the buffer to finish here, but this didn't work
559 because, as we were keeping the state as VIDEOBUF_QUEUED,
560 videobuf_queue_cancel marked it as finished for us.
561 (Also, it could wedge forever if the hardware was misconfigured.)
562
563 This should be safe; by the time we get here, the buffer isn't
564 queued anymore. If we ever start marking the buffers as
565 VIDEOBUF_ACTIVE, it won't be, though.
566 */
567 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300568 if (dev->usb_ctl.vid_buf == buf)
569 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300570 spin_unlock_irqrestore(&dev->slock, flags);
571
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300572 videobuf_vmalloc_free(&buf->vb);
573 buf->vb.state = VIDEOBUF_NEEDS_INIT;
574}
575
576static int
577buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
578 enum v4l2_field field)
579{
580 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300581 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300582 struct em28xx *dev = fh->dev;
583 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300584
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300585 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
586 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300587
588 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
589 return -EINVAL;
590
Brandon Philips05612972008-04-13 14:57:01 -0300591 buf->vb.width = dev->width;
592 buf->vb.height = dev->height;
593 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300594
595 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300596 rc = videobuf_iolock(vq, &buf->vb, NULL);
597 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300598 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300599 }
600
Frank Schaefer74209dc2012-11-08 14:11:37 -0300601 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300602 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300603
604 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300605 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300606 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
607 dev->analog_xfer_bulk,
608 EM28XX_NUM_BUFS,
609 dev->max_pkt_size,
610 dev->packet_multiplier,
611 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300612 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300613 goto fail;
614 }
615
616 buf->vb.state = VIDEOBUF_PREPARED;
617 return 0;
618
619fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300620 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300621 return rc;
622}
623
624static void
625buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
626{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300627 struct em28xx_buffer *buf = container_of(vb,
628 struct em28xx_buffer,
629 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300630 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300631 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300633
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300634 buf->vb.state = VIDEOBUF_QUEUED;
635 list_add_tail(&buf->vb.queue, &vidq->active);
636
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300637}
638
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300639static void buffer_release(struct videobuf_queue *vq,
640 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300641{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300642 struct em28xx_buffer *buf = container_of(vb,
643 struct em28xx_buffer,
644 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300645 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300646 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300647
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300648 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300649
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300650 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300651}
652
653static struct videobuf_queue_ops em28xx_video_qops = {
654 .buf_setup = buffer_setup,
655 .buf_prepare = buffer_prepare,
656 .buf_queue = buffer_queue,
657 .buf_release = buffer_release,
658};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800659
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300660/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800661
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800662static void video_mux(struct em28xx *dev, int index)
663{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800664 dev->ctl_input = index;
665 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300666 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800667
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300668 if (!dev->ctl_aoutput)
669 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
670
Hans Verkuil5325b422009-04-02 11:26:22 -0300671 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
672 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800673
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300674 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300675 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300676 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
677 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300678 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300679 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300680 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
681 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800682 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300683
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300684 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300685 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
686 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300687 }
688
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300689 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800690}
691
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300692/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300693static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300694{
695 struct em28xx *dev = fh->dev;
696
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300697 if (fh->resources & bit)
698 /* have it already allocated */
699 return 1;
700
701 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300702 if (dev->resources & bit) {
703 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300704 return 0;
705 }
706 /* it's free, grab it */
707 fh->resources |= bit;
708 dev->resources |= bit;
709 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300710 return 1;
711}
712
713static int res_check(struct em28xx_fh *fh, unsigned int bit)
714{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300715 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300716}
717
718static int res_locked(struct em28xx *dev, unsigned int bit)
719{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300720 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300721}
722
723static void res_free(struct em28xx_fh *fh, unsigned int bits)
724{
725 struct em28xx *dev = fh->dev;
726
727 BUG_ON((fh->resources & bits) != bits);
728
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300729 fh->resources &= ~bits;
730 dev->resources &= ~bits;
731 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300732}
733
734static int get_ressource(struct em28xx_fh *fh)
735{
736 switch (fh->type) {
737 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
738 return EM28XX_RESOURCE_VIDEO;
739 case V4L2_BUF_TYPE_VBI_CAPTURE:
740 return EM28XX_RESOURCE_VBI;
741 default:
742 BUG();
743 return 0;
744 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300745}
746
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800747/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300748 * ac97_queryctrl()
749 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300750 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300751static int ac97_queryctrl(struct v4l2_queryctrl *qc)
752{
753 int i;
754
755 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
756 if (qc->id && qc->id == ac97_qctrl[i].id) {
757 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
758 return 0;
759 }
760 }
761
762 /* Control is not ac97 related */
763 return 1;
764}
765
766/*
767 * ac97_get_ctrl()
768 * return the current values for ac97 mute and volume
769 */
770static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300771{
772 switch (ctrl->id) {
773 case V4L2_CID_AUDIO_MUTE:
774 ctrl->value = dev->mute;
775 return 0;
776 case V4L2_CID_AUDIO_VOLUME:
777 ctrl->value = dev->volume;
778 return 0;
779 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300780 /* Control is not ac97 related */
781 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300782 }
783}
784
785/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300786 * ac97_set_ctrl()
787 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300788 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300789static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300790{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300791 int i;
792
793 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
794 if (ctrl->id == ac97_qctrl[i].id)
795 goto handle;
796
797 /* Announce that hasn't handle it */
798 return 1;
799
800handle:
801 if (ctrl->value < ac97_qctrl[i].minimum ||
802 ctrl->value > ac97_qctrl[i].maximum)
803 return -ERANGE;
804
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805 switch (ctrl->id) {
806 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300807 dev->mute = ctrl->value;
808 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 case V4L2_CID_AUDIO_VOLUME:
810 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300811 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300813
814 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300815}
816
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817static int check_dev(struct em28xx *dev)
818{
819 if (dev->state & DEV_DISCONNECTED) {
820 em28xx_errdev("v4l2 ioctl: device not present\n");
821 return -ENODEV;
822 }
823
824 if (dev->state & DEV_MISCONFIGURED) {
825 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
826 "close and open it again\n");
827 return -EIO;
828 }
829 return 0;
830}
831
832static void get_scale(struct em28xx *dev,
833 unsigned int width, unsigned int height,
834 unsigned int *hscale, unsigned int *vscale)
835{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300836 unsigned int maxw = norm_maxw(dev);
837 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300838
839 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
840 if (*hscale >= 0x4000)
841 *hscale = 0x3fff;
842
843 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
844 if (*vscale >= 0x4000)
845 *vscale = 0x3fff;
846}
847
848/* ------------------------------------------------------------------
849 IOCTL vidioc handling
850 ------------------------------------------------------------------*/
851
Hans Verkuil78b526a2008-05-28 12:16:41 -0300852static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300853 struct v4l2_format *f)
854{
855 struct em28xx_fh *fh = priv;
856 struct em28xx *dev = fh->dev;
857
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300858 f->fmt.pix.width = dev->width;
859 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300860 f->fmt.pix.pixelformat = dev->format->fourcc;
861 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300862 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300863 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
864
865 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300866 if (dev->progressive)
867 f->fmt.pix.field = V4L2_FIELD_NONE;
868 else
869 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300870 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300871 return 0;
872}
873
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300874static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
875{
876 unsigned int i;
877
878 for (i = 0; i < ARRAY_SIZE(format); i++)
879 if (format[i].fourcc == fourcc)
880 return &format[i];
881
882 return NULL;
883}
884
Hans Verkuil78b526a2008-05-28 12:16:41 -0300885static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300886 struct v4l2_format *f)
887{
888 struct em28xx_fh *fh = priv;
889 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300890 unsigned int width = f->fmt.pix.width;
891 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300892 unsigned int maxw = norm_maxw(dev);
893 unsigned int maxh = norm_maxh(dev);
894 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300895 struct em28xx_fmt *fmt;
896
897 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
898 if (!fmt) {
899 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
900 f->fmt.pix.pixelformat);
901 return -EINVAL;
902 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300903
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300904 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300905 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300906 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
907 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300908 /* MaxPacketSize for em2800 is too small to capture at full resolution
909 * use half of maxw as the scaler can only scale to 50% */
910 if (width == maxw && height == maxh)
911 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300912 } else {
913 /* width must even because of the YUYV format
914 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300915 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
916 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300917 }
918
919 get_scale(dev, width, height, &hscale, &vscale);
920
921 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
922 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
923
924 f->fmt.pix.width = width;
925 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300926 f->fmt.pix.pixelformat = fmt->fourcc;
927 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
928 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300929 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300930 if (dev->progressive)
931 f->fmt.pix.field = V4L2_FIELD_NONE;
932 else
933 f->fmt.pix.field = dev->interlaced ?
934 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936 return 0;
937}
938
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300939static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
940 unsigned width, unsigned height)
941{
942 struct em28xx_fmt *fmt;
943
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300944 fmt = format_by_fourcc(fourcc);
945 if (!fmt)
946 return -EINVAL;
947
948 dev->format = fmt;
949 dev->width = width;
950 dev->height = height;
951
952 /* set new image size */
953 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
954
955 em28xx_set_alternate(dev);
956 em28xx_resolution_set(dev);
957
958 return 0;
959}
960
Hans Verkuil78b526a2008-05-28 12:16:41 -0300961static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 struct v4l2_format *f)
963{
964 struct em28xx_fh *fh = priv;
965 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300966 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967
968 rc = check_dev(dev);
969 if (rc < 0)
970 return rc;
971
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300972 vidioc_try_fmt_vid_cap(file, priv, f);
973
Brandon Philips05612972008-04-13 14:57:01 -0300974 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
975 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300976 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300977 }
978
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300979 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300980 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300981}
982
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300983static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
984{
985 struct em28xx_fh *fh = priv;
986 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300987 int rc;
988
989 rc = check_dev(dev);
990 if (rc < 0)
991 return rc;
992
993 *norm = dev->norm;
994
995 return 0;
996}
997
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300998static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
999{
1000 struct em28xx_fh *fh = priv;
1001 struct em28xx *dev = fh->dev;
1002 int rc;
1003
1004 rc = check_dev(dev);
1005 if (rc < 0)
1006 return rc;
1007
1008 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1009
1010 return 0;
1011}
1012
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001013static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001018 int rc;
1019
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
1023
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001024 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001025
1026 /* Adjusts width/height, if needed */
1027 f.fmt.pix.width = dev->width;
1028 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001029 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001031 /* set new image size */
1032 dev->width = f.fmt.pix.width;
1033 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001034 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1035
1036 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001037 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001039 return 0;
1040}
1041
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001042static int vidioc_g_parm(struct file *file, void *priv,
1043 struct v4l2_streamparm *p)
1044{
1045 struct em28xx_fh *fh = priv;
1046 struct em28xx *dev = fh->dev;
1047 int rc = 0;
1048
1049 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1050 return -EINVAL;
1051
1052 if (dev->board.is_webcam)
1053 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1054 video, g_parm, p);
1055 else
1056 v4l2_video_std_frame_period(dev->norm,
1057 &p->parm.capture.timeperframe);
1058
1059 return rc;
1060}
1061
1062static int vidioc_s_parm(struct file *file, void *priv,
1063 struct v4l2_streamparm *p)
1064{
1065 struct em28xx_fh *fh = priv;
1066 struct em28xx *dev = fh->dev;
1067
1068 if (!dev->board.is_webcam)
1069 return -EINVAL;
1070
1071 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1072 return -EINVAL;
1073
1074 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1075}
1076
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077static const char *iname[] = {
1078 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1079 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1080 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1081 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1082 [EM28XX_VMUX_SVIDEO] = "S-Video",
1083 [EM28XX_VMUX_TELEVISION] = "Television",
1084 [EM28XX_VMUX_CABLE] = "Cable TV",
1085 [EM28XX_VMUX_DVB] = "DVB",
1086 [EM28XX_VMUX_DEBUG] = "for debug only",
1087};
1088
1089static int vidioc_enum_input(struct file *file, void *priv,
1090 struct v4l2_input *i)
1091{
1092 struct em28xx_fh *fh = priv;
1093 struct em28xx *dev = fh->dev;
1094 unsigned int n;
1095
1096 n = i->index;
1097 if (n >= MAX_EM28XX_INPUT)
1098 return -EINVAL;
1099 if (0 == INPUT(n)->type)
1100 return -EINVAL;
1101
1102 i->index = n;
1103 i->type = V4L2_INPUT_TYPE_CAMERA;
1104
1105 strcpy(i->name, iname[INPUT(n)->type]);
1106
1107 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1108 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1109 i->type = V4L2_INPUT_TYPE_TUNER;
1110
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001111 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001112
1113 return 0;
1114}
1115
1116static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1117{
1118 struct em28xx_fh *fh = priv;
1119 struct em28xx *dev = fh->dev;
1120
1121 *i = dev->ctl_input;
1122
1123 return 0;
1124}
1125
1126static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1127{
1128 struct em28xx_fh *fh = priv;
1129 struct em28xx *dev = fh->dev;
1130 int rc;
1131
1132 rc = check_dev(dev);
1133 if (rc < 0)
1134 return rc;
1135
1136 if (i >= MAX_EM28XX_INPUT)
1137 return -EINVAL;
1138 if (0 == INPUT(i)->type)
1139 return -EINVAL;
1140
Ezequiel García96371fc2012-03-23 18:09:34 -03001141 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001142 return 0;
1143}
1144
1145static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1146{
1147 struct em28xx_fh *fh = priv;
1148 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001149
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001150 if (!dev->audio_mode.has_audio)
1151 return -EINVAL;
1152
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001153 switch (a->index) {
1154 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001155 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001156 break;
1157 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001158 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001159 break;
1160 case EM28XX_AMUX_VIDEO2:
1161 strcpy(a->name, "Television alt");
1162 break;
1163 case EM28XX_AMUX_PHONE:
1164 strcpy(a->name, "Phone");
1165 break;
1166 case EM28XX_AMUX_MIC:
1167 strcpy(a->name, "Mic");
1168 break;
1169 case EM28XX_AMUX_CD:
1170 strcpy(a->name, "CD");
1171 break;
1172 case EM28XX_AMUX_AUX:
1173 strcpy(a->name, "Aux");
1174 break;
1175 case EM28XX_AMUX_PCM_OUT:
1176 strcpy(a->name, "PCM");
1177 break;
1178 default:
1179 return -EINVAL;
1180 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001181
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001182 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001183 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184
1185 return 0;
1186}
1187
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001188static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001189{
1190 struct em28xx_fh *fh = priv;
1191 struct em28xx *dev = fh->dev;
1192
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001193
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001194 if (!dev->audio_mode.has_audio)
1195 return -EINVAL;
1196
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001197 if (a->index >= MAX_EM28XX_INPUT)
1198 return -EINVAL;
1199 if (0 == INPUT(a->index)->type)
1200 return -EINVAL;
1201
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001202 dev->ctl_ainput = INPUT(a->index)->amux;
1203 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001204
1205 if (!dev->ctl_aoutput)
1206 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001207
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001208 return 0;
1209}
1210
1211static int vidioc_queryctrl(struct file *file, void *priv,
1212 struct v4l2_queryctrl *qc)
1213{
1214 struct em28xx_fh *fh = priv;
1215 struct em28xx *dev = fh->dev;
1216 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001217 int rc;
1218
1219 rc = check_dev(dev);
1220 if (rc < 0)
1221 return rc;
1222
1223 memset(qc, 0, sizeof(*qc));
1224
1225 qc->id = id;
1226
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001227 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001228 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1229 rc = ac97_queryctrl(qc);
1230 if (!rc)
1231 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001232 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001233
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001234 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001235 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001236
1237 if (qc->type)
1238 return 0;
1239 else
1240 return -EINVAL;
1241}
1242
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001243/*
1244 * FIXME: This is an indirect way to check if a control exists at a
1245 * subdev. Instead of that hack, maybe the better would be to change all
1246 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1247 */
1248static int check_subdev_ctrl(struct em28xx *dev, int id)
1249{
1250 struct v4l2_queryctrl qc;
1251
1252 memset(&qc, 0, sizeof(qc));
1253 qc.id = id;
1254
1255 /* enumerate V4L2 device controls */
1256 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1257
1258 if (qc.type)
1259 return 0;
1260 else
1261 return -EINVAL;
1262}
1263
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001264static int vidioc_g_ctrl(struct file *file, void *priv,
1265 struct v4l2_control *ctrl)
1266{
1267 struct em28xx_fh *fh = priv;
1268 struct em28xx *dev = fh->dev;
1269 int rc;
1270
1271 rc = check_dev(dev);
1272 if (rc < 0)
1273 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001274 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001275
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001276 /* Set an AC97 control */
1277 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1278 rc = ac97_get_ctrl(dev, ctrl);
1279 else
1280 rc = 1;
1281
1282 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1283 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001284 if (check_subdev_ctrl(dev, ctrl->id))
1285 return -EINVAL;
1286
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001287 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001288 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001289 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291 return rc;
1292}
1293
1294static int vidioc_s_ctrl(struct file *file, void *priv,
1295 struct v4l2_control *ctrl)
1296{
1297 struct em28xx_fh *fh = priv;
1298 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001299 int rc;
1300
1301 rc = check_dev(dev);
1302 if (rc < 0)
1303 return rc;
1304
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001305 /* Set an AC97 control */
1306 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1307 rc = ac97_set_ctrl(dev, ctrl);
1308 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001310
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001311 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001312 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001313 rc = check_subdev_ctrl(dev, ctrl->id);
1314 if (!rc)
1315 v4l2_device_call_all(&dev->v4l2_dev, 0,
1316 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001317 /*
1318 * In the case of non-AC97 volume controls, we still need
1319 * to do some setups at em28xx, in order to mute/unmute
1320 * and to adjust audio volume. However, the value ranges
1321 * should be checked by the corresponding V4L subdriver.
1322 */
1323 switch (ctrl->id) {
1324 case V4L2_CID_AUDIO_MUTE:
1325 dev->mute = ctrl->value;
1326 rc = em28xx_audio_analog_set(dev);
1327 break;
1328 case V4L2_CID_AUDIO_VOLUME:
1329 dev->volume = ctrl->value;
1330 rc = em28xx_audio_analog_set(dev);
1331 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001333 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001334}
1335
1336static int vidioc_g_tuner(struct file *file, void *priv,
1337 struct v4l2_tuner *t)
1338{
1339 struct em28xx_fh *fh = priv;
1340 struct em28xx *dev = fh->dev;
1341 int rc;
1342
1343 rc = check_dev(dev);
1344 if (rc < 0)
1345 return rc;
1346
1347 if (0 != t->index)
1348 return -EINVAL;
1349
1350 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001351 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001352
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001353 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354 return 0;
1355}
1356
1357static int vidioc_s_tuner(struct file *file, void *priv,
1358 struct v4l2_tuner *t)
1359{
1360 struct em28xx_fh *fh = priv;
1361 struct em28xx *dev = fh->dev;
1362 int rc;
1363
1364 rc = check_dev(dev);
1365 if (rc < 0)
1366 return rc;
1367
1368 if (0 != t->index)
1369 return -EINVAL;
1370
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001371 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372 return 0;
1373}
1374
1375static int vidioc_g_frequency(struct file *file, void *priv,
1376 struct v4l2_frequency *f)
1377{
1378 struct em28xx_fh *fh = priv;
1379 struct em28xx *dev = fh->dev;
1380
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001381 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383 return 0;
1384}
1385
1386static int vidioc_s_frequency(struct file *file, void *priv,
1387 struct v4l2_frequency *f)
1388{
1389 struct em28xx_fh *fh = priv;
1390 struct em28xx *dev = fh->dev;
1391 int rc;
1392
1393 rc = check_dev(dev);
1394 if (rc < 0)
1395 return rc;
1396
1397 if (0 != f->tuner)
1398 return -EINVAL;
1399
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001400 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1401 return -EINVAL;
1402 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001403 return -EINVAL;
1404
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001405 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001406 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408 return 0;
1409}
1410
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001411#ifdef CONFIG_VIDEO_ADV_DEBUG
1412static int em28xx_reg_len(int reg)
1413{
1414 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001415 case EM28XX_R40_AC97LSB:
1416 case EM28XX_R30_HSCALELOW:
1417 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001418 return 2;
1419 default:
1420 return 1;
1421 }
1422}
1423
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001424static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001425 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001426{
1427 struct em28xx_fh *fh = priv;
1428 struct em28xx *dev = fh->dev;
1429
1430 chip->ident = V4L2_IDENT_NONE;
1431 chip->revision = 0;
1432
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001433 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001434
1435 return 0;
1436}
1437
1438
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001439static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001440 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001441{
1442 struct em28xx_fh *fh = priv;
1443 struct em28xx *dev = fh->dev;
1444 int ret;
1445
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001446 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001447 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001448 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001449 if (ret < 0)
1450 return ret;
1451
1452 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001453 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001454 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001455 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001456 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001457 return 0;
1458 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001459 /* TODO: is this correct? */
1460 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1461 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001462 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001463 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001464 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001465 }
1466
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001467 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001468 reg->size = em28xx_reg_len(reg->reg);
1469 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001470 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001471
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001472 if (ret < 0)
1473 return ret;
1474
1475 reg->val = ret;
1476 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001477 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001478 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1479 reg->reg, (char *)&val, 2);
1480 if (ret < 0)
1481 return ret;
1482
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001483 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001484 }
1485
1486 return 0;
1487}
1488
1489static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001490 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001491{
1492 struct em28xx_fh *fh = priv;
1493 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001494 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001495
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001496 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001497 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001498 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001499 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001500 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001501 return 0;
1502 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001503 /* TODO: is this correct? */
1504 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1505 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001506 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001507 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001508 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001509 }
1510
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001511 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001512 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001513
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001514 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001515 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001516}
1517#endif
1518
1519
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001520static int vidioc_cropcap(struct file *file, void *priv,
1521 struct v4l2_cropcap *cc)
1522{
1523 struct em28xx_fh *fh = priv;
1524 struct em28xx *dev = fh->dev;
1525
1526 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1527 return -EINVAL;
1528
1529 cc->bounds.left = 0;
1530 cc->bounds.top = 0;
1531 cc->bounds.width = dev->width;
1532 cc->bounds.height = dev->height;
1533 cc->defrect = cc->bounds;
1534 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1535 cc->pixelaspect.denominator = 59;
1536
1537 return 0;
1538}
1539
1540static int vidioc_streamon(struct file *file, void *priv,
1541 enum v4l2_buf_type type)
1542{
1543 struct em28xx_fh *fh = priv;
1544 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001545 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546
1547 rc = check_dev(dev);
1548 if (rc < 0)
1549 return rc;
1550
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001551 if (unlikely(type != fh->type))
1552 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001553
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001554 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1555 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001556
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001557 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001558 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001559
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001560 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1561 rc = videobuf_streamon(&fh->vb_vidq);
1562 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1563 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001564
1565 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001566}
1567
1568static int vidioc_streamoff(struct file *file, void *priv,
1569 enum v4l2_buf_type type)
1570{
1571 struct em28xx_fh *fh = priv;
1572 struct em28xx *dev = fh->dev;
1573 int rc;
1574
1575 rc = check_dev(dev);
1576 if (rc < 0)
1577 return rc;
1578
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001579 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1580 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001581 return -EINVAL;
1582 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001583 return -EINVAL;
1584
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001585 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1586 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001587
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001588 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001589 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1590 videobuf_streamoff(&fh->vb_vidq);
1591 res_free(fh, EM28XX_RESOURCE_VIDEO);
1592 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001593 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001594 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1595 videobuf_streamoff(&fh->vb_vbiq);
1596 res_free(fh, EM28XX_RESOURCE_VBI);
1597 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001598 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001599
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001600 return 0;
1601}
1602
1603static int vidioc_querycap(struct file *file, void *priv,
1604 struct v4l2_capability *cap)
1605{
1606 struct em28xx_fh *fh = priv;
1607 struct em28xx *dev = fh->dev;
1608
1609 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1610 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001611 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001612
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001613 cap->capabilities =
1614 V4L2_CAP_SLICED_VBI_CAPTURE |
1615 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001616 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1617
Devin Heitmueller04146142009-09-11 00:08:44 -03001618 if (dev->vbi_dev)
1619 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1620
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001621 if (dev->audio_mode.has_audio)
1622 cap->capabilities |= V4L2_CAP_AUDIO;
1623
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001624 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001625 cap->capabilities |= V4L2_CAP_TUNER;
1626
1627 return 0;
1628}
1629
Hans Verkuil78b526a2008-05-28 12:16:41 -03001630static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001631 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001632{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001633 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001634 return -EINVAL;
1635
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001636 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1637 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001638
1639 return 0;
1640}
1641
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001642static int vidioc_enum_framesizes(struct file *file, void *priv,
1643 struct v4l2_frmsizeenum *fsize)
1644{
1645 struct em28xx_fh *fh = priv;
1646 struct em28xx *dev = fh->dev;
1647 struct em28xx_fmt *fmt;
1648 unsigned int maxw = norm_maxw(dev);
1649 unsigned int maxh = norm_maxh(dev);
1650
1651 fmt = format_by_fourcc(fsize->pixel_format);
1652 if (!fmt) {
1653 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1654 fsize->pixel_format);
1655 return -EINVAL;
1656 }
1657
1658 if (dev->board.is_em2800) {
1659 if (fsize->index > 1)
1660 return -EINVAL;
1661 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1662 fsize->discrete.width = maxw / (1 + fsize->index);
1663 fsize->discrete.height = maxh / (1 + fsize->index);
1664 return 0;
1665 }
1666
1667 if (fsize->index != 0)
1668 return -EINVAL;
1669
1670 /* Report a continuous range */
1671 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1672 fsize->stepwise.min_width = 48;
1673 fsize->stepwise.min_height = 32;
1674 fsize->stepwise.max_width = maxw;
1675 fsize->stepwise.max_height = maxh;
1676 fsize->stepwise.step_width = 1;
1677 fsize->stepwise.step_height = 1;
1678 return 0;
1679}
1680
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001681/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001682static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001683 struct v4l2_format *f)
1684{
1685 struct em28xx_fh *fh = priv;
1686 struct em28xx *dev = fh->dev;
1687 int rc;
1688
1689 rc = check_dev(dev);
1690 if (rc < 0)
1691 return rc;
1692
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001693 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001694 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001695
1696 if (f->fmt.sliced.service_set == 0)
1697 rc = -EINVAL;
1698
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001699 return rc;
1700}
1701
Hans Verkuil78b526a2008-05-28 12:16:41 -03001702static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001703 struct v4l2_format *f)
1704{
1705 struct em28xx_fh *fh = priv;
1706 struct em28xx *dev = fh->dev;
1707 int rc;
1708
1709 rc = check_dev(dev);
1710 if (rc < 0)
1711 return rc;
1712
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001713 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001714
1715 if (f->fmt.sliced.service_set == 0)
1716 return -EINVAL;
1717
1718 return 0;
1719}
1720
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001721/* RAW VBI ioctls */
1722
1723static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1724 struct v4l2_format *format)
1725{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001726 struct em28xx_fh *fh = priv;
1727 struct em28xx *dev = fh->dev;
1728
1729 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001730 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1731 format->fmt.vbi.offset = 0;
1732 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001733 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1734 format->fmt.vbi.count[0] = dev->vbi_height;
1735 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001736
1737 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001738 if (dev->norm & V4L2_STD_525_60) {
1739 /* NTSC */
1740 format->fmt.vbi.start[0] = 10;
1741 format->fmt.vbi.start[1] = 273;
1742 } else if (dev->norm & V4L2_STD_625_50) {
1743 /* PAL */
1744 format->fmt.vbi.start[0] = 6;
1745 format->fmt.vbi.start[1] = 318;
1746 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001747
1748 return 0;
1749}
1750
1751static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1752 struct v4l2_format *format)
1753{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001754 struct em28xx_fh *fh = priv;
1755 struct em28xx *dev = fh->dev;
1756
1757 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001758 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1759 format->fmt.vbi.offset = 0;
1760 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001761 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1762 format->fmt.vbi.count[0] = dev->vbi_height;
1763 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001764
1765 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001766 if (dev->norm & V4L2_STD_525_60) {
1767 /* NTSC */
1768 format->fmt.vbi.start[0] = 10;
1769 format->fmt.vbi.start[1] = 273;
1770 } else if (dev->norm & V4L2_STD_625_50) {
1771 /* PAL */
1772 format->fmt.vbi.start[0] = 6;
1773 format->fmt.vbi.start[1] = 318;
1774 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001775
1776 return 0;
1777}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001778
1779static int vidioc_reqbufs(struct file *file, void *priv,
1780 struct v4l2_requestbuffers *rb)
1781{
1782 struct em28xx_fh *fh = priv;
1783 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 int rc;
1785
1786 rc = check_dev(dev);
1787 if (rc < 0)
1788 return rc;
1789
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001790 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1791 return videobuf_reqbufs(&fh->vb_vidq, rb);
1792 else
1793 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001794}
1795
1796static int vidioc_querybuf(struct file *file, void *priv,
1797 struct v4l2_buffer *b)
1798{
1799 struct em28xx_fh *fh = priv;
1800 struct em28xx *dev = fh->dev;
1801 int rc;
1802
1803 rc = check_dev(dev);
1804 if (rc < 0)
1805 return rc;
1806
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001807 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1808 return videobuf_querybuf(&fh->vb_vidq, b);
1809 else {
1810 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1811 the videobuf framework, but we probably shouldn't be
1812 returning a buffer larger than that which was asked for.
1813 At a minimum, it causes a crash in zvbi since it does
1814 a memcpy based on the source buffer length */
1815 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001816 b->length = dev->vbi_width * dev->vbi_height * 2;
1817
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001818 return result;
1819 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001820}
1821
1822static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1823{
1824 struct em28xx_fh *fh = priv;
1825 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001826 int rc;
1827
1828 rc = check_dev(dev);
1829 if (rc < 0)
1830 return rc;
1831
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001832 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1833 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001834 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001835 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001836}
1837
1838static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1839{
1840 struct em28xx_fh *fh = priv;
1841 struct em28xx *dev = fh->dev;
1842 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001843
1844 rc = check_dev(dev);
1845 if (rc < 0)
1846 return rc;
1847
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001848 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1849 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1850 O_NONBLOCK);
1851 else
1852 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1853 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001854}
1855
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001856/* ----------------------------------------------------------- */
1857/* RADIO ESPECIFIC IOCTLS */
1858/* ----------------------------------------------------------- */
1859
1860static int radio_querycap(struct file *file, void *priv,
1861 struct v4l2_capability *cap)
1862{
1863 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1864
1865 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1866 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001867 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001868
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001869 cap->capabilities = V4L2_CAP_TUNER;
1870 return 0;
1871}
1872
1873static int radio_g_tuner(struct file *file, void *priv,
1874 struct v4l2_tuner *t)
1875{
1876 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1877
1878 if (unlikely(t->index > 0))
1879 return -EINVAL;
1880
1881 strcpy(t->name, "Radio");
1882 t->type = V4L2_TUNER_RADIO;
1883
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001884 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001885
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001886 return 0;
1887}
1888
1889static int radio_enum_input(struct file *file, void *priv,
1890 struct v4l2_input *i)
1891{
1892 if (i->index != 0)
1893 return -EINVAL;
1894 strcpy(i->name, "Radio");
1895 i->type = V4L2_INPUT_TYPE_TUNER;
1896
1897 return 0;
1898}
1899
1900static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1901{
1902 if (unlikely(a->index))
1903 return -EINVAL;
1904
1905 strcpy(a->name, "Radio");
1906 return 0;
1907}
1908
1909static int radio_s_tuner(struct file *file, void *priv,
1910 struct v4l2_tuner *t)
1911{
1912 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1913
1914 if (0 != t->index)
1915 return -EINVAL;
1916
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001917 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001918
1919 return 0;
1920}
1921
1922static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001923 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001924{
1925 return 0;
1926}
1927
1928static int radio_s_input(struct file *file, void *fh, unsigned int i)
1929{
1930 return 0;
1931}
1932
1933static int radio_queryctrl(struct file *file, void *priv,
1934 struct v4l2_queryctrl *qc)
1935{
1936 int i;
1937
1938 if (qc->id < V4L2_CID_BASE ||
1939 qc->id >= V4L2_CID_LASTP1)
1940 return -EINVAL;
1941
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001942 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1943 if (qc->id && qc->id == ac97_qctrl[i].id) {
1944 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001945 return 0;
1946 }
1947 }
1948
1949 return -EINVAL;
1950}
1951
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001952/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001953 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001954 * inits the device and starts isoc transfer
1955 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001956static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001957{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001958 int errCode = 0, radio = 0;
1959 struct video_device *vdev = video_devdata(filp);
1960 struct em28xx *dev = video_drvdata(filp);
1961 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001962 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001963 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001964
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001965 switch (vdev->vfl_type) {
1966 case VFL_TYPE_GRABBER:
1967 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1968 break;
1969 case VFL_TYPE_VBI:
1970 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1971 break;
1972 case VFL_TYPE_RADIO:
1973 radio = 1;
1974 break;
1975 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001976
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001977 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1978 video_device_node_name(vdev), v4l2_type_names[fh_type],
1979 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001980
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001981
Hans Verkuil876cb142012-06-23 08:12:47 -03001982 if (mutex_lock_interruptible(&dev->lock))
1983 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001984 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001985 if (!fh) {
1986 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001987 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001988 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001989 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001990 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001991 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001992 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001993 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001994
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001995 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001996 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001997 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001998 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001999
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002000 /* Needed, since GPIO might have disabled power of
2001 some i2c device
2002 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002003 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002004
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002005 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002006 if (fh->radio) {
2007 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002008 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002009 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002010
2011 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002012
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002013 if (dev->progressive)
2014 field = V4L2_FIELD_NONE;
2015 else
2016 field = V4L2_FIELD_INTERLACED;
2017
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002018 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2019 NULL, &dev->slock,
2020 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002021 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002022
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002023 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2024 NULL, &dev->slock,
2025 V4L2_BUF_TYPE_VBI_CAPTURE,
2026 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002027 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002028 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002029
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002030 return errCode;
2031}
2032
2033/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002034 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002035 * unregisters the v4l2,i2c and usb devices
2036 * called when the device gets disconected or at module unload
2037*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002038void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002039{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002040
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002041 /*FIXME: I2C IR should be disconnected */
2042
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002043 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002044 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002045 video_unregister_device(dev->radio_dev);
2046 else
2047 video_device_release(dev->radio_dev);
2048 dev->radio_dev = NULL;
2049 }
2050 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002051 em28xx_info("V4L2 device %s deregistered\n",
2052 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002053 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002054 video_unregister_device(dev->vbi_dev);
2055 else
2056 video_device_release(dev->vbi_dev);
2057 dev->vbi_dev = NULL;
2058 }
2059 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002060 em28xx_info("V4L2 device %s deregistered\n",
2061 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002062 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002063 video_unregister_device(dev->vdev);
2064 else
2065 video_device_release(dev->vdev);
2066 dev->vdev = NULL;
2067 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002068}
2069
2070/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002071 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002072 * stops streaming and deallocates all resources allocated by the v4l2
2073 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002074 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002075static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002076{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002077 struct em28xx_fh *fh = filp->private_data;
2078 struct em28xx *dev = fh->dev;
2079 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002080
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002081 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002082
Hans Verkuil876cb142012-06-23 08:12:47 -03002083 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002084 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002085 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002086 res_free(fh, EM28XX_RESOURCE_VIDEO);
2087 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002089 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2090 videobuf_stop(&fh->vb_vbiq);
2091 res_free(fh, EM28XX_RESOURCE_VBI);
2092 }
2093
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002094 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002095 /* the device is already disconnect,
2096 free the remaining resources */
2097 if (dev->state & DEV_DISCONNECTED) {
2098 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002099 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002100 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002101 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002102 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002103 return 0;
2104 }
2105
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002106 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002107 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002108
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002109 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002110 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002111 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002112
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002113 /* set alternate 0 */
2114 dev->alt = 0;
2115 em28xx_videodbg("setting alternate 0\n");
2116 errCode = usb_set_interface(dev->udev, 0, 0);
2117 if (errCode < 0) {
2118 em28xx_errdev("cannot change alternate number to "
2119 "0 (error=%i)\n", errCode);
2120 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002121 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002122
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002123 videobuf_mmap_free(&fh->vb_vidq);
2124 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002125 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002127 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128 return 0;
2129}
2130
2131/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002132 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133 * will allocate buffers when called for the first time
2134 */
2135static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002136em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002137 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002138{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002139 struct em28xx_fh *fh = filp->private_data;
2140 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002141 int rc;
2142
2143 rc = check_dev(dev);
2144 if (rc < 0)
2145 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146
Hans Verkuil876cb142012-06-23 08:12:47 -03002147 if (mutex_lock_interruptible(&dev->lock))
2148 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002149 /* FIXME: read() is not prepared to allow changing the video
2150 resolution while streaming. Seems a bug at em28xx_set_fmt
2151 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002152
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002153 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002154 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002155 rc = -EBUSY;
2156 else
2157 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002158 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002159 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002160 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002161 rc = -EBUSY;
2162 else
2163 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002164 filp->f_flags & O_NONBLOCK);
2165 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002166 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002167
Hans Verkuil876cb142012-06-23 08:12:47 -03002168 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002169}
2170
2171/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002172 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173 * will allocate buffers when called for the first time
2174 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002175static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002176{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002177 struct em28xx_fh *fh = filp->private_data;
2178 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002179 int rc;
2180
2181 rc = check_dev(dev);
2182 if (rc < 0)
2183 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002185 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2186 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2187 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002188 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002189 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2190 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2191 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002192 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002193 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002194 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002195 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002196}
2197
Hans Verkuil876cb142012-06-23 08:12:47 -03002198static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2199{
2200 struct em28xx_fh *fh = filp->private_data;
2201 struct em28xx *dev = fh->dev;
2202 unsigned int res;
2203
2204 mutex_lock(&dev->lock);
2205 res = em28xx_poll(filp, wait);
2206 mutex_unlock(&dev->lock);
2207 return res;
2208}
2209
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002210/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002211 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002213static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002215 struct em28xx_fh *fh = filp->private_data;
2216 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002217 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002218
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002219 rc = check_dev(dev);
2220 if (rc < 0)
2221 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002222
Hans Verkuil876cb142012-06-23 08:12:47 -03002223 if (mutex_lock_interruptible(&dev->lock))
2224 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002225 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2226 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2227 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2228 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002229 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002230
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002231 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2232 (unsigned long)vma->vm_start,
2233 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2234 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002235
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002236 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237}
2238
Hans Verkuilbec43662008-12-30 06:58:20 -03002239static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002240 .owner = THIS_MODULE,
2241 .open = em28xx_v4l2_open,
2242 .release = em28xx_v4l2_close,
2243 .read = em28xx_v4l2_read,
2244 .poll = em28xx_v4l2_poll,
2245 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002246 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247};
2248
Hans Verkuila3998102008-07-21 02:57:38 -03002249static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002250 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002251 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2252 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2253 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2254 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002255 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2256 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002257 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002258 .vidioc_g_audio = vidioc_g_audio,
2259 .vidioc_s_audio = vidioc_s_audio,
2260 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002261 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2262 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2263 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002264
2265 .vidioc_reqbufs = vidioc_reqbufs,
2266 .vidioc_querybuf = vidioc_querybuf,
2267 .vidioc_qbuf = vidioc_qbuf,
2268 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002269 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002270 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002271 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002272 .vidioc_g_parm = vidioc_g_parm,
2273 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002274 .vidioc_enum_input = vidioc_enum_input,
2275 .vidioc_g_input = vidioc_g_input,
2276 .vidioc_s_input = vidioc_s_input,
2277 .vidioc_queryctrl = vidioc_queryctrl,
2278 .vidioc_g_ctrl = vidioc_g_ctrl,
2279 .vidioc_s_ctrl = vidioc_s_ctrl,
2280 .vidioc_streamon = vidioc_streamon,
2281 .vidioc_streamoff = vidioc_streamoff,
2282 .vidioc_g_tuner = vidioc_g_tuner,
2283 .vidioc_s_tuner = vidioc_s_tuner,
2284 .vidioc_g_frequency = vidioc_g_frequency,
2285 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002286#ifdef CONFIG_VIDEO_ADV_DEBUG
2287 .vidioc_g_register = vidioc_g_register,
2288 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002289 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002290#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002291};
2292
2293static const struct video_device em28xx_video_template = {
2294 .fops = &em28xx_v4l_fops,
2295 .release = video_device_release,
2296 .ioctl_ops = &video_ioctl_ops,
2297
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002298 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002299 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002300};
2301
Hans Verkuilbec43662008-12-30 06:58:20 -03002302static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002303 .owner = THIS_MODULE,
2304 .open = em28xx_v4l2_open,
2305 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002306 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002307};
2308
2309static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002310 .vidioc_querycap = radio_querycap,
2311 .vidioc_g_tuner = radio_g_tuner,
2312 .vidioc_enum_input = radio_enum_input,
2313 .vidioc_g_audio = radio_g_audio,
2314 .vidioc_s_tuner = radio_s_tuner,
2315 .vidioc_s_audio = radio_s_audio,
2316 .vidioc_s_input = radio_s_input,
2317 .vidioc_queryctrl = radio_queryctrl,
2318 .vidioc_g_ctrl = vidioc_g_ctrl,
2319 .vidioc_s_ctrl = vidioc_s_ctrl,
2320 .vidioc_g_frequency = vidioc_g_frequency,
2321 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002322#ifdef CONFIG_VIDEO_ADV_DEBUG
2323 .vidioc_g_register = vidioc_g_register,
2324 .vidioc_s_register = vidioc_s_register,
2325#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002326};
2327
Hans Verkuila3998102008-07-21 02:57:38 -03002328static struct video_device em28xx_radio_template = {
2329 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002330 .fops = &radio_fops,
2331 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002332};
2333
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002334/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002335
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002336
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002337
Adrian Bunk532fe652008-01-28 22:10:48 -03002338static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002339 const struct video_device *template,
2340 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002341{
2342 struct video_device *vfd;
2343
2344 vfd = video_device_alloc();
2345 if (NULL == vfd)
2346 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002347
2348 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002349 vfd->v4l2_dev = &dev->v4l2_dev;
2350 vfd->release = video_device_release;
2351 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002352 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002353
2354 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2355 dev->name, type_name);
2356
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002357 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002358 return vfd;
2359}
2360
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002361int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002362{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002363 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002364 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002365 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002366
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002367 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2368 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002369
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002370 /* set default norm */
2371 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002372 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002373 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002374
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002375 /* Analog specific initialization */
2376 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002377
2378 maxw = norm_maxw(dev);
2379 /* MaxPacketSize for em2800 is too small to capture at full resolution
2380 * use half of maxw as the scaler can only scale to 50% */
2381 if (dev->board.is_em2800)
2382 maxw /= 2;
2383
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002384 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002385 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002386
Ezequiel García96371fc2012-03-23 18:09:34 -03002387 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002388
2389 /* Audio defaults */
2390 dev->mute = 1;
2391 dev->volume = 0x1f;
2392
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002393/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002394 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2395 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2396 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002397
2398 em28xx_set_outfmt(dev);
2399 em28xx_colorlevels_set_default(dev);
2400 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002401
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002402 /* allocate and fill video video_device struct */
2403 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2404 if (!dev->vdev) {
2405 em28xx_errdev("cannot allocate video_device.\n");
2406 return -ENODEV;
2407 }
2408
2409 /* register v4l2 video video_device */
2410 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2411 video_nr[dev->devno]);
2412 if (ret) {
2413 em28xx_errdev("unable to register video device (error=%i).\n",
2414 ret);
2415 return ret;
2416 }
2417
2418 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002419 if (em28xx_vbi_supported(dev) == 1) {
2420 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2421 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002422
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002423 /* register v4l2 vbi video_device */
2424 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2425 vbi_nr[dev->devno]);
2426 if (ret < 0) {
2427 em28xx_errdev("unable to register vbi device\n");
2428 return ret;
2429 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002430 }
2431
2432 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002433 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2434 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002435 if (!dev->radio_dev) {
2436 em28xx_errdev("cannot allocate video_device.\n");
2437 return -ENODEV;
2438 }
2439 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2440 radio_nr[dev->devno]);
2441 if (ret < 0) {
2442 em28xx_errdev("can't register radio device\n");
2443 return ret;
2444 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002445 em28xx_info("Registered radio device as %s\n",
2446 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002447 }
2448
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002449 em28xx_info("V4L2 video device registered as %s\n",
2450 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002451
2452 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002453 em28xx_info("V4L2 VBI device registered as %s\n",
2454 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002455
2456 return 0;
2457}