blob: db27499ecaa233de37a7e1f2db8c4b89b8addff0 [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/*
157 * Announces that a buffer were filled and request the next
158 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300159static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
162{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300167 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer74209dc2012-11-08 14:11:37 -0300169 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
175static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
178{
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300184 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300185
Frank Schaefer74209dc2012-11-08 14:11:37 -0300186 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
190}
191
192/*
193 * Identify the buffer header type and properly handles
194 */
195static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
198 unsigned char *p,
199 unsigned char *outp, unsigned long len)
200{
201 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300208 startread = p;
209 remain = len;
210
Frank Schaeferc02ec712012-11-08 14:11:33 -0300211 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300218
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
221 else
222 offset = linesdone * bytesperline * 2 + currlinedone;
223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = lencopy > remain ? remain : lencopy;
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300234 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300235 if (lencopy <= 0)
236 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300238
239 remain -= lencopy;
240
241 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300242 if (dev->progressive)
243 startwrite += lencopy;
244 else
245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300362 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300363 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300364static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
365 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300367 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300372 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 }
374
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300376 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300377 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300378 outp = videobuf_to_vmalloc(&buf->vb);
379 memset(outp, 0, buf->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300380
Frank Schaefer24a6d842012-12-08 11:31:24 -0300381 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300382}
383
Frank Schaefer960da932012-11-25 06:37:37 -0300384/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300385static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300386{
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300387 struct em28xx_buffer *buf, *vbi_buf;
388 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300389 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer79ff8692012-11-25 06:37:36 -0300390 int xfer_bulk, num_packets, i, rc = 1;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300391 unsigned int actual_length, len = 0;
392 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300393
394 if (!dev)
395 return 0;
396
397 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
398 return 0;
399
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300400 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300401 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300402
Frank Schaefer4601cc32012-11-08 14:11:46 -0300403 xfer_bulk = usb_pipebulk(urb->pipe);
404
Frank Schaefer74209dc2012-11-08 14:11:37 -0300405 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300406 if (buf != NULL)
407 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300408
Frank Schaefer74209dc2012-11-08 14:11:37 -0300409 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300410 if (vbi_buf != NULL)
411 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
412
Frank Schaefer4601cc32012-11-08 14:11:46 -0300413 if (xfer_bulk) /* bulk */
414 num_packets = 1;
415 else /* isoc */
416 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300417
Frank Schaefer4601cc32012-11-08 14:11:46 -0300418 for (i = 0; i < num_packets; i++) {
419 if (xfer_bulk) { /* bulk */
420 actual_length = urb->actual_length;
421
422 p = urb->transfer_buffer;
423 } else { /* isoc */
424 if (urb->iso_frame_desc[i].status < 0) {
425 print_err_status(dev, i,
426 urb->iso_frame_desc[i].status);
427 if (urb->iso_frame_desc[i].status != -EPROTO)
428 continue;
429 }
430
431 actual_length = urb->iso_frame_desc[i].actual_length;
432 if (actual_length > dev->max_pkt_size) {
433 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300434 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300435 }
436
437 p = urb->transfer_buffer +
438 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300439 }
440
Frank Schaefer3610f582012-11-25 06:37:33 -0300441 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300442 /* NOTE: happens very often with isoc transfers */
443 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300444 continue;
445 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300446
447 /* capture type 0 = vbi start
448 capture type 1 = video start
449 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300450 len = actual_length;
451 if (len >= 4) {
452 /* NOTE: headers are always 4 bytes and
453 * never split across packets */
454 if (p[0] == 0x33 && p[1] == 0x95) {
455 dev->capture_type = 0;
456 dev->vbi_read = 0;
457 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300458 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300459 p += 4;
460 len -= 4;
461 } else if (p[0] == 0x88 && p[1] == 0x88 &&
462 p[2] == 0x88 && p[3] == 0x88) {
463 /* continuation */
464 p += 4;
465 len -= 4;
466 } else if (p[0] == 0x22 && p[1] == 0x5a) {
467 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300468 dev->capture_type = 1;
469 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300470 p += 4;
471 len -= 4;
472 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300473 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300474 /* NOTE: with bulk transfers, intermediate data packets
475 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300476
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300477 if (dev->capture_type == 0) {
Frank Schaefer79ff8692012-11-25 06:37:36 -0300478 int vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300479 if (dev->vbi_read >= vbi_size) {
480 /* We've already read all the VBI data, so
481 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300482 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300483 } else if ((dev->vbi_read + len) < vbi_size) {
484 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300485 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300486 /* Brand new frame */
487 if (vbi_buf != NULL)
488 vbi_buffer_filled(dev,
489 vbi_dma_q,
490 vbi_buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300491 vbi_buf = get_next_buf(dev, vbi_dma_q);
492 dev->usb_ctl.vbi_buf = vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300493 if (vbi_buf == NULL)
494 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300495 else
496 vbioutp = videobuf_to_vmalloc(
497 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300498 }
499
500 if (dev->vbi_read == 0) {
501 vbi_dma_q->pos = 0;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300502 if (vbi_buf != NULL)
Frank Schaefer24a6d842012-12-08 11:31:24 -0300503 vbi_buf->top_field
504 = dev->top_field;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300505 }
506
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300507 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300508 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
509 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300510 } else {
511 /* Some of this frame is VBI data and some is
512 video data */
513 int vbi_data_len = vbi_size - dev->vbi_read;
514 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300515 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
516 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300517 dev->capture_type = 1;
518 p += vbi_data_len;
519 len -= vbi_data_len;
520 }
521 }
522
523 if (dev->capture_type == 1) {
524 dev->capture_type = 2;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300525 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300526 if (buf != NULL)
527 buffer_filled(dev, dma_q, buf);
Frank Schaefer24a6d842012-12-08 11:31:24 -0300528 buf = get_next_buf(dev, dma_q);
529 dev->usb_ctl.vid_buf = buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300530 if (buf == NULL)
531 outp = NULL;
532 else
533 outp = videobuf_to_vmalloc(&buf->vb);
534 }
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300535 if (buf != NULL)
536 buf->top_field = dev->top_field;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300537
538 dma_q->pos = 0;
539 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300540
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300541 if (buf != NULL && dev->capture_type == 2 && len > 0)
542 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300543 }
544 return rc;
545}
546
547
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300548/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300549 Videobuf operations
550 ------------------------------------------------------------------*/
551
552static int
553buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
554{
555 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300556 struct em28xx *dev = fh->dev;
557 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300558
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300559 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
560 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300561
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300562 if (0 == *count)
563 *count = EM28XX_DEF_BUF;
564
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300565 if (*count < EM28XX_MIN_BUF)
566 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300567
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300568 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300569 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300570 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300571 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300572
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300573 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300574
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300575 return 0;
576}
577
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300578/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300579static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
580{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300581 struct em28xx_fh *fh = vq->priv_data;
582 struct em28xx *dev = fh->dev;
583 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300584 if (in_interrupt())
585 BUG();
586
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300587 /* We used to wait for the buffer to finish here, but this didn't work
588 because, as we were keeping the state as VIDEOBUF_QUEUED,
589 videobuf_queue_cancel marked it as finished for us.
590 (Also, it could wedge forever if the hardware was misconfigured.)
591
592 This should be safe; by the time we get here, the buffer isn't
593 queued anymore. If we ever start marking the buffers as
594 VIDEOBUF_ACTIVE, it won't be, though.
595 */
596 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300597 if (dev->usb_ctl.vid_buf == buf)
598 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300599 spin_unlock_irqrestore(&dev->slock, flags);
600
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300601 videobuf_vmalloc_free(&buf->vb);
602 buf->vb.state = VIDEOBUF_NEEDS_INIT;
603}
604
605static int
606buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
607 enum v4l2_field field)
608{
609 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300610 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300611 struct em28xx *dev = fh->dev;
612 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300613
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300614 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
615 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300616
617 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
618 return -EINVAL;
619
Brandon Philips05612972008-04-13 14:57:01 -0300620 buf->vb.width = dev->width;
621 buf->vb.height = dev->height;
622 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300623
624 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300625 rc = videobuf_iolock(vq, &buf->vb, NULL);
626 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300627 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300628 }
629
Frank Schaefer74209dc2012-11-08 14:11:37 -0300630 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300631 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632
633 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300634 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300635 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
636 dev->analog_xfer_bulk,
637 EM28XX_NUM_BUFS,
638 dev->max_pkt_size,
639 dev->packet_multiplier,
640 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300641 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300642 goto fail;
643 }
644
645 buf->vb.state = VIDEOBUF_PREPARED;
646 return 0;
647
648fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300649 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650 return rc;
651}
652
653static void
654buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
655{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300656 struct em28xx_buffer *buf = container_of(vb,
657 struct em28xx_buffer,
658 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300659 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300660 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300662
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300663 buf->vb.state = VIDEOBUF_QUEUED;
664 list_add_tail(&buf->vb.queue, &vidq->active);
665
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300666}
667
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300668static void buffer_release(struct videobuf_queue *vq,
669 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300670{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300671 struct em28xx_buffer *buf = container_of(vb,
672 struct em28xx_buffer,
673 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300674 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300675 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300676
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300677 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300678
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300679 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680}
681
682static struct videobuf_queue_ops em28xx_video_qops = {
683 .buf_setup = buffer_setup,
684 .buf_prepare = buffer_prepare,
685 .buf_queue = buffer_queue,
686 .buf_release = buffer_release,
687};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800688
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300689/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800690
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800691static void video_mux(struct em28xx *dev, int index)
692{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800693 dev->ctl_input = index;
694 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300695 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800696
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300697 if (!dev->ctl_aoutput)
698 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
699
Hans Verkuil5325b422009-04-02 11:26:22 -0300700 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
701 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800702
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300703 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300704 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300705 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
706 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300707 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300708 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300709 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
710 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800711 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300712
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300713 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300714 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
715 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300716 }
717
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300718 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800719}
720
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300721/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300722static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300723{
724 struct em28xx *dev = fh->dev;
725
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300726 if (fh->resources & bit)
727 /* have it already allocated */
728 return 1;
729
730 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300731 if (dev->resources & bit) {
732 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300733 return 0;
734 }
735 /* it's free, grab it */
736 fh->resources |= bit;
737 dev->resources |= bit;
738 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300739 return 1;
740}
741
742static int res_check(struct em28xx_fh *fh, unsigned int bit)
743{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300744 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300745}
746
747static int res_locked(struct em28xx *dev, unsigned int bit)
748{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300749 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300750}
751
752static void res_free(struct em28xx_fh *fh, unsigned int bits)
753{
754 struct em28xx *dev = fh->dev;
755
756 BUG_ON((fh->resources & bits) != bits);
757
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300758 fh->resources &= ~bits;
759 dev->resources &= ~bits;
760 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300761}
762
763static int get_ressource(struct em28xx_fh *fh)
764{
765 switch (fh->type) {
766 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
767 return EM28XX_RESOURCE_VIDEO;
768 case V4L2_BUF_TYPE_VBI_CAPTURE:
769 return EM28XX_RESOURCE_VBI;
770 default:
771 BUG();
772 return 0;
773 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300774}
775
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800776/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300777 * ac97_queryctrl()
778 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300779 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300780static int ac97_queryctrl(struct v4l2_queryctrl *qc)
781{
782 int i;
783
784 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
785 if (qc->id && qc->id == ac97_qctrl[i].id) {
786 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
787 return 0;
788 }
789 }
790
791 /* Control is not ac97 related */
792 return 1;
793}
794
795/*
796 * ac97_get_ctrl()
797 * return the current values for ac97 mute and volume
798 */
799static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300800{
801 switch (ctrl->id) {
802 case V4L2_CID_AUDIO_MUTE:
803 ctrl->value = dev->mute;
804 return 0;
805 case V4L2_CID_AUDIO_VOLUME:
806 ctrl->value = dev->volume;
807 return 0;
808 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300809 /* Control is not ac97 related */
810 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 }
812}
813
814/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300815 * ac97_set_ctrl()
816 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300818static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300819{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300820 int i;
821
822 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
823 if (ctrl->id == ac97_qctrl[i].id)
824 goto handle;
825
826 /* Announce that hasn't handle it */
827 return 1;
828
829handle:
830 if (ctrl->value < ac97_qctrl[i].minimum ||
831 ctrl->value > ac97_qctrl[i].maximum)
832 return -ERANGE;
833
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300834 switch (ctrl->id) {
835 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300836 dev->mute = ctrl->value;
837 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300838 case V4L2_CID_AUDIO_VOLUME:
839 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300840 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300842
843 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300844}
845
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300846static int check_dev(struct em28xx *dev)
847{
848 if (dev->state & DEV_DISCONNECTED) {
849 em28xx_errdev("v4l2 ioctl: device not present\n");
850 return -ENODEV;
851 }
852
853 if (dev->state & DEV_MISCONFIGURED) {
854 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
855 "close and open it again\n");
856 return -EIO;
857 }
858 return 0;
859}
860
861static void get_scale(struct em28xx *dev,
862 unsigned int width, unsigned int height,
863 unsigned int *hscale, unsigned int *vscale)
864{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300865 unsigned int maxw = norm_maxw(dev);
866 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300867
868 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
869 if (*hscale >= 0x4000)
870 *hscale = 0x3fff;
871
872 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
873 if (*vscale >= 0x4000)
874 *vscale = 0x3fff;
875}
876
877/* ------------------------------------------------------------------
878 IOCTL vidioc handling
879 ------------------------------------------------------------------*/
880
Hans Verkuil78b526a2008-05-28 12:16:41 -0300881static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300882 struct v4l2_format *f)
883{
884 struct em28xx_fh *fh = priv;
885 struct em28xx *dev = fh->dev;
886
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300887 f->fmt.pix.width = dev->width;
888 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300889 f->fmt.pix.pixelformat = dev->format->fourcc;
890 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300891 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300892 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
893
894 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300895 if (dev->progressive)
896 f->fmt.pix.field = V4L2_FIELD_NONE;
897 else
898 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300899 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300900 return 0;
901}
902
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300903static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
904{
905 unsigned int i;
906
907 for (i = 0; i < ARRAY_SIZE(format); i++)
908 if (format[i].fourcc == fourcc)
909 return &format[i];
910
911 return NULL;
912}
913
Hans Verkuil78b526a2008-05-28 12:16:41 -0300914static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300915 struct v4l2_format *f)
916{
917 struct em28xx_fh *fh = priv;
918 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300919 unsigned int width = f->fmt.pix.width;
920 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300921 unsigned int maxw = norm_maxw(dev);
922 unsigned int maxh = norm_maxh(dev);
923 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300924 struct em28xx_fmt *fmt;
925
926 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
927 if (!fmt) {
928 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
929 f->fmt.pix.pixelformat);
930 return -EINVAL;
931 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300933 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300935 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
936 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300937 /* MaxPacketSize for em2800 is too small to capture at full resolution
938 * use half of maxw as the scaler can only scale to 50% */
939 if (width == maxw && height == maxh)
940 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300941 } else {
942 /* width must even because of the YUYV format
943 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300944 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
945 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300946 }
947
948 get_scale(dev, width, height, &hscale, &vscale);
949
950 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
951 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
952
953 f->fmt.pix.width = width;
954 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300955 f->fmt.pix.pixelformat = fmt->fourcc;
956 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
957 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300958 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300959 if (dev->progressive)
960 f->fmt.pix.field = V4L2_FIELD_NONE;
961 else
962 f->fmt.pix.field = dev->interlaced ?
963 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965 return 0;
966}
967
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300968static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
969 unsigned width, unsigned height)
970{
971 struct em28xx_fmt *fmt;
972
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300973 fmt = format_by_fourcc(fourcc);
974 if (!fmt)
975 return -EINVAL;
976
977 dev->format = fmt;
978 dev->width = width;
979 dev->height = height;
980
981 /* set new image size */
982 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
983
984 em28xx_set_alternate(dev);
985 em28xx_resolution_set(dev);
986
987 return 0;
988}
989
Hans Verkuil78b526a2008-05-28 12:16:41 -0300990static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991 struct v4l2_format *f)
992{
993 struct em28xx_fh *fh = priv;
994 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300995 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300996
997 rc = check_dev(dev);
998 if (rc < 0)
999 return rc;
1000
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001001 vidioc_try_fmt_vid_cap(file, priv, f);
1002
Brandon Philips05612972008-04-13 14:57:01 -03001003 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1004 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001005 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001006 }
1007
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001008 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001009 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001010}
1011
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001012static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1013{
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001016 int rc;
1017
1018 rc = check_dev(dev);
1019 if (rc < 0)
1020 return rc;
1021
1022 *norm = dev->norm;
1023
1024 return 0;
1025}
1026
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001027static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1028{
1029 struct em28xx_fh *fh = priv;
1030 struct em28xx *dev = fh->dev;
1031 int rc;
1032
1033 rc = check_dev(dev);
1034 if (rc < 0)
1035 return rc;
1036
1037 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1038
1039 return 0;
1040}
1041
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001042static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043{
1044 struct em28xx_fh *fh = priv;
1045 struct em28xx *dev = fh->dev;
1046 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001047 int rc;
1048
1049 rc = check_dev(dev);
1050 if (rc < 0)
1051 return rc;
1052
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001053 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001054
1055 /* Adjusts width/height, if needed */
1056 f.fmt.pix.width = dev->width;
1057 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001058 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001060 /* set new image size */
1061 dev->width = f.fmt.pix.width;
1062 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001063 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1064
1065 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001066 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001067
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068 return 0;
1069}
1070
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001071static int vidioc_g_parm(struct file *file, void *priv,
1072 struct v4l2_streamparm *p)
1073{
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1076 int rc = 0;
1077
1078 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1079 return -EINVAL;
1080
1081 if (dev->board.is_webcam)
1082 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1083 video, g_parm, p);
1084 else
1085 v4l2_video_std_frame_period(dev->norm,
1086 &p->parm.capture.timeperframe);
1087
1088 return rc;
1089}
1090
1091static int vidioc_s_parm(struct file *file, void *priv,
1092 struct v4l2_streamparm *p)
1093{
1094 struct em28xx_fh *fh = priv;
1095 struct em28xx *dev = fh->dev;
1096
1097 if (!dev->board.is_webcam)
1098 return -EINVAL;
1099
1100 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1101 return -EINVAL;
1102
1103 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1104}
1105
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001106static const char *iname[] = {
1107 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1108 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1109 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1110 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1111 [EM28XX_VMUX_SVIDEO] = "S-Video",
1112 [EM28XX_VMUX_TELEVISION] = "Television",
1113 [EM28XX_VMUX_CABLE] = "Cable TV",
1114 [EM28XX_VMUX_DVB] = "DVB",
1115 [EM28XX_VMUX_DEBUG] = "for debug only",
1116};
1117
1118static int vidioc_enum_input(struct file *file, void *priv,
1119 struct v4l2_input *i)
1120{
1121 struct em28xx_fh *fh = priv;
1122 struct em28xx *dev = fh->dev;
1123 unsigned int n;
1124
1125 n = i->index;
1126 if (n >= MAX_EM28XX_INPUT)
1127 return -EINVAL;
1128 if (0 == INPUT(n)->type)
1129 return -EINVAL;
1130
1131 i->index = n;
1132 i->type = V4L2_INPUT_TYPE_CAMERA;
1133
1134 strcpy(i->name, iname[INPUT(n)->type]);
1135
1136 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1137 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1138 i->type = V4L2_INPUT_TYPE_TUNER;
1139
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001140 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141
1142 return 0;
1143}
1144
1145static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1146{
1147 struct em28xx_fh *fh = priv;
1148 struct em28xx *dev = fh->dev;
1149
1150 *i = dev->ctl_input;
1151
1152 return 0;
1153}
1154
1155static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1156{
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 int rc;
1160
1161 rc = check_dev(dev);
1162 if (rc < 0)
1163 return rc;
1164
1165 if (i >= MAX_EM28XX_INPUT)
1166 return -EINVAL;
1167 if (0 == INPUT(i)->type)
1168 return -EINVAL;
1169
Ezequiel García96371fc2012-03-23 18:09:34 -03001170 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001171 return 0;
1172}
1173
1174static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1175{
1176 struct em28xx_fh *fh = priv;
1177 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001178
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001179 if (!dev->audio_mode.has_audio)
1180 return -EINVAL;
1181
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001182 switch (a->index) {
1183 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001185 break;
1186 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001187 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001188 break;
1189 case EM28XX_AMUX_VIDEO2:
1190 strcpy(a->name, "Television alt");
1191 break;
1192 case EM28XX_AMUX_PHONE:
1193 strcpy(a->name, "Phone");
1194 break;
1195 case EM28XX_AMUX_MIC:
1196 strcpy(a->name, "Mic");
1197 break;
1198 case EM28XX_AMUX_CD:
1199 strcpy(a->name, "CD");
1200 break;
1201 case EM28XX_AMUX_AUX:
1202 strcpy(a->name, "Aux");
1203 break;
1204 case EM28XX_AMUX_PCM_OUT:
1205 strcpy(a->name, "PCM");
1206 break;
1207 default:
1208 return -EINVAL;
1209 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001210
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001211 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001212 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001213
1214 return 0;
1215}
1216
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001217static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001218{
1219 struct em28xx_fh *fh = priv;
1220 struct em28xx *dev = fh->dev;
1221
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001222
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001223 if (!dev->audio_mode.has_audio)
1224 return -EINVAL;
1225
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001226 if (a->index >= MAX_EM28XX_INPUT)
1227 return -EINVAL;
1228 if (0 == INPUT(a->index)->type)
1229 return -EINVAL;
1230
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001231 dev->ctl_ainput = INPUT(a->index)->amux;
1232 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001233
1234 if (!dev->ctl_aoutput)
1235 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001236
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001237 return 0;
1238}
1239
1240static int vidioc_queryctrl(struct file *file, void *priv,
1241 struct v4l2_queryctrl *qc)
1242{
1243 struct em28xx_fh *fh = priv;
1244 struct em28xx *dev = fh->dev;
1245 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001246 int rc;
1247
1248 rc = check_dev(dev);
1249 if (rc < 0)
1250 return rc;
1251
1252 memset(qc, 0, sizeof(*qc));
1253
1254 qc->id = id;
1255
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001256 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001257 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1258 rc = ac97_queryctrl(qc);
1259 if (!rc)
1260 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001261 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001262
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001263 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001264 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001265
1266 if (qc->type)
1267 return 0;
1268 else
1269 return -EINVAL;
1270}
1271
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001272/*
1273 * FIXME: This is an indirect way to check if a control exists at a
1274 * subdev. Instead of that hack, maybe the better would be to change all
1275 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1276 */
1277static int check_subdev_ctrl(struct em28xx *dev, int id)
1278{
1279 struct v4l2_queryctrl qc;
1280
1281 memset(&qc, 0, sizeof(qc));
1282 qc.id = id;
1283
1284 /* enumerate V4L2 device controls */
1285 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1286
1287 if (qc.type)
1288 return 0;
1289 else
1290 return -EINVAL;
1291}
1292
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001293static int vidioc_g_ctrl(struct file *file, void *priv,
1294 struct v4l2_control *ctrl)
1295{
1296 struct em28xx_fh *fh = priv;
1297 struct em28xx *dev = fh->dev;
1298 int rc;
1299
1300 rc = check_dev(dev);
1301 if (rc < 0)
1302 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001303 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001304
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001305 /* Set an AC97 control */
1306 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1307 rc = ac97_get_ctrl(dev, ctrl);
1308 else
1309 rc = 1;
1310
1311 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1312 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001313 if (check_subdev_ctrl(dev, ctrl->id))
1314 return -EINVAL;
1315
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001316 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001317 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001318 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001319
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001320 return rc;
1321}
1322
1323static int vidioc_s_ctrl(struct file *file, void *priv,
1324 struct v4l2_control *ctrl)
1325{
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328 int rc;
1329
1330 rc = check_dev(dev);
1331 if (rc < 0)
1332 return rc;
1333
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001334 /* Set an AC97 control */
1335 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1336 rc = ac97_set_ctrl(dev, ctrl);
1337 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001338 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001339
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001340 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001341 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001342 rc = check_subdev_ctrl(dev, ctrl->id);
1343 if (!rc)
1344 v4l2_device_call_all(&dev->v4l2_dev, 0,
1345 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001346 /*
1347 * In the case of non-AC97 volume controls, we still need
1348 * to do some setups at em28xx, in order to mute/unmute
1349 * and to adjust audio volume. However, the value ranges
1350 * should be checked by the corresponding V4L subdriver.
1351 */
1352 switch (ctrl->id) {
1353 case V4L2_CID_AUDIO_MUTE:
1354 dev->mute = ctrl->value;
1355 rc = em28xx_audio_analog_set(dev);
1356 break;
1357 case V4L2_CID_AUDIO_VOLUME:
1358 dev->volume = ctrl->value;
1359 rc = em28xx_audio_analog_set(dev);
1360 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001361 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001362 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363}
1364
1365static int vidioc_g_tuner(struct file *file, void *priv,
1366 struct v4l2_tuner *t)
1367{
1368 struct em28xx_fh *fh = priv;
1369 struct em28xx *dev = fh->dev;
1370 int rc;
1371
1372 rc = check_dev(dev);
1373 if (rc < 0)
1374 return rc;
1375
1376 if (0 != t->index)
1377 return -EINVAL;
1378
1379 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001380 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001382 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383 return 0;
1384}
1385
1386static int vidioc_s_tuner(struct file *file, void *priv,
1387 struct v4l2_tuner *t)
1388{
1389 struct em28xx_fh *fh = priv;
1390 struct em28xx *dev = fh->dev;
1391 int rc;
1392
1393 rc = check_dev(dev);
1394 if (rc < 0)
1395 return rc;
1396
1397 if (0 != t->index)
1398 return -EINVAL;
1399
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001400 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401 return 0;
1402}
1403
1404static int vidioc_g_frequency(struct file *file, void *priv,
1405 struct v4l2_frequency *f)
1406{
1407 struct em28xx_fh *fh = priv;
1408 struct em28xx *dev = fh->dev;
1409
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001410 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001412 return 0;
1413}
1414
1415static int vidioc_s_frequency(struct file *file, void *priv,
1416 struct v4l2_frequency *f)
1417{
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1420 int rc;
1421
1422 rc = check_dev(dev);
1423 if (rc < 0)
1424 return rc;
1425
1426 if (0 != f->tuner)
1427 return -EINVAL;
1428
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001429 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1430 return -EINVAL;
1431 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432 return -EINVAL;
1433
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001434 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001435 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001437 return 0;
1438}
1439
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001440#ifdef CONFIG_VIDEO_ADV_DEBUG
1441static int em28xx_reg_len(int reg)
1442{
1443 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001444 case EM28XX_R40_AC97LSB:
1445 case EM28XX_R30_HSCALELOW:
1446 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001447 return 2;
1448 default:
1449 return 1;
1450 }
1451}
1452
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001453static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001454 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001455{
1456 struct em28xx_fh *fh = priv;
1457 struct em28xx *dev = fh->dev;
1458
1459 chip->ident = V4L2_IDENT_NONE;
1460 chip->revision = 0;
1461
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001462 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001463
1464 return 0;
1465}
1466
1467
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001468static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001469 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001470{
1471 struct em28xx_fh *fh = priv;
1472 struct em28xx *dev = fh->dev;
1473 int ret;
1474
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001475 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001476 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001477 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001478 if (ret < 0)
1479 return ret;
1480
1481 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001482 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001483 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001484 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001485 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001486 return 0;
1487 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001488 /* TODO: is this correct? */
1489 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1490 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001491 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001492 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001493 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001494 }
1495
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001496 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001497 reg->size = em28xx_reg_len(reg->reg);
1498 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001499 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001500
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001501 if (ret < 0)
1502 return ret;
1503
1504 reg->val = ret;
1505 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001506 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001507 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1508 reg->reg, (char *)&val, 2);
1509 if (ret < 0)
1510 return ret;
1511
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001512 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001513 }
1514
1515 return 0;
1516}
1517
1518static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001519 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001520{
1521 struct em28xx_fh *fh = priv;
1522 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001523 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001524
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001525 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001526 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001527 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001528 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001529 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001530 return 0;
1531 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001532 /* TODO: is this correct? */
1533 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1534 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001535 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001536 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001537 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001538 }
1539
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001540 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001541 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001542
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001543 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001544 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001545}
1546#endif
1547
1548
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001549static int vidioc_cropcap(struct file *file, void *priv,
1550 struct v4l2_cropcap *cc)
1551{
1552 struct em28xx_fh *fh = priv;
1553 struct em28xx *dev = fh->dev;
1554
1555 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1556 return -EINVAL;
1557
1558 cc->bounds.left = 0;
1559 cc->bounds.top = 0;
1560 cc->bounds.width = dev->width;
1561 cc->bounds.height = dev->height;
1562 cc->defrect = cc->bounds;
1563 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1564 cc->pixelaspect.denominator = 59;
1565
1566 return 0;
1567}
1568
1569static int vidioc_streamon(struct file *file, void *priv,
1570 enum v4l2_buf_type type)
1571{
1572 struct em28xx_fh *fh = priv;
1573 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001574 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575
1576 rc = check_dev(dev);
1577 if (rc < 0)
1578 return rc;
1579
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001580 if (unlikely(type != fh->type))
1581 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001583 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1584 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001585
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001586 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001587 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001588
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001589 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1590 rc = videobuf_streamon(&fh->vb_vidq);
1591 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1592 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001593
1594 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595}
1596
1597static int vidioc_streamoff(struct file *file, void *priv,
1598 enum v4l2_buf_type type)
1599{
1600 struct em28xx_fh *fh = priv;
1601 struct em28xx *dev = fh->dev;
1602 int rc;
1603
1604 rc = check_dev(dev);
1605 if (rc < 0)
1606 return rc;
1607
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001608 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1609 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001610 return -EINVAL;
1611 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001612 return -EINVAL;
1613
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001614 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1615 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001616
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001617 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001618 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1619 videobuf_streamoff(&fh->vb_vidq);
1620 res_free(fh, EM28XX_RESOURCE_VIDEO);
1621 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001622 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001623 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1624 videobuf_streamoff(&fh->vb_vbiq);
1625 res_free(fh, EM28XX_RESOURCE_VBI);
1626 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001627 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001628
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001629 return 0;
1630}
1631
1632static int vidioc_querycap(struct file *file, void *priv,
1633 struct v4l2_capability *cap)
1634{
1635 struct em28xx_fh *fh = priv;
1636 struct em28xx *dev = fh->dev;
1637
1638 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1639 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001640 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001641
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001642 cap->capabilities =
1643 V4L2_CAP_SLICED_VBI_CAPTURE |
1644 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001645 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1646
Devin Heitmueller04146142009-09-11 00:08:44 -03001647 if (dev->vbi_dev)
1648 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1649
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001650 if (dev->audio_mode.has_audio)
1651 cap->capabilities |= V4L2_CAP_AUDIO;
1652
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001653 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001654 cap->capabilities |= V4L2_CAP_TUNER;
1655
1656 return 0;
1657}
1658
Hans Verkuil78b526a2008-05-28 12:16:41 -03001659static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001660 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001661{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001662 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001663 return -EINVAL;
1664
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001665 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1666 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001667
1668 return 0;
1669}
1670
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001671static int vidioc_enum_framesizes(struct file *file, void *priv,
1672 struct v4l2_frmsizeenum *fsize)
1673{
1674 struct em28xx_fh *fh = priv;
1675 struct em28xx *dev = fh->dev;
1676 struct em28xx_fmt *fmt;
1677 unsigned int maxw = norm_maxw(dev);
1678 unsigned int maxh = norm_maxh(dev);
1679
1680 fmt = format_by_fourcc(fsize->pixel_format);
1681 if (!fmt) {
1682 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1683 fsize->pixel_format);
1684 return -EINVAL;
1685 }
1686
1687 if (dev->board.is_em2800) {
1688 if (fsize->index > 1)
1689 return -EINVAL;
1690 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1691 fsize->discrete.width = maxw / (1 + fsize->index);
1692 fsize->discrete.height = maxh / (1 + fsize->index);
1693 return 0;
1694 }
1695
1696 if (fsize->index != 0)
1697 return -EINVAL;
1698
1699 /* Report a continuous range */
1700 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1701 fsize->stepwise.min_width = 48;
1702 fsize->stepwise.min_height = 32;
1703 fsize->stepwise.max_width = maxw;
1704 fsize->stepwise.max_height = maxh;
1705 fsize->stepwise.step_width = 1;
1706 fsize->stepwise.step_height = 1;
1707 return 0;
1708}
1709
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001710/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001711static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001712 struct v4l2_format *f)
1713{
1714 struct em28xx_fh *fh = priv;
1715 struct em28xx *dev = fh->dev;
1716 int rc;
1717
1718 rc = check_dev(dev);
1719 if (rc < 0)
1720 return rc;
1721
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001722 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001723 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001724
1725 if (f->fmt.sliced.service_set == 0)
1726 rc = -EINVAL;
1727
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001728 return rc;
1729}
1730
Hans Verkuil78b526a2008-05-28 12:16:41 -03001731static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001732 struct v4l2_format *f)
1733{
1734 struct em28xx_fh *fh = priv;
1735 struct em28xx *dev = fh->dev;
1736 int rc;
1737
1738 rc = check_dev(dev);
1739 if (rc < 0)
1740 return rc;
1741
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001742 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001743
1744 if (f->fmt.sliced.service_set == 0)
1745 return -EINVAL;
1746
1747 return 0;
1748}
1749
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001750/* RAW VBI ioctls */
1751
1752static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1753 struct v4l2_format *format)
1754{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001755 struct em28xx_fh *fh = priv;
1756 struct em28xx *dev = fh->dev;
1757
1758 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001759 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1760 format->fmt.vbi.offset = 0;
1761 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001762 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1763 format->fmt.vbi.count[0] = dev->vbi_height;
1764 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001765
1766 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001767 if (dev->norm & V4L2_STD_525_60) {
1768 /* NTSC */
1769 format->fmt.vbi.start[0] = 10;
1770 format->fmt.vbi.start[1] = 273;
1771 } else if (dev->norm & V4L2_STD_625_50) {
1772 /* PAL */
1773 format->fmt.vbi.start[0] = 6;
1774 format->fmt.vbi.start[1] = 318;
1775 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001776
1777 return 0;
1778}
1779
1780static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1781 struct v4l2_format *format)
1782{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001783 struct em28xx_fh *fh = priv;
1784 struct em28xx *dev = fh->dev;
1785
1786 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001787 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1788 format->fmt.vbi.offset = 0;
1789 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001790 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1791 format->fmt.vbi.count[0] = dev->vbi_height;
1792 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001793
1794 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001795 if (dev->norm & V4L2_STD_525_60) {
1796 /* NTSC */
1797 format->fmt.vbi.start[0] = 10;
1798 format->fmt.vbi.start[1] = 273;
1799 } else if (dev->norm & V4L2_STD_625_50) {
1800 /* PAL */
1801 format->fmt.vbi.start[0] = 6;
1802 format->fmt.vbi.start[1] = 318;
1803 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001804
1805 return 0;
1806}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001807
1808static int vidioc_reqbufs(struct file *file, void *priv,
1809 struct v4l2_requestbuffers *rb)
1810{
1811 struct em28xx_fh *fh = priv;
1812 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001813 int rc;
1814
1815 rc = check_dev(dev);
1816 if (rc < 0)
1817 return rc;
1818
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001819 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1820 return videobuf_reqbufs(&fh->vb_vidq, rb);
1821 else
1822 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001823}
1824
1825static int vidioc_querybuf(struct file *file, void *priv,
1826 struct v4l2_buffer *b)
1827{
1828 struct em28xx_fh *fh = priv;
1829 struct em28xx *dev = fh->dev;
1830 int rc;
1831
1832 rc = check_dev(dev);
1833 if (rc < 0)
1834 return rc;
1835
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001836 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1837 return videobuf_querybuf(&fh->vb_vidq, b);
1838 else {
1839 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1840 the videobuf framework, but we probably shouldn't be
1841 returning a buffer larger than that which was asked for.
1842 At a minimum, it causes a crash in zvbi since it does
1843 a memcpy based on the source buffer length */
1844 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001845 b->length = dev->vbi_width * dev->vbi_height * 2;
1846
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001847 return result;
1848 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001849}
1850
1851static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1852{
1853 struct em28xx_fh *fh = priv;
1854 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001855 int rc;
1856
1857 rc = check_dev(dev);
1858 if (rc < 0)
1859 return rc;
1860
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001861 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1862 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001863 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001864 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001865}
1866
1867static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1868{
1869 struct em28xx_fh *fh = priv;
1870 struct em28xx *dev = fh->dev;
1871 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001872
1873 rc = check_dev(dev);
1874 if (rc < 0)
1875 return rc;
1876
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001877 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1878 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1879 O_NONBLOCK);
1880 else
1881 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1882 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001883}
1884
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001885/* ----------------------------------------------------------- */
1886/* RADIO ESPECIFIC IOCTLS */
1887/* ----------------------------------------------------------- */
1888
1889static int radio_querycap(struct file *file, void *priv,
1890 struct v4l2_capability *cap)
1891{
1892 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1893
1894 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1895 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001896 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001897
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001898 cap->capabilities = V4L2_CAP_TUNER;
1899 return 0;
1900}
1901
1902static int radio_g_tuner(struct file *file, void *priv,
1903 struct v4l2_tuner *t)
1904{
1905 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1906
1907 if (unlikely(t->index > 0))
1908 return -EINVAL;
1909
1910 strcpy(t->name, "Radio");
1911 t->type = V4L2_TUNER_RADIO;
1912
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001913 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001914
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001915 return 0;
1916}
1917
1918static int radio_enum_input(struct file *file, void *priv,
1919 struct v4l2_input *i)
1920{
1921 if (i->index != 0)
1922 return -EINVAL;
1923 strcpy(i->name, "Radio");
1924 i->type = V4L2_INPUT_TYPE_TUNER;
1925
1926 return 0;
1927}
1928
1929static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1930{
1931 if (unlikely(a->index))
1932 return -EINVAL;
1933
1934 strcpy(a->name, "Radio");
1935 return 0;
1936}
1937
1938static int radio_s_tuner(struct file *file, void *priv,
1939 struct v4l2_tuner *t)
1940{
1941 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1942
1943 if (0 != t->index)
1944 return -EINVAL;
1945
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001946 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001947
1948 return 0;
1949}
1950
1951static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001952 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001953{
1954 return 0;
1955}
1956
1957static int radio_s_input(struct file *file, void *fh, unsigned int i)
1958{
1959 return 0;
1960}
1961
1962static int radio_queryctrl(struct file *file, void *priv,
1963 struct v4l2_queryctrl *qc)
1964{
1965 int i;
1966
1967 if (qc->id < V4L2_CID_BASE ||
1968 qc->id >= V4L2_CID_LASTP1)
1969 return -EINVAL;
1970
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001971 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1972 if (qc->id && qc->id == ac97_qctrl[i].id) {
1973 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001974 return 0;
1975 }
1976 }
1977
1978 return -EINVAL;
1979}
1980
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001981/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001982 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001983 * inits the device and starts isoc transfer
1984 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001985static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001986{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001987 int errCode = 0, radio = 0;
1988 struct video_device *vdev = video_devdata(filp);
1989 struct em28xx *dev = video_drvdata(filp);
1990 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001991 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001992 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001993
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001994 switch (vdev->vfl_type) {
1995 case VFL_TYPE_GRABBER:
1996 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1997 break;
1998 case VFL_TYPE_VBI:
1999 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2000 break;
2001 case VFL_TYPE_RADIO:
2002 radio = 1;
2003 break;
2004 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002005
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002006 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2007 video_device_node_name(vdev), v4l2_type_names[fh_type],
2008 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002009
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002010
Hans Verkuil876cb142012-06-23 08:12:47 -03002011 if (mutex_lock_interruptible(&dev->lock))
2012 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002013 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002014 if (!fh) {
2015 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002016 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002017 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002018 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002019 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002020 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002021 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002022 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002023
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002024 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002025 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002026 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002027 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002028
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002029 /* Needed, since GPIO might have disabled power of
2030 some i2c device
2031 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002032 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002033
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002034 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002035 if (fh->radio) {
2036 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002037 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002038 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002039
2040 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002041
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002042 if (dev->progressive)
2043 field = V4L2_FIELD_NONE;
2044 else
2045 field = V4L2_FIELD_INTERLACED;
2046
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002047 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2048 NULL, &dev->slock,
2049 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002050 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002051
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002052 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2053 NULL, &dev->slock,
2054 V4L2_BUF_TYPE_VBI_CAPTURE,
2055 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002056 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002057 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002058
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002059 return errCode;
2060}
2061
2062/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002063 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002064 * unregisters the v4l2,i2c and usb devices
2065 * called when the device gets disconected or at module unload
2066*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002067void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002068{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002069
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002070 /*FIXME: I2C IR should be disconnected */
2071
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002072 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002073 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002074 video_unregister_device(dev->radio_dev);
2075 else
2076 video_device_release(dev->radio_dev);
2077 dev->radio_dev = NULL;
2078 }
2079 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002080 em28xx_info("V4L2 device %s deregistered\n",
2081 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002082 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002083 video_unregister_device(dev->vbi_dev);
2084 else
2085 video_device_release(dev->vbi_dev);
2086 dev->vbi_dev = NULL;
2087 }
2088 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002089 em28xx_info("V4L2 device %s deregistered\n",
2090 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002091 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002092 video_unregister_device(dev->vdev);
2093 else
2094 video_device_release(dev->vdev);
2095 dev->vdev = NULL;
2096 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002097}
2098
2099/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002100 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002101 * stops streaming and deallocates all resources allocated by the v4l2
2102 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002103 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002104static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002106 struct em28xx_fh *fh = filp->private_data;
2107 struct em28xx *dev = fh->dev;
2108 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002109
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002110 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111
Hans Verkuil876cb142012-06-23 08:12:47 -03002112 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002113 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002114 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002115 res_free(fh, EM28XX_RESOURCE_VIDEO);
2116 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002117
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002118 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2119 videobuf_stop(&fh->vb_vbiq);
2120 res_free(fh, EM28XX_RESOURCE_VBI);
2121 }
2122
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002123 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002124 /* the device is already disconnect,
2125 free the remaining resources */
2126 if (dev->state & DEV_DISCONNECTED) {
2127 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002128 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002129 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002130 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002131 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002132 return 0;
2133 }
2134
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002135 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002136 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002137
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002138 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002139 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002140 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002141
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002142 /* set alternate 0 */
2143 dev->alt = 0;
2144 em28xx_videodbg("setting alternate 0\n");
2145 errCode = usb_set_interface(dev->udev, 0, 0);
2146 if (errCode < 0) {
2147 em28xx_errdev("cannot change alternate number to "
2148 "0 (error=%i)\n", errCode);
2149 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002150 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002151
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002152 videobuf_mmap_free(&fh->vb_vidq);
2153 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002154 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002155 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002156 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157 return 0;
2158}
2159
2160/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002161 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162 * will allocate buffers when called for the first time
2163 */
2164static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002165em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002166 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002167{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002168 struct em28xx_fh *fh = filp->private_data;
2169 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002170 int rc;
2171
2172 rc = check_dev(dev);
2173 if (rc < 0)
2174 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002175
Hans Verkuil876cb142012-06-23 08:12:47 -03002176 if (mutex_lock_interruptible(&dev->lock))
2177 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002178 /* FIXME: read() is not prepared to allow changing the video
2179 resolution while streaming. Seems a bug at em28xx_set_fmt
2180 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002181
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002182 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002183 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002184 rc = -EBUSY;
2185 else
2186 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002187 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002188 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002189 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002190 rc = -EBUSY;
2191 else
2192 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002193 filp->f_flags & O_NONBLOCK);
2194 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002195 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002196
Hans Verkuil876cb142012-06-23 08:12:47 -03002197 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198}
2199
2200/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002201 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202 * will allocate buffers when called for the first time
2203 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002204static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002206 struct em28xx_fh *fh = filp->private_data;
2207 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002208 int rc;
2209
2210 rc = check_dev(dev);
2211 if (rc < 0)
2212 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002214 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2215 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2216 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002217 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002218 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2219 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2220 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002221 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002222 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002223 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002224 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002225}
2226
Hans Verkuil876cb142012-06-23 08:12:47 -03002227static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2228{
2229 struct em28xx_fh *fh = filp->private_data;
2230 struct em28xx *dev = fh->dev;
2231 unsigned int res;
2232
2233 mutex_lock(&dev->lock);
2234 res = em28xx_poll(filp, wait);
2235 mutex_unlock(&dev->lock);
2236 return res;
2237}
2238
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002240 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002242static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002243{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002244 struct em28xx_fh *fh = filp->private_data;
2245 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002246 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002247
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002248 rc = check_dev(dev);
2249 if (rc < 0)
2250 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002251
Hans Verkuil876cb142012-06-23 08:12:47 -03002252 if (mutex_lock_interruptible(&dev->lock))
2253 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002254 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2255 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2256 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2257 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002258 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002259
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002260 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2261 (unsigned long)vma->vm_start,
2262 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2263 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002265 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002266}
2267
Hans Verkuilbec43662008-12-30 06:58:20 -03002268static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002269 .owner = THIS_MODULE,
2270 .open = em28xx_v4l2_open,
2271 .release = em28xx_v4l2_close,
2272 .read = em28xx_v4l2_read,
2273 .poll = em28xx_v4l2_poll,
2274 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002275 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002276};
2277
Hans Verkuila3998102008-07-21 02:57:38 -03002278static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002279 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002280 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2281 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2282 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2283 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002284 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2285 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002286 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002287 .vidioc_g_audio = vidioc_g_audio,
2288 .vidioc_s_audio = vidioc_s_audio,
2289 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002290 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2291 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2292 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002293
2294 .vidioc_reqbufs = vidioc_reqbufs,
2295 .vidioc_querybuf = vidioc_querybuf,
2296 .vidioc_qbuf = vidioc_qbuf,
2297 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002298 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002299 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002300 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002301 .vidioc_g_parm = vidioc_g_parm,
2302 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002303 .vidioc_enum_input = vidioc_enum_input,
2304 .vidioc_g_input = vidioc_g_input,
2305 .vidioc_s_input = vidioc_s_input,
2306 .vidioc_queryctrl = vidioc_queryctrl,
2307 .vidioc_g_ctrl = vidioc_g_ctrl,
2308 .vidioc_s_ctrl = vidioc_s_ctrl,
2309 .vidioc_streamon = vidioc_streamon,
2310 .vidioc_streamoff = vidioc_streamoff,
2311 .vidioc_g_tuner = vidioc_g_tuner,
2312 .vidioc_s_tuner = vidioc_s_tuner,
2313 .vidioc_g_frequency = vidioc_g_frequency,
2314 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002315#ifdef CONFIG_VIDEO_ADV_DEBUG
2316 .vidioc_g_register = vidioc_g_register,
2317 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002318 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002319#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002320};
2321
2322static const struct video_device em28xx_video_template = {
2323 .fops = &em28xx_v4l_fops,
2324 .release = video_device_release,
2325 .ioctl_ops = &video_ioctl_ops,
2326
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002327 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002328 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002329};
2330
Hans Verkuilbec43662008-12-30 06:58:20 -03002331static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002332 .owner = THIS_MODULE,
2333 .open = em28xx_v4l2_open,
2334 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002335 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002336};
2337
2338static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002339 .vidioc_querycap = radio_querycap,
2340 .vidioc_g_tuner = radio_g_tuner,
2341 .vidioc_enum_input = radio_enum_input,
2342 .vidioc_g_audio = radio_g_audio,
2343 .vidioc_s_tuner = radio_s_tuner,
2344 .vidioc_s_audio = radio_s_audio,
2345 .vidioc_s_input = radio_s_input,
2346 .vidioc_queryctrl = radio_queryctrl,
2347 .vidioc_g_ctrl = vidioc_g_ctrl,
2348 .vidioc_s_ctrl = vidioc_s_ctrl,
2349 .vidioc_g_frequency = vidioc_g_frequency,
2350 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002351#ifdef CONFIG_VIDEO_ADV_DEBUG
2352 .vidioc_g_register = vidioc_g_register,
2353 .vidioc_s_register = vidioc_s_register,
2354#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002355};
2356
Hans Verkuila3998102008-07-21 02:57:38 -03002357static struct video_device em28xx_radio_template = {
2358 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002359 .fops = &radio_fops,
2360 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002361};
2362
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002363/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002364
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002365
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002366
Adrian Bunk532fe652008-01-28 22:10:48 -03002367static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002368 const struct video_device *template,
2369 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002370{
2371 struct video_device *vfd;
2372
2373 vfd = video_device_alloc();
2374 if (NULL == vfd)
2375 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002376
2377 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002378 vfd->v4l2_dev = &dev->v4l2_dev;
2379 vfd->release = video_device_release;
2380 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002381 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002382
2383 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2384 dev->name, type_name);
2385
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002386 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002387 return vfd;
2388}
2389
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002390int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002391{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002392 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002393 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002394 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002395
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002396 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2397 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002398
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002399 /* set default norm */
2400 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002401 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002402 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002403
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002404 /* Analog specific initialization */
2405 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002406
2407 maxw = norm_maxw(dev);
2408 /* MaxPacketSize for em2800 is too small to capture at full resolution
2409 * use half of maxw as the scaler can only scale to 50% */
2410 if (dev->board.is_em2800)
2411 maxw /= 2;
2412
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002413 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002414 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002415
Ezequiel García96371fc2012-03-23 18:09:34 -03002416 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002417
2418 /* Audio defaults */
2419 dev->mute = 1;
2420 dev->volume = 0x1f;
2421
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002422/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002423 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2424 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2425 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002426
2427 em28xx_set_outfmt(dev);
2428 em28xx_colorlevels_set_default(dev);
2429 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002430
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002431 /* allocate and fill video video_device struct */
2432 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2433 if (!dev->vdev) {
2434 em28xx_errdev("cannot allocate video_device.\n");
2435 return -ENODEV;
2436 }
2437
2438 /* register v4l2 video video_device */
2439 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2440 video_nr[dev->devno]);
2441 if (ret) {
2442 em28xx_errdev("unable to register video device (error=%i).\n",
2443 ret);
2444 return ret;
2445 }
2446
2447 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002448 if (em28xx_vbi_supported(dev) == 1) {
2449 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2450 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002451
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002452 /* register v4l2 vbi video_device */
2453 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2454 vbi_nr[dev->devno]);
2455 if (ret < 0) {
2456 em28xx_errdev("unable to register vbi device\n");
2457 return ret;
2458 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002459 }
2460
2461 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002462 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2463 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002464 if (!dev->radio_dev) {
2465 em28xx_errdev("cannot allocate video_device.\n");
2466 return -ENODEV;
2467 }
2468 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2469 radio_nr[dev->devno]);
2470 if (ret < 0) {
2471 em28xx_errdev("can't register radio device\n");
2472 return ret;
2473 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002474 em28xx_info("Registered radio device as %s\n",
2475 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002476 }
2477
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002478 em28xx_info("V4L2 video device registered as %s\n",
2479 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002480
2481 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002482 em28xx_info("V4L2 VBI device registered as %s\n",
2483 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002484
2485 return 0;
2486}