blob: fec8847e84d82b5fe7f82a9ddbea09debf8f4e02 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
54#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
86MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080088
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030089static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030090module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080092
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030093/* supported video standards */
94static struct em28xx_fmt format[] = {
95 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030096 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030099 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300102 .fourcc = V4L2_PIX_FMT_RGB565,
103 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300104 .reg = EM28XX_OUTFMT_RGB_16_656,
105 }, {
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .depth = 8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
110 }, {
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
113 .depth = 8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
115 }, {
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
120 }, {
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
123 .depth = 12,
124 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300125 },
126};
127
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300130static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800131 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200132 .id = V4L2_CID_AUDIO_VOLUME,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Volume",
135 .minimum = 0x0,
136 .maximum = 0x1f,
137 .step = 0x1,
138 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300139 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300140 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200141 .id = V4L2_CID_AUDIO_MUTE,
142 .type = V4L2_CTRL_TYPE_BOOLEAN,
143 .name = "Mute",
144 .minimum = 0,
145 .maximum = 1,
146 .step = 1,
147 .default_value = 1,
148 .flags = 0,
149 }
150};
151
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300152/* ------------------------------------------------------------------
153 DMA and thread functions
154 ------------------------------------------------------------------*/
155
156/*
157 * Announces that a buffer were filled and request the next
158 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300159static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
162{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300167 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer74209dc2012-11-08 14:11:37 -0300169 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
175static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
178{
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300184 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300185
Frank Schaefer74209dc2012-11-08 14:11:37 -0300186 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
190}
191
192/*
193 * Identify the buffer header type and properly handles
194 */
195static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
198 unsigned char *p,
199 unsigned char *outp, unsigned long len)
200{
201 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300208 startread = p;
209 remain = len;
210
Frank Schaeferc02ec712012-11-08 14:11:33 -0300211 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300218
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
221 else
222 offset = linesdone * bytesperline * 2 + currlinedone;
223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = lencopy > remain ? remain : lencopy;
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300234 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300235 if (lencopy <= 0)
236 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300238
239 remain -= lencopy;
240
241 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300242 if (dev->progressive)
243 startwrite += lencopy;
244 else
245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300372 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
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 Thorntone0fadfd342008-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 Schaefer4601cc32012-11-08 14:11:46 -0300579 if (actual_length <= 0) {
580 /* 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 */
588 if (p[0] == 0x33 && p[1] == 0x95) {
589 dev->capture_type = 0;
590 dev->vbi_read = 0;
591 em28xx_isocdbg("VBI START HEADER!!!\n");
592 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300593 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300594 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300595 } else if (p[0] == 0x88 && p[1] == 0x88 &&
596 p[2] == 0x88 && p[3] == 0x88) {
597 /* continuation */
598 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300599 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300600 } else if (p[0] == 0x22 && p[1] == 0x5a) {
601 /* start video */
602 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300603 len = actual_length - 4;
604 } else {
605 /* NOTE: With bulk transfers, intermediate data packets
606 * have no continuation header */
607 len = actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300608 }
609
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300610 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300611
612 if (dev->capture_type == 0) {
613 if (dev->vbi_read >= vbi_size) {
614 /* We've already read all the VBI data, so
615 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300616 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300617 } else if ((dev->vbi_read + len) < vbi_size) {
618 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300619 if (dev->vbi_read == 0 &&
620 (!(dev->cur_field & 1))) {
621 /* Brand new frame */
622 if (vbi_buf != NULL)
623 vbi_buffer_filled(dev,
624 vbi_dma_q,
625 vbi_buf);
626 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
627 if (vbi_buf == NULL)
628 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300629 else
630 vbioutp = videobuf_to_vmalloc(
631 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300632 }
633
634 if (dev->vbi_read == 0) {
635 vbi_dma_q->pos = 0;
636 if (vbi_buf != NULL) {
637 if (dev->cur_field & 1)
638 vbi_buf->top_field = 0;
639 else
640 vbi_buf->top_field = 1;
641 }
642 }
643
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300644 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300645 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300647 } else {
648 /* Some of this frame is VBI data and some is
649 video data */
650 int vbi_data_len = vbi_size - dev->vbi_read;
651 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300652 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
653 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300654 dev->capture_type = 1;
655 p += vbi_data_len;
656 len -= vbi_data_len;
657 }
658 }
659
660 if (dev->capture_type == 1) {
661 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300662 if (dev->progressive || !(dev->cur_field & 1)) {
663 if (buf != NULL)
664 buffer_filled(dev, dma_q, buf);
665 get_next_buf(dma_q, &buf);
666 if (buf == NULL)
667 outp = NULL;
668 else
669 outp = videobuf_to_vmalloc(&buf->vb);
670 }
671 if (buf != NULL) {
672 if (dev->cur_field & 1)
673 buf->top_field = 0;
674 else
675 buf->top_field = 1;
676 }
677
678 dma_q->pos = 0;
679 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300680
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300681 if (buf != NULL && dev->capture_type == 2 && len > 0)
682 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300683 }
684 return rc;
685}
686
687
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300688/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689 Videobuf operations
690 ------------------------------------------------------------------*/
691
692static int
693buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
694{
695 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300696 struct em28xx *dev = fh->dev;
697 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300699 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
700 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300701
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300702 if (0 == *count)
703 *count = EM28XX_DEF_BUF;
704
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300705 if (*count < EM28XX_MIN_BUF)
706 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300707
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300708 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300709 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300710 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300711 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300712
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300713 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300714
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 return 0;
716}
717
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300718/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300719static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
720{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300721 struct em28xx_fh *fh = vq->priv_data;
722 struct em28xx *dev = fh->dev;
723 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300724 if (in_interrupt())
725 BUG();
726
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300727 /* We used to wait for the buffer to finish here, but this didn't work
728 because, as we were keeping the state as VIDEOBUF_QUEUED,
729 videobuf_queue_cancel marked it as finished for us.
730 (Also, it could wedge forever if the hardware was misconfigured.)
731
732 This should be safe; by the time we get here, the buffer isn't
733 queued anymore. If we ever start marking the buffers as
734 VIDEOBUF_ACTIVE, it won't be, though.
735 */
736 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300737 if (dev->usb_ctl.vid_buf == buf)
738 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300739 spin_unlock_irqrestore(&dev->slock, flags);
740
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 videobuf_vmalloc_free(&buf->vb);
742 buf->vb.state = VIDEOBUF_NEEDS_INIT;
743}
744
745static int
746buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
747 enum v4l2_field field)
748{
749 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300750 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300751 struct em28xx *dev = fh->dev;
752 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300753
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300754 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
755 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756
757 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
758 return -EINVAL;
759
Brandon Philips05612972008-04-13 14:57:01 -0300760 buf->vb.width = dev->width;
761 buf->vb.height = dev->height;
762 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763
764 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300765 rc = videobuf_iolock(vq, &buf->vb, NULL);
766 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300768 }
769
Frank Schaefer74209dc2012-11-08 14:11:37 -0300770 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300771 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300772
773 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300774 if (em28xx_vbi_supported(dev) == 1)
Frank Schaeferc647a912012-11-08 14:11:52 -0300775 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
776 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300777 EM28XX_NUM_BUFS,
778 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300779 dev->packet_multiplier,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300780 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300781 else
Frank Schaeferc647a912012-11-08 14:11:52 -0300782 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
783 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300784 EM28XX_NUM_BUFS,
785 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300786 dev->packet_multiplier,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300787 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300788 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300789 goto fail;
790 }
791
792 buf->vb.state = VIDEOBUF_PREPARED;
793 return 0;
794
795fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300796 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797 return rc;
798}
799
800static void
801buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
802{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300803 struct em28xx_buffer *buf = container_of(vb,
804 struct em28xx_buffer,
805 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300807 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300808 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300809
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300810 buf->vb.state = VIDEOBUF_QUEUED;
811 list_add_tail(&buf->vb.queue, &vidq->active);
812
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300813}
814
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300815static void buffer_release(struct videobuf_queue *vq,
816 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300817{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300818 struct em28xx_buffer *buf = container_of(vb,
819 struct em28xx_buffer,
820 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300821 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300822 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300823
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300824 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300825
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300826 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300827}
828
829static struct videobuf_queue_ops em28xx_video_qops = {
830 .buf_setup = buffer_setup,
831 .buf_prepare = buffer_prepare,
832 .buf_queue = buffer_queue,
833 .buf_release = buffer_release,
834};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800835
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300836/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800837
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800838static void video_mux(struct em28xx *dev, int index)
839{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800840 dev->ctl_input = index;
841 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300842 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800843
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300844 if (!dev->ctl_aoutput)
845 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
846
Hans Verkuil5325b422009-04-02 11:26:22 -0300847 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
848 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800849
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300850 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300851 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300852 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
853 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300854 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300855 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300856 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
857 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800858 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300859
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300860 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300861 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
862 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300863 }
864
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300865 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800866}
867
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300868/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300870{
871 struct em28xx *dev = fh->dev;
872
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300873 if (fh->resources & bit)
874 /* have it already allocated */
875 return 1;
876
877 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300878 if (dev->resources & bit) {
879 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300880 return 0;
881 }
882 /* it's free, grab it */
883 fh->resources |= bit;
884 dev->resources |= bit;
885 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300886 return 1;
887}
888
889static int res_check(struct em28xx_fh *fh, unsigned int bit)
890{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300891 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300892}
893
894static int res_locked(struct em28xx *dev, unsigned int bit)
895{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300896 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300897}
898
899static void res_free(struct em28xx_fh *fh, unsigned int bits)
900{
901 struct em28xx *dev = fh->dev;
902
903 BUG_ON((fh->resources & bits) != bits);
904
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300905 fh->resources &= ~bits;
906 dev->resources &= ~bits;
907 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300908}
909
910static int get_ressource(struct em28xx_fh *fh)
911{
912 switch (fh->type) {
913 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
914 return EM28XX_RESOURCE_VIDEO;
915 case V4L2_BUF_TYPE_VBI_CAPTURE:
916 return EM28XX_RESOURCE_VBI;
917 default:
918 BUG();
919 return 0;
920 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300921}
922
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800923/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300924 * ac97_queryctrl()
925 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300926 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300927static int ac97_queryctrl(struct v4l2_queryctrl *qc)
928{
929 int i;
930
931 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
932 if (qc->id && qc->id == ac97_qctrl[i].id) {
933 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
934 return 0;
935 }
936 }
937
938 /* Control is not ac97 related */
939 return 1;
940}
941
942/*
943 * ac97_get_ctrl()
944 * return the current values for ac97 mute and volume
945 */
946static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947{
948 switch (ctrl->id) {
949 case V4L2_CID_AUDIO_MUTE:
950 ctrl->value = dev->mute;
951 return 0;
952 case V4L2_CID_AUDIO_VOLUME:
953 ctrl->value = dev->volume;
954 return 0;
955 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300956 /* Control is not ac97 related */
957 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300958 }
959}
960
961/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300962 * ac97_set_ctrl()
963 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300965static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300967 int i;
968
969 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
970 if (ctrl->id == ac97_qctrl[i].id)
971 goto handle;
972
973 /* Announce that hasn't handle it */
974 return 1;
975
976handle:
977 if (ctrl->value < ac97_qctrl[i].minimum ||
978 ctrl->value > ac97_qctrl[i].maximum)
979 return -ERANGE;
980
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300981 switch (ctrl->id) {
982 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300983 dev->mute = ctrl->value;
984 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300985 case V4L2_CID_AUDIO_VOLUME:
986 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300987 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300989
990 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991}
992
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300993static int check_dev(struct em28xx *dev)
994{
995 if (dev->state & DEV_DISCONNECTED) {
996 em28xx_errdev("v4l2 ioctl: device not present\n");
997 return -ENODEV;
998 }
999
1000 if (dev->state & DEV_MISCONFIGURED) {
1001 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1002 "close and open it again\n");
1003 return -EIO;
1004 }
1005 return 0;
1006}
1007
1008static void get_scale(struct em28xx *dev,
1009 unsigned int width, unsigned int height,
1010 unsigned int *hscale, unsigned int *vscale)
1011{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001012 unsigned int maxw = norm_maxw(dev);
1013 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001014
1015 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1016 if (*hscale >= 0x4000)
1017 *hscale = 0x3fff;
1018
1019 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1020 if (*vscale >= 0x4000)
1021 *vscale = 0x3fff;
1022}
1023
1024/* ------------------------------------------------------------------
1025 IOCTL vidioc handling
1026 ------------------------------------------------------------------*/
1027
Hans Verkuil78b526a2008-05-28 12:16:41 -03001028static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001029 struct v4l2_format *f)
1030{
1031 struct em28xx_fh *fh = priv;
1032 struct em28xx *dev = fh->dev;
1033
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001034 f->fmt.pix.width = dev->width;
1035 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001036 f->fmt.pix.pixelformat = dev->format->fourcc;
1037 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001038 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001039 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1040
1041 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001042 if (dev->progressive)
1043 f->fmt.pix.field = V4L2_FIELD_NONE;
1044 else
1045 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001046 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001047 return 0;
1048}
1049
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001050static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1051{
1052 unsigned int i;
1053
1054 for (i = 0; i < ARRAY_SIZE(format); i++)
1055 if (format[i].fourcc == fourcc)
1056 return &format[i];
1057
1058 return NULL;
1059}
1060
Hans Verkuil78b526a2008-05-28 12:16:41 -03001061static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001062 struct v4l2_format *f)
1063{
1064 struct em28xx_fh *fh = priv;
1065 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001066 unsigned int width = f->fmt.pix.width;
1067 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068 unsigned int maxw = norm_maxw(dev);
1069 unsigned int maxh = norm_maxh(dev);
1070 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001071 struct em28xx_fmt *fmt;
1072
1073 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1074 if (!fmt) {
1075 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1076 f->fmt.pix.pixelformat);
1077 return -EINVAL;
1078 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001079
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001080 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001081 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001082 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1083 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001084 /* MaxPacketSize for em2800 is too small to capture at full resolution
1085 * use half of maxw as the scaler can only scale to 50% */
1086 if (width == maxw && height == maxh)
1087 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001088 } else {
1089 /* width must even because of the YUYV format
1090 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001091 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1092 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093 }
1094
1095 get_scale(dev, width, height, &hscale, &vscale);
1096
1097 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1098 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1099
1100 f->fmt.pix.width = width;
1101 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001102 f->fmt.pix.pixelformat = fmt->fourcc;
1103 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1104 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001105 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001106 if (dev->progressive)
1107 f->fmt.pix.field = V4L2_FIELD_NONE;
1108 else
1109 f->fmt.pix.field = dev->interlaced ?
1110 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001111
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001112 return 0;
1113}
1114
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001115static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1116 unsigned width, unsigned height)
1117{
1118 struct em28xx_fmt *fmt;
1119
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001120 fmt = format_by_fourcc(fourcc);
1121 if (!fmt)
1122 return -EINVAL;
1123
1124 dev->format = fmt;
1125 dev->width = width;
1126 dev->height = height;
1127
1128 /* set new image size */
1129 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1130
1131 em28xx_set_alternate(dev);
1132 em28xx_resolution_set(dev);
1133
1134 return 0;
1135}
1136
Hans Verkuil78b526a2008-05-28 12:16:41 -03001137static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001138 struct v4l2_format *f)
1139{
1140 struct em28xx_fh *fh = priv;
1141 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001142 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001143
1144 rc = check_dev(dev);
1145 if (rc < 0)
1146 return rc;
1147
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001148 vidioc_try_fmt_vid_cap(file, priv, f);
1149
Brandon Philips05612972008-04-13 14:57:01 -03001150 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1151 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001152 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001153 }
1154
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001155 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001156 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157}
1158
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001159static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1160{
1161 struct em28xx_fh *fh = priv;
1162 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001163 int rc;
1164
1165 rc = check_dev(dev);
1166 if (rc < 0)
1167 return rc;
1168
1169 *norm = dev->norm;
1170
1171 return 0;
1172}
1173
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001174static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1175{
1176 struct em28xx_fh *fh = priv;
1177 struct em28xx *dev = fh->dev;
1178 int rc;
1179
1180 rc = check_dev(dev);
1181 if (rc < 0)
1182 return rc;
1183
1184 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1185
1186 return 0;
1187}
1188
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001189static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001190{
1191 struct em28xx_fh *fh = priv;
1192 struct em28xx *dev = fh->dev;
1193 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001194 int rc;
1195
1196 rc = check_dev(dev);
1197 if (rc < 0)
1198 return rc;
1199
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001200 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001201
1202 /* Adjusts width/height, if needed */
1203 f.fmt.pix.width = dev->width;
1204 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001205 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001207 /* set new image size */
1208 dev->width = f.fmt.pix.width;
1209 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001210 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1211
1212 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001213 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001214
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001215 return 0;
1216}
1217
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001218static int vidioc_g_parm(struct file *file, void *priv,
1219 struct v4l2_streamparm *p)
1220{
1221 struct em28xx_fh *fh = priv;
1222 struct em28xx *dev = fh->dev;
1223 int rc = 0;
1224
1225 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1226 return -EINVAL;
1227
1228 if (dev->board.is_webcam)
1229 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1230 video, g_parm, p);
1231 else
1232 v4l2_video_std_frame_period(dev->norm,
1233 &p->parm.capture.timeperframe);
1234
1235 return rc;
1236}
1237
1238static int vidioc_s_parm(struct file *file, void *priv,
1239 struct v4l2_streamparm *p)
1240{
1241 struct em28xx_fh *fh = priv;
1242 struct em28xx *dev = fh->dev;
1243
1244 if (!dev->board.is_webcam)
1245 return -EINVAL;
1246
1247 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1248 return -EINVAL;
1249
1250 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1251}
1252
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253static const char *iname[] = {
1254 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1255 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1256 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1257 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1258 [EM28XX_VMUX_SVIDEO] = "S-Video",
1259 [EM28XX_VMUX_TELEVISION] = "Television",
1260 [EM28XX_VMUX_CABLE] = "Cable TV",
1261 [EM28XX_VMUX_DVB] = "DVB",
1262 [EM28XX_VMUX_DEBUG] = "for debug only",
1263};
1264
1265static int vidioc_enum_input(struct file *file, void *priv,
1266 struct v4l2_input *i)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270 unsigned int n;
1271
1272 n = i->index;
1273 if (n >= MAX_EM28XX_INPUT)
1274 return -EINVAL;
1275 if (0 == INPUT(n)->type)
1276 return -EINVAL;
1277
1278 i->index = n;
1279 i->type = V4L2_INPUT_TYPE_CAMERA;
1280
1281 strcpy(i->name, iname[INPUT(n)->type]);
1282
1283 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1284 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1285 i->type = V4L2_INPUT_TYPE_TUNER;
1286
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001287 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001288
1289 return 0;
1290}
1291
1292static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1293{
1294 struct em28xx_fh *fh = priv;
1295 struct em28xx *dev = fh->dev;
1296
1297 *i = dev->ctl_input;
1298
1299 return 0;
1300}
1301
1302static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1303{
1304 struct em28xx_fh *fh = priv;
1305 struct em28xx *dev = fh->dev;
1306 int rc;
1307
1308 rc = check_dev(dev);
1309 if (rc < 0)
1310 return rc;
1311
1312 if (i >= MAX_EM28XX_INPUT)
1313 return -EINVAL;
1314 if (0 == INPUT(i)->type)
1315 return -EINVAL;
1316
Ezequiel García96371fc2012-03-23 18:09:34 -03001317 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001318 return 0;
1319}
1320
1321static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1322{
1323 struct em28xx_fh *fh = priv;
1324 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001325
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001326 if (!dev->audio_mode.has_audio)
1327 return -EINVAL;
1328
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001329 switch (a->index) {
1330 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001332 break;
1333 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001334 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001335 break;
1336 case EM28XX_AMUX_VIDEO2:
1337 strcpy(a->name, "Television alt");
1338 break;
1339 case EM28XX_AMUX_PHONE:
1340 strcpy(a->name, "Phone");
1341 break;
1342 case EM28XX_AMUX_MIC:
1343 strcpy(a->name, "Mic");
1344 break;
1345 case EM28XX_AMUX_CD:
1346 strcpy(a->name, "CD");
1347 break;
1348 case EM28XX_AMUX_AUX:
1349 strcpy(a->name, "Aux");
1350 break;
1351 case EM28XX_AMUX_PCM_OUT:
1352 strcpy(a->name, "PCM");
1353 break;
1354 default:
1355 return -EINVAL;
1356 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001357
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001358 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001359 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001360
1361 return 0;
1362}
1363
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001364static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001365{
1366 struct em28xx_fh *fh = priv;
1367 struct em28xx *dev = fh->dev;
1368
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001369
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001370 if (!dev->audio_mode.has_audio)
1371 return -EINVAL;
1372
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001373 if (a->index >= MAX_EM28XX_INPUT)
1374 return -EINVAL;
1375 if (0 == INPUT(a->index)->type)
1376 return -EINVAL;
1377
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001378 dev->ctl_ainput = INPUT(a->index)->amux;
1379 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001380
1381 if (!dev->ctl_aoutput)
1382 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001383
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384 return 0;
1385}
1386
1387static int vidioc_queryctrl(struct file *file, void *priv,
1388 struct v4l2_queryctrl *qc)
1389{
1390 struct em28xx_fh *fh = priv;
1391 struct em28xx *dev = fh->dev;
1392 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001393 int rc;
1394
1395 rc = check_dev(dev);
1396 if (rc < 0)
1397 return rc;
1398
1399 memset(qc, 0, sizeof(*qc));
1400
1401 qc->id = id;
1402
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001403 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001404 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1405 rc = ac97_queryctrl(qc);
1406 if (!rc)
1407 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001409
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001410 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001411 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001412
1413 if (qc->type)
1414 return 0;
1415 else
1416 return -EINVAL;
1417}
1418
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001419/*
1420 * FIXME: This is an indirect way to check if a control exists at a
1421 * subdev. Instead of that hack, maybe the better would be to change all
1422 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1423 */
1424static int check_subdev_ctrl(struct em28xx *dev, int id)
1425{
1426 struct v4l2_queryctrl qc;
1427
1428 memset(&qc, 0, sizeof(qc));
1429 qc.id = id;
1430
1431 /* enumerate V4L2 device controls */
1432 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1433
1434 if (qc.type)
1435 return 0;
1436 else
1437 return -EINVAL;
1438}
1439
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001440static int vidioc_g_ctrl(struct file *file, void *priv,
1441 struct v4l2_control *ctrl)
1442{
1443 struct em28xx_fh *fh = priv;
1444 struct em28xx *dev = fh->dev;
1445 int rc;
1446
1447 rc = check_dev(dev);
1448 if (rc < 0)
1449 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001450 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001451
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001452 /* Set an AC97 control */
1453 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1454 rc = ac97_get_ctrl(dev, ctrl);
1455 else
1456 rc = 1;
1457
1458 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1459 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001460 if (check_subdev_ctrl(dev, ctrl->id))
1461 return -EINVAL;
1462
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001463 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001464 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001465 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001467 return rc;
1468}
1469
1470static int vidioc_s_ctrl(struct file *file, void *priv,
1471 struct v4l2_control *ctrl)
1472{
1473 struct em28xx_fh *fh = priv;
1474 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001475 int rc;
1476
1477 rc = check_dev(dev);
1478 if (rc < 0)
1479 return rc;
1480
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001481 /* Set an AC97 control */
1482 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1483 rc = ac97_set_ctrl(dev, ctrl);
1484 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001485 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001486
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001487 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001489 rc = check_subdev_ctrl(dev, ctrl->id);
1490 if (!rc)
1491 v4l2_device_call_all(&dev->v4l2_dev, 0,
1492 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001493 /*
1494 * In the case of non-AC97 volume controls, we still need
1495 * to do some setups at em28xx, in order to mute/unmute
1496 * and to adjust audio volume. However, the value ranges
1497 * should be checked by the corresponding V4L subdriver.
1498 */
1499 switch (ctrl->id) {
1500 case V4L2_CID_AUDIO_MUTE:
1501 dev->mute = ctrl->value;
1502 rc = em28xx_audio_analog_set(dev);
1503 break;
1504 case V4L2_CID_AUDIO_VOLUME:
1505 dev->volume = ctrl->value;
1506 rc = em28xx_audio_analog_set(dev);
1507 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001508 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001509 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001510}
1511
1512static int vidioc_g_tuner(struct file *file, void *priv,
1513 struct v4l2_tuner *t)
1514{
1515 struct em28xx_fh *fh = priv;
1516 struct em28xx *dev = fh->dev;
1517 int rc;
1518
1519 rc = check_dev(dev);
1520 if (rc < 0)
1521 return rc;
1522
1523 if (0 != t->index)
1524 return -EINVAL;
1525
1526 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001527 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001528
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001529 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001530 return 0;
1531}
1532
1533static int vidioc_s_tuner(struct file *file, void *priv,
1534 struct v4l2_tuner *t)
1535{
1536 struct em28xx_fh *fh = priv;
1537 struct em28xx *dev = fh->dev;
1538 int rc;
1539
1540 rc = check_dev(dev);
1541 if (rc < 0)
1542 return rc;
1543
1544 if (0 != t->index)
1545 return -EINVAL;
1546
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001547 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001548 return 0;
1549}
1550
1551static int vidioc_g_frequency(struct file *file, void *priv,
1552 struct v4l2_frequency *f)
1553{
1554 struct em28xx_fh *fh = priv;
1555 struct em28xx *dev = fh->dev;
1556
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001557 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001558 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001559 return 0;
1560}
1561
1562static int vidioc_s_frequency(struct file *file, void *priv,
1563 struct v4l2_frequency *f)
1564{
1565 struct em28xx_fh *fh = priv;
1566 struct em28xx *dev = fh->dev;
1567 int rc;
1568
1569 rc = check_dev(dev);
1570 if (rc < 0)
1571 return rc;
1572
1573 if (0 != f->tuner)
1574 return -EINVAL;
1575
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001576 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1577 return -EINVAL;
1578 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001579 return -EINVAL;
1580
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001581 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001582 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001583
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001584 return 0;
1585}
1586
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001587#ifdef CONFIG_VIDEO_ADV_DEBUG
1588static int em28xx_reg_len(int reg)
1589{
1590 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001591 case EM28XX_R40_AC97LSB:
1592 case EM28XX_R30_HSCALELOW:
1593 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001594 return 2;
1595 default:
1596 return 1;
1597 }
1598}
1599
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001600static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001601 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001602{
1603 struct em28xx_fh *fh = priv;
1604 struct em28xx *dev = fh->dev;
1605
1606 chip->ident = V4L2_IDENT_NONE;
1607 chip->revision = 0;
1608
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001609 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001610
1611 return 0;
1612}
1613
1614
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001615static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001616 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001617{
1618 struct em28xx_fh *fh = priv;
1619 struct em28xx *dev = fh->dev;
1620 int ret;
1621
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001622 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001623 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001624 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001625 if (ret < 0)
1626 return ret;
1627
1628 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001629 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001630 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001631 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001632 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001633 return 0;
1634 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001635 /* TODO: is this correct? */
1636 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1637 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001638 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001639 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001640 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001641 }
1642
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001643 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001644 reg->size = em28xx_reg_len(reg->reg);
1645 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001646 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001647
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001648 if (ret < 0)
1649 return ret;
1650
1651 reg->val = ret;
1652 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001653 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001654 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1655 reg->reg, (char *)&val, 2);
1656 if (ret < 0)
1657 return ret;
1658
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001659 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001660 }
1661
1662 return 0;
1663}
1664
1665static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001666 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001667{
1668 struct em28xx_fh *fh = priv;
1669 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001670 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001671
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001672 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001673 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001674 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001675 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001676 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001677 return 0;
1678 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001679 /* TODO: is this correct? */
1680 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1681 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001682 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001683 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001684 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001685 }
1686
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001687 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001688 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001689
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001690 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001691 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001692}
1693#endif
1694
1695
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001696static int vidioc_cropcap(struct file *file, void *priv,
1697 struct v4l2_cropcap *cc)
1698{
1699 struct em28xx_fh *fh = priv;
1700 struct em28xx *dev = fh->dev;
1701
1702 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1703 return -EINVAL;
1704
1705 cc->bounds.left = 0;
1706 cc->bounds.top = 0;
1707 cc->bounds.width = dev->width;
1708 cc->bounds.height = dev->height;
1709 cc->defrect = cc->bounds;
1710 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1711 cc->pixelaspect.denominator = 59;
1712
1713 return 0;
1714}
1715
1716static int vidioc_streamon(struct file *file, void *priv,
1717 enum v4l2_buf_type type)
1718{
1719 struct em28xx_fh *fh = priv;
1720 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001721 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001722
1723 rc = check_dev(dev);
1724 if (rc < 0)
1725 return rc;
1726
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001727 if (unlikely(type != fh->type))
1728 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001729
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001730 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1731 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001732
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001733 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001734 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001735
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001736 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1737 rc = videobuf_streamon(&fh->vb_vidq);
1738 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1739 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001740
1741 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001742}
1743
1744static int vidioc_streamoff(struct file *file, void *priv,
1745 enum v4l2_buf_type type)
1746{
1747 struct em28xx_fh *fh = priv;
1748 struct em28xx *dev = fh->dev;
1749 int rc;
1750
1751 rc = check_dev(dev);
1752 if (rc < 0)
1753 return rc;
1754
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001755 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1756 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001757 return -EINVAL;
1758 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001759 return -EINVAL;
1760
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001761 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1762 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001763
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001764 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001765 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1766 videobuf_streamoff(&fh->vb_vidq);
1767 res_free(fh, EM28XX_RESOURCE_VIDEO);
1768 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001769 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001770 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1771 videobuf_streamoff(&fh->vb_vbiq);
1772 res_free(fh, EM28XX_RESOURCE_VBI);
1773 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001774 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001776 return 0;
1777}
1778
1779static int vidioc_querycap(struct file *file, void *priv,
1780 struct v4l2_capability *cap)
1781{
1782 struct em28xx_fh *fh = priv;
1783 struct em28xx *dev = fh->dev;
1784
1785 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1786 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001787 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001788
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001789 cap->capabilities =
1790 V4L2_CAP_SLICED_VBI_CAPTURE |
1791 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1793
Devin Heitmueller04146142009-09-11 00:08:44 -03001794 if (dev->vbi_dev)
1795 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1796
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001797 if (dev->audio_mode.has_audio)
1798 cap->capabilities |= V4L2_CAP_AUDIO;
1799
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001800 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001801 cap->capabilities |= V4L2_CAP_TUNER;
1802
1803 return 0;
1804}
1805
Hans Verkuil78b526a2008-05-28 12:16:41 -03001806static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001807 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001808{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001809 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001810 return -EINVAL;
1811
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001812 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1813 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001814
1815 return 0;
1816}
1817
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001818static int vidioc_enum_framesizes(struct file *file, void *priv,
1819 struct v4l2_frmsizeenum *fsize)
1820{
1821 struct em28xx_fh *fh = priv;
1822 struct em28xx *dev = fh->dev;
1823 struct em28xx_fmt *fmt;
1824 unsigned int maxw = norm_maxw(dev);
1825 unsigned int maxh = norm_maxh(dev);
1826
1827 fmt = format_by_fourcc(fsize->pixel_format);
1828 if (!fmt) {
1829 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1830 fsize->pixel_format);
1831 return -EINVAL;
1832 }
1833
1834 if (dev->board.is_em2800) {
1835 if (fsize->index > 1)
1836 return -EINVAL;
1837 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1838 fsize->discrete.width = maxw / (1 + fsize->index);
1839 fsize->discrete.height = maxh / (1 + fsize->index);
1840 return 0;
1841 }
1842
1843 if (fsize->index != 0)
1844 return -EINVAL;
1845
1846 /* Report a continuous range */
1847 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1848 fsize->stepwise.min_width = 48;
1849 fsize->stepwise.min_height = 32;
1850 fsize->stepwise.max_width = maxw;
1851 fsize->stepwise.max_height = maxh;
1852 fsize->stepwise.step_width = 1;
1853 fsize->stepwise.step_height = 1;
1854 return 0;
1855}
1856
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001857/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001858static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001859 struct v4l2_format *f)
1860{
1861 struct em28xx_fh *fh = priv;
1862 struct em28xx *dev = fh->dev;
1863 int rc;
1864
1865 rc = check_dev(dev);
1866 if (rc < 0)
1867 return rc;
1868
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001869 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001870 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001871
1872 if (f->fmt.sliced.service_set == 0)
1873 rc = -EINVAL;
1874
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001875 return rc;
1876}
1877
Hans Verkuil78b526a2008-05-28 12:16:41 -03001878static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001879 struct v4l2_format *f)
1880{
1881 struct em28xx_fh *fh = priv;
1882 struct em28xx *dev = fh->dev;
1883 int rc;
1884
1885 rc = check_dev(dev);
1886 if (rc < 0)
1887 return rc;
1888
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001889 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001890
1891 if (f->fmt.sliced.service_set == 0)
1892 return -EINVAL;
1893
1894 return 0;
1895}
1896
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001897/* RAW VBI ioctls */
1898
1899static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1900 struct v4l2_format *format)
1901{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001902 struct em28xx_fh *fh = priv;
1903 struct em28xx *dev = fh->dev;
1904
1905 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001906 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1907 format->fmt.vbi.offset = 0;
1908 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001909 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1910 format->fmt.vbi.count[0] = dev->vbi_height;
1911 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001912
1913 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001914 if (dev->norm & V4L2_STD_525_60) {
1915 /* NTSC */
1916 format->fmt.vbi.start[0] = 10;
1917 format->fmt.vbi.start[1] = 273;
1918 } else if (dev->norm & V4L2_STD_625_50) {
1919 /* PAL */
1920 format->fmt.vbi.start[0] = 6;
1921 format->fmt.vbi.start[1] = 318;
1922 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001923
1924 return 0;
1925}
1926
1927static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1928 struct v4l2_format *format)
1929{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001930 struct em28xx_fh *fh = priv;
1931 struct em28xx *dev = fh->dev;
1932
1933 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001934 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1935 format->fmt.vbi.offset = 0;
1936 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001937 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1938 format->fmt.vbi.count[0] = dev->vbi_height;
1939 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001940
1941 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001942 if (dev->norm & V4L2_STD_525_60) {
1943 /* NTSC */
1944 format->fmt.vbi.start[0] = 10;
1945 format->fmt.vbi.start[1] = 273;
1946 } else if (dev->norm & V4L2_STD_625_50) {
1947 /* PAL */
1948 format->fmt.vbi.start[0] = 6;
1949 format->fmt.vbi.start[1] = 318;
1950 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001951
1952 return 0;
1953}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001954
1955static int vidioc_reqbufs(struct file *file, void *priv,
1956 struct v4l2_requestbuffers *rb)
1957{
1958 struct em28xx_fh *fh = priv;
1959 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001960 int rc;
1961
1962 rc = check_dev(dev);
1963 if (rc < 0)
1964 return rc;
1965
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001966 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1967 return videobuf_reqbufs(&fh->vb_vidq, rb);
1968 else
1969 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001970}
1971
1972static int vidioc_querybuf(struct file *file, void *priv,
1973 struct v4l2_buffer *b)
1974{
1975 struct em28xx_fh *fh = priv;
1976 struct em28xx *dev = fh->dev;
1977 int rc;
1978
1979 rc = check_dev(dev);
1980 if (rc < 0)
1981 return rc;
1982
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001983 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1984 return videobuf_querybuf(&fh->vb_vidq, b);
1985 else {
1986 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1987 the videobuf framework, but we probably shouldn't be
1988 returning a buffer larger than that which was asked for.
1989 At a minimum, it causes a crash in zvbi since it does
1990 a memcpy based on the source buffer length */
1991 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001992 b->length = dev->vbi_width * dev->vbi_height * 2;
1993
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001994 return result;
1995 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001996}
1997
1998static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1999{
2000 struct em28xx_fh *fh = priv;
2001 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002002 int rc;
2003
2004 rc = check_dev(dev);
2005 if (rc < 0)
2006 return rc;
2007
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002008 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2009 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002010 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002011 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012}
2013
2014static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2015{
2016 struct em28xx_fh *fh = priv;
2017 struct em28xx *dev = fh->dev;
2018 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002019
2020 rc = check_dev(dev);
2021 if (rc < 0)
2022 return rc;
2023
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002024 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2025 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2026 O_NONBLOCK);
2027 else
2028 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2029 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002030}
2031
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002032/* ----------------------------------------------------------- */
2033/* RADIO ESPECIFIC IOCTLS */
2034/* ----------------------------------------------------------- */
2035
2036static int radio_querycap(struct file *file, void *priv,
2037 struct v4l2_capability *cap)
2038{
2039 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2040
2041 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2042 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002043 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002044
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002045 cap->capabilities = V4L2_CAP_TUNER;
2046 return 0;
2047}
2048
2049static int radio_g_tuner(struct file *file, void *priv,
2050 struct v4l2_tuner *t)
2051{
2052 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2053
2054 if (unlikely(t->index > 0))
2055 return -EINVAL;
2056
2057 strcpy(t->name, "Radio");
2058 t->type = V4L2_TUNER_RADIO;
2059
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002060 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002061
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002062 return 0;
2063}
2064
2065static int radio_enum_input(struct file *file, void *priv,
2066 struct v4l2_input *i)
2067{
2068 if (i->index != 0)
2069 return -EINVAL;
2070 strcpy(i->name, "Radio");
2071 i->type = V4L2_INPUT_TYPE_TUNER;
2072
2073 return 0;
2074}
2075
2076static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2077{
2078 if (unlikely(a->index))
2079 return -EINVAL;
2080
2081 strcpy(a->name, "Radio");
2082 return 0;
2083}
2084
2085static int radio_s_tuner(struct file *file, void *priv,
2086 struct v4l2_tuner *t)
2087{
2088 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2089
2090 if (0 != t->index)
2091 return -EINVAL;
2092
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002093 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002094
2095 return 0;
2096}
2097
2098static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002099 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002100{
2101 return 0;
2102}
2103
2104static int radio_s_input(struct file *file, void *fh, unsigned int i)
2105{
2106 return 0;
2107}
2108
2109static int radio_queryctrl(struct file *file, void *priv,
2110 struct v4l2_queryctrl *qc)
2111{
2112 int i;
2113
2114 if (qc->id < V4L2_CID_BASE ||
2115 qc->id >= V4L2_CID_LASTP1)
2116 return -EINVAL;
2117
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002118 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2119 if (qc->id && qc->id == ac97_qctrl[i].id) {
2120 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002121 return 0;
2122 }
2123 }
2124
2125 return -EINVAL;
2126}
2127
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002128/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002129 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002130 * inits the device and starts isoc transfer
2131 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002132static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002134 int errCode = 0, radio = 0;
2135 struct video_device *vdev = video_devdata(filp);
2136 struct em28xx *dev = video_drvdata(filp);
2137 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002138 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002139 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002140
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002141 switch (vdev->vfl_type) {
2142 case VFL_TYPE_GRABBER:
2143 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2144 break;
2145 case VFL_TYPE_VBI:
2146 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2147 break;
2148 case VFL_TYPE_RADIO:
2149 radio = 1;
2150 break;
2151 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002152
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002153 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2154 video_device_node_name(vdev), v4l2_type_names[fh_type],
2155 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002156
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157
Hans Verkuil876cb142012-06-23 08:12:47 -03002158 if (mutex_lock_interruptible(&dev->lock))
2159 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002160 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002161 if (!fh) {
2162 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002163 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002164 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002165 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002166 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002167 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002168 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002169 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002170
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002171 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002172 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002173 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002174 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002175
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002176 /* Needed, since GPIO might have disabled power of
2177 some i2c device
2178 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002179 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002180
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002181 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002182 if (fh->radio) {
2183 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002184 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002185 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
2187 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002188
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002189 if (dev->progressive)
2190 field = V4L2_FIELD_NONE;
2191 else
2192 field = V4L2_FIELD_INTERLACED;
2193
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002194 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2195 NULL, &dev->slock,
2196 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002197 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002198
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002199 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2200 NULL, &dev->slock,
2201 V4L2_BUF_TYPE_VBI_CAPTURE,
2202 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002203 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002204 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002205
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002206 return errCode;
2207}
2208
2209/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002210 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002211 * unregisters the v4l2,i2c and usb devices
2212 * called when the device gets disconected or at module unload
2213*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002214void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002215{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002216
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002217 /*FIXME: I2C IR should be disconnected */
2218
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002219 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002220 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002221 video_unregister_device(dev->radio_dev);
2222 else
2223 video_device_release(dev->radio_dev);
2224 dev->radio_dev = NULL;
2225 }
2226 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002227 em28xx_info("V4L2 device %s deregistered\n",
2228 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002229 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002230 video_unregister_device(dev->vbi_dev);
2231 else
2232 video_device_release(dev->vbi_dev);
2233 dev->vbi_dev = NULL;
2234 }
2235 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002236 em28xx_info("V4L2 device %s deregistered\n",
2237 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002238 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002239 video_unregister_device(dev->vdev);
2240 else
2241 video_device_release(dev->vdev);
2242 dev->vdev = NULL;
2243 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002244}
2245
2246/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002247 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002248 * stops streaming and deallocates all resources allocated by the v4l2
2249 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002250 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002251static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002252{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002253 struct em28xx_fh *fh = filp->private_data;
2254 struct em28xx *dev = fh->dev;
2255 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002256
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002257 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002258
Hans Verkuil876cb142012-06-23 08:12:47 -03002259 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002260 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002261 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002262 res_free(fh, EM28XX_RESOURCE_VIDEO);
2263 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002265 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2266 videobuf_stop(&fh->vb_vbiq);
2267 res_free(fh, EM28XX_RESOURCE_VBI);
2268 }
2269
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002270 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002271 /* the device is already disconnect,
2272 free the remaining resources */
2273 if (dev->state & DEV_DISCONNECTED) {
2274 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002275 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002276 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002277 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002278 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002279 return 0;
2280 }
2281
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002282 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002283 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002284
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002285 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002286 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002287 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002288
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002289 /* set alternate 0 */
2290 dev->alt = 0;
2291 em28xx_videodbg("setting alternate 0\n");
2292 errCode = usb_set_interface(dev->udev, 0, 0);
2293 if (errCode < 0) {
2294 em28xx_errdev("cannot change alternate number to "
2295 "0 (error=%i)\n", errCode);
2296 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002298
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002299 videobuf_mmap_free(&fh->vb_vidq);
2300 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002301 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002302 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002303 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002304 return 0;
2305}
2306
2307/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002308 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002309 * will allocate buffers when called for the first time
2310 */
2311static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002312em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002313 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002314{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002315 struct em28xx_fh *fh = filp->private_data;
2316 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002317 int rc;
2318
2319 rc = check_dev(dev);
2320 if (rc < 0)
2321 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002322
Hans Verkuil876cb142012-06-23 08:12:47 -03002323 if (mutex_lock_interruptible(&dev->lock))
2324 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002325 /* FIXME: read() is not prepared to allow changing the video
2326 resolution while streaming. Seems a bug at em28xx_set_fmt
2327 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002328
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002329 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002330 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002331 rc = -EBUSY;
2332 else
2333 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002334 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002335 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002336 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002337 rc = -EBUSY;
2338 else
2339 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002340 filp->f_flags & O_NONBLOCK);
2341 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002342 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002343
Hans Verkuil876cb142012-06-23 08:12:47 -03002344 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002345}
2346
2347/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002348 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002349 * will allocate buffers when called for the first time
2350 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002351static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002352{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002353 struct em28xx_fh *fh = filp->private_data;
2354 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002355 int rc;
2356
2357 rc = check_dev(dev);
2358 if (rc < 0)
2359 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002360
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002361 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2362 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2363 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002364 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002365 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2366 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2367 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002368 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002369 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002370 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002371 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002372}
2373
Hans Verkuil876cb142012-06-23 08:12:47 -03002374static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2375{
2376 struct em28xx_fh *fh = filp->private_data;
2377 struct em28xx *dev = fh->dev;
2378 unsigned int res;
2379
2380 mutex_lock(&dev->lock);
2381 res = em28xx_poll(filp, wait);
2382 mutex_unlock(&dev->lock);
2383 return res;
2384}
2385
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002387 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002388 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002389static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002390{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002391 struct em28xx_fh *fh = filp->private_data;
2392 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002393 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002394
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002395 rc = check_dev(dev);
2396 if (rc < 0)
2397 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002398
Hans Verkuil876cb142012-06-23 08:12:47 -03002399 if (mutex_lock_interruptible(&dev->lock))
2400 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002401 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2402 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2403 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2404 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002405 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002406
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002407 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2408 (unsigned long)vma->vm_start,
2409 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2410 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002412 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413}
2414
Hans Verkuilbec43662008-12-30 06:58:20 -03002415static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002416 .owner = THIS_MODULE,
2417 .open = em28xx_v4l2_open,
2418 .release = em28xx_v4l2_close,
2419 .read = em28xx_v4l2_read,
2420 .poll = em28xx_v4l2_poll,
2421 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002422 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002423};
2424
Hans Verkuila3998102008-07-21 02:57:38 -03002425static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002426 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002427 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2428 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2429 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2430 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002431 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2432 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002433 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002434 .vidioc_g_audio = vidioc_g_audio,
2435 .vidioc_s_audio = vidioc_s_audio,
2436 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002437 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2438 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2439 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002440
2441 .vidioc_reqbufs = vidioc_reqbufs,
2442 .vidioc_querybuf = vidioc_querybuf,
2443 .vidioc_qbuf = vidioc_qbuf,
2444 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002445 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002446 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002447 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002448 .vidioc_g_parm = vidioc_g_parm,
2449 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002450 .vidioc_enum_input = vidioc_enum_input,
2451 .vidioc_g_input = vidioc_g_input,
2452 .vidioc_s_input = vidioc_s_input,
2453 .vidioc_queryctrl = vidioc_queryctrl,
2454 .vidioc_g_ctrl = vidioc_g_ctrl,
2455 .vidioc_s_ctrl = vidioc_s_ctrl,
2456 .vidioc_streamon = vidioc_streamon,
2457 .vidioc_streamoff = vidioc_streamoff,
2458 .vidioc_g_tuner = vidioc_g_tuner,
2459 .vidioc_s_tuner = vidioc_s_tuner,
2460 .vidioc_g_frequency = vidioc_g_frequency,
2461 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002462#ifdef CONFIG_VIDEO_ADV_DEBUG
2463 .vidioc_g_register = vidioc_g_register,
2464 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002465 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002466#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002467};
2468
2469static const struct video_device em28xx_video_template = {
2470 .fops = &em28xx_v4l_fops,
2471 .release = video_device_release,
2472 .ioctl_ops = &video_ioctl_ops,
2473
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002474 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002475 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002476};
2477
Hans Verkuilbec43662008-12-30 06:58:20 -03002478static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002479 .owner = THIS_MODULE,
2480 .open = em28xx_v4l2_open,
2481 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002482 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002483};
2484
2485static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002486 .vidioc_querycap = radio_querycap,
2487 .vidioc_g_tuner = radio_g_tuner,
2488 .vidioc_enum_input = radio_enum_input,
2489 .vidioc_g_audio = radio_g_audio,
2490 .vidioc_s_tuner = radio_s_tuner,
2491 .vidioc_s_audio = radio_s_audio,
2492 .vidioc_s_input = radio_s_input,
2493 .vidioc_queryctrl = radio_queryctrl,
2494 .vidioc_g_ctrl = vidioc_g_ctrl,
2495 .vidioc_s_ctrl = vidioc_s_ctrl,
2496 .vidioc_g_frequency = vidioc_g_frequency,
2497 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002498#ifdef CONFIG_VIDEO_ADV_DEBUG
2499 .vidioc_g_register = vidioc_g_register,
2500 .vidioc_s_register = vidioc_s_register,
2501#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002502};
2503
Hans Verkuila3998102008-07-21 02:57:38 -03002504static struct video_device em28xx_radio_template = {
2505 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002506 .fops = &radio_fops,
2507 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002508};
2509
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002510/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002511
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002512
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002513
Adrian Bunk532fe652008-01-28 22:10:48 -03002514static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002515 const struct video_device *template,
2516 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002517{
2518 struct video_device *vfd;
2519
2520 vfd = video_device_alloc();
2521 if (NULL == vfd)
2522 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002523
2524 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002525 vfd->v4l2_dev = &dev->v4l2_dev;
2526 vfd->release = video_device_release;
2527 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002528 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002529
2530 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2531 dev->name, type_name);
2532
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002533 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002534 return vfd;
2535}
2536
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002537int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002538{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002539 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002540 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002541 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002542
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002543 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2544 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002545
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002546 /* set default norm */
2547 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002548 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002549 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002550
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002551 /* Analog specific initialization */
2552 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002553
2554 maxw = norm_maxw(dev);
2555 /* MaxPacketSize for em2800 is too small to capture at full resolution
2556 * use half of maxw as the scaler can only scale to 50% */
2557 if (dev->board.is_em2800)
2558 maxw /= 2;
2559
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002560 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002561 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002562
Ezequiel García96371fc2012-03-23 18:09:34 -03002563 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002564
2565 /* Audio defaults */
2566 dev->mute = 1;
2567 dev->volume = 0x1f;
2568
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002569/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002570 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2571 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2572 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002573
2574 em28xx_set_outfmt(dev);
2575 em28xx_colorlevels_set_default(dev);
2576 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002577
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002578 /* allocate and fill video video_device struct */
2579 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2580 if (!dev->vdev) {
2581 em28xx_errdev("cannot allocate video_device.\n");
2582 return -ENODEV;
2583 }
2584
2585 /* register v4l2 video video_device */
2586 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2587 video_nr[dev->devno]);
2588 if (ret) {
2589 em28xx_errdev("unable to register video device (error=%i).\n",
2590 ret);
2591 return ret;
2592 }
2593
2594 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002595 if (em28xx_vbi_supported(dev) == 1) {
2596 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2597 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002598
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002599 /* register v4l2 vbi video_device */
2600 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2601 vbi_nr[dev->devno]);
2602 if (ret < 0) {
2603 em28xx_errdev("unable to register vbi device\n");
2604 return ret;
2605 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002606 }
2607
2608 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002609 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2610 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002611 if (!dev->radio_dev) {
2612 em28xx_errdev("cannot allocate video_device.\n");
2613 return -ENODEV;
2614 }
2615 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2616 radio_nr[dev->devno]);
2617 if (ret < 0) {
2618 em28xx_errdev("can't register radio device\n");
2619 return ret;
2620 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002621 em28xx_info("Registered radio device as %s\n",
2622 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002623 }
2624
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002625 em28xx_info("V4L2 video device registered as %s\n",
2626 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002627
2628 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002629 em28xx_info("V4L2 VBI device registered as %s\n",
2630 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002631
2632 return 0;
2633}