blob: f9f24215b56cf6d9f9c685bdcf4dda8e8bd88d8c [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,
174 struct em28xx_dmaqueue *dma_q,
175 struct em28xx_buffer *buf,
176 unsigned char *p,
177 unsigned char *outp, unsigned long len)
178{
179 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300180 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300181 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182
183 if (dma_q->pos + len > buf->vb.size)
184 len = buf->vb.size - dma_q->pos;
185
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 startread = p;
187 remain = len;
188
Frank Schaeferc02ec712012-11-08 14:11:33 -0300189 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300190 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300191 else /* interlaced mode, even nr. of lines */
192 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300193
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300194 linesdone = dma_q->pos / bytesperline;
195 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300196
197 if (dev->progressive)
198 offset = linesdone * bytesperline + currlinedone;
199 else
200 offset = linesdone * bytesperline * 2 + currlinedone;
201
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204 lencopy = lencopy > remain ? remain : lencopy;
205
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300210 remain = (char *)outp + buf->vb.size - (char *)startwrite;
211 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300213 if (lencopy <= 0)
214 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218
219 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300220 if (dev->progressive)
221 startwrite += lencopy;
222 else
223 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = remain;
227 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300228 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300230 if ((char *)startwrite + lencopy > (char *)outp +
231 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300232 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
233 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size));
236 lencopy = remain = (char *)outp + buf->vb.size -
237 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300238 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300239 if (lencopy <= 0)
240 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300241
242 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300243
244 remain -= lencopy;
245 }
246
247 dma_q->pos += len;
248}
249
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300250static void em28xx_copy_vbi(struct em28xx *dev,
251 struct em28xx_dmaqueue *dma_q,
252 struct em28xx_buffer *buf,
253 unsigned char *p,
254 unsigned char *outp, unsigned long len)
255{
256 void *startwrite, *startread;
257 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300258 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300259
260 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300261 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300262 return;
263 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300264 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300265
266 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300267 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300268 return;
269 }
270 if (buf == NULL) {
271 return;
272 }
273 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300274 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300275 return;
276 }
277 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300278 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300279 return;
280 }
281
282 if (dma_q->pos + len > buf->vb.size)
283 len = buf->vb.size - dma_q->pos;
284
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300285 startread = p;
286
287 startwrite = outp + dma_q->pos;
288 offset = dma_q->pos;
289
290 /* Make sure the bottom field populates the second half of the frame */
291 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300292 startwrite += bytesperline * dev->vbi_height;
293 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300294 }
295
296 memcpy(startwrite, startread, len);
297 dma_q->pos += len;
298}
299
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300300static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301 int packet, int status)
302{
303 char *errmsg = "Unknown";
304
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300305 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300306 case -ENOENT:
307 errmsg = "unlinked synchronuously";
308 break;
309 case -ECONNRESET:
310 errmsg = "unlinked asynchronuously";
311 break;
312 case -ENOSR:
313 errmsg = "Buffer error (overrun)";
314 break;
315 case -EPIPE:
316 errmsg = "Stalled (device not responding)";
317 break;
318 case -EOVERFLOW:
319 errmsg = "Babble (bad cable?)";
320 break;
321 case -EPROTO:
322 errmsg = "Bit-stuff error (bad cable?)";
323 break;
324 case -EILSEQ:
325 errmsg = "CRC/Timeout (could be anything)";
326 break;
327 case -ETIME:
328 errmsg = "Device does not respond";
329 break;
330 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300331 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300332 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
333 } else {
334 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
335 packet, status, errmsg);
336 }
337}
338
339/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300340 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300342static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
343 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300344{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300345 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300346 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300347
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300348 if (list_empty(&dma_q->active)) {
349 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300350 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300351 }
352
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300353 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300354 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300355 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300356 outp = videobuf_to_vmalloc(&buf->vb);
357 memset(outp, 0, buf->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300358
Frank Schaefer24a6d842012-12-08 11:31:24 -0300359 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300360}
361
Frank Schaefer960da932012-11-25 06:37:37 -0300362/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300363static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364{
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300365 struct em28xx_buffer *buf, *vbi_buf;
366 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300367 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer79ff8692012-11-25 06:37:36 -0300368 int xfer_bulk, num_packets, i, rc = 1;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300369 unsigned int actual_length, len = 0;
370 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300371
372 if (!dev)
373 return 0;
374
375 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
376 return 0;
377
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300378 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300379 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300380
Frank Schaefer4601cc32012-11-08 14:11:46 -0300381 xfer_bulk = usb_pipebulk(urb->pipe);
382
Frank Schaefer74209dc2012-11-08 14:11:37 -0300383 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300384 if (buf != NULL)
385 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300386
Frank Schaefer74209dc2012-11-08 14:11:37 -0300387 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300388 if (vbi_buf != NULL)
389 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
390
Frank Schaefer4601cc32012-11-08 14:11:46 -0300391 if (xfer_bulk) /* bulk */
392 num_packets = 1;
393 else /* isoc */
394 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300395
Frank Schaefer4601cc32012-11-08 14:11:46 -0300396 for (i = 0; i < num_packets; i++) {
397 if (xfer_bulk) { /* bulk */
398 actual_length = urb->actual_length;
399
400 p = urb->transfer_buffer;
401 } else { /* isoc */
402 if (urb->iso_frame_desc[i].status < 0) {
403 print_err_status(dev, i,
404 urb->iso_frame_desc[i].status);
405 if (urb->iso_frame_desc[i].status != -EPROTO)
406 continue;
407 }
408
409 actual_length = urb->iso_frame_desc[i].actual_length;
410 if (actual_length > dev->max_pkt_size) {
411 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300412 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300413 }
414
415 p = urb->transfer_buffer +
416 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300417 }
418
Frank Schaefer3610f582012-11-25 06:37:33 -0300419 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300420 /* NOTE: happens very often with isoc transfers */
421 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300422 continue;
423 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300424
425 /* capture type 0 = vbi start
426 capture type 1 = video start
427 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300428 len = actual_length;
429 if (len >= 4) {
430 /* NOTE: headers are always 4 bytes and
431 * never split across packets */
432 if (p[0] == 0x33 && p[1] == 0x95) {
433 dev->capture_type = 0;
434 dev->vbi_read = 0;
435 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300436 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300437 p += 4;
438 len -= 4;
439 } else if (p[0] == 0x88 && p[1] == 0x88 &&
440 p[2] == 0x88 && p[3] == 0x88) {
441 /* continuation */
442 p += 4;
443 len -= 4;
444 } else if (p[0] == 0x22 && p[1] == 0x5a) {
445 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300446 dev->capture_type = 1;
447 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300448 p += 4;
449 len -= 4;
450 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300451 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300452 /* NOTE: with bulk transfers, intermediate data packets
453 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300454
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300455 if (dev->capture_type == 0) {
Frank Schaefer79ff8692012-11-25 06:37:36 -0300456 int vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300457 if (dev->vbi_read >= vbi_size) {
458 /* We've already read all the VBI data, so
459 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300460 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300461 } else if ((dev->vbi_read + len) < vbi_size) {
462 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300463 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300464 /* Brand new frame */
465 if (vbi_buf != NULL)
Frank Schaefer948a49a2012-12-08 11:31:25 -0300466 finish_buffer(dev, vbi_buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300467 vbi_buf = get_next_buf(dev, vbi_dma_q);
468 dev->usb_ctl.vbi_buf = vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300469 if (vbi_buf == NULL)
470 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300471 else
472 vbioutp = videobuf_to_vmalloc(
473 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300474 }
475
476 if (dev->vbi_read == 0) {
477 vbi_dma_q->pos = 0;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300478 if (vbi_buf != NULL)
Frank Schaefer24a6d842012-12-08 11:31:24 -0300479 vbi_buf->top_field
480 = dev->top_field;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300481 }
482
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300483 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300484 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
485 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300486 } else {
487 /* Some of this frame is VBI data and some is
488 video data */
489 int vbi_data_len = vbi_size - dev->vbi_read;
490 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300491 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
492 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300493 dev->capture_type = 1;
494 p += vbi_data_len;
495 len -= vbi_data_len;
496 }
497 }
498
499 if (dev->capture_type == 1) {
500 dev->capture_type = 2;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300501 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300502 if (buf != NULL)
Frank Schaefer948a49a2012-12-08 11:31:25 -0300503 finish_buffer(dev, buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300504 buf = get_next_buf(dev, dma_q);
505 dev->usb_ctl.vid_buf = buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300506 if (buf == NULL)
507 outp = NULL;
508 else
509 outp = videobuf_to_vmalloc(&buf->vb);
510 }
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300511 if (buf != NULL)
512 buf->top_field = dev->top_field;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300513
514 dma_q->pos = 0;
515 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300516
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300517 if (buf != NULL && dev->capture_type == 2 && len > 0)
518 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300519 }
520 return rc;
521}
522
523
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300524/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300525 Videobuf operations
526 ------------------------------------------------------------------*/
527
528static int
529buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
530{
531 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300532 struct em28xx *dev = fh->dev;
533 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300534
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300535 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
536 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300537
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300538 if (0 == *count)
539 *count = EM28XX_DEF_BUF;
540
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300541 if (*count < EM28XX_MIN_BUF)
542 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300543
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300544 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300545 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300546 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300547 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300548
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300549 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300550
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300551 return 0;
552}
553
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300554/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300555static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
556{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300557 struct em28xx_fh *fh = vq->priv_data;
558 struct em28xx *dev = fh->dev;
559 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300560 if (in_interrupt())
561 BUG();
562
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300563 /* We used to wait for the buffer to finish here, but this didn't work
564 because, as we were keeping the state as VIDEOBUF_QUEUED,
565 videobuf_queue_cancel marked it as finished for us.
566 (Also, it could wedge forever if the hardware was misconfigured.)
567
568 This should be safe; by the time we get here, the buffer isn't
569 queued anymore. If we ever start marking the buffers as
570 VIDEOBUF_ACTIVE, it won't be, though.
571 */
572 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300573 if (dev->usb_ctl.vid_buf == buf)
574 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300575 spin_unlock_irqrestore(&dev->slock, flags);
576
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300577 videobuf_vmalloc_free(&buf->vb);
578 buf->vb.state = VIDEOBUF_NEEDS_INIT;
579}
580
581static int
582buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
583 enum v4l2_field field)
584{
585 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300586 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300587 struct em28xx *dev = fh->dev;
588 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300589
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300590 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
591 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300592
593 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
594 return -EINVAL;
595
Brandon Philips05612972008-04-13 14:57:01 -0300596 buf->vb.width = dev->width;
597 buf->vb.height = dev->height;
598 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300599
600 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300601 rc = videobuf_iolock(vq, &buf->vb, NULL);
602 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300603 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300604 }
605
Frank Schaefer74209dc2012-11-08 14:11:37 -0300606 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300607 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300608
609 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300610 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300611 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
612 dev->analog_xfer_bulk,
613 EM28XX_NUM_BUFS,
614 dev->max_pkt_size,
615 dev->packet_multiplier,
616 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300617 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300618 goto fail;
619 }
620
621 buf->vb.state = VIDEOBUF_PREPARED;
622 return 0;
623
624fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300625 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300626 return rc;
627}
628
629static void
630buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
631{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300632 struct em28xx_buffer *buf = container_of(vb,
633 struct em28xx_buffer,
634 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300635 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300636 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300637 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300638
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300639 buf->vb.state = VIDEOBUF_QUEUED;
640 list_add_tail(&buf->vb.queue, &vidq->active);
641
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300642}
643
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300644static void buffer_release(struct videobuf_queue *vq,
645 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300646{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300647 struct em28xx_buffer *buf = container_of(vb,
648 struct em28xx_buffer,
649 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300651 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300652
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300653 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300654
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300655 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300656}
657
658static struct videobuf_queue_ops em28xx_video_qops = {
659 .buf_setup = buffer_setup,
660 .buf_prepare = buffer_prepare,
661 .buf_queue = buffer_queue,
662 .buf_release = buffer_release,
663};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800664
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300665/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800666
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800667static void video_mux(struct em28xx *dev, int index)
668{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800669 dev->ctl_input = index;
670 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300671 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800672
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300673 if (!dev->ctl_aoutput)
674 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
675
Hans Verkuil5325b422009-04-02 11:26:22 -0300676 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
677 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800678
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300679 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300680 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300681 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
682 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300683 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300684 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300685 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
686 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800687 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300688
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300689 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300690 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
691 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300692 }
693
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300694 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800695}
696
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300697/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300698static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300699{
700 struct em28xx *dev = fh->dev;
701
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300702 if (fh->resources & bit)
703 /* have it already allocated */
704 return 1;
705
706 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300707 if (dev->resources & bit) {
708 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300709 return 0;
710 }
711 /* it's free, grab it */
712 fh->resources |= bit;
713 dev->resources |= bit;
714 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300715 return 1;
716}
717
718static int res_check(struct em28xx_fh *fh, unsigned int bit)
719{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300720 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300721}
722
723static int res_locked(struct em28xx *dev, unsigned int bit)
724{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300725 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300726}
727
728static void res_free(struct em28xx_fh *fh, unsigned int bits)
729{
730 struct em28xx *dev = fh->dev;
731
732 BUG_ON((fh->resources & bits) != bits);
733
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300734 fh->resources &= ~bits;
735 dev->resources &= ~bits;
736 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300737}
738
739static int get_ressource(struct em28xx_fh *fh)
740{
741 switch (fh->type) {
742 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
743 return EM28XX_RESOURCE_VIDEO;
744 case V4L2_BUF_TYPE_VBI_CAPTURE:
745 return EM28XX_RESOURCE_VBI;
746 default:
747 BUG();
748 return 0;
749 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300750}
751
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800752/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300753 * ac97_queryctrl()
754 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300755 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300756static int ac97_queryctrl(struct v4l2_queryctrl *qc)
757{
758 int i;
759
760 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
761 if (qc->id && qc->id == ac97_qctrl[i].id) {
762 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
763 return 0;
764 }
765 }
766
767 /* Control is not ac97 related */
768 return 1;
769}
770
771/*
772 * ac97_get_ctrl()
773 * return the current values for ac97 mute and volume
774 */
775static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300776{
777 switch (ctrl->id) {
778 case V4L2_CID_AUDIO_MUTE:
779 ctrl->value = dev->mute;
780 return 0;
781 case V4L2_CID_AUDIO_VOLUME:
782 ctrl->value = dev->volume;
783 return 0;
784 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300785 /* Control is not ac97 related */
786 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300787 }
788}
789
790/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300791 * ac97_set_ctrl()
792 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300794static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300796 int i;
797
798 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
799 if (ctrl->id == ac97_qctrl[i].id)
800 goto handle;
801
802 /* Announce that hasn't handle it */
803 return 1;
804
805handle:
806 if (ctrl->value < ac97_qctrl[i].minimum ||
807 ctrl->value > ac97_qctrl[i].maximum)
808 return -ERANGE;
809
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810 switch (ctrl->id) {
811 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300812 dev->mute = ctrl->value;
813 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814 case V4L2_CID_AUDIO_VOLUME:
815 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300816 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300818
819 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300820}
821
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822static int check_dev(struct em28xx *dev)
823{
824 if (dev->state & DEV_DISCONNECTED) {
825 em28xx_errdev("v4l2 ioctl: device not present\n");
826 return -ENODEV;
827 }
828
829 if (dev->state & DEV_MISCONFIGURED) {
830 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
831 "close and open it again\n");
832 return -EIO;
833 }
834 return 0;
835}
836
837static void get_scale(struct em28xx *dev,
838 unsigned int width, unsigned int height,
839 unsigned int *hscale, unsigned int *vscale)
840{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300841 unsigned int maxw = norm_maxw(dev);
842 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300843
844 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
845 if (*hscale >= 0x4000)
846 *hscale = 0x3fff;
847
848 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
849 if (*vscale >= 0x4000)
850 *vscale = 0x3fff;
851}
852
853/* ------------------------------------------------------------------
854 IOCTL vidioc handling
855 ------------------------------------------------------------------*/
856
Hans Verkuil78b526a2008-05-28 12:16:41 -0300857static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300858 struct v4l2_format *f)
859{
860 struct em28xx_fh *fh = priv;
861 struct em28xx *dev = fh->dev;
862
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300863 f->fmt.pix.width = dev->width;
864 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300865 f->fmt.pix.pixelformat = dev->format->fourcc;
866 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300867 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300868 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
869
870 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300871 if (dev->progressive)
872 f->fmt.pix.field = V4L2_FIELD_NONE;
873 else
874 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300875 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300876 return 0;
877}
878
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300879static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
880{
881 unsigned int i;
882
883 for (i = 0; i < ARRAY_SIZE(format); i++)
884 if (format[i].fourcc == fourcc)
885 return &format[i];
886
887 return NULL;
888}
889
Hans Verkuil78b526a2008-05-28 12:16:41 -0300890static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891 struct v4l2_format *f)
892{
893 struct em28xx_fh *fh = priv;
894 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300895 unsigned int width = f->fmt.pix.width;
896 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300897 unsigned int maxw = norm_maxw(dev);
898 unsigned int maxh = norm_maxh(dev);
899 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300900 struct em28xx_fmt *fmt;
901
902 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
903 if (!fmt) {
904 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
905 f->fmt.pix.pixelformat);
906 return -EINVAL;
907 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300908
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300909 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300911 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
912 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300913 /* MaxPacketSize for em2800 is too small to capture at full resolution
914 * use half of maxw as the scaler can only scale to 50% */
915 if (width == maxw && height == maxh)
916 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300917 } else {
918 /* width must even because of the YUYV format
919 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300920 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
921 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300922 }
923
924 get_scale(dev, width, height, &hscale, &vscale);
925
926 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
927 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
928
929 f->fmt.pix.width = width;
930 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300931 f->fmt.pix.pixelformat = fmt->fourcc;
932 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
933 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300935 if (dev->progressive)
936 f->fmt.pix.field = V4L2_FIELD_NONE;
937 else
938 f->fmt.pix.field = dev->interlaced ?
939 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300940
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941 return 0;
942}
943
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300944static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
945 unsigned width, unsigned height)
946{
947 struct em28xx_fmt *fmt;
948
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300949 fmt = format_by_fourcc(fourcc);
950 if (!fmt)
951 return -EINVAL;
952
953 dev->format = fmt;
954 dev->width = width;
955 dev->height = height;
956
957 /* set new image size */
958 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
959
960 em28xx_set_alternate(dev);
961 em28xx_resolution_set(dev);
962
963 return 0;
964}
965
Hans Verkuil78b526a2008-05-28 12:16:41 -0300966static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967 struct v4l2_format *f)
968{
969 struct em28xx_fh *fh = priv;
970 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300971 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300972
973 rc = check_dev(dev);
974 if (rc < 0)
975 return rc;
976
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300977 vidioc_try_fmt_vid_cap(file, priv, f);
978
Brandon Philips05612972008-04-13 14:57:01 -0300979 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
980 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300981 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300982 }
983
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300984 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300985 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986}
987
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300988static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
989{
990 struct em28xx_fh *fh = priv;
991 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300992 int rc;
993
994 rc = check_dev(dev);
995 if (rc < 0)
996 return rc;
997
998 *norm = dev->norm;
999
1000 return 0;
1001}
1002
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001003static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1004{
1005 struct em28xx_fh *fh = priv;
1006 struct em28xx *dev = fh->dev;
1007 int rc;
1008
1009 rc = check_dev(dev);
1010 if (rc < 0)
1011 return rc;
1012
1013 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1014
1015 return 0;
1016}
1017
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001018static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001019{
1020 struct em28xx_fh *fh = priv;
1021 struct em28xx *dev = fh->dev;
1022 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 int rc;
1024
1025 rc = check_dev(dev);
1026 if (rc < 0)
1027 return rc;
1028
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001029 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030
1031 /* Adjusts width/height, if needed */
1032 f.fmt.pix.width = dev->width;
1033 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001034 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001036 /* set new image size */
1037 dev->width = f.fmt.pix.width;
1038 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001039 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1040
1041 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001042 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044 return 0;
1045}
1046
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001047static int vidioc_g_parm(struct file *file, void *priv,
1048 struct v4l2_streamparm *p)
1049{
1050 struct em28xx_fh *fh = priv;
1051 struct em28xx *dev = fh->dev;
1052 int rc = 0;
1053
1054 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1055 return -EINVAL;
1056
1057 if (dev->board.is_webcam)
1058 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1059 video, g_parm, p);
1060 else
1061 v4l2_video_std_frame_period(dev->norm,
1062 &p->parm.capture.timeperframe);
1063
1064 return rc;
1065}
1066
1067static int vidioc_s_parm(struct file *file, void *priv,
1068 struct v4l2_streamparm *p)
1069{
1070 struct em28xx_fh *fh = priv;
1071 struct em28xx *dev = fh->dev;
1072
1073 if (!dev->board.is_webcam)
1074 return -EINVAL;
1075
1076 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1077 return -EINVAL;
1078
1079 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1080}
1081
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082static const char *iname[] = {
1083 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1084 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1085 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1086 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1087 [EM28XX_VMUX_SVIDEO] = "S-Video",
1088 [EM28XX_VMUX_TELEVISION] = "Television",
1089 [EM28XX_VMUX_CABLE] = "Cable TV",
1090 [EM28XX_VMUX_DVB] = "DVB",
1091 [EM28XX_VMUX_DEBUG] = "for debug only",
1092};
1093
1094static int vidioc_enum_input(struct file *file, void *priv,
1095 struct v4l2_input *i)
1096{
1097 struct em28xx_fh *fh = priv;
1098 struct em28xx *dev = fh->dev;
1099 unsigned int n;
1100
1101 n = i->index;
1102 if (n >= MAX_EM28XX_INPUT)
1103 return -EINVAL;
1104 if (0 == INPUT(n)->type)
1105 return -EINVAL;
1106
1107 i->index = n;
1108 i->type = V4L2_INPUT_TYPE_CAMERA;
1109
1110 strcpy(i->name, iname[INPUT(n)->type]);
1111
1112 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1113 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1114 i->type = V4L2_INPUT_TYPE_TUNER;
1115
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001116 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001117
1118 return 0;
1119}
1120
1121static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1122{
1123 struct em28xx_fh *fh = priv;
1124 struct em28xx *dev = fh->dev;
1125
1126 *i = dev->ctl_input;
1127
1128 return 0;
1129}
1130
1131static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1132{
1133 struct em28xx_fh *fh = priv;
1134 struct em28xx *dev = fh->dev;
1135 int rc;
1136
1137 rc = check_dev(dev);
1138 if (rc < 0)
1139 return rc;
1140
1141 if (i >= MAX_EM28XX_INPUT)
1142 return -EINVAL;
1143 if (0 == INPUT(i)->type)
1144 return -EINVAL;
1145
Ezequiel García96371fc2012-03-23 18:09:34 -03001146 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001147 return 0;
1148}
1149
1150static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1151{
1152 struct em28xx_fh *fh = priv;
1153 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001155 if (!dev->audio_mode.has_audio)
1156 return -EINVAL;
1157
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001158 switch (a->index) {
1159 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001161 break;
1162 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001163 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001164 break;
1165 case EM28XX_AMUX_VIDEO2:
1166 strcpy(a->name, "Television alt");
1167 break;
1168 case EM28XX_AMUX_PHONE:
1169 strcpy(a->name, "Phone");
1170 break;
1171 case EM28XX_AMUX_MIC:
1172 strcpy(a->name, "Mic");
1173 break;
1174 case EM28XX_AMUX_CD:
1175 strcpy(a->name, "CD");
1176 break;
1177 case EM28XX_AMUX_AUX:
1178 strcpy(a->name, "Aux");
1179 break;
1180 case EM28XX_AMUX_PCM_OUT:
1181 strcpy(a->name, "PCM");
1182 break;
1183 default:
1184 return -EINVAL;
1185 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001186
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001187 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001188 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001189
1190 return 0;
1191}
1192
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001193static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001194{
1195 struct em28xx_fh *fh = priv;
1196 struct em28xx *dev = fh->dev;
1197
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001198
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001199 if (!dev->audio_mode.has_audio)
1200 return -EINVAL;
1201
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001202 if (a->index >= MAX_EM28XX_INPUT)
1203 return -EINVAL;
1204 if (0 == INPUT(a->index)->type)
1205 return -EINVAL;
1206
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001207 dev->ctl_ainput = INPUT(a->index)->amux;
1208 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001209
1210 if (!dev->ctl_aoutput)
1211 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001212
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001213 return 0;
1214}
1215
1216static int vidioc_queryctrl(struct file *file, void *priv,
1217 struct v4l2_queryctrl *qc)
1218{
1219 struct em28xx_fh *fh = priv;
1220 struct em28xx *dev = fh->dev;
1221 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001222 int rc;
1223
1224 rc = check_dev(dev);
1225 if (rc < 0)
1226 return rc;
1227
1228 memset(qc, 0, sizeof(*qc));
1229
1230 qc->id = id;
1231
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001232 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001233 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1234 rc = ac97_queryctrl(qc);
1235 if (!rc)
1236 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001237 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001238
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001239 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001240 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001241
1242 if (qc->type)
1243 return 0;
1244 else
1245 return -EINVAL;
1246}
1247
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001248/*
1249 * FIXME: This is an indirect way to check if a control exists at a
1250 * subdev. Instead of that hack, maybe the better would be to change all
1251 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1252 */
1253static int check_subdev_ctrl(struct em28xx *dev, int id)
1254{
1255 struct v4l2_queryctrl qc;
1256
1257 memset(&qc, 0, sizeof(qc));
1258 qc.id = id;
1259
1260 /* enumerate V4L2 device controls */
1261 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1262
1263 if (qc.type)
1264 return 0;
1265 else
1266 return -EINVAL;
1267}
1268
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001269static int vidioc_g_ctrl(struct file *file, void *priv,
1270 struct v4l2_control *ctrl)
1271{
1272 struct em28xx_fh *fh = priv;
1273 struct em28xx *dev = fh->dev;
1274 int rc;
1275
1276 rc = check_dev(dev);
1277 if (rc < 0)
1278 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001279 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001280
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001281 /* Set an AC97 control */
1282 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1283 rc = ac97_get_ctrl(dev, ctrl);
1284 else
1285 rc = 1;
1286
1287 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1288 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001289 if (check_subdev_ctrl(dev, ctrl->id))
1290 return -EINVAL;
1291
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001292 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001293 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001294 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001295
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296 return rc;
1297}
1298
1299static int vidioc_s_ctrl(struct file *file, void *priv,
1300 struct v4l2_control *ctrl)
1301{
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001304 int rc;
1305
1306 rc = check_dev(dev);
1307 if (rc < 0)
1308 return rc;
1309
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001310 /* Set an AC97 control */
1311 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1312 rc = ac97_set_ctrl(dev, ctrl);
1313 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001314 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001315
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001316 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001317 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001318 rc = check_subdev_ctrl(dev, ctrl->id);
1319 if (!rc)
1320 v4l2_device_call_all(&dev->v4l2_dev, 0,
1321 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001322 /*
1323 * In the case of non-AC97 volume controls, we still need
1324 * to do some setups at em28xx, in order to mute/unmute
1325 * and to adjust audio volume. However, the value ranges
1326 * should be checked by the corresponding V4L subdriver.
1327 */
1328 switch (ctrl->id) {
1329 case V4L2_CID_AUDIO_MUTE:
1330 dev->mute = ctrl->value;
1331 rc = em28xx_audio_analog_set(dev);
1332 break;
1333 case V4L2_CID_AUDIO_VOLUME:
1334 dev->volume = ctrl->value;
1335 rc = em28xx_audio_analog_set(dev);
1336 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001337 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001338 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001339}
1340
1341static int vidioc_g_tuner(struct file *file, void *priv,
1342 struct v4l2_tuner *t)
1343{
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
1346 int rc;
1347
1348 rc = check_dev(dev);
1349 if (rc < 0)
1350 return rc;
1351
1352 if (0 != t->index)
1353 return -EINVAL;
1354
1355 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001356 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001357
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001358 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001359 return 0;
1360}
1361
1362static int vidioc_s_tuner(struct file *file, void *priv,
1363 struct v4l2_tuner *t)
1364{
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1367 int rc;
1368
1369 rc = check_dev(dev);
1370 if (rc < 0)
1371 return rc;
1372
1373 if (0 != t->index)
1374 return -EINVAL;
1375
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001376 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377 return 0;
1378}
1379
1380static int vidioc_g_frequency(struct file *file, void *priv,
1381 struct v4l2_frequency *f)
1382{
1383 struct em28xx_fh *fh = priv;
1384 struct em28xx *dev = fh->dev;
1385
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001386 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001388 return 0;
1389}
1390
1391static int vidioc_s_frequency(struct file *file, void *priv,
1392 struct v4l2_frequency *f)
1393{
1394 struct em28xx_fh *fh = priv;
1395 struct em28xx *dev = fh->dev;
1396 int rc;
1397
1398 rc = check_dev(dev);
1399 if (rc < 0)
1400 return rc;
1401
1402 if (0 != f->tuner)
1403 return -EINVAL;
1404
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001405 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1406 return -EINVAL;
1407 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408 return -EINVAL;
1409
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001410 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001411 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001412
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001413 return 0;
1414}
1415
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001416#ifdef CONFIG_VIDEO_ADV_DEBUG
1417static int em28xx_reg_len(int reg)
1418{
1419 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001420 case EM28XX_R40_AC97LSB:
1421 case EM28XX_R30_HSCALELOW:
1422 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001423 return 2;
1424 default:
1425 return 1;
1426 }
1427}
1428
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001429static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001430 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001431{
1432 struct em28xx_fh *fh = priv;
1433 struct em28xx *dev = fh->dev;
1434
1435 chip->ident = V4L2_IDENT_NONE;
1436 chip->revision = 0;
1437
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001438 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001439
1440 return 0;
1441}
1442
1443
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001444static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001445 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001446{
1447 struct em28xx_fh *fh = priv;
1448 struct em28xx *dev = fh->dev;
1449 int ret;
1450
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001451 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001452 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001453 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001454 if (ret < 0)
1455 return ret;
1456
1457 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001458 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001459 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001460 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001461 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001462 return 0;
1463 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001464 /* TODO: is this correct? */
1465 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1466 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001467 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001468 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001469 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001470 }
1471
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001472 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001473 reg->size = em28xx_reg_len(reg->reg);
1474 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001475 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001476
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001477 if (ret < 0)
1478 return ret;
1479
1480 reg->val = ret;
1481 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001482 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001483 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1484 reg->reg, (char *)&val, 2);
1485 if (ret < 0)
1486 return ret;
1487
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001488 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001489 }
1490
1491 return 0;
1492}
1493
1494static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001495 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001496{
1497 struct em28xx_fh *fh = priv;
1498 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001499 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001500
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001501 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001502 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001503 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001504 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001505 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001506 return 0;
1507 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001508 /* TODO: is this correct? */
1509 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1510 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001511 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001512 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001513 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001514 }
1515
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001516 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001517 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001518
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001519 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001520 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001521}
1522#endif
1523
1524
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001525static int vidioc_cropcap(struct file *file, void *priv,
1526 struct v4l2_cropcap *cc)
1527{
1528 struct em28xx_fh *fh = priv;
1529 struct em28xx *dev = fh->dev;
1530
1531 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1532 return -EINVAL;
1533
1534 cc->bounds.left = 0;
1535 cc->bounds.top = 0;
1536 cc->bounds.width = dev->width;
1537 cc->bounds.height = dev->height;
1538 cc->defrect = cc->bounds;
1539 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1540 cc->pixelaspect.denominator = 59;
1541
1542 return 0;
1543}
1544
1545static int vidioc_streamon(struct file *file, void *priv,
1546 enum v4l2_buf_type type)
1547{
1548 struct em28xx_fh *fh = priv;
1549 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001550 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001551
1552 rc = check_dev(dev);
1553 if (rc < 0)
1554 return rc;
1555
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001556 if (unlikely(type != fh->type))
1557 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001558
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001559 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1560 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001561
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001562 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001563 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001564
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001565 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1566 rc = videobuf_streamon(&fh->vb_vidq);
1567 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1568 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001569
1570 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001571}
1572
1573static int vidioc_streamoff(struct file *file, void *priv,
1574 enum v4l2_buf_type type)
1575{
1576 struct em28xx_fh *fh = priv;
1577 struct em28xx *dev = fh->dev;
1578 int rc;
1579
1580 rc = check_dev(dev);
1581 if (rc < 0)
1582 return rc;
1583
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001584 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1585 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001586 return -EINVAL;
1587 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001588 return -EINVAL;
1589
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001590 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1591 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001592
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001593 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001594 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1595 videobuf_streamoff(&fh->vb_vidq);
1596 res_free(fh, EM28XX_RESOURCE_VIDEO);
1597 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001598 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001599 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1600 videobuf_streamoff(&fh->vb_vbiq);
1601 res_free(fh, EM28XX_RESOURCE_VBI);
1602 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001603 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001604
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001605 return 0;
1606}
1607
1608static int vidioc_querycap(struct file *file, void *priv,
1609 struct v4l2_capability *cap)
1610{
1611 struct em28xx_fh *fh = priv;
1612 struct em28xx *dev = fh->dev;
1613
1614 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1615 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001616 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001617
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001618 cap->capabilities =
1619 V4L2_CAP_SLICED_VBI_CAPTURE |
1620 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001621 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1622
Devin Heitmueller04146142009-09-11 00:08:44 -03001623 if (dev->vbi_dev)
1624 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1625
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001626 if (dev->audio_mode.has_audio)
1627 cap->capabilities |= V4L2_CAP_AUDIO;
1628
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001629 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001630 cap->capabilities |= V4L2_CAP_TUNER;
1631
1632 return 0;
1633}
1634
Hans Verkuil78b526a2008-05-28 12:16:41 -03001635static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001636 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001637{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001638 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001639 return -EINVAL;
1640
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001641 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1642 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001643
1644 return 0;
1645}
1646
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001647static int vidioc_enum_framesizes(struct file *file, void *priv,
1648 struct v4l2_frmsizeenum *fsize)
1649{
1650 struct em28xx_fh *fh = priv;
1651 struct em28xx *dev = fh->dev;
1652 struct em28xx_fmt *fmt;
1653 unsigned int maxw = norm_maxw(dev);
1654 unsigned int maxh = norm_maxh(dev);
1655
1656 fmt = format_by_fourcc(fsize->pixel_format);
1657 if (!fmt) {
1658 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1659 fsize->pixel_format);
1660 return -EINVAL;
1661 }
1662
1663 if (dev->board.is_em2800) {
1664 if (fsize->index > 1)
1665 return -EINVAL;
1666 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1667 fsize->discrete.width = maxw / (1 + fsize->index);
1668 fsize->discrete.height = maxh / (1 + fsize->index);
1669 return 0;
1670 }
1671
1672 if (fsize->index != 0)
1673 return -EINVAL;
1674
1675 /* Report a continuous range */
1676 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1677 fsize->stepwise.min_width = 48;
1678 fsize->stepwise.min_height = 32;
1679 fsize->stepwise.max_width = maxw;
1680 fsize->stepwise.max_height = maxh;
1681 fsize->stepwise.step_width = 1;
1682 fsize->stepwise.step_height = 1;
1683 return 0;
1684}
1685
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001686/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001687static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001688 struct v4l2_format *f)
1689{
1690 struct em28xx_fh *fh = priv;
1691 struct em28xx *dev = fh->dev;
1692 int rc;
1693
1694 rc = check_dev(dev);
1695 if (rc < 0)
1696 return rc;
1697
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001698 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001699 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001700
1701 if (f->fmt.sliced.service_set == 0)
1702 rc = -EINVAL;
1703
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001704 return rc;
1705}
1706
Hans Verkuil78b526a2008-05-28 12:16:41 -03001707static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001708 struct v4l2_format *f)
1709{
1710 struct em28xx_fh *fh = priv;
1711 struct em28xx *dev = fh->dev;
1712 int rc;
1713
1714 rc = check_dev(dev);
1715 if (rc < 0)
1716 return rc;
1717
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001718 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001719
1720 if (f->fmt.sliced.service_set == 0)
1721 return -EINVAL;
1722
1723 return 0;
1724}
1725
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001726/* RAW VBI ioctls */
1727
1728static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1729 struct v4l2_format *format)
1730{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001731 struct em28xx_fh *fh = priv;
1732 struct em28xx *dev = fh->dev;
1733
1734 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001735 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1736 format->fmt.vbi.offset = 0;
1737 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001738 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1739 format->fmt.vbi.count[0] = dev->vbi_height;
1740 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001741
1742 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001743 if (dev->norm & V4L2_STD_525_60) {
1744 /* NTSC */
1745 format->fmt.vbi.start[0] = 10;
1746 format->fmt.vbi.start[1] = 273;
1747 } else if (dev->norm & V4L2_STD_625_50) {
1748 /* PAL */
1749 format->fmt.vbi.start[0] = 6;
1750 format->fmt.vbi.start[1] = 318;
1751 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001752
1753 return 0;
1754}
1755
1756static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1757 struct v4l2_format *format)
1758{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001759 struct em28xx_fh *fh = priv;
1760 struct em28xx *dev = fh->dev;
1761
1762 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001763 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1764 format->fmt.vbi.offset = 0;
1765 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001766 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1767 format->fmt.vbi.count[0] = dev->vbi_height;
1768 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001769
1770 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001771 if (dev->norm & V4L2_STD_525_60) {
1772 /* NTSC */
1773 format->fmt.vbi.start[0] = 10;
1774 format->fmt.vbi.start[1] = 273;
1775 } else if (dev->norm & V4L2_STD_625_50) {
1776 /* PAL */
1777 format->fmt.vbi.start[0] = 6;
1778 format->fmt.vbi.start[1] = 318;
1779 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001780
1781 return 0;
1782}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001783
1784static int vidioc_reqbufs(struct file *file, void *priv,
1785 struct v4l2_requestbuffers *rb)
1786{
1787 struct em28xx_fh *fh = priv;
1788 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001789 int rc;
1790
1791 rc = check_dev(dev);
1792 if (rc < 0)
1793 return rc;
1794
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001795 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1796 return videobuf_reqbufs(&fh->vb_vidq, rb);
1797 else
1798 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001799}
1800
1801static int vidioc_querybuf(struct file *file, void *priv,
1802 struct v4l2_buffer *b)
1803{
1804 struct em28xx_fh *fh = priv;
1805 struct em28xx *dev = fh->dev;
1806 int rc;
1807
1808 rc = check_dev(dev);
1809 if (rc < 0)
1810 return rc;
1811
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001812 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1813 return videobuf_querybuf(&fh->vb_vidq, b);
1814 else {
1815 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1816 the videobuf framework, but we probably shouldn't be
1817 returning a buffer larger than that which was asked for.
1818 At a minimum, it causes a crash in zvbi since it does
1819 a memcpy based on the source buffer length */
1820 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001821 b->length = dev->vbi_width * dev->vbi_height * 2;
1822
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001823 return result;
1824 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001825}
1826
1827static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1828{
1829 struct em28xx_fh *fh = priv;
1830 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001831 int rc;
1832
1833 rc = check_dev(dev);
1834 if (rc < 0)
1835 return rc;
1836
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001837 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1838 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001839 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001840 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001841}
1842
1843static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1844{
1845 struct em28xx_fh *fh = priv;
1846 struct em28xx *dev = fh->dev;
1847 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001848
1849 rc = check_dev(dev);
1850 if (rc < 0)
1851 return rc;
1852
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001853 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1854 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1855 O_NONBLOCK);
1856 else
1857 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1858 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001859}
1860
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001861/* ----------------------------------------------------------- */
1862/* RADIO ESPECIFIC IOCTLS */
1863/* ----------------------------------------------------------- */
1864
1865static int radio_querycap(struct file *file, void *priv,
1866 struct v4l2_capability *cap)
1867{
1868 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1869
1870 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1871 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001872 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001873
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001874 cap->capabilities = V4L2_CAP_TUNER;
1875 return 0;
1876}
1877
1878static int radio_g_tuner(struct file *file, void *priv,
1879 struct v4l2_tuner *t)
1880{
1881 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1882
1883 if (unlikely(t->index > 0))
1884 return -EINVAL;
1885
1886 strcpy(t->name, "Radio");
1887 t->type = V4L2_TUNER_RADIO;
1888
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001889 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001890
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001891 return 0;
1892}
1893
1894static int radio_enum_input(struct file *file, void *priv,
1895 struct v4l2_input *i)
1896{
1897 if (i->index != 0)
1898 return -EINVAL;
1899 strcpy(i->name, "Radio");
1900 i->type = V4L2_INPUT_TYPE_TUNER;
1901
1902 return 0;
1903}
1904
1905static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1906{
1907 if (unlikely(a->index))
1908 return -EINVAL;
1909
1910 strcpy(a->name, "Radio");
1911 return 0;
1912}
1913
1914static int radio_s_tuner(struct file *file, void *priv,
1915 struct v4l2_tuner *t)
1916{
1917 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1918
1919 if (0 != t->index)
1920 return -EINVAL;
1921
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001922 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001923
1924 return 0;
1925}
1926
1927static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001928 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001929{
1930 return 0;
1931}
1932
1933static int radio_s_input(struct file *file, void *fh, unsigned int i)
1934{
1935 return 0;
1936}
1937
1938static int radio_queryctrl(struct file *file, void *priv,
1939 struct v4l2_queryctrl *qc)
1940{
1941 int i;
1942
1943 if (qc->id < V4L2_CID_BASE ||
1944 qc->id >= V4L2_CID_LASTP1)
1945 return -EINVAL;
1946
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001947 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1948 if (qc->id && qc->id == ac97_qctrl[i].id) {
1949 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001950 return 0;
1951 }
1952 }
1953
1954 return -EINVAL;
1955}
1956
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001957/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001958 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001959 * inits the device and starts isoc transfer
1960 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001961static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001962{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001963 int errCode = 0, radio = 0;
1964 struct video_device *vdev = video_devdata(filp);
1965 struct em28xx *dev = video_drvdata(filp);
1966 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001967 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001968 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001969
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001970 switch (vdev->vfl_type) {
1971 case VFL_TYPE_GRABBER:
1972 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1973 break;
1974 case VFL_TYPE_VBI:
1975 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1976 break;
1977 case VFL_TYPE_RADIO:
1978 radio = 1;
1979 break;
1980 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001981
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001982 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1983 video_device_node_name(vdev), v4l2_type_names[fh_type],
1984 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001985
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001986
Hans Verkuil876cb142012-06-23 08:12:47 -03001987 if (mutex_lock_interruptible(&dev->lock))
1988 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001989 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001990 if (!fh) {
1991 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001992 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001993 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001994 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001995 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001996 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001997 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001998 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001999
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002000 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002001 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002002 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002003 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002004
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002005 /* Needed, since GPIO might have disabled power of
2006 some i2c device
2007 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002008 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002009
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002010 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002011 if (fh->radio) {
2012 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002013 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002014 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002015
2016 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002017
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002018 if (dev->progressive)
2019 field = V4L2_FIELD_NONE;
2020 else
2021 field = V4L2_FIELD_INTERLACED;
2022
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002023 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2024 NULL, &dev->slock,
2025 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002026 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002027
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002028 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2029 NULL, &dev->slock,
2030 V4L2_BUF_TYPE_VBI_CAPTURE,
2031 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002032 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002033 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002034
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002035 return errCode;
2036}
2037
2038/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002039 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002040 * unregisters the v4l2,i2c and usb devices
2041 * called when the device gets disconected or at module unload
2042*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002043void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002044{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002045
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002046 /*FIXME: I2C IR should be disconnected */
2047
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002048 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002049 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002050 video_unregister_device(dev->radio_dev);
2051 else
2052 video_device_release(dev->radio_dev);
2053 dev->radio_dev = NULL;
2054 }
2055 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002056 em28xx_info("V4L2 device %s deregistered\n",
2057 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002058 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002059 video_unregister_device(dev->vbi_dev);
2060 else
2061 video_device_release(dev->vbi_dev);
2062 dev->vbi_dev = NULL;
2063 }
2064 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002065 em28xx_info("V4L2 device %s deregistered\n",
2066 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002067 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002068 video_unregister_device(dev->vdev);
2069 else
2070 video_device_release(dev->vdev);
2071 dev->vdev = NULL;
2072 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073}
2074
2075/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002076 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002077 * stops streaming and deallocates all resources allocated by the v4l2
2078 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002079 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002080static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002081{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002082 struct em28xx_fh *fh = filp->private_data;
2083 struct em28xx *dev = fh->dev;
2084 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002085
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002086 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087
Hans Verkuil876cb142012-06-23 08:12:47 -03002088 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002089 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002090 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002091 res_free(fh, EM28XX_RESOURCE_VIDEO);
2092 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002094 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2095 videobuf_stop(&fh->vb_vbiq);
2096 res_free(fh, EM28XX_RESOURCE_VBI);
2097 }
2098
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002099 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002100 /* the device is already disconnect,
2101 free the remaining resources */
2102 if (dev->state & DEV_DISCONNECTED) {
2103 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002104 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002105 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002106 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002107 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002108 return 0;
2109 }
2110
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002111 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002112 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002113
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002114 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002115 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002116 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002117
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002118 /* set alternate 0 */
2119 dev->alt = 0;
2120 em28xx_videodbg("setting alternate 0\n");
2121 errCode = usb_set_interface(dev->udev, 0, 0);
2122 if (errCode < 0) {
2123 em28xx_errdev("cannot change alternate number to "
2124 "0 (error=%i)\n", errCode);
2125 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002127
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002128 videobuf_mmap_free(&fh->vb_vidq);
2129 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002130 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002131 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002132 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133 return 0;
2134}
2135
2136/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002137 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002138 * will allocate buffers when called for the first time
2139 */
2140static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002141em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002142 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002143{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002144 struct em28xx_fh *fh = filp->private_data;
2145 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002146 int rc;
2147
2148 rc = check_dev(dev);
2149 if (rc < 0)
2150 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002151
Hans Verkuil876cb142012-06-23 08:12:47 -03002152 if (mutex_lock_interruptible(&dev->lock))
2153 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002154 /* FIXME: read() is not prepared to allow changing the video
2155 resolution while streaming. Seems a bug at em28xx_set_fmt
2156 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002157
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002158 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002159 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002160 rc = -EBUSY;
2161 else
2162 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002163 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002164 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002165 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002166 rc = -EBUSY;
2167 else
2168 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002169 filp->f_flags & O_NONBLOCK);
2170 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002171 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002172
Hans Verkuil876cb142012-06-23 08:12:47 -03002173 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174}
2175
2176/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002177 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002178 * will allocate buffers when called for the first time
2179 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002180static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002182 struct em28xx_fh *fh = filp->private_data;
2183 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002184 int rc;
2185
2186 rc = check_dev(dev);
2187 if (rc < 0)
2188 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002190 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2191 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2192 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002193 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002194 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2195 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2196 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002197 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002198 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002199 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002200 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002201}
2202
Hans Verkuil876cb142012-06-23 08:12:47 -03002203static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2204{
2205 struct em28xx_fh *fh = filp->private_data;
2206 struct em28xx *dev = fh->dev;
2207 unsigned int res;
2208
2209 mutex_lock(&dev->lock);
2210 res = em28xx_poll(filp, wait);
2211 mutex_unlock(&dev->lock);
2212 return res;
2213}
2214
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002215/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002216 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002217 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002218static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002219{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002220 struct em28xx_fh *fh = filp->private_data;
2221 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002222 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002224 rc = check_dev(dev);
2225 if (rc < 0)
2226 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002227
Hans Verkuil876cb142012-06-23 08:12:47 -03002228 if (mutex_lock_interruptible(&dev->lock))
2229 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002230 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2231 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2232 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2233 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002234 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002235
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002236 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2237 (unsigned long)vma->vm_start,
2238 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2239 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002241 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242}
2243
Hans Verkuilbec43662008-12-30 06:58:20 -03002244static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002245 .owner = THIS_MODULE,
2246 .open = em28xx_v4l2_open,
2247 .release = em28xx_v4l2_close,
2248 .read = em28xx_v4l2_read,
2249 .poll = em28xx_v4l2_poll,
2250 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002251 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002252};
2253
Hans Verkuila3998102008-07-21 02:57:38 -03002254static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002255 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002256 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2257 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2258 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2259 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002260 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2261 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002262 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002263 .vidioc_g_audio = vidioc_g_audio,
2264 .vidioc_s_audio = vidioc_s_audio,
2265 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002266 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2267 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2268 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002269
2270 .vidioc_reqbufs = vidioc_reqbufs,
2271 .vidioc_querybuf = vidioc_querybuf,
2272 .vidioc_qbuf = vidioc_qbuf,
2273 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002274 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002275 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002276 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002277 .vidioc_g_parm = vidioc_g_parm,
2278 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002279 .vidioc_enum_input = vidioc_enum_input,
2280 .vidioc_g_input = vidioc_g_input,
2281 .vidioc_s_input = vidioc_s_input,
2282 .vidioc_queryctrl = vidioc_queryctrl,
2283 .vidioc_g_ctrl = vidioc_g_ctrl,
2284 .vidioc_s_ctrl = vidioc_s_ctrl,
2285 .vidioc_streamon = vidioc_streamon,
2286 .vidioc_streamoff = vidioc_streamoff,
2287 .vidioc_g_tuner = vidioc_g_tuner,
2288 .vidioc_s_tuner = vidioc_s_tuner,
2289 .vidioc_g_frequency = vidioc_g_frequency,
2290 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002291#ifdef CONFIG_VIDEO_ADV_DEBUG
2292 .vidioc_g_register = vidioc_g_register,
2293 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002294 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002295#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002296};
2297
2298static const struct video_device em28xx_video_template = {
2299 .fops = &em28xx_v4l_fops,
2300 .release = video_device_release,
2301 .ioctl_ops = &video_ioctl_ops,
2302
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002303 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002304 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002305};
2306
Hans Verkuilbec43662008-12-30 06:58:20 -03002307static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002308 .owner = THIS_MODULE,
2309 .open = em28xx_v4l2_open,
2310 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002311 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002312};
2313
2314static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002315 .vidioc_querycap = radio_querycap,
2316 .vidioc_g_tuner = radio_g_tuner,
2317 .vidioc_enum_input = radio_enum_input,
2318 .vidioc_g_audio = radio_g_audio,
2319 .vidioc_s_tuner = radio_s_tuner,
2320 .vidioc_s_audio = radio_s_audio,
2321 .vidioc_s_input = radio_s_input,
2322 .vidioc_queryctrl = radio_queryctrl,
2323 .vidioc_g_ctrl = vidioc_g_ctrl,
2324 .vidioc_s_ctrl = vidioc_s_ctrl,
2325 .vidioc_g_frequency = vidioc_g_frequency,
2326 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002327#ifdef CONFIG_VIDEO_ADV_DEBUG
2328 .vidioc_g_register = vidioc_g_register,
2329 .vidioc_s_register = vidioc_s_register,
2330#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002331};
2332
Hans Verkuila3998102008-07-21 02:57:38 -03002333static struct video_device em28xx_radio_template = {
2334 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002335 .fops = &radio_fops,
2336 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002337};
2338
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002339/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002340
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002341
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002342
Adrian Bunk532fe652008-01-28 22:10:48 -03002343static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002344 const struct video_device *template,
2345 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002346{
2347 struct video_device *vfd;
2348
2349 vfd = video_device_alloc();
2350 if (NULL == vfd)
2351 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002352
2353 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002354 vfd->v4l2_dev = &dev->v4l2_dev;
2355 vfd->release = video_device_release;
2356 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002357 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002358
2359 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2360 dev->name, type_name);
2361
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002362 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002363 return vfd;
2364}
2365
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002366int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002367{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002368 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002369 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002370 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002371
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002372 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2373 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002374
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002375 /* set default norm */
2376 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002377 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002378 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002379
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002380 /* Analog specific initialization */
2381 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002382
2383 maxw = norm_maxw(dev);
2384 /* MaxPacketSize for em2800 is too small to capture at full resolution
2385 * use half of maxw as the scaler can only scale to 50% */
2386 if (dev->board.is_em2800)
2387 maxw /= 2;
2388
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002389 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002390 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002391
Ezequiel García96371fc2012-03-23 18:09:34 -03002392 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002393
2394 /* Audio defaults */
2395 dev->mute = 1;
2396 dev->volume = 0x1f;
2397
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002398/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002399 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2400 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2401 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002402
2403 em28xx_set_outfmt(dev);
2404 em28xx_colorlevels_set_default(dev);
2405 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002406
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002407 /* allocate and fill video video_device struct */
2408 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2409 if (!dev->vdev) {
2410 em28xx_errdev("cannot allocate video_device.\n");
2411 return -ENODEV;
2412 }
2413
2414 /* register v4l2 video video_device */
2415 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2416 video_nr[dev->devno]);
2417 if (ret) {
2418 em28xx_errdev("unable to register video device (error=%i).\n",
2419 ret);
2420 return ret;
2421 }
2422
2423 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002424 if (em28xx_vbi_supported(dev) == 1) {
2425 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2426 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002427
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002428 /* register v4l2 vbi video_device */
2429 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2430 vbi_nr[dev->devno]);
2431 if (ret < 0) {
2432 em28xx_errdev("unable to register vbi device\n");
2433 return ret;
2434 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002435 }
2436
2437 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002438 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2439 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002440 if (!dev->radio_dev) {
2441 em28xx_errdev("cannot allocate video_device.\n");
2442 return -ENODEV;
2443 }
2444 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2445 radio_nr[dev->devno]);
2446 if (ret < 0) {
2447 em28xx_errdev("can't register radio device\n");
2448 return ret;
2449 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002450 em28xx_info("Registered radio device as %s\n",
2451 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002452 }
2453
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002454 em28xx_info("V4L2 video device registered as %s\n",
2455 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002456
2457 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002458 em28xx_info("V4L2 VBI device registered as %s\n",
2459 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002460
2461 return 0;
2462}