blob: a1436a453bc895d9d2b60b469ce8a365a49d86ec [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/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
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 Schaefer74209dc2012-11-08 14:11:37 -0300372 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300379 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 outp = videobuf_to_vmalloc(&(*buf)->vb);
381 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300382
Frank Schaefer74209dc2012-11-08 14:11:37 -0300383 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384
385 return;
386}
387
388/*
389 * video-buf generic routine to get the next available VBI buffer
390 */
391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 struct em28xx_buffer **buf)
393{
394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 char *outp;
396
397 if (list_empty(&dma_q->active)) {
398 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300399 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300400 *buf = NULL;
401 return;
402 }
403
404 /* Get the next buffer */
405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300407 outp = videobuf_to_vmalloc(&(*buf)->vb);
408 memset(outp, 0x00, (*buf)->vb.size);
409
Frank Schaefer74209dc2012-11-08 14:11:37 -0300410 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300411
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300412 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300413}
414
Frank Schaefer960da932012-11-25 06:37:37 -0300415/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300416static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300417{
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300418 struct em28xx_buffer *buf, *vbi_buf;
419 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300420 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer79ff8692012-11-25 06:37:36 -0300421 int xfer_bulk, num_packets, i, rc = 1;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300424
425 if (!dev)
426 return 0;
427
428 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
429 return 0;
430
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300431 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300432 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300433
Frank Schaefer4601cc32012-11-08 14:11:46 -0300434 xfer_bulk = usb_pipebulk(urb->pipe);
435
Frank Schaefer74209dc2012-11-08 14:11:37 -0300436 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300437 if (buf != NULL)
438 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300439
Frank Schaefer74209dc2012-11-08 14:11:37 -0300440 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300441 if (vbi_buf != NULL)
442 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
443
Frank Schaefer4601cc32012-11-08 14:11:46 -0300444 if (xfer_bulk) /* bulk */
445 num_packets = 1;
446 else /* isoc */
447 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300448
Frank Schaefer4601cc32012-11-08 14:11:46 -0300449 for (i = 0; i < num_packets; i++) {
450 if (xfer_bulk) { /* bulk */
451 actual_length = urb->actual_length;
452
453 p = urb->transfer_buffer;
454 } else { /* isoc */
455 if (urb->iso_frame_desc[i].status < 0) {
456 print_err_status(dev, i,
457 urb->iso_frame_desc[i].status);
458 if (urb->iso_frame_desc[i].status != -EPROTO)
459 continue;
460 }
461
462 actual_length = urb->iso_frame_desc[i].actual_length;
463 if (actual_length > dev->max_pkt_size) {
464 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300465 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300466 }
467
468 p = urb->transfer_buffer +
469 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300470 }
471
Frank Schaefer3610f582012-11-25 06:37:33 -0300472 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300473 /* NOTE: happens very often with isoc transfers */
474 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300475 continue;
476 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300477
478 /* capture type 0 = vbi start
479 capture type 1 = video start
480 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300481 len = actual_length;
482 if (len >= 4) {
483 /* NOTE: headers are always 4 bytes and
484 * never split across packets */
485 if (p[0] == 0x33 && p[1] == 0x95) {
486 dev->capture_type = 0;
487 dev->vbi_read = 0;
488 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300489 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300490 p += 4;
491 len -= 4;
492 } else if (p[0] == 0x88 && p[1] == 0x88 &&
493 p[2] == 0x88 && p[3] == 0x88) {
494 /* continuation */
495 p += 4;
496 len -= 4;
497 } else if (p[0] == 0x22 && p[1] == 0x5a) {
498 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300499 dev->capture_type = 1;
500 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300501 p += 4;
502 len -= 4;
503 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300504 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300505 /* NOTE: with bulk transfers, intermediate data packets
506 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300507
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300508 if (dev->capture_type == 0) {
Frank Schaefer79ff8692012-11-25 06:37:36 -0300509 int vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300510 if (dev->vbi_read >= vbi_size) {
511 /* We've already read all the VBI data, so
512 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300513 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300514 } else if ((dev->vbi_read + len) < vbi_size) {
515 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300516 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300517 /* Brand new frame */
518 if (vbi_buf != NULL)
519 vbi_buffer_filled(dev,
520 vbi_dma_q,
521 vbi_buf);
522 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
523 if (vbi_buf == NULL)
524 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300525 else
526 vbioutp = videobuf_to_vmalloc(
527 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300528 }
529
530 if (dev->vbi_read == 0) {
531 vbi_dma_q->pos = 0;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300532 if (vbi_buf != NULL)
533 vbi_buf->top_field = dev->top_field;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300534 }
535
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300536 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300537 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
538 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300539 } else {
540 /* Some of this frame is VBI data and some is
541 video data */
542 int vbi_data_len = vbi_size - dev->vbi_read;
543 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300544 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
545 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300546 dev->capture_type = 1;
547 p += vbi_data_len;
548 len -= vbi_data_len;
549 }
550 }
551
552 if (dev->capture_type == 1) {
553 dev->capture_type = 2;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300554 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300555 if (buf != NULL)
556 buffer_filled(dev, dma_q, buf);
557 get_next_buf(dma_q, &buf);
558 if (buf == NULL)
559 outp = NULL;
560 else
561 outp = videobuf_to_vmalloc(&buf->vb);
562 }
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300563 if (buf != NULL)
564 buf->top_field = dev->top_field;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300565
566 dma_q->pos = 0;
567 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300568
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300569 if (buf != NULL && dev->capture_type == 2 && len > 0)
570 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300571 }
572 return rc;
573}
574
575
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300576/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300577 Videobuf operations
578 ------------------------------------------------------------------*/
579
580static int
581buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
582{
583 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300584 struct em28xx *dev = fh->dev;
585 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300586
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300587 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
588 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300589
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300590 if (0 == *count)
591 *count = EM28XX_DEF_BUF;
592
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300593 if (*count < EM28XX_MIN_BUF)
594 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300596 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300597 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300598 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300599 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300600
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300601 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300602
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300603 return 0;
604}
605
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300606/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300607static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
608{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300609 struct em28xx_fh *fh = vq->priv_data;
610 struct em28xx *dev = fh->dev;
611 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300612 if (in_interrupt())
613 BUG();
614
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300615 /* We used to wait for the buffer to finish here, but this didn't work
616 because, as we were keeping the state as VIDEOBUF_QUEUED,
617 videobuf_queue_cancel marked it as finished for us.
618 (Also, it could wedge forever if the hardware was misconfigured.)
619
620 This should be safe; by the time we get here, the buffer isn't
621 queued anymore. If we ever start marking the buffers as
622 VIDEOBUF_ACTIVE, it won't be, though.
623 */
624 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300625 if (dev->usb_ctl.vid_buf == buf)
626 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300627 spin_unlock_irqrestore(&dev->slock, flags);
628
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300629 videobuf_vmalloc_free(&buf->vb);
630 buf->vb.state = VIDEOBUF_NEEDS_INIT;
631}
632
633static int
634buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
635 enum v4l2_field field)
636{
637 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300638 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300639 struct em28xx *dev = fh->dev;
640 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300641
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300642 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
643 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300644
645 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
646 return -EINVAL;
647
Brandon Philips05612972008-04-13 14:57:01 -0300648 buf->vb.width = dev->width;
649 buf->vb.height = dev->height;
650 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300651
652 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300653 rc = videobuf_iolock(vq, &buf->vb, NULL);
654 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300655 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300656 }
657
Frank Schaefer74209dc2012-11-08 14:11:37 -0300658 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300659 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300660
661 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300662 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300663 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
664 dev->analog_xfer_bulk,
665 EM28XX_NUM_BUFS,
666 dev->max_pkt_size,
667 dev->packet_multiplier,
668 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300669 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300670 goto fail;
671 }
672
673 buf->vb.state = VIDEOBUF_PREPARED;
674 return 0;
675
676fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300677 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300678 return rc;
679}
680
681static void
682buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
683{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300684 struct em28xx_buffer *buf = container_of(vb,
685 struct em28xx_buffer,
686 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300687 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300688 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300690
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300691 buf->vb.state = VIDEOBUF_QUEUED;
692 list_add_tail(&buf->vb.queue, &vidq->active);
693
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300694}
695
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300696static void buffer_release(struct videobuf_queue *vq,
697 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300699 struct em28xx_buffer *buf = container_of(vb,
700 struct em28xx_buffer,
701 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300702 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300703 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300704
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300705 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300706
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300707 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300708}
709
710static struct videobuf_queue_ops em28xx_video_qops = {
711 .buf_setup = buffer_setup,
712 .buf_prepare = buffer_prepare,
713 .buf_queue = buffer_queue,
714 .buf_release = buffer_release,
715};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800716
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300717/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800718
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800719static void video_mux(struct em28xx *dev, int index)
720{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800721 dev->ctl_input = index;
722 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300723 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800724
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300725 if (!dev->ctl_aoutput)
726 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
727
Hans Verkuil5325b422009-04-02 11:26:22 -0300728 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
729 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800730
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300731 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300732 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300733 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
734 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300735 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300736 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300737 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
738 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800739 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300740
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300741 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300742 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
743 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300744 }
745
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300746 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800747}
748
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300749/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300750static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300751{
752 struct em28xx *dev = fh->dev;
753
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300754 if (fh->resources & bit)
755 /* have it already allocated */
756 return 1;
757
758 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300759 if (dev->resources & bit) {
760 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300761 return 0;
762 }
763 /* it's free, grab it */
764 fh->resources |= bit;
765 dev->resources |= bit;
766 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300767 return 1;
768}
769
770static int res_check(struct em28xx_fh *fh, unsigned int bit)
771{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300772 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300773}
774
775static int res_locked(struct em28xx *dev, unsigned int bit)
776{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300777 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300778}
779
780static void res_free(struct em28xx_fh *fh, unsigned int bits)
781{
782 struct em28xx *dev = fh->dev;
783
784 BUG_ON((fh->resources & bits) != bits);
785
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300786 fh->resources &= ~bits;
787 dev->resources &= ~bits;
788 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300789}
790
791static int get_ressource(struct em28xx_fh *fh)
792{
793 switch (fh->type) {
794 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
795 return EM28XX_RESOURCE_VIDEO;
796 case V4L2_BUF_TYPE_VBI_CAPTURE:
797 return EM28XX_RESOURCE_VBI;
798 default:
799 BUG();
800 return 0;
801 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300802}
803
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800804/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300805 * ac97_queryctrl()
806 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300807 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300808static int ac97_queryctrl(struct v4l2_queryctrl *qc)
809{
810 int i;
811
812 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
813 if (qc->id && qc->id == ac97_qctrl[i].id) {
814 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
815 return 0;
816 }
817 }
818
819 /* Control is not ac97 related */
820 return 1;
821}
822
823/*
824 * ac97_get_ctrl()
825 * return the current values for ac97 mute and volume
826 */
827static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300828{
829 switch (ctrl->id) {
830 case V4L2_CID_AUDIO_MUTE:
831 ctrl->value = dev->mute;
832 return 0;
833 case V4L2_CID_AUDIO_VOLUME:
834 ctrl->value = dev->volume;
835 return 0;
836 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300837 /* Control is not ac97 related */
838 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300839 }
840}
841
842/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300843 * ac97_set_ctrl()
844 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300846static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300847{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300848 int i;
849
850 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
851 if (ctrl->id == ac97_qctrl[i].id)
852 goto handle;
853
854 /* Announce that hasn't handle it */
855 return 1;
856
857handle:
858 if (ctrl->value < ac97_qctrl[i].minimum ||
859 ctrl->value > ac97_qctrl[i].maximum)
860 return -ERANGE;
861
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300862 switch (ctrl->id) {
863 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300864 dev->mute = ctrl->value;
865 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866 case V4L2_CID_AUDIO_VOLUME:
867 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300868 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300869 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300870
871 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300872}
873
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300874static int check_dev(struct em28xx *dev)
875{
876 if (dev->state & DEV_DISCONNECTED) {
877 em28xx_errdev("v4l2 ioctl: device not present\n");
878 return -ENODEV;
879 }
880
881 if (dev->state & DEV_MISCONFIGURED) {
882 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
883 "close and open it again\n");
884 return -EIO;
885 }
886 return 0;
887}
888
889static void get_scale(struct em28xx *dev,
890 unsigned int width, unsigned int height,
891 unsigned int *hscale, unsigned int *vscale)
892{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300893 unsigned int maxw = norm_maxw(dev);
894 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300895
896 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
897 if (*hscale >= 0x4000)
898 *hscale = 0x3fff;
899
900 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
901 if (*vscale >= 0x4000)
902 *vscale = 0x3fff;
903}
904
905/* ------------------------------------------------------------------
906 IOCTL vidioc handling
907 ------------------------------------------------------------------*/
908
Hans Verkuil78b526a2008-05-28 12:16:41 -0300909static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910 struct v4l2_format *f)
911{
912 struct em28xx_fh *fh = priv;
913 struct em28xx *dev = fh->dev;
914
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300915 f->fmt.pix.width = dev->width;
916 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300917 f->fmt.pix.pixelformat = dev->format->fourcc;
918 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300919 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300920 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
921
922 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300923 if (dev->progressive)
924 f->fmt.pix.field = V4L2_FIELD_NONE;
925 else
926 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300927 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300928 return 0;
929}
930
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300931static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
932{
933 unsigned int i;
934
935 for (i = 0; i < ARRAY_SIZE(format); i++)
936 if (format[i].fourcc == fourcc)
937 return &format[i];
938
939 return NULL;
940}
941
Hans Verkuil78b526a2008-05-28 12:16:41 -0300942static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300943 struct v4l2_format *f)
944{
945 struct em28xx_fh *fh = priv;
946 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300947 unsigned int width = f->fmt.pix.width;
948 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300949 unsigned int maxw = norm_maxw(dev);
950 unsigned int maxh = norm_maxh(dev);
951 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300952 struct em28xx_fmt *fmt;
953
954 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
955 if (!fmt) {
956 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
957 f->fmt.pix.pixelformat);
958 return -EINVAL;
959 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300961 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300963 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
964 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300965 /* MaxPacketSize for em2800 is too small to capture at full resolution
966 * use half of maxw as the scaler can only scale to 50% */
967 if (width == maxw && height == maxh)
968 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300969 } else {
970 /* width must even because of the YUYV format
971 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300972 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
973 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974 }
975
976 get_scale(dev, width, height, &hscale, &vscale);
977
978 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
979 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
980
981 f->fmt.pix.width = width;
982 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300983 f->fmt.pix.pixelformat = fmt->fourcc;
984 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
985 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300987 if (dev->progressive)
988 f->fmt.pix.field = V4L2_FIELD_NONE;
989 else
990 f->fmt.pix.field = dev->interlaced ?
991 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300992
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300993 return 0;
994}
995
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300996static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
997 unsigned width, unsigned height)
998{
999 struct em28xx_fmt *fmt;
1000
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001001 fmt = format_by_fourcc(fourcc);
1002 if (!fmt)
1003 return -EINVAL;
1004
1005 dev->format = fmt;
1006 dev->width = width;
1007 dev->height = height;
1008
1009 /* set new image size */
1010 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1011
1012 em28xx_set_alternate(dev);
1013 em28xx_resolution_set(dev);
1014
1015 return 0;
1016}
1017
Hans Verkuil78b526a2008-05-28 12:16:41 -03001018static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001019 struct v4l2_format *f)
1020{
1021 struct em28xx_fh *fh = priv;
1022 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001023 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001024
1025 rc = check_dev(dev);
1026 if (rc < 0)
1027 return rc;
1028
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001029 vidioc_try_fmt_vid_cap(file, priv, f);
1030
Brandon Philips05612972008-04-13 14:57:01 -03001031 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1032 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001033 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001034 }
1035
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001036 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001037 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038}
1039
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001040static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1041{
1042 struct em28xx_fh *fh = priv;
1043 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001044 int rc;
1045
1046 rc = check_dev(dev);
1047 if (rc < 0)
1048 return rc;
1049
1050 *norm = dev->norm;
1051
1052 return 0;
1053}
1054
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001055static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1056{
1057 struct em28xx_fh *fh = priv;
1058 struct em28xx *dev = fh->dev;
1059 int rc;
1060
1061 rc = check_dev(dev);
1062 if (rc < 0)
1063 return rc;
1064
1065 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1066
1067 return 0;
1068}
1069
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001070static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001071{
1072 struct em28xx_fh *fh = priv;
1073 struct em28xx *dev = fh->dev;
1074 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001075 int rc;
1076
1077 rc = check_dev(dev);
1078 if (rc < 0)
1079 return rc;
1080
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001081 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082
1083 /* Adjusts width/height, if needed */
1084 f.fmt.pix.width = dev->width;
1085 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001086 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001087
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001088 /* set new image size */
1089 dev->width = f.fmt.pix.width;
1090 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001091 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1092
1093 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001094 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 return 0;
1097}
1098
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001099static int vidioc_g_parm(struct file *file, void *priv,
1100 struct v4l2_streamparm *p)
1101{
1102 struct em28xx_fh *fh = priv;
1103 struct em28xx *dev = fh->dev;
1104 int rc = 0;
1105
1106 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1107 return -EINVAL;
1108
1109 if (dev->board.is_webcam)
1110 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1111 video, g_parm, p);
1112 else
1113 v4l2_video_std_frame_period(dev->norm,
1114 &p->parm.capture.timeperframe);
1115
1116 return rc;
1117}
1118
1119static int vidioc_s_parm(struct file *file, void *priv,
1120 struct v4l2_streamparm *p)
1121{
1122 struct em28xx_fh *fh = priv;
1123 struct em28xx *dev = fh->dev;
1124
1125 if (!dev->board.is_webcam)
1126 return -EINVAL;
1127
1128 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1129 return -EINVAL;
1130
1131 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1132}
1133
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134static const char *iname[] = {
1135 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1136 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1137 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1138 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1139 [EM28XX_VMUX_SVIDEO] = "S-Video",
1140 [EM28XX_VMUX_TELEVISION] = "Television",
1141 [EM28XX_VMUX_CABLE] = "Cable TV",
1142 [EM28XX_VMUX_DVB] = "DVB",
1143 [EM28XX_VMUX_DEBUG] = "for debug only",
1144};
1145
1146static int vidioc_enum_input(struct file *file, void *priv,
1147 struct v4l2_input *i)
1148{
1149 struct em28xx_fh *fh = priv;
1150 struct em28xx *dev = fh->dev;
1151 unsigned int n;
1152
1153 n = i->index;
1154 if (n >= MAX_EM28XX_INPUT)
1155 return -EINVAL;
1156 if (0 == INPUT(n)->type)
1157 return -EINVAL;
1158
1159 i->index = n;
1160 i->type = V4L2_INPUT_TYPE_CAMERA;
1161
1162 strcpy(i->name, iname[INPUT(n)->type]);
1163
1164 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1165 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1166 i->type = V4L2_INPUT_TYPE_TUNER;
1167
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001168 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169
1170 return 0;
1171}
1172
1173static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1177
1178 *i = dev->ctl_input;
1179
1180 return 0;
1181}
1182
1183static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1184{
1185 struct em28xx_fh *fh = priv;
1186 struct em28xx *dev = fh->dev;
1187 int rc;
1188
1189 rc = check_dev(dev);
1190 if (rc < 0)
1191 return rc;
1192
1193 if (i >= MAX_EM28XX_INPUT)
1194 return -EINVAL;
1195 if (0 == INPUT(i)->type)
1196 return -EINVAL;
1197
Ezequiel García96371fc2012-03-23 18:09:34 -03001198 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199 return 0;
1200}
1201
1202static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1203{
1204 struct em28xx_fh *fh = priv;
1205 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001207 if (!dev->audio_mode.has_audio)
1208 return -EINVAL;
1209
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001210 switch (a->index) {
1211 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001212 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001213 break;
1214 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001215 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001216 break;
1217 case EM28XX_AMUX_VIDEO2:
1218 strcpy(a->name, "Television alt");
1219 break;
1220 case EM28XX_AMUX_PHONE:
1221 strcpy(a->name, "Phone");
1222 break;
1223 case EM28XX_AMUX_MIC:
1224 strcpy(a->name, "Mic");
1225 break;
1226 case EM28XX_AMUX_CD:
1227 strcpy(a->name, "CD");
1228 break;
1229 case EM28XX_AMUX_AUX:
1230 strcpy(a->name, "Aux");
1231 break;
1232 case EM28XX_AMUX_PCM_OUT:
1233 strcpy(a->name, "PCM");
1234 break;
1235 default:
1236 return -EINVAL;
1237 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001238
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001239 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001240 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001241
1242 return 0;
1243}
1244
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001245static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001246{
1247 struct em28xx_fh *fh = priv;
1248 struct em28xx *dev = fh->dev;
1249
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001250
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001251 if (!dev->audio_mode.has_audio)
1252 return -EINVAL;
1253
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001254 if (a->index >= MAX_EM28XX_INPUT)
1255 return -EINVAL;
1256 if (0 == INPUT(a->index)->type)
1257 return -EINVAL;
1258
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001259 dev->ctl_ainput = INPUT(a->index)->amux;
1260 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001261
1262 if (!dev->ctl_aoutput)
1263 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001264
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001265 return 0;
1266}
1267
1268static int vidioc_queryctrl(struct file *file, void *priv,
1269 struct v4l2_queryctrl *qc)
1270{
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
1273 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001274 int rc;
1275
1276 rc = check_dev(dev);
1277 if (rc < 0)
1278 return rc;
1279
1280 memset(qc, 0, sizeof(*qc));
1281
1282 qc->id = id;
1283
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001284 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001285 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1286 rc = ac97_queryctrl(qc);
1287 if (!rc)
1288 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001289 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001290
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001291 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001292 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001293
1294 if (qc->type)
1295 return 0;
1296 else
1297 return -EINVAL;
1298}
1299
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001300/*
1301 * FIXME: This is an indirect way to check if a control exists at a
1302 * subdev. Instead of that hack, maybe the better would be to change all
1303 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1304 */
1305static int check_subdev_ctrl(struct em28xx *dev, int id)
1306{
1307 struct v4l2_queryctrl qc;
1308
1309 memset(&qc, 0, sizeof(qc));
1310 qc.id = id;
1311
1312 /* enumerate V4L2 device controls */
1313 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1314
1315 if (qc.type)
1316 return 0;
1317 else
1318 return -EINVAL;
1319}
1320
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321static int vidioc_g_ctrl(struct file *file, void *priv,
1322 struct v4l2_control *ctrl)
1323{
1324 struct em28xx_fh *fh = priv;
1325 struct em28xx *dev = fh->dev;
1326 int rc;
1327
1328 rc = check_dev(dev);
1329 if (rc < 0)
1330 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001331 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001332
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001333 /* Set an AC97 control */
1334 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1335 rc = ac97_get_ctrl(dev, ctrl);
1336 else
1337 rc = 1;
1338
1339 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1340 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001341 if (check_subdev_ctrl(dev, ctrl->id))
1342 return -EINVAL;
1343
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001344 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001345 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001346 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 return rc;
1349}
1350
1351static int vidioc_s_ctrl(struct file *file, void *priv,
1352 struct v4l2_control *ctrl)
1353{
1354 struct em28xx_fh *fh = priv;
1355 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 int rc;
1357
1358 rc = check_dev(dev);
1359 if (rc < 0)
1360 return rc;
1361
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001362 /* Set an AC97 control */
1363 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1364 rc = ac97_set_ctrl(dev, ctrl);
1365 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001366 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001368 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001369 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001370 rc = check_subdev_ctrl(dev, ctrl->id);
1371 if (!rc)
1372 v4l2_device_call_all(&dev->v4l2_dev, 0,
1373 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001374 /*
1375 * In the case of non-AC97 volume controls, we still need
1376 * to do some setups at em28xx, in order to mute/unmute
1377 * and to adjust audio volume. However, the value ranges
1378 * should be checked by the corresponding V4L subdriver.
1379 */
1380 switch (ctrl->id) {
1381 case V4L2_CID_AUDIO_MUTE:
1382 dev->mute = ctrl->value;
1383 rc = em28xx_audio_analog_set(dev);
1384 break;
1385 case V4L2_CID_AUDIO_VOLUME:
1386 dev->volume = ctrl->value;
1387 rc = em28xx_audio_analog_set(dev);
1388 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001390 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001391}
1392
1393static int vidioc_g_tuner(struct file *file, void *priv,
1394 struct v4l2_tuner *t)
1395{
1396 struct em28xx_fh *fh = priv;
1397 struct em28xx *dev = fh->dev;
1398 int rc;
1399
1400 rc = check_dev(dev);
1401 if (rc < 0)
1402 return rc;
1403
1404 if (0 != t->index)
1405 return -EINVAL;
1406
1407 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001408 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001409
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001410 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411 return 0;
1412}
1413
1414static int vidioc_s_tuner(struct file *file, void *priv,
1415 struct v4l2_tuner *t)
1416{
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1419 int rc;
1420
1421 rc = check_dev(dev);
1422 if (rc < 0)
1423 return rc;
1424
1425 if (0 != t->index)
1426 return -EINVAL;
1427
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001428 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001429 return 0;
1430}
1431
1432static int vidioc_g_frequency(struct file *file, void *priv,
1433 struct v4l2_frequency *f)
1434{
1435 struct em28xx_fh *fh = priv;
1436 struct em28xx *dev = fh->dev;
1437
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001438 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001440 return 0;
1441}
1442
1443static int vidioc_s_frequency(struct file *file, void *priv,
1444 struct v4l2_frequency *f)
1445{
1446 struct em28xx_fh *fh = priv;
1447 struct em28xx *dev = fh->dev;
1448 int rc;
1449
1450 rc = check_dev(dev);
1451 if (rc < 0)
1452 return rc;
1453
1454 if (0 != f->tuner)
1455 return -EINVAL;
1456
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001457 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1458 return -EINVAL;
1459 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001460 return -EINVAL;
1461
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001463 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001465 return 0;
1466}
1467
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001468#ifdef CONFIG_VIDEO_ADV_DEBUG
1469static int em28xx_reg_len(int reg)
1470{
1471 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001472 case EM28XX_R40_AC97LSB:
1473 case EM28XX_R30_HSCALELOW:
1474 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001475 return 2;
1476 default:
1477 return 1;
1478 }
1479}
1480
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001481static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001482 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001483{
1484 struct em28xx_fh *fh = priv;
1485 struct em28xx *dev = fh->dev;
1486
1487 chip->ident = V4L2_IDENT_NONE;
1488 chip->revision = 0;
1489
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001490 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001491
1492 return 0;
1493}
1494
1495
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001496static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001497 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001498{
1499 struct em28xx_fh *fh = priv;
1500 struct em28xx *dev = fh->dev;
1501 int ret;
1502
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001503 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001504 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001505 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001506 if (ret < 0)
1507 return ret;
1508
1509 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001510 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001511 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001512 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001513 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001514 return 0;
1515 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001516 /* TODO: is this correct? */
1517 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1518 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001519 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001520 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001521 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001522 }
1523
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001524 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001525 reg->size = em28xx_reg_len(reg->reg);
1526 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001527 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001528
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001529 if (ret < 0)
1530 return ret;
1531
1532 reg->val = ret;
1533 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001534 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001535 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1536 reg->reg, (char *)&val, 2);
1537 if (ret < 0)
1538 return ret;
1539
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001540 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001541 }
1542
1543 return 0;
1544}
1545
1546static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001547 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001548{
1549 struct em28xx_fh *fh = priv;
1550 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001551 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001552
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001553 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001554 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001555 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001556 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001557 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001558 return 0;
1559 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001560 /* TODO: is this correct? */
1561 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1562 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001563 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001564 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001565 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001566 }
1567
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001568 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001569 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001570
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001571 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001572 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001573}
1574#endif
1575
1576
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001577static int vidioc_cropcap(struct file *file, void *priv,
1578 struct v4l2_cropcap *cc)
1579{
1580 struct em28xx_fh *fh = priv;
1581 struct em28xx *dev = fh->dev;
1582
1583 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1584 return -EINVAL;
1585
1586 cc->bounds.left = 0;
1587 cc->bounds.top = 0;
1588 cc->bounds.width = dev->width;
1589 cc->bounds.height = dev->height;
1590 cc->defrect = cc->bounds;
1591 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1592 cc->pixelaspect.denominator = 59;
1593
1594 return 0;
1595}
1596
1597static int vidioc_streamon(struct file *file, void *priv,
1598 enum v4l2_buf_type type)
1599{
1600 struct em28xx_fh *fh = priv;
1601 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001602 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001603
1604 rc = check_dev(dev);
1605 if (rc < 0)
1606 return rc;
1607
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001608 if (unlikely(type != fh->type))
1609 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001610
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001611 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1612 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001613
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001614 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001615 return -EBUSY;
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)
1618 rc = videobuf_streamon(&fh->vb_vidq);
1619 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1620 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001621
1622 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001623}
1624
1625static int vidioc_streamoff(struct file *file, void *priv,
1626 enum v4l2_buf_type type)
1627{
1628 struct em28xx_fh *fh = priv;
1629 struct em28xx *dev = fh->dev;
1630 int rc;
1631
1632 rc = check_dev(dev);
1633 if (rc < 0)
1634 return rc;
1635
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001636 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1637 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001638 return -EINVAL;
1639 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001640 return -EINVAL;
1641
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001642 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1643 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001644
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001645 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001646 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1647 videobuf_streamoff(&fh->vb_vidq);
1648 res_free(fh, EM28XX_RESOURCE_VIDEO);
1649 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001650 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001651 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1652 videobuf_streamoff(&fh->vb_vbiq);
1653 res_free(fh, EM28XX_RESOURCE_VBI);
1654 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001655 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001656
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001657 return 0;
1658}
1659
1660static int vidioc_querycap(struct file *file, void *priv,
1661 struct v4l2_capability *cap)
1662{
1663 struct em28xx_fh *fh = priv;
1664 struct em28xx *dev = fh->dev;
1665
1666 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1667 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001668 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001669
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001670 cap->capabilities =
1671 V4L2_CAP_SLICED_VBI_CAPTURE |
1672 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001673 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1674
Devin Heitmueller04146142009-09-11 00:08:44 -03001675 if (dev->vbi_dev)
1676 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1677
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001678 if (dev->audio_mode.has_audio)
1679 cap->capabilities |= V4L2_CAP_AUDIO;
1680
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001681 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001682 cap->capabilities |= V4L2_CAP_TUNER;
1683
1684 return 0;
1685}
1686
Hans Verkuil78b526a2008-05-28 12:16:41 -03001687static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001688 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001689{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001690 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001691 return -EINVAL;
1692
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001693 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1694 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001695
1696 return 0;
1697}
1698
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001699static int vidioc_enum_framesizes(struct file *file, void *priv,
1700 struct v4l2_frmsizeenum *fsize)
1701{
1702 struct em28xx_fh *fh = priv;
1703 struct em28xx *dev = fh->dev;
1704 struct em28xx_fmt *fmt;
1705 unsigned int maxw = norm_maxw(dev);
1706 unsigned int maxh = norm_maxh(dev);
1707
1708 fmt = format_by_fourcc(fsize->pixel_format);
1709 if (!fmt) {
1710 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1711 fsize->pixel_format);
1712 return -EINVAL;
1713 }
1714
1715 if (dev->board.is_em2800) {
1716 if (fsize->index > 1)
1717 return -EINVAL;
1718 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1719 fsize->discrete.width = maxw / (1 + fsize->index);
1720 fsize->discrete.height = maxh / (1 + fsize->index);
1721 return 0;
1722 }
1723
1724 if (fsize->index != 0)
1725 return -EINVAL;
1726
1727 /* Report a continuous range */
1728 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1729 fsize->stepwise.min_width = 48;
1730 fsize->stepwise.min_height = 32;
1731 fsize->stepwise.max_width = maxw;
1732 fsize->stepwise.max_height = maxh;
1733 fsize->stepwise.step_width = 1;
1734 fsize->stepwise.step_height = 1;
1735 return 0;
1736}
1737
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001738/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001739static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001740 struct v4l2_format *f)
1741{
1742 struct em28xx_fh *fh = priv;
1743 struct em28xx *dev = fh->dev;
1744 int rc;
1745
1746 rc = check_dev(dev);
1747 if (rc < 0)
1748 return rc;
1749
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001750 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001751 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001752
1753 if (f->fmt.sliced.service_set == 0)
1754 rc = -EINVAL;
1755
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001756 return rc;
1757}
1758
Hans Verkuil78b526a2008-05-28 12:16:41 -03001759static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001760 struct v4l2_format *f)
1761{
1762 struct em28xx_fh *fh = priv;
1763 struct em28xx *dev = fh->dev;
1764 int rc;
1765
1766 rc = check_dev(dev);
1767 if (rc < 0)
1768 return rc;
1769
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001770 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001771
1772 if (f->fmt.sliced.service_set == 0)
1773 return -EINVAL;
1774
1775 return 0;
1776}
1777
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001778/* RAW VBI ioctls */
1779
1780static int vidioc_g_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}
1807
1808static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1809 struct v4l2_format *format)
1810{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001811 struct em28xx_fh *fh = priv;
1812 struct em28xx *dev = fh->dev;
1813
1814 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001815 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1816 format->fmt.vbi.offset = 0;
1817 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001818 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1819 format->fmt.vbi.count[0] = dev->vbi_height;
1820 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001821
1822 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001823 if (dev->norm & V4L2_STD_525_60) {
1824 /* NTSC */
1825 format->fmt.vbi.start[0] = 10;
1826 format->fmt.vbi.start[1] = 273;
1827 } else if (dev->norm & V4L2_STD_625_50) {
1828 /* PAL */
1829 format->fmt.vbi.start[0] = 6;
1830 format->fmt.vbi.start[1] = 318;
1831 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001832
1833 return 0;
1834}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001835
1836static int vidioc_reqbufs(struct file *file, void *priv,
1837 struct v4l2_requestbuffers *rb)
1838{
1839 struct em28xx_fh *fh = priv;
1840 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001841 int rc;
1842
1843 rc = check_dev(dev);
1844 if (rc < 0)
1845 return rc;
1846
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001847 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1848 return videobuf_reqbufs(&fh->vb_vidq, rb);
1849 else
1850 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001851}
1852
1853static int vidioc_querybuf(struct file *file, void *priv,
1854 struct v4l2_buffer *b)
1855{
1856 struct em28xx_fh *fh = priv;
1857 struct em28xx *dev = fh->dev;
1858 int rc;
1859
1860 rc = check_dev(dev);
1861 if (rc < 0)
1862 return rc;
1863
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001864 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1865 return videobuf_querybuf(&fh->vb_vidq, b);
1866 else {
1867 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1868 the videobuf framework, but we probably shouldn't be
1869 returning a buffer larger than that which was asked for.
1870 At a minimum, it causes a crash in zvbi since it does
1871 a memcpy based on the source buffer length */
1872 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001873 b->length = dev->vbi_width * dev->vbi_height * 2;
1874
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001875 return result;
1876 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001877}
1878
1879static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1880{
1881 struct em28xx_fh *fh = priv;
1882 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001883 int rc;
1884
1885 rc = check_dev(dev);
1886 if (rc < 0)
1887 return rc;
1888
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001889 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1890 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001891 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001892 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001893}
1894
1895static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1896{
1897 struct em28xx_fh *fh = priv;
1898 struct em28xx *dev = fh->dev;
1899 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001900
1901 rc = check_dev(dev);
1902 if (rc < 0)
1903 return rc;
1904
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001905 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1906 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1907 O_NONBLOCK);
1908 else
1909 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1910 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001911}
1912
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001913/* ----------------------------------------------------------- */
1914/* RADIO ESPECIFIC IOCTLS */
1915/* ----------------------------------------------------------- */
1916
1917static int radio_querycap(struct file *file, void *priv,
1918 struct v4l2_capability *cap)
1919{
1920 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1921
1922 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1923 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001924 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001925
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001926 cap->capabilities = V4L2_CAP_TUNER;
1927 return 0;
1928}
1929
1930static int radio_g_tuner(struct file *file, void *priv,
1931 struct v4l2_tuner *t)
1932{
1933 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1934
1935 if (unlikely(t->index > 0))
1936 return -EINVAL;
1937
1938 strcpy(t->name, "Radio");
1939 t->type = V4L2_TUNER_RADIO;
1940
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001941 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001942
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001943 return 0;
1944}
1945
1946static int radio_enum_input(struct file *file, void *priv,
1947 struct v4l2_input *i)
1948{
1949 if (i->index != 0)
1950 return -EINVAL;
1951 strcpy(i->name, "Radio");
1952 i->type = V4L2_INPUT_TYPE_TUNER;
1953
1954 return 0;
1955}
1956
1957static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1958{
1959 if (unlikely(a->index))
1960 return -EINVAL;
1961
1962 strcpy(a->name, "Radio");
1963 return 0;
1964}
1965
1966static int radio_s_tuner(struct file *file, void *priv,
1967 struct v4l2_tuner *t)
1968{
1969 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1970
1971 if (0 != t->index)
1972 return -EINVAL;
1973
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001974 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001975
1976 return 0;
1977}
1978
1979static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001980 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001981{
1982 return 0;
1983}
1984
1985static int radio_s_input(struct file *file, void *fh, unsigned int i)
1986{
1987 return 0;
1988}
1989
1990static int radio_queryctrl(struct file *file, void *priv,
1991 struct v4l2_queryctrl *qc)
1992{
1993 int i;
1994
1995 if (qc->id < V4L2_CID_BASE ||
1996 qc->id >= V4L2_CID_LASTP1)
1997 return -EINVAL;
1998
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001999 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2000 if (qc->id && qc->id == ac97_qctrl[i].id) {
2001 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002002 return 0;
2003 }
2004 }
2005
2006 return -EINVAL;
2007}
2008
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002009/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002010 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002011 * inits the device and starts isoc transfer
2012 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002013static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002014{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002015 int errCode = 0, radio = 0;
2016 struct video_device *vdev = video_devdata(filp);
2017 struct em28xx *dev = video_drvdata(filp);
2018 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002019 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002020 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002021
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002022 switch (vdev->vfl_type) {
2023 case VFL_TYPE_GRABBER:
2024 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2025 break;
2026 case VFL_TYPE_VBI:
2027 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2028 break;
2029 case VFL_TYPE_RADIO:
2030 radio = 1;
2031 break;
2032 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002033
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002034 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2035 video_device_node_name(vdev), v4l2_type_names[fh_type],
2036 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002037
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002038
Hans Verkuil876cb142012-06-23 08:12:47 -03002039 if (mutex_lock_interruptible(&dev->lock))
2040 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002041 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002042 if (!fh) {
2043 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002044 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002045 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002046 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002047 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002048 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002049 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002050 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002051
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002052 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002053 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002054 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002055 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002056
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002057 /* Needed, since GPIO might have disabled power of
2058 some i2c device
2059 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002060 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002061
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002062 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002063 if (fh->radio) {
2064 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002065 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002066 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002067
2068 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002069
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002070 if (dev->progressive)
2071 field = V4L2_FIELD_NONE;
2072 else
2073 field = V4L2_FIELD_INTERLACED;
2074
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002075 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2076 NULL, &dev->slock,
2077 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002078 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002079
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002080 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2081 NULL, &dev->slock,
2082 V4L2_BUF_TYPE_VBI_CAPTURE,
2083 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002084 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002085 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002086
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087 return errCode;
2088}
2089
2090/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002091 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002092 * unregisters the v4l2,i2c and usb devices
2093 * called when the device gets disconected or at module unload
2094*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002095void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002097
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002098 /*FIXME: I2C IR should be disconnected */
2099
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002100 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002101 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002102 video_unregister_device(dev->radio_dev);
2103 else
2104 video_device_release(dev->radio_dev);
2105 dev->radio_dev = NULL;
2106 }
2107 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002108 em28xx_info("V4L2 device %s deregistered\n",
2109 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002110 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002111 video_unregister_device(dev->vbi_dev);
2112 else
2113 video_device_release(dev->vbi_dev);
2114 dev->vbi_dev = NULL;
2115 }
2116 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002117 em28xx_info("V4L2 device %s deregistered\n",
2118 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002119 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002120 video_unregister_device(dev->vdev);
2121 else
2122 video_device_release(dev->vdev);
2123 dev->vdev = NULL;
2124 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002125}
2126
2127/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002128 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002129 * stops streaming and deallocates all resources allocated by the v4l2
2130 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002131 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002132static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002134 struct em28xx_fh *fh = filp->private_data;
2135 struct em28xx *dev = fh->dev;
2136 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002137
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002138 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139
Hans Verkuil876cb142012-06-23 08:12:47 -03002140 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002141 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002142 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002143 res_free(fh, EM28XX_RESOURCE_VIDEO);
2144 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002146 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2147 videobuf_stop(&fh->vb_vbiq);
2148 res_free(fh, EM28XX_RESOURCE_VBI);
2149 }
2150
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002151 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002152 /* the device is already disconnect,
2153 free the remaining resources */
2154 if (dev->state & DEV_DISCONNECTED) {
2155 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002156 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002157 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002158 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002159 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002160 return 0;
2161 }
2162
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002163 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002164 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002165
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002166 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002167 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002168 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002169
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002170 /* set alternate 0 */
2171 dev->alt = 0;
2172 em28xx_videodbg("setting alternate 0\n");
2173 errCode = usb_set_interface(dev->udev, 0, 0);
2174 if (errCode < 0) {
2175 em28xx_errdev("cannot change alternate number to "
2176 "0 (error=%i)\n", errCode);
2177 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002178 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002179
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002180 videobuf_mmap_free(&fh->vb_vidq);
2181 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002182 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002184 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185 return 0;
2186}
2187
2188/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002189 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002190 * will allocate buffers when called for the first time
2191 */
2192static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002193em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002194 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002196 struct em28xx_fh *fh = filp->private_data;
2197 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002198 int rc;
2199
2200 rc = check_dev(dev);
2201 if (rc < 0)
2202 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203
Hans Verkuil876cb142012-06-23 08:12:47 -03002204 if (mutex_lock_interruptible(&dev->lock))
2205 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002206 /* FIXME: read() is not prepared to allow changing the video
2207 resolution while streaming. Seems a bug at em28xx_set_fmt
2208 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002209
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002210 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002211 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002212 rc = -EBUSY;
2213 else
2214 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002215 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002216 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002217 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002218 rc = -EBUSY;
2219 else
2220 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002221 filp->f_flags & O_NONBLOCK);
2222 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002223 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002224
Hans Verkuil876cb142012-06-23 08:12:47 -03002225 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002226}
2227
2228/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002229 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002230 * will allocate buffers when called for the first time
2231 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002232static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002234 struct em28xx_fh *fh = filp->private_data;
2235 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002236 int rc;
2237
2238 rc = check_dev(dev);
2239 if (rc < 0)
2240 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002242 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2243 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2244 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002245 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002246 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2247 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2248 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002249 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002250 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002251 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002252 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002253}
2254
Hans Verkuil876cb142012-06-23 08:12:47 -03002255static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2256{
2257 struct em28xx_fh *fh = filp->private_data;
2258 struct em28xx *dev = fh->dev;
2259 unsigned int res;
2260
2261 mutex_lock(&dev->lock);
2262 res = em28xx_poll(filp, wait);
2263 mutex_unlock(&dev->lock);
2264 return res;
2265}
2266
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002267/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002268 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002269 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002270static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002271{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002272 struct em28xx_fh *fh = filp->private_data;
2273 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002274 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002275
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002276 rc = check_dev(dev);
2277 if (rc < 0)
2278 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002279
Hans Verkuil876cb142012-06-23 08:12:47 -03002280 if (mutex_lock_interruptible(&dev->lock))
2281 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002282 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2283 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2284 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2285 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002286 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002288 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2289 (unsigned long)vma->vm_start,
2290 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2291 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002292
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002293 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002294}
2295
Hans Verkuilbec43662008-12-30 06:58:20 -03002296static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002297 .owner = THIS_MODULE,
2298 .open = em28xx_v4l2_open,
2299 .release = em28xx_v4l2_close,
2300 .read = em28xx_v4l2_read,
2301 .poll = em28xx_v4l2_poll,
2302 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002303 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002304};
2305
Hans Verkuila3998102008-07-21 02:57:38 -03002306static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002307 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002308 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2309 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2310 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2311 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002312 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2313 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002314 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002315 .vidioc_g_audio = vidioc_g_audio,
2316 .vidioc_s_audio = vidioc_s_audio,
2317 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002318 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2319 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2320 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002321
2322 .vidioc_reqbufs = vidioc_reqbufs,
2323 .vidioc_querybuf = vidioc_querybuf,
2324 .vidioc_qbuf = vidioc_qbuf,
2325 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002326 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002327 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002328 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002329 .vidioc_g_parm = vidioc_g_parm,
2330 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002331 .vidioc_enum_input = vidioc_enum_input,
2332 .vidioc_g_input = vidioc_g_input,
2333 .vidioc_s_input = vidioc_s_input,
2334 .vidioc_queryctrl = vidioc_queryctrl,
2335 .vidioc_g_ctrl = vidioc_g_ctrl,
2336 .vidioc_s_ctrl = vidioc_s_ctrl,
2337 .vidioc_streamon = vidioc_streamon,
2338 .vidioc_streamoff = vidioc_streamoff,
2339 .vidioc_g_tuner = vidioc_g_tuner,
2340 .vidioc_s_tuner = vidioc_s_tuner,
2341 .vidioc_g_frequency = vidioc_g_frequency,
2342 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002343#ifdef CONFIG_VIDEO_ADV_DEBUG
2344 .vidioc_g_register = vidioc_g_register,
2345 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002346 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002347#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002348};
2349
2350static const struct video_device em28xx_video_template = {
2351 .fops = &em28xx_v4l_fops,
2352 .release = video_device_release,
2353 .ioctl_ops = &video_ioctl_ops,
2354
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002355 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002356 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002357};
2358
Hans Verkuilbec43662008-12-30 06:58:20 -03002359static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002360 .owner = THIS_MODULE,
2361 .open = em28xx_v4l2_open,
2362 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002363 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002364};
2365
2366static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002367 .vidioc_querycap = radio_querycap,
2368 .vidioc_g_tuner = radio_g_tuner,
2369 .vidioc_enum_input = radio_enum_input,
2370 .vidioc_g_audio = radio_g_audio,
2371 .vidioc_s_tuner = radio_s_tuner,
2372 .vidioc_s_audio = radio_s_audio,
2373 .vidioc_s_input = radio_s_input,
2374 .vidioc_queryctrl = radio_queryctrl,
2375 .vidioc_g_ctrl = vidioc_g_ctrl,
2376 .vidioc_s_ctrl = vidioc_s_ctrl,
2377 .vidioc_g_frequency = vidioc_g_frequency,
2378 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002379#ifdef CONFIG_VIDEO_ADV_DEBUG
2380 .vidioc_g_register = vidioc_g_register,
2381 .vidioc_s_register = vidioc_s_register,
2382#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002383};
2384
Hans Verkuila3998102008-07-21 02:57:38 -03002385static struct video_device em28xx_radio_template = {
2386 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002387 .fops = &radio_fops,
2388 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002389};
2390
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002391/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002392
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002393
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002394
Adrian Bunk532fe652008-01-28 22:10:48 -03002395static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002396 const struct video_device *template,
2397 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002398{
2399 struct video_device *vfd;
2400
2401 vfd = video_device_alloc();
2402 if (NULL == vfd)
2403 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002404
2405 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002406 vfd->v4l2_dev = &dev->v4l2_dev;
2407 vfd->release = video_device_release;
2408 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002409 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002410
2411 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2412 dev->name, type_name);
2413
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002414 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002415 return vfd;
2416}
2417
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002418int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002419{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002420 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002421 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002422 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002423
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002424 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2425 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002426
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002427 /* set default norm */
2428 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002429 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002430 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002431
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002432 /* Analog specific initialization */
2433 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002434
2435 maxw = norm_maxw(dev);
2436 /* MaxPacketSize for em2800 is too small to capture at full resolution
2437 * use half of maxw as the scaler can only scale to 50% */
2438 if (dev->board.is_em2800)
2439 maxw /= 2;
2440
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002441 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002442 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002443
Ezequiel García96371fc2012-03-23 18:09:34 -03002444 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002445
2446 /* Audio defaults */
2447 dev->mute = 1;
2448 dev->volume = 0x1f;
2449
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002450/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002451 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2452 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2453 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002454
2455 em28xx_set_outfmt(dev);
2456 em28xx_colorlevels_set_default(dev);
2457 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002458
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002459 /* allocate and fill video video_device struct */
2460 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2461 if (!dev->vdev) {
2462 em28xx_errdev("cannot allocate video_device.\n");
2463 return -ENODEV;
2464 }
2465
2466 /* register v4l2 video video_device */
2467 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2468 video_nr[dev->devno]);
2469 if (ret) {
2470 em28xx_errdev("unable to register video device (error=%i).\n",
2471 ret);
2472 return ret;
2473 }
2474
2475 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002476 if (em28xx_vbi_supported(dev) == 1) {
2477 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2478 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002479
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002480 /* register v4l2 vbi video_device */
2481 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2482 vbi_nr[dev->devno]);
2483 if (ret < 0) {
2484 em28xx_errdev("unable to register vbi device\n");
2485 return ret;
2486 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002487 }
2488
2489 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002490 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2491 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002492 if (!dev->radio_dev) {
2493 em28xx_errdev("cannot allocate video_device.\n");
2494 return -ENODEV;
2495 }
2496 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2497 radio_nr[dev->devno]);
2498 if (ret < 0) {
2499 em28xx_errdev("can't register radio device\n");
2500 return ret;
2501 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002502 em28xx_info("Registered radio device as %s\n",
2503 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002504 }
2505
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002506 em28xx_info("V4L2 video device registered as %s\n",
2507 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002508
2509 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002510 em28xx_info("V4L2 VBI device registered as %s\n",
2511 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002512
2513 return 0;
2514}