blob: da31fd495466018cf2734721a10eec045b132408 [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 Thorntone0fadfd2008-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);
379
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300380 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300383
Frank Schaefer74209dc2012-11-08 14:11:37 -0300384 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385
386 return;
387}
388
389/*
390 * video-buf generic routine to get the next available VBI buffer
391 */
392static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
394{
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396 char *outp;
397
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300400 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300401 *buf = NULL;
402 return;
403 }
404
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
410
Frank Schaefer74209dc2012-11-08 14:11:37 -0300411 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414}
415
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300416/* Processes and copies the URB data content to a frame buffer queue */
417static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300418{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300419 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300420 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300421 int xfer_bulk, num_packets, i, rc = 1;
422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -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)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300433
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300434 xfer_bulk = usb_pipebulk(urb->pipe);
435
Frank Schaefer74209dc2012-11-08 14:11:37 -0300436 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300437 if (buf != NULL)
438 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300439
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300440 if (xfer_bulk) /* bulk */
441 num_packets = 1;
442 else /* isoc */
443 num_packets = urb->number_of_packets;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300445 for (i = 0; i < num_packets; i++) {
446 if (xfer_bulk) { /* bulk */
447 actual_length = urb->actual_length;
448
449 p = urb->transfer_buffer;
450 } else { /* isoc */
451 if (urb->iso_frame_desc[i].status < 0) {
452 print_err_status(dev, i,
453 urb->iso_frame_desc[i].status);
454 if (urb->iso_frame_desc[i].status != -EPROTO)
455 continue;
456 }
457
458 actual_length = urb->iso_frame_desc[i].actual_length;
459 if (actual_length > dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 continue;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300462 }
463
464 p = urb->transfer_buffer +
465 urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 }
467
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300468 if (actual_length <= 0) {
469 /* NOTE: happens very often with isoc transfers */
470 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300471 continue;
472 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473
474 /* FIXME: incomplete buffer checks where removed to make
475 logic simpler. Impacts of those changes should be evaluated
476 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300477 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478 em28xx_isocdbg("VBI HEADER!!!\n");
479 /* FIXME: Should add vbi copy */
480 continue;
481 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300482 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300483 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300484 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300486 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300487 if (buf != NULL)
488 buffer_filled(dev, dma_q, buf);
489 get_next_buf(dma_q, &buf);
490 if (buf == NULL)
491 outp = NULL;
492 else
493 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300494 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300495
496 if (buf != NULL) {
497 if (p[2] & 1)
498 buf->top_field = 0;
499 else
500 buf->top_field = 1;
501 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300502
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300505 if (buf != NULL) {
506 if (p[0] != 0x88 && p[0] != 0x22) {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300507 /* NOTE: no intermediate data packet header
508 * 88 88 88 88 when using bulk transfers */
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300509 em28xx_isocdbg("frame is not complete\n");
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300510 len = actual_length;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300511 } else {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300512 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300513 p += 4;
514 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300515 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300516 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300517 }
518 return rc;
519}
520
Frank Schaefer4601cc32012-11-08 14:11:46 -0300521/* Version of the urb data handler that takes into account a mixture of
522 video and VBI data */
523static inline int em28xx_urb_data_copy_vbi(struct em28xx *dev, struct urb *urb)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300524{
525 struct em28xx_buffer *buf, *vbi_buf;
526 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300527 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300528 int xfer_bulk, vbi_size, num_packets, i, rc = 1;
529 unsigned int actual_length, len = 0;
530 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300531
532 if (!dev)
533 return 0;
534
535 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
536 return 0;
537
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300538 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300539 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300540
Frank Schaefer4601cc32012-11-08 14:11:46 -0300541 xfer_bulk = usb_pipebulk(urb->pipe);
542
Frank Schaefer74209dc2012-11-08 14:11:37 -0300543 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300544 if (buf != NULL)
545 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300546
Frank Schaefer74209dc2012-11-08 14:11:37 -0300547 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300548 if (vbi_buf != NULL)
549 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
550
Frank Schaefer4601cc32012-11-08 14:11:46 -0300551 if (xfer_bulk) /* bulk */
552 num_packets = 1;
553 else /* isoc */
554 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300555
Frank Schaefer4601cc32012-11-08 14:11:46 -0300556 for (i = 0; i < num_packets; i++) {
557 if (xfer_bulk) { /* bulk */
558 actual_length = urb->actual_length;
559
560 p = urb->transfer_buffer;
561 } else { /* isoc */
562 if (urb->iso_frame_desc[i].status < 0) {
563 print_err_status(dev, i,
564 urb->iso_frame_desc[i].status);
565 if (urb->iso_frame_desc[i].status != -EPROTO)
566 continue;
567 }
568
569 actual_length = urb->iso_frame_desc[i].actual_length;
570 if (actual_length > dev->max_pkt_size) {
571 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300572 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300573 }
574
575 p = urb->transfer_buffer +
576 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300577 }
578
Frank Schaefer3610f582012-11-25 06:37:33 -0300579 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300580 /* NOTE: happens very often with isoc transfers */
581 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300582 continue;
583 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300584
585 /* capture type 0 = vbi start
586 capture type 1 = video start
587 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300588 len = actual_length;
589 if (len >= 4) {
590 /* NOTE: headers are always 4 bytes and
591 * never split across packets */
592 if (p[0] == 0x33 && p[1] == 0x95) {
593 dev->capture_type = 0;
594 dev->vbi_read = 0;
595 em28xx_isocdbg("VBI START HEADER!!!\n");
596 dev->cur_field = p[2];
597 p += 4;
598 len -= 4;
599 } else if (p[0] == 0x88 && p[1] == 0x88 &&
600 p[2] == 0x88 && p[3] == 0x88) {
601 /* continuation */
602 p += 4;
603 len -= 4;
604 } else if (p[0] == 0x22 && p[1] == 0x5a) {
605 /* start video */
606 p += 4;
607 len -= 4;
608 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300609 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300610 /* NOTE: with bulk transfers, intermediate data packets
611 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300612
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300613 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300614
615 if (dev->capture_type == 0) {
616 if (dev->vbi_read >= vbi_size) {
617 /* We've already read all the VBI data, so
618 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300619 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300620 } else if ((dev->vbi_read + len) < vbi_size) {
621 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300622 if (dev->vbi_read == 0 &&
623 (!(dev->cur_field & 1))) {
624 /* Brand new frame */
625 if (vbi_buf != NULL)
626 vbi_buffer_filled(dev,
627 vbi_dma_q,
628 vbi_buf);
629 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
630 if (vbi_buf == NULL)
631 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300632 else
633 vbioutp = videobuf_to_vmalloc(
634 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300635 }
636
637 if (dev->vbi_read == 0) {
638 vbi_dma_q->pos = 0;
639 if (vbi_buf != NULL) {
640 if (dev->cur_field & 1)
641 vbi_buf->top_field = 0;
642 else
643 vbi_buf->top_field = 1;
644 }
645 }
646
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300647 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300648 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
649 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300650 } else {
651 /* Some of this frame is VBI data and some is
652 video data */
653 int vbi_data_len = vbi_size - dev->vbi_read;
654 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300655 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
656 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300657 dev->capture_type = 1;
658 p += vbi_data_len;
659 len -= vbi_data_len;
660 }
661 }
662
663 if (dev->capture_type == 1) {
664 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300665 if (dev->progressive || !(dev->cur_field & 1)) {
666 if (buf != NULL)
667 buffer_filled(dev, dma_q, buf);
668 get_next_buf(dma_q, &buf);
669 if (buf == NULL)
670 outp = NULL;
671 else
672 outp = videobuf_to_vmalloc(&buf->vb);
673 }
674 if (buf != NULL) {
675 if (dev->cur_field & 1)
676 buf->top_field = 0;
677 else
678 buf->top_field = 1;
679 }
680
681 dma_q->pos = 0;
682 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300683
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300684 if (buf != NULL && dev->capture_type == 2 && len > 0)
685 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300686 }
687 return rc;
688}
689
690
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300691/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300692 Videobuf operations
693 ------------------------------------------------------------------*/
694
695static int
696buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
697{
698 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300699 struct em28xx *dev = fh->dev;
700 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300701
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300702 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
703 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300704
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300705 if (0 == *count)
706 *count = EM28XX_DEF_BUF;
707
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300708 if (*count < EM28XX_MIN_BUF)
709 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300710
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300711 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300712 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300713 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300714 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300715
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300716 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300717
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300718 return 0;
719}
720
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300721/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300722static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
723{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300724 struct em28xx_fh *fh = vq->priv_data;
725 struct em28xx *dev = fh->dev;
726 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300727 if (in_interrupt())
728 BUG();
729
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300730 /* We used to wait for the buffer to finish here, but this didn't work
731 because, as we were keeping the state as VIDEOBUF_QUEUED,
732 videobuf_queue_cancel marked it as finished for us.
733 (Also, it could wedge forever if the hardware was misconfigured.)
734
735 This should be safe; by the time we get here, the buffer isn't
736 queued anymore. If we ever start marking the buffers as
737 VIDEOBUF_ACTIVE, it won't be, though.
738 */
739 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300740 if (dev->usb_ctl.vid_buf == buf)
741 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300742 spin_unlock_irqrestore(&dev->slock, flags);
743
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744 videobuf_vmalloc_free(&buf->vb);
745 buf->vb.state = VIDEOBUF_NEEDS_INIT;
746}
747
748static int
749buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
750 enum v4l2_field field)
751{
752 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300753 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300754 struct em28xx *dev = fh->dev;
755 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300757 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
758 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759
760 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
761 return -EINVAL;
762
Brandon Philips05612972008-04-13 14:57:01 -0300763 buf->vb.width = dev->width;
764 buf->vb.height = dev->height;
765 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300766
767 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300768 rc = videobuf_iolock(vq, &buf->vb, NULL);
769 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300770 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300771 }
772
Frank Schaefer74209dc2012-11-08 14:11:37 -0300773 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300774 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300775
776 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300777 if (em28xx_vbi_supported(dev) == 1)
Frank Schaeferc647a912012-11-08 14:11:52 -0300778 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
779 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300780 EM28XX_NUM_BUFS,
781 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300782 dev->packet_multiplier,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300783 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300784 else
Frank Schaeferc647a912012-11-08 14:11:52 -0300785 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
786 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300787 EM28XX_NUM_BUFS,
788 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300789 dev->packet_multiplier,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300790 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300791 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300792 goto fail;
793 }
794
795 buf->vb.state = VIDEOBUF_PREPARED;
796 return 0;
797
798fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300799 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300800 return rc;
801}
802
803static void
804buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
805{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300806 struct em28xx_buffer *buf = container_of(vb,
807 struct em28xx_buffer,
808 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300809 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300810 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300811 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300812
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300813 buf->vb.state = VIDEOBUF_QUEUED;
814 list_add_tail(&buf->vb.queue, &vidq->active);
815
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300816}
817
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300818static void buffer_release(struct videobuf_queue *vq,
819 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300820{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300821 struct em28xx_buffer *buf = container_of(vb,
822 struct em28xx_buffer,
823 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300824 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300825 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300826
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300827 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300828
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300829 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300830}
831
832static struct videobuf_queue_ops em28xx_video_qops = {
833 .buf_setup = buffer_setup,
834 .buf_prepare = buffer_prepare,
835 .buf_queue = buffer_queue,
836 .buf_release = buffer_release,
837};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800838
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300839/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800840
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800841static void video_mux(struct em28xx *dev, int index)
842{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800843 dev->ctl_input = index;
844 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300845 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800846
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300847 if (!dev->ctl_aoutput)
848 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
849
Hans Verkuil5325b422009-04-02 11:26:22 -0300850 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
851 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800852
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300853 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300854 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300855 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
856 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300857 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300858 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300859 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
860 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800861 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300862
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300863 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300864 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
865 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300866 }
867
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300868 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800869}
870
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300871/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300872static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300873{
874 struct em28xx *dev = fh->dev;
875
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300876 if (fh->resources & bit)
877 /* have it already allocated */
878 return 1;
879
880 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300881 if (dev->resources & bit) {
882 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300883 return 0;
884 }
885 /* it's free, grab it */
886 fh->resources |= bit;
887 dev->resources |= bit;
888 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300889 return 1;
890}
891
892static int res_check(struct em28xx_fh *fh, unsigned int bit)
893{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300894 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300895}
896
897static int res_locked(struct em28xx *dev, unsigned int bit)
898{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300899 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300900}
901
902static void res_free(struct em28xx_fh *fh, unsigned int bits)
903{
904 struct em28xx *dev = fh->dev;
905
906 BUG_ON((fh->resources & bits) != bits);
907
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300908 fh->resources &= ~bits;
909 dev->resources &= ~bits;
910 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300911}
912
913static int get_ressource(struct em28xx_fh *fh)
914{
915 switch (fh->type) {
916 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
917 return EM28XX_RESOURCE_VIDEO;
918 case V4L2_BUF_TYPE_VBI_CAPTURE:
919 return EM28XX_RESOURCE_VBI;
920 default:
921 BUG();
922 return 0;
923 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300924}
925
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800926/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300927 * ac97_queryctrl()
928 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300929 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300930static int ac97_queryctrl(struct v4l2_queryctrl *qc)
931{
932 int i;
933
934 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
935 if (qc->id && qc->id == ac97_qctrl[i].id) {
936 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
937 return 0;
938 }
939 }
940
941 /* Control is not ac97 related */
942 return 1;
943}
944
945/*
946 * ac97_get_ctrl()
947 * return the current values for ac97 mute and volume
948 */
949static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300950{
951 switch (ctrl->id) {
952 case V4L2_CID_AUDIO_MUTE:
953 ctrl->value = dev->mute;
954 return 0;
955 case V4L2_CID_AUDIO_VOLUME:
956 ctrl->value = dev->volume;
957 return 0;
958 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300959 /* Control is not ac97 related */
960 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961 }
962}
963
964/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300965 * ac97_set_ctrl()
966 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300968static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300969{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300970 int i;
971
972 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
973 if (ctrl->id == ac97_qctrl[i].id)
974 goto handle;
975
976 /* Announce that hasn't handle it */
977 return 1;
978
979handle:
980 if (ctrl->value < ac97_qctrl[i].minimum ||
981 ctrl->value > ac97_qctrl[i].maximum)
982 return -ERANGE;
983
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984 switch (ctrl->id) {
985 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300986 dev->mute = ctrl->value;
987 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988 case V4L2_CID_AUDIO_VOLUME:
989 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300990 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300992
993 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300994}
995
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300996static int check_dev(struct em28xx *dev)
997{
998 if (dev->state & DEV_DISCONNECTED) {
999 em28xx_errdev("v4l2 ioctl: device not present\n");
1000 return -ENODEV;
1001 }
1002
1003 if (dev->state & DEV_MISCONFIGURED) {
1004 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1005 "close and open it again\n");
1006 return -EIO;
1007 }
1008 return 0;
1009}
1010
1011static void get_scale(struct em28xx *dev,
1012 unsigned int width, unsigned int height,
1013 unsigned int *hscale, unsigned int *vscale)
1014{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001015 unsigned int maxw = norm_maxw(dev);
1016 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001017
1018 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1019 if (*hscale >= 0x4000)
1020 *hscale = 0x3fff;
1021
1022 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1023 if (*vscale >= 0x4000)
1024 *vscale = 0x3fff;
1025}
1026
1027/* ------------------------------------------------------------------
1028 IOCTL vidioc handling
1029 ------------------------------------------------------------------*/
1030
Hans Verkuil78b526a2008-05-28 12:16:41 -03001031static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001032 struct v4l2_format *f)
1033{
1034 struct em28xx_fh *fh = priv;
1035 struct em28xx *dev = fh->dev;
1036
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001037 f->fmt.pix.width = dev->width;
1038 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001039 f->fmt.pix.pixelformat = dev->format->fourcc;
1040 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001041 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001042 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1043
1044 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001045 if (dev->progressive)
1046 f->fmt.pix.field = V4L2_FIELD_NONE;
1047 else
1048 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001049 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001050 return 0;
1051}
1052
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001053static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1054{
1055 unsigned int i;
1056
1057 for (i = 0; i < ARRAY_SIZE(format); i++)
1058 if (format[i].fourcc == fourcc)
1059 return &format[i];
1060
1061 return NULL;
1062}
1063
Hans Verkuil78b526a2008-05-28 12:16:41 -03001064static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001065 struct v4l2_format *f)
1066{
1067 struct em28xx_fh *fh = priv;
1068 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001069 unsigned int width = f->fmt.pix.width;
1070 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001071 unsigned int maxw = norm_maxw(dev);
1072 unsigned int maxh = norm_maxh(dev);
1073 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001074 struct em28xx_fmt *fmt;
1075
1076 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1077 if (!fmt) {
1078 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1079 f->fmt.pix.pixelformat);
1080 return -EINVAL;
1081 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001083 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001084 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001085 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1086 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001087 /* MaxPacketSize for em2800 is too small to capture at full resolution
1088 * use half of maxw as the scaler can only scale to 50% */
1089 if (width == maxw && height == maxh)
1090 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001091 } else {
1092 /* width must even because of the YUYV format
1093 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001094 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1095 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 }
1097
1098 get_scale(dev, width, height, &hscale, &vscale);
1099
1100 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1101 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1102
1103 f->fmt.pix.width = width;
1104 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001105 f->fmt.pix.pixelformat = fmt->fourcc;
1106 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1107 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001108 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001109 if (dev->progressive)
1110 f->fmt.pix.field = V4L2_FIELD_NONE;
1111 else
1112 f->fmt.pix.field = dev->interlaced ?
1113 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001114
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001115 return 0;
1116}
1117
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001118static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1119 unsigned width, unsigned height)
1120{
1121 struct em28xx_fmt *fmt;
1122
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001123 fmt = format_by_fourcc(fourcc);
1124 if (!fmt)
1125 return -EINVAL;
1126
1127 dev->format = fmt;
1128 dev->width = width;
1129 dev->height = height;
1130
1131 /* set new image size */
1132 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1133
1134 em28xx_set_alternate(dev);
1135 em28xx_resolution_set(dev);
1136
1137 return 0;
1138}
1139
Hans Verkuil78b526a2008-05-28 12:16:41 -03001140static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141 struct v4l2_format *f)
1142{
1143 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001145 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001146
1147 rc = check_dev(dev);
1148 if (rc < 0)
1149 return rc;
1150
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001151 vidioc_try_fmt_vid_cap(file, priv, f);
1152
Brandon Philips05612972008-04-13 14:57:01 -03001153 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1154 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001155 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001156 }
1157
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001158 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001159 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160}
1161
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001162static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1163{
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001166 int rc;
1167
1168 rc = check_dev(dev);
1169 if (rc < 0)
1170 return rc;
1171
1172 *norm = dev->norm;
1173
1174 return 0;
1175}
1176
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001177static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1178{
1179 struct em28xx_fh *fh = priv;
1180 struct em28xx *dev = fh->dev;
1181 int rc;
1182
1183 rc = check_dev(dev);
1184 if (rc < 0)
1185 return rc;
1186
1187 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1188
1189 return 0;
1190}
1191
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001192static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001193{
1194 struct em28xx_fh *fh = priv;
1195 struct em28xx *dev = fh->dev;
1196 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197 int rc;
1198
1199 rc = check_dev(dev);
1200 if (rc < 0)
1201 return rc;
1202
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001203 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204
1205 /* Adjusts width/height, if needed */
1206 f.fmt.pix.width = dev->width;
1207 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001208 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001209
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001210 /* set new image size */
1211 dev->width = f.fmt.pix.width;
1212 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001213 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1214
1215 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001216 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001217
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001218 return 0;
1219}
1220
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001221static int vidioc_g_parm(struct file *file, void *priv,
1222 struct v4l2_streamparm *p)
1223{
1224 struct em28xx_fh *fh = priv;
1225 struct em28xx *dev = fh->dev;
1226 int rc = 0;
1227
1228 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1229 return -EINVAL;
1230
1231 if (dev->board.is_webcam)
1232 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1233 video, g_parm, p);
1234 else
1235 v4l2_video_std_frame_period(dev->norm,
1236 &p->parm.capture.timeperframe);
1237
1238 return rc;
1239}
1240
1241static int vidioc_s_parm(struct file *file, void *priv,
1242 struct v4l2_streamparm *p)
1243{
1244 struct em28xx_fh *fh = priv;
1245 struct em28xx *dev = fh->dev;
1246
1247 if (!dev->board.is_webcam)
1248 return -EINVAL;
1249
1250 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1251 return -EINVAL;
1252
1253 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1254}
1255
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001256static const char *iname[] = {
1257 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1258 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1259 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1260 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1261 [EM28XX_VMUX_SVIDEO] = "S-Video",
1262 [EM28XX_VMUX_TELEVISION] = "Television",
1263 [EM28XX_VMUX_CABLE] = "Cable TV",
1264 [EM28XX_VMUX_DVB] = "DVB",
1265 [EM28XX_VMUX_DEBUG] = "for debug only",
1266};
1267
1268static int vidioc_enum_input(struct file *file, void *priv,
1269 struct v4l2_input *i)
1270{
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
1273 unsigned int n;
1274
1275 n = i->index;
1276 if (n >= MAX_EM28XX_INPUT)
1277 return -EINVAL;
1278 if (0 == INPUT(n)->type)
1279 return -EINVAL;
1280
1281 i->index = n;
1282 i->type = V4L2_INPUT_TYPE_CAMERA;
1283
1284 strcpy(i->name, iname[INPUT(n)->type]);
1285
1286 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1287 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1288 i->type = V4L2_INPUT_TYPE_TUNER;
1289
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001290 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291
1292 return 0;
1293}
1294
1295static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1296{
1297 struct em28xx_fh *fh = priv;
1298 struct em28xx *dev = fh->dev;
1299
1300 *i = dev->ctl_input;
1301
1302 return 0;
1303}
1304
1305static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1306{
1307 struct em28xx_fh *fh = priv;
1308 struct em28xx *dev = fh->dev;
1309 int rc;
1310
1311 rc = check_dev(dev);
1312 if (rc < 0)
1313 return rc;
1314
1315 if (i >= MAX_EM28XX_INPUT)
1316 return -EINVAL;
1317 if (0 == INPUT(i)->type)
1318 return -EINVAL;
1319
Ezequiel García96371fc2012-03-23 18:09:34 -03001320 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321 return 0;
1322}
1323
1324static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1325{
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001329 if (!dev->audio_mode.has_audio)
1330 return -EINVAL;
1331
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001332 switch (a->index) {
1333 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001334 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001335 break;
1336 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001337 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001338 break;
1339 case EM28XX_AMUX_VIDEO2:
1340 strcpy(a->name, "Television alt");
1341 break;
1342 case EM28XX_AMUX_PHONE:
1343 strcpy(a->name, "Phone");
1344 break;
1345 case EM28XX_AMUX_MIC:
1346 strcpy(a->name, "Mic");
1347 break;
1348 case EM28XX_AMUX_CD:
1349 strcpy(a->name, "CD");
1350 break;
1351 case EM28XX_AMUX_AUX:
1352 strcpy(a->name, "Aux");
1353 break;
1354 case EM28XX_AMUX_PCM_OUT:
1355 strcpy(a->name, "PCM");
1356 break;
1357 default:
1358 return -EINVAL;
1359 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001360
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001361 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363
1364 return 0;
1365}
1366
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001367static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368{
1369 struct em28xx_fh *fh = priv;
1370 struct em28xx *dev = fh->dev;
1371
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001372
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001373 if (!dev->audio_mode.has_audio)
1374 return -EINVAL;
1375
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001376 if (a->index >= MAX_EM28XX_INPUT)
1377 return -EINVAL;
1378 if (0 == INPUT(a->index)->type)
1379 return -EINVAL;
1380
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001381 dev->ctl_ainput = INPUT(a->index)->amux;
1382 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001383
1384 if (!dev->ctl_aoutput)
1385 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001386
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387 return 0;
1388}
1389
1390static int vidioc_queryctrl(struct file *file, void *priv,
1391 struct v4l2_queryctrl *qc)
1392{
1393 struct em28xx_fh *fh = priv;
1394 struct em28xx *dev = fh->dev;
1395 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396 int rc;
1397
1398 rc = check_dev(dev);
1399 if (rc < 0)
1400 return rc;
1401
1402 memset(qc, 0, sizeof(*qc));
1403
1404 qc->id = id;
1405
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001406 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001407 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1408 rc = ac97_queryctrl(qc);
1409 if (!rc)
1410 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001412
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001413 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001414 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415
1416 if (qc->type)
1417 return 0;
1418 else
1419 return -EINVAL;
1420}
1421
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001422/*
1423 * FIXME: This is an indirect way to check if a control exists at a
1424 * subdev. Instead of that hack, maybe the better would be to change all
1425 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1426 */
1427static int check_subdev_ctrl(struct em28xx *dev, int id)
1428{
1429 struct v4l2_queryctrl qc;
1430
1431 memset(&qc, 0, sizeof(qc));
1432 qc.id = id;
1433
1434 /* enumerate V4L2 device controls */
1435 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1436
1437 if (qc.type)
1438 return 0;
1439 else
1440 return -EINVAL;
1441}
1442
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443static int vidioc_g_ctrl(struct file *file, void *priv,
1444 struct v4l2_control *ctrl)
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;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001453 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001454
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001455 /* Set an AC97 control */
1456 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1457 rc = ac97_get_ctrl(dev, ctrl);
1458 else
1459 rc = 1;
1460
1461 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1462 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001463 if (check_subdev_ctrl(dev, ctrl->id))
1464 return -EINVAL;
1465
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001466 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001467 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001468 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001469
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001470 return rc;
1471}
1472
1473static int vidioc_s_ctrl(struct file *file, void *priv,
1474 struct v4l2_control *ctrl)
1475{
1476 struct em28xx_fh *fh = priv;
1477 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001478 int rc;
1479
1480 rc = check_dev(dev);
1481 if (rc < 0)
1482 return rc;
1483
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001484 /* Set an AC97 control */
1485 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1486 rc = ac97_set_ctrl(dev, ctrl);
1487 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001489
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001490 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001491 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001492 rc = check_subdev_ctrl(dev, ctrl->id);
1493 if (!rc)
1494 v4l2_device_call_all(&dev->v4l2_dev, 0,
1495 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001496 /*
1497 * In the case of non-AC97 volume controls, we still need
1498 * to do some setups at em28xx, in order to mute/unmute
1499 * and to adjust audio volume. However, the value ranges
1500 * should be checked by the corresponding V4L subdriver.
1501 */
1502 switch (ctrl->id) {
1503 case V4L2_CID_AUDIO_MUTE:
1504 dev->mute = ctrl->value;
1505 rc = em28xx_audio_analog_set(dev);
1506 break;
1507 case V4L2_CID_AUDIO_VOLUME:
1508 dev->volume = ctrl->value;
1509 rc = em28xx_audio_analog_set(dev);
1510 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001511 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001512 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001513}
1514
1515static int vidioc_g_tuner(struct file *file, void *priv,
1516 struct v4l2_tuner *t)
1517{
1518 struct em28xx_fh *fh = priv;
1519 struct em28xx *dev = fh->dev;
1520 int rc;
1521
1522 rc = check_dev(dev);
1523 if (rc < 0)
1524 return rc;
1525
1526 if (0 != t->index)
1527 return -EINVAL;
1528
1529 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001530 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001531
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001532 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001533 return 0;
1534}
1535
1536static int vidioc_s_tuner(struct file *file, void *priv,
1537 struct v4l2_tuner *t)
1538{
1539 struct em28xx_fh *fh = priv;
1540 struct em28xx *dev = fh->dev;
1541 int rc;
1542
1543 rc = check_dev(dev);
1544 if (rc < 0)
1545 return rc;
1546
1547 if (0 != t->index)
1548 return -EINVAL;
1549
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001550 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001551 return 0;
1552}
1553
1554static int vidioc_g_frequency(struct file *file, void *priv,
1555 struct v4l2_frequency *f)
1556{
1557 struct em28xx_fh *fh = priv;
1558 struct em28xx *dev = fh->dev;
1559
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001560 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001561 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001562 return 0;
1563}
1564
1565static int vidioc_s_frequency(struct file *file, void *priv,
1566 struct v4l2_frequency *f)
1567{
1568 struct em28xx_fh *fh = priv;
1569 struct em28xx *dev = fh->dev;
1570 int rc;
1571
1572 rc = check_dev(dev);
1573 if (rc < 0)
1574 return rc;
1575
1576 if (0 != f->tuner)
1577 return -EINVAL;
1578
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001579 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1580 return -EINVAL;
1581 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582 return -EINVAL;
1583
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001584 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001585 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001586
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001587 return 0;
1588}
1589
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001590#ifdef CONFIG_VIDEO_ADV_DEBUG
1591static int em28xx_reg_len(int reg)
1592{
1593 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001594 case EM28XX_R40_AC97LSB:
1595 case EM28XX_R30_HSCALELOW:
1596 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001597 return 2;
1598 default:
1599 return 1;
1600 }
1601}
1602
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001603static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001604 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001605{
1606 struct em28xx_fh *fh = priv;
1607 struct em28xx *dev = fh->dev;
1608
1609 chip->ident = V4L2_IDENT_NONE;
1610 chip->revision = 0;
1611
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001612 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001613
1614 return 0;
1615}
1616
1617
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001618static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001619 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001620{
1621 struct em28xx_fh *fh = priv;
1622 struct em28xx *dev = fh->dev;
1623 int ret;
1624
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001625 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001626 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001627 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001628 if (ret < 0)
1629 return ret;
1630
1631 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001632 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001633 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001634 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001635 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001636 return 0;
1637 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001638 /* TODO: is this correct? */
1639 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1640 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001641 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001642 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001643 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001644 }
1645
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001646 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001647 reg->size = em28xx_reg_len(reg->reg);
1648 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001649 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001650
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001651 if (ret < 0)
1652 return ret;
1653
1654 reg->val = ret;
1655 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001656 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001657 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1658 reg->reg, (char *)&val, 2);
1659 if (ret < 0)
1660 return ret;
1661
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001662 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001663 }
1664
1665 return 0;
1666}
1667
1668static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001669 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001670{
1671 struct em28xx_fh *fh = priv;
1672 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001673 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001674
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001675 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001676 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001677 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001678 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001679 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001680 return 0;
1681 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001682 /* TODO: is this correct? */
1683 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1684 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001685 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001686 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001687 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001688 }
1689
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001690 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001691 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001692
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001693 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001694 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001695}
1696#endif
1697
1698
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001699static int vidioc_cropcap(struct file *file, void *priv,
1700 struct v4l2_cropcap *cc)
1701{
1702 struct em28xx_fh *fh = priv;
1703 struct em28xx *dev = fh->dev;
1704
1705 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1706 return -EINVAL;
1707
1708 cc->bounds.left = 0;
1709 cc->bounds.top = 0;
1710 cc->bounds.width = dev->width;
1711 cc->bounds.height = dev->height;
1712 cc->defrect = cc->bounds;
1713 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1714 cc->pixelaspect.denominator = 59;
1715
1716 return 0;
1717}
1718
1719static int vidioc_streamon(struct file *file, void *priv,
1720 enum v4l2_buf_type type)
1721{
1722 struct em28xx_fh *fh = priv;
1723 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001724 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001725
1726 rc = check_dev(dev);
1727 if (rc < 0)
1728 return rc;
1729
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001730 if (unlikely(type != fh->type))
1731 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001732
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001733 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1734 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001735
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001736 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001737 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001738
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001739 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1740 rc = videobuf_streamon(&fh->vb_vidq);
1741 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1742 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001743
1744 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001745}
1746
1747static int vidioc_streamoff(struct file *file, void *priv,
1748 enum v4l2_buf_type type)
1749{
1750 struct em28xx_fh *fh = priv;
1751 struct em28xx *dev = fh->dev;
1752 int rc;
1753
1754 rc = check_dev(dev);
1755 if (rc < 0)
1756 return rc;
1757
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001758 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1759 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001760 return -EINVAL;
1761 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001762 return -EINVAL;
1763
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001764 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1765 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001766
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001767 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001768 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1769 videobuf_streamoff(&fh->vb_vidq);
1770 res_free(fh, EM28XX_RESOURCE_VIDEO);
1771 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001772 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001773 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1774 videobuf_streamoff(&fh->vb_vbiq);
1775 res_free(fh, EM28XX_RESOURCE_VBI);
1776 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001777 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001778
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001779 return 0;
1780}
1781
1782static int vidioc_querycap(struct file *file, void *priv,
1783 struct v4l2_capability *cap)
1784{
1785 struct em28xx_fh *fh = priv;
1786 struct em28xx *dev = fh->dev;
1787
1788 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1789 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001790 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 cap->capabilities =
1793 V4L2_CAP_SLICED_VBI_CAPTURE |
1794 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001795 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1796
Devin Heitmueller04146142009-09-11 00:08:44 -03001797 if (dev->vbi_dev)
1798 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1799
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001800 if (dev->audio_mode.has_audio)
1801 cap->capabilities |= V4L2_CAP_AUDIO;
1802
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001803 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804 cap->capabilities |= V4L2_CAP_TUNER;
1805
1806 return 0;
1807}
1808
Hans Verkuil78b526a2008-05-28 12:16:41 -03001809static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001810 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001811{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001812 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001813 return -EINVAL;
1814
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001815 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1816 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817
1818 return 0;
1819}
1820
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001821static int vidioc_enum_framesizes(struct file *file, void *priv,
1822 struct v4l2_frmsizeenum *fsize)
1823{
1824 struct em28xx_fh *fh = priv;
1825 struct em28xx *dev = fh->dev;
1826 struct em28xx_fmt *fmt;
1827 unsigned int maxw = norm_maxw(dev);
1828 unsigned int maxh = norm_maxh(dev);
1829
1830 fmt = format_by_fourcc(fsize->pixel_format);
1831 if (!fmt) {
1832 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1833 fsize->pixel_format);
1834 return -EINVAL;
1835 }
1836
1837 if (dev->board.is_em2800) {
1838 if (fsize->index > 1)
1839 return -EINVAL;
1840 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1841 fsize->discrete.width = maxw / (1 + fsize->index);
1842 fsize->discrete.height = maxh / (1 + fsize->index);
1843 return 0;
1844 }
1845
1846 if (fsize->index != 0)
1847 return -EINVAL;
1848
1849 /* Report a continuous range */
1850 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1851 fsize->stepwise.min_width = 48;
1852 fsize->stepwise.min_height = 32;
1853 fsize->stepwise.max_width = maxw;
1854 fsize->stepwise.max_height = maxh;
1855 fsize->stepwise.step_width = 1;
1856 fsize->stepwise.step_height = 1;
1857 return 0;
1858}
1859
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001861static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001862 struct v4l2_format *f)
1863{
1864 struct em28xx_fh *fh = priv;
1865 struct em28xx *dev = fh->dev;
1866 int rc;
1867
1868 rc = check_dev(dev);
1869 if (rc < 0)
1870 return rc;
1871
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001872 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001873 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001874
1875 if (f->fmt.sliced.service_set == 0)
1876 rc = -EINVAL;
1877
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001878 return rc;
1879}
1880
Hans Verkuil78b526a2008-05-28 12:16:41 -03001881static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001882 struct v4l2_format *f)
1883{
1884 struct em28xx_fh *fh = priv;
1885 struct em28xx *dev = fh->dev;
1886 int rc;
1887
1888 rc = check_dev(dev);
1889 if (rc < 0)
1890 return rc;
1891
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001892 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001893
1894 if (f->fmt.sliced.service_set == 0)
1895 return -EINVAL;
1896
1897 return 0;
1898}
1899
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001900/* RAW VBI ioctls */
1901
1902static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1903 struct v4l2_format *format)
1904{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001905 struct em28xx_fh *fh = priv;
1906 struct em28xx *dev = fh->dev;
1907
1908 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001909 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1910 format->fmt.vbi.offset = 0;
1911 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001912 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1913 format->fmt.vbi.count[0] = dev->vbi_height;
1914 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001915
1916 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001917 if (dev->norm & V4L2_STD_525_60) {
1918 /* NTSC */
1919 format->fmt.vbi.start[0] = 10;
1920 format->fmt.vbi.start[1] = 273;
1921 } else if (dev->norm & V4L2_STD_625_50) {
1922 /* PAL */
1923 format->fmt.vbi.start[0] = 6;
1924 format->fmt.vbi.start[1] = 318;
1925 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001926
1927 return 0;
1928}
1929
1930static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1931 struct v4l2_format *format)
1932{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001933 struct em28xx_fh *fh = priv;
1934 struct em28xx *dev = fh->dev;
1935
1936 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001937 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1938 format->fmt.vbi.offset = 0;
1939 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001940 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1941 format->fmt.vbi.count[0] = dev->vbi_height;
1942 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001943
1944 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001945 if (dev->norm & V4L2_STD_525_60) {
1946 /* NTSC */
1947 format->fmt.vbi.start[0] = 10;
1948 format->fmt.vbi.start[1] = 273;
1949 } else if (dev->norm & V4L2_STD_625_50) {
1950 /* PAL */
1951 format->fmt.vbi.start[0] = 6;
1952 format->fmt.vbi.start[1] = 318;
1953 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001954
1955 return 0;
1956}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001957
1958static int vidioc_reqbufs(struct file *file, void *priv,
1959 struct v4l2_requestbuffers *rb)
1960{
1961 struct em28xx_fh *fh = priv;
1962 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001963 int rc;
1964
1965 rc = check_dev(dev);
1966 if (rc < 0)
1967 return rc;
1968
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001969 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1970 return videobuf_reqbufs(&fh->vb_vidq, rb);
1971 else
1972 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001973}
1974
1975static int vidioc_querybuf(struct file *file, void *priv,
1976 struct v4l2_buffer *b)
1977{
1978 struct em28xx_fh *fh = priv;
1979 struct em28xx *dev = fh->dev;
1980 int rc;
1981
1982 rc = check_dev(dev);
1983 if (rc < 0)
1984 return rc;
1985
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001986 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1987 return videobuf_querybuf(&fh->vb_vidq, b);
1988 else {
1989 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1990 the videobuf framework, but we probably shouldn't be
1991 returning a buffer larger than that which was asked for.
1992 At a minimum, it causes a crash in zvbi since it does
1993 a memcpy based on the source buffer length */
1994 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001995 b->length = dev->vbi_width * dev->vbi_height * 2;
1996
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001997 return result;
1998 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001999}
2000
2001static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2002{
2003 struct em28xx_fh *fh = priv;
2004 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002005 int rc;
2006
2007 rc = check_dev(dev);
2008 if (rc < 0)
2009 return rc;
2010
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002011 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2012 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002013 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002014 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002015}
2016
2017static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2018{
2019 struct em28xx_fh *fh = priv;
2020 struct em28xx *dev = fh->dev;
2021 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002022
2023 rc = check_dev(dev);
2024 if (rc < 0)
2025 return rc;
2026
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002027 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2028 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2029 O_NONBLOCK);
2030 else
2031 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2032 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002033}
2034
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002035/* ----------------------------------------------------------- */
2036/* RADIO ESPECIFIC IOCTLS */
2037/* ----------------------------------------------------------- */
2038
2039static int radio_querycap(struct file *file, void *priv,
2040 struct v4l2_capability *cap)
2041{
2042 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2043
2044 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2045 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002046 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002047
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002048 cap->capabilities = V4L2_CAP_TUNER;
2049 return 0;
2050}
2051
2052static int radio_g_tuner(struct file *file, void *priv,
2053 struct v4l2_tuner *t)
2054{
2055 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2056
2057 if (unlikely(t->index > 0))
2058 return -EINVAL;
2059
2060 strcpy(t->name, "Radio");
2061 t->type = V4L2_TUNER_RADIO;
2062
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002063 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002064
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002065 return 0;
2066}
2067
2068static int radio_enum_input(struct file *file, void *priv,
2069 struct v4l2_input *i)
2070{
2071 if (i->index != 0)
2072 return -EINVAL;
2073 strcpy(i->name, "Radio");
2074 i->type = V4L2_INPUT_TYPE_TUNER;
2075
2076 return 0;
2077}
2078
2079static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2080{
2081 if (unlikely(a->index))
2082 return -EINVAL;
2083
2084 strcpy(a->name, "Radio");
2085 return 0;
2086}
2087
2088static int radio_s_tuner(struct file *file, void *priv,
2089 struct v4l2_tuner *t)
2090{
2091 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2092
2093 if (0 != t->index)
2094 return -EINVAL;
2095
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002096 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002097
2098 return 0;
2099}
2100
2101static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b2012-09-04 11:59:31 -03002102 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002103{
2104 return 0;
2105}
2106
2107static int radio_s_input(struct file *file, void *fh, unsigned int i)
2108{
2109 return 0;
2110}
2111
2112static int radio_queryctrl(struct file *file, void *priv,
2113 struct v4l2_queryctrl *qc)
2114{
2115 int i;
2116
2117 if (qc->id < V4L2_CID_BASE ||
2118 qc->id >= V4L2_CID_LASTP1)
2119 return -EINVAL;
2120
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002121 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2122 if (qc->id && qc->id == ac97_qctrl[i].id) {
2123 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002124 return 0;
2125 }
2126 }
2127
2128 return -EINVAL;
2129}
2130
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002131/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002132 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133 * inits the device and starts isoc transfer
2134 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002135static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002136{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002137 int errCode = 0, radio = 0;
2138 struct video_device *vdev = video_devdata(filp);
2139 struct em28xx *dev = video_drvdata(filp);
2140 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002141 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002142 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002143
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002144 switch (vdev->vfl_type) {
2145 case VFL_TYPE_GRABBER:
2146 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2147 break;
2148 case VFL_TYPE_VBI:
2149 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2150 break;
2151 case VFL_TYPE_RADIO:
2152 radio = 1;
2153 break;
2154 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002155
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002156 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2157 video_device_node_name(vdev), v4l2_type_names[fh_type],
2158 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002159
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002160
Hans Verkuil876cb142012-06-23 08:12:47 -03002161 if (mutex_lock_interruptible(&dev->lock))
2162 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002163 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002164 if (!fh) {
2165 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002166 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002167 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002168 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002169 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002170 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002171 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002172 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002174 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002175 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002176 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002177 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002178
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002179 /* Needed, since GPIO might have disabled power of
2180 some i2c device
2181 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002182 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002183
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002184 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002185 if (fh->radio) {
2186 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002187 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002188 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189
2190 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002191
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002192 if (dev->progressive)
2193 field = V4L2_FIELD_NONE;
2194 else
2195 field = V4L2_FIELD_INTERLACED;
2196
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002197 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2198 NULL, &dev->slock,
2199 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002200 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002201
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002202 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2203 NULL, &dev->slock,
2204 V4L2_BUF_TYPE_VBI_CAPTURE,
2205 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002206 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002207 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002208
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209 return errCode;
2210}
2211
2212/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002213 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214 * unregisters the v4l2,i2c and usb devices
2215 * called when the device gets disconected or at module unload
2216*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002217void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002218{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002219
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002220 /*FIXME: I2C IR should be disconnected */
2221
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002222 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002223 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002224 video_unregister_device(dev->radio_dev);
2225 else
2226 video_device_release(dev->radio_dev);
2227 dev->radio_dev = NULL;
2228 }
2229 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002230 em28xx_info("V4L2 device %s deregistered\n",
2231 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002232 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002233 video_unregister_device(dev->vbi_dev);
2234 else
2235 video_device_release(dev->vbi_dev);
2236 dev->vbi_dev = NULL;
2237 }
2238 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002239 em28xx_info("V4L2 device %s deregistered\n",
2240 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002241 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002242 video_unregister_device(dev->vdev);
2243 else
2244 video_device_release(dev->vdev);
2245 dev->vdev = NULL;
2246 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247}
2248
2249/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002250 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002251 * stops streaming and deallocates all resources allocated by the v4l2
2252 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002253 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002254static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002256 struct em28xx_fh *fh = filp->private_data;
2257 struct em28xx *dev = fh->dev;
2258 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002259
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002260 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002261
Hans Verkuil876cb142012-06-23 08:12:47 -03002262 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002263 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002264 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002265 res_free(fh, EM28XX_RESOURCE_VIDEO);
2266 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002267
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002268 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2269 videobuf_stop(&fh->vb_vbiq);
2270 res_free(fh, EM28XX_RESOURCE_VBI);
2271 }
2272
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002273 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002274 /* the device is already disconnect,
2275 free the remaining resources */
2276 if (dev->state & DEV_DISCONNECTED) {
2277 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002278 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002279 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002280 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002281 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002282 return 0;
2283 }
2284
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002285 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002286 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002287
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002288 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002289 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002290 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002291
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002292 /* set alternate 0 */
2293 dev->alt = 0;
2294 em28xx_videodbg("setting alternate 0\n");
2295 errCode = usb_set_interface(dev->udev, 0, 0);
2296 if (errCode < 0) {
2297 em28xx_errdev("cannot change alternate number to "
2298 "0 (error=%i)\n", errCode);
2299 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002300 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002301
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002302 videobuf_mmap_free(&fh->vb_vidq);
2303 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002304 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002306 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002307 return 0;
2308}
2309
2310/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002311 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002312 * will allocate buffers when called for the first time
2313 */
2314static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002315em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002316 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002317{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002318 struct em28xx_fh *fh = filp->private_data;
2319 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002320 int rc;
2321
2322 rc = check_dev(dev);
2323 if (rc < 0)
2324 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002325
Hans Verkuil876cb142012-06-23 08:12:47 -03002326 if (mutex_lock_interruptible(&dev->lock))
2327 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002328 /* FIXME: read() is not prepared to allow changing the video
2329 resolution while streaming. Seems a bug at em28xx_set_fmt
2330 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002331
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002332 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002333 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002334 rc = -EBUSY;
2335 else
2336 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002337 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002338 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002339 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002340 rc = -EBUSY;
2341 else
2342 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002343 filp->f_flags & O_NONBLOCK);
2344 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002345 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002346
Hans Verkuil876cb142012-06-23 08:12:47 -03002347 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002348}
2349
2350/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002351 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002352 * will allocate buffers when called for the first time
2353 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002354static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002355{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002356 struct em28xx_fh *fh = filp->private_data;
2357 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002358 int rc;
2359
2360 rc = check_dev(dev);
2361 if (rc < 0)
2362 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002363
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002364 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2365 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2366 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002367 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002368 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2369 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2370 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002371 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002372 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002373 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002374 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002375}
2376
Hans Verkuil876cb142012-06-23 08:12:47 -03002377static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2378{
2379 struct em28xx_fh *fh = filp->private_data;
2380 struct em28xx *dev = fh->dev;
2381 unsigned int res;
2382
2383 mutex_lock(&dev->lock);
2384 res = em28xx_poll(filp, wait);
2385 mutex_unlock(&dev->lock);
2386 return res;
2387}
2388
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002389/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002390 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002391 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002392static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002393{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002394 struct em28xx_fh *fh = filp->private_data;
2395 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002396 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002397
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002398 rc = check_dev(dev);
2399 if (rc < 0)
2400 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002401
Hans Verkuil876cb142012-06-23 08:12:47 -03002402 if (mutex_lock_interruptible(&dev->lock))
2403 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002404 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2405 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2406 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2407 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002408 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002409
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002410 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2411 (unsigned long)vma->vm_start,
2412 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2413 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002414
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002415 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002416}
2417
Hans Verkuilbec43662008-12-30 06:58:20 -03002418static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002419 .owner = THIS_MODULE,
2420 .open = em28xx_v4l2_open,
2421 .release = em28xx_v4l2_close,
2422 .read = em28xx_v4l2_read,
2423 .poll = em28xx_v4l2_poll,
2424 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002425 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002426};
2427
Hans Verkuila3998102008-07-21 02:57:38 -03002428static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002429 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002430 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2431 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2432 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2433 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002434 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2435 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002436 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002437 .vidioc_g_audio = vidioc_g_audio,
2438 .vidioc_s_audio = vidioc_s_audio,
2439 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002440 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2441 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2442 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002443
2444 .vidioc_reqbufs = vidioc_reqbufs,
2445 .vidioc_querybuf = vidioc_querybuf,
2446 .vidioc_qbuf = vidioc_qbuf,
2447 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002448 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002449 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002450 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002451 .vidioc_g_parm = vidioc_g_parm,
2452 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002453 .vidioc_enum_input = vidioc_enum_input,
2454 .vidioc_g_input = vidioc_g_input,
2455 .vidioc_s_input = vidioc_s_input,
2456 .vidioc_queryctrl = vidioc_queryctrl,
2457 .vidioc_g_ctrl = vidioc_g_ctrl,
2458 .vidioc_s_ctrl = vidioc_s_ctrl,
2459 .vidioc_streamon = vidioc_streamon,
2460 .vidioc_streamoff = vidioc_streamoff,
2461 .vidioc_g_tuner = vidioc_g_tuner,
2462 .vidioc_s_tuner = vidioc_s_tuner,
2463 .vidioc_g_frequency = vidioc_g_frequency,
2464 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002465#ifdef CONFIG_VIDEO_ADV_DEBUG
2466 .vidioc_g_register = vidioc_g_register,
2467 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002468 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002469#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002470};
2471
2472static const struct video_device em28xx_video_template = {
2473 .fops = &em28xx_v4l_fops,
2474 .release = video_device_release,
2475 .ioctl_ops = &video_ioctl_ops,
2476
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002477 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002478 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002479};
2480
Hans Verkuilbec43662008-12-30 06:58:20 -03002481static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002482 .owner = THIS_MODULE,
2483 .open = em28xx_v4l2_open,
2484 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002485 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002486};
2487
2488static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002489 .vidioc_querycap = radio_querycap,
2490 .vidioc_g_tuner = radio_g_tuner,
2491 .vidioc_enum_input = radio_enum_input,
2492 .vidioc_g_audio = radio_g_audio,
2493 .vidioc_s_tuner = radio_s_tuner,
2494 .vidioc_s_audio = radio_s_audio,
2495 .vidioc_s_input = radio_s_input,
2496 .vidioc_queryctrl = radio_queryctrl,
2497 .vidioc_g_ctrl = vidioc_g_ctrl,
2498 .vidioc_s_ctrl = vidioc_s_ctrl,
2499 .vidioc_g_frequency = vidioc_g_frequency,
2500 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002501#ifdef CONFIG_VIDEO_ADV_DEBUG
2502 .vidioc_g_register = vidioc_g_register,
2503 .vidioc_s_register = vidioc_s_register,
2504#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002505};
2506
Hans Verkuila3998102008-07-21 02:57:38 -03002507static struct video_device em28xx_radio_template = {
2508 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002509 .fops = &radio_fops,
2510 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002511};
2512
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002513/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002514
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002515
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002516
Adrian Bunk532fe652008-01-28 22:10:48 -03002517static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002518 const struct video_device *template,
2519 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002520{
2521 struct video_device *vfd;
2522
2523 vfd = video_device_alloc();
2524 if (NULL == vfd)
2525 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002526
2527 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002528 vfd->v4l2_dev = &dev->v4l2_dev;
2529 vfd->release = video_device_release;
2530 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002531 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002532
2533 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2534 dev->name, type_name);
2535
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002536 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002537 return vfd;
2538}
2539
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002540int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002541{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002542 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002543 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002544 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002545
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002546 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2547 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002548
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002549 /* set default norm */
2550 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002551 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002552 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002553
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002554 /* Analog specific initialization */
2555 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002556
2557 maxw = norm_maxw(dev);
2558 /* MaxPacketSize for em2800 is too small to capture at full resolution
2559 * use half of maxw as the scaler can only scale to 50% */
2560 if (dev->board.is_em2800)
2561 maxw /= 2;
2562
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002563 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002564 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002565
Ezequiel García96371fc2012-03-23 18:09:34 -03002566 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002567
2568 /* Audio defaults */
2569 dev->mute = 1;
2570 dev->volume = 0x1f;
2571
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002572/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002573 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2574 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2575 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002576
2577 em28xx_set_outfmt(dev);
2578 em28xx_colorlevels_set_default(dev);
2579 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002580
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002581 /* allocate and fill video video_device struct */
2582 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2583 if (!dev->vdev) {
2584 em28xx_errdev("cannot allocate video_device.\n");
2585 return -ENODEV;
2586 }
2587
2588 /* register v4l2 video video_device */
2589 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2590 video_nr[dev->devno]);
2591 if (ret) {
2592 em28xx_errdev("unable to register video device (error=%i).\n",
2593 ret);
2594 return ret;
2595 }
2596
2597 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002598 if (em28xx_vbi_supported(dev) == 1) {
2599 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2600 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002601
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002602 /* register v4l2 vbi video_device */
2603 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2604 vbi_nr[dev->devno]);
2605 if (ret < 0) {
2606 em28xx_errdev("unable to register vbi device\n");
2607 return ret;
2608 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002609 }
2610
2611 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002612 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2613 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002614 if (!dev->radio_dev) {
2615 em28xx_errdev("cannot allocate video_device.\n");
2616 return -ENODEV;
2617 }
2618 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2619 radio_nr[dev->devno]);
2620 if (ret < 0) {
2621 em28xx_errdev("can't register radio device\n");
2622 return ret;
2623 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002624 em28xx_info("Registered radio device as %s\n",
2625 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002626 }
2627
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002628 em28xx_info("V4L2 video device registered as %s\n",
2629 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002630
2631 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002632 em28xx_info("V4L2 VBI device registered as %s\n",
2633 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002634
2635 return 0;
2636}