blob: d4f230002747319cd202cdafa0846a129279fc10 [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 Schaefer3610f582012-11-25 06:37:33 -0300579 if (actual_length == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300580 /* NOTE: happens very often with isoc transfers */
581 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300582 continue;
583 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300584
585 /* capture type 0 = vbi start
586 capture type 1 = video start
587 capture type 2 = video in progress */
Frank Schaefer3610f582012-11-25 06:37:33 -0300588 len = actual_length;
589 if (len >= 4) {
590 /* NOTE: headers are always 4 bytes and
591 * never split across packets */
592 if (p[0] == 0x33 && p[1] == 0x95) {
593 dev->capture_type = 0;
594 dev->vbi_read = 0;
595 em28xx_isocdbg("VBI START HEADER!!!\n");
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300596 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300597 p += 4;
598 len -= 4;
599 } else if (p[0] == 0x88 && p[1] == 0x88 &&
600 p[2] == 0x88 && p[3] == 0x88) {
601 /* continuation */
602 p += 4;
603 len -= 4;
604 } else if (p[0] == 0x22 && p[1] == 0x5a) {
605 /* start video */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300606 dev->capture_type = 1;
607 dev->top_field = !(p[2] & 1);
Frank Schaefer3610f582012-11-25 06:37:33 -0300608 p += 4;
609 len -= 4;
610 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300611 }
Frank Schaefer3610f582012-11-25 06:37:33 -0300612 /* NOTE: with bulk transfers, intermediate data packets
613 * have no continuation header */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300614
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300615 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300616
617 if (dev->capture_type == 0) {
618 if (dev->vbi_read >= vbi_size) {
619 /* We've already read all the VBI data, so
620 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300621 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300622 } else if ((dev->vbi_read + len) < vbi_size) {
623 /* This entire frame is VBI data */
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300624 if (dev->vbi_read == 0 && dev->top_field) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300625 /* Brand new frame */
626 if (vbi_buf != NULL)
627 vbi_buffer_filled(dev,
628 vbi_dma_q,
629 vbi_buf);
630 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
631 if (vbi_buf == NULL)
632 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300633 else
634 vbioutp = videobuf_to_vmalloc(
635 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300636 }
637
638 if (dev->vbi_read == 0) {
639 vbi_dma_q->pos = 0;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300640 if (vbi_buf != NULL)
641 vbi_buf->top_field = dev->top_field;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300642 }
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;
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300662 if (dev->progressive || dev->top_field) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300663 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 }
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300671 if (buf != NULL)
672 buf->top_field = dev->top_field;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300673
674 dma_q->pos = 0;
675 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300676
Frank Schaeferb77e0c02012-11-25 06:37:32 -0300677 if (buf != NULL && dev->capture_type == 2 && len > 0)
678 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300679 }
680 return rc;
681}
682
683
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300684/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300685 Videobuf operations
686 ------------------------------------------------------------------*/
687
688static int
689buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
690{
691 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300692 struct em28xx *dev = fh->dev;
693 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300694
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300695 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
696 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300697
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698 if (0 == *count)
699 *count = EM28XX_DEF_BUF;
700
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300701 if (*count < EM28XX_MIN_BUF)
702 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300703
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300704 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300705 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300706 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300707 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300708
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300709 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300710
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300711 return 0;
712}
713
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300714/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
716{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300717 struct em28xx_fh *fh = vq->priv_data;
718 struct em28xx *dev = fh->dev;
719 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300720 if (in_interrupt())
721 BUG();
722
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300723 /* We used to wait for the buffer to finish here, but this didn't work
724 because, as we were keeping the state as VIDEOBUF_QUEUED,
725 videobuf_queue_cancel marked it as finished for us.
726 (Also, it could wedge forever if the hardware was misconfigured.)
727
728 This should be safe; by the time we get here, the buffer isn't
729 queued anymore. If we ever start marking the buffers as
730 VIDEOBUF_ACTIVE, it won't be, though.
731 */
732 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300733 if (dev->usb_ctl.vid_buf == buf)
734 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300735 spin_unlock_irqrestore(&dev->slock, flags);
736
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300737 videobuf_vmalloc_free(&buf->vb);
738 buf->vb.state = VIDEOBUF_NEEDS_INIT;
739}
740
741static int
742buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
743 enum v4l2_field field)
744{
745 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300746 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300747 struct em28xx *dev = fh->dev;
748 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300749
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300750 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
751 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300752
753 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
754 return -EINVAL;
755
Brandon Philips05612972008-04-13 14:57:01 -0300756 buf->vb.width = dev->width;
757 buf->vb.height = dev->height;
758 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759
760 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300761 rc = videobuf_iolock(vq, &buf->vb, NULL);
762 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300764 }
765
Frank Schaefer74209dc2012-11-08 14:11:37 -0300766 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300767 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300768
769 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300770 dev->capture_type = -1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300771 if (em28xx_vbi_supported(dev) == 1)
Frank Schaeferc647a912012-11-08 14:11:52 -0300772 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
773 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300774 EM28XX_NUM_BUFS,
775 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300776 dev->packet_multiplier,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300777 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300778 else
Frank Schaeferc647a912012-11-08 14:11:52 -0300779 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
780 dev->analog_xfer_bulk,
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300781 EM28XX_NUM_BUFS,
782 dev->max_pkt_size,
Frank Schaeferc647a912012-11-08 14:11:52 -0300783 dev->packet_multiplier,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300784 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300785 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300786 goto fail;
787 }
788
789 buf->vb.state = VIDEOBUF_PREPARED;
790 return 0;
791
792fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300793 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300794 return rc;
795}
796
797static void
798buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
799{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300800 struct em28xx_buffer *buf = container_of(vb,
801 struct em28xx_buffer,
802 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300803 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300804 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300805 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300807 buf->vb.state = VIDEOBUF_QUEUED;
808 list_add_tail(&buf->vb.queue, &vidq->active);
809
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810}
811
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300812static void buffer_release(struct videobuf_queue *vq,
813 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300814{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300815 struct em28xx_buffer *buf = container_of(vb,
816 struct em28xx_buffer,
817 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300818 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300819 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300820
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300821 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300822
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300823 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300824}
825
826static struct videobuf_queue_ops em28xx_video_qops = {
827 .buf_setup = buffer_setup,
828 .buf_prepare = buffer_prepare,
829 .buf_queue = buffer_queue,
830 .buf_release = buffer_release,
831};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800832
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300833/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800834
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800835static void video_mux(struct em28xx *dev, int index)
836{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800837 dev->ctl_input = index;
838 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300839 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800840
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300841 if (!dev->ctl_aoutput)
842 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
843
Hans Verkuil5325b422009-04-02 11:26:22 -0300844 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
845 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800846
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300847 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300848 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300849 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
850 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300851 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300852 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300853 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
854 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800855 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300856
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300857 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300858 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
859 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300860 }
861
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300862 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800863}
864
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300865/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300866static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300867{
868 struct em28xx *dev = fh->dev;
869
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300870 if (fh->resources & bit)
871 /* have it already allocated */
872 return 1;
873
874 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300875 if (dev->resources & bit) {
876 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300877 return 0;
878 }
879 /* it's free, grab it */
880 fh->resources |= bit;
881 dev->resources |= bit;
882 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300883 return 1;
884}
885
886static int res_check(struct em28xx_fh *fh, unsigned int bit)
887{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300888 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300889}
890
891static int res_locked(struct em28xx *dev, unsigned int bit)
892{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300893 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300894}
895
896static void res_free(struct em28xx_fh *fh, unsigned int bits)
897{
898 struct em28xx *dev = fh->dev;
899
900 BUG_ON((fh->resources & bits) != bits);
901
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300902 fh->resources &= ~bits;
903 dev->resources &= ~bits;
904 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300905}
906
907static int get_ressource(struct em28xx_fh *fh)
908{
909 switch (fh->type) {
910 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
911 return EM28XX_RESOURCE_VIDEO;
912 case V4L2_BUF_TYPE_VBI_CAPTURE:
913 return EM28XX_RESOURCE_VBI;
914 default:
915 BUG();
916 return 0;
917 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300918}
919
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800920/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300921 * ac97_queryctrl()
922 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300923 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300924static int ac97_queryctrl(struct v4l2_queryctrl *qc)
925{
926 int i;
927
928 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
929 if (qc->id && qc->id == ac97_qctrl[i].id) {
930 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
931 return 0;
932 }
933 }
934
935 /* Control is not ac97 related */
936 return 1;
937}
938
939/*
940 * ac97_get_ctrl()
941 * return the current values for ac97 mute and volume
942 */
943static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300944{
945 switch (ctrl->id) {
946 case V4L2_CID_AUDIO_MUTE:
947 ctrl->value = dev->mute;
948 return 0;
949 case V4L2_CID_AUDIO_VOLUME:
950 ctrl->value = dev->volume;
951 return 0;
952 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300953 /* Control is not ac97 related */
954 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955 }
956}
957
958/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300959 * ac97_set_ctrl()
960 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300962static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300964 int i;
965
966 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
967 if (ctrl->id == ac97_qctrl[i].id)
968 goto handle;
969
970 /* Announce that hasn't handle it */
971 return 1;
972
973handle:
974 if (ctrl->value < ac97_qctrl[i].minimum ||
975 ctrl->value > ac97_qctrl[i].maximum)
976 return -ERANGE;
977
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300978 switch (ctrl->id) {
979 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300980 dev->mute = ctrl->value;
981 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300982 case V4L2_CID_AUDIO_VOLUME:
983 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300984 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300985 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300986
987 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988}
989
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300990static int check_dev(struct em28xx *dev)
991{
992 if (dev->state & DEV_DISCONNECTED) {
993 em28xx_errdev("v4l2 ioctl: device not present\n");
994 return -ENODEV;
995 }
996
997 if (dev->state & DEV_MISCONFIGURED) {
998 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
999 "close and open it again\n");
1000 return -EIO;
1001 }
1002 return 0;
1003}
1004
1005static void get_scale(struct em28xx *dev,
1006 unsigned int width, unsigned int height,
1007 unsigned int *hscale, unsigned int *vscale)
1008{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001009 unsigned int maxw = norm_maxw(dev);
1010 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001011
1012 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1013 if (*hscale >= 0x4000)
1014 *hscale = 0x3fff;
1015
1016 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1017 if (*vscale >= 0x4000)
1018 *vscale = 0x3fff;
1019}
1020
1021/* ------------------------------------------------------------------
1022 IOCTL vidioc handling
1023 ------------------------------------------------------------------*/
1024
Hans Verkuil78b526a2008-05-28 12:16:41 -03001025static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001026 struct v4l2_format *f)
1027{
1028 struct em28xx_fh *fh = priv;
1029 struct em28xx *dev = fh->dev;
1030
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001031 f->fmt.pix.width = dev->width;
1032 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001033 f->fmt.pix.pixelformat = dev->format->fourcc;
1034 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001035 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001036 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1037
1038 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001039 if (dev->progressive)
1040 f->fmt.pix.field = V4L2_FIELD_NONE;
1041 else
1042 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044 return 0;
1045}
1046
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001047static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1048{
1049 unsigned int i;
1050
1051 for (i = 0; i < ARRAY_SIZE(format); i++)
1052 if (format[i].fourcc == fourcc)
1053 return &format[i];
1054
1055 return NULL;
1056}
1057
Hans Verkuil78b526a2008-05-28 12:16:41 -03001058static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059 struct v4l2_format *f)
1060{
1061 struct em28xx_fh *fh = priv;
1062 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001063 unsigned int width = f->fmt.pix.width;
1064 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001065 unsigned int maxw = norm_maxw(dev);
1066 unsigned int maxh = norm_maxh(dev);
1067 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001068 struct em28xx_fmt *fmt;
1069
1070 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1071 if (!fmt) {
1072 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1073 f->fmt.pix.pixelformat);
1074 return -EINVAL;
1075 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001076
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001077 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001079 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1080 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001081 /* MaxPacketSize for em2800 is too small to capture at full resolution
1082 * use half of maxw as the scaler can only scale to 50% */
1083 if (width == maxw && height == maxh)
1084 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001085 } else {
1086 /* width must even because of the YUYV format
1087 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001088 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1089 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090 }
1091
1092 get_scale(dev, width, height, &hscale, &vscale);
1093
1094 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1095 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1096
1097 f->fmt.pix.width = width;
1098 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001099 f->fmt.pix.pixelformat = fmt->fourcc;
1100 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1101 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001102 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001103 if (dev->progressive)
1104 f->fmt.pix.field = V4L2_FIELD_NONE;
1105 else
1106 f->fmt.pix.field = dev->interlaced ?
1107 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001108
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001109 return 0;
1110}
1111
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001112static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1113 unsigned width, unsigned height)
1114{
1115 struct em28xx_fmt *fmt;
1116
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001117 fmt = format_by_fourcc(fourcc);
1118 if (!fmt)
1119 return -EINVAL;
1120
1121 dev->format = fmt;
1122 dev->width = width;
1123 dev->height = height;
1124
1125 /* set new image size */
1126 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1127
1128 em28xx_set_alternate(dev);
1129 em28xx_resolution_set(dev);
1130
1131 return 0;
1132}
1133
Hans Verkuil78b526a2008-05-28 12:16:41 -03001134static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001135 struct v4l2_format *f)
1136{
1137 struct em28xx_fh *fh = priv;
1138 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001139 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001140
1141 rc = check_dev(dev);
1142 if (rc < 0)
1143 return rc;
1144
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001145 vidioc_try_fmt_vid_cap(file, priv, f);
1146
Brandon Philips05612972008-04-13 14:57:01 -03001147 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1148 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001149 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001150 }
1151
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001152 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001153 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154}
1155
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001156static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1157{
1158 struct em28xx_fh *fh = priv;
1159 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001160 int rc;
1161
1162 rc = check_dev(dev);
1163 if (rc < 0)
1164 return rc;
1165
1166 *norm = dev->norm;
1167
1168 return 0;
1169}
1170
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001171static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1172{
1173 struct em28xx_fh *fh = priv;
1174 struct em28xx *dev = fh->dev;
1175 int rc;
1176
1177 rc = check_dev(dev);
1178 if (rc < 0)
1179 return rc;
1180
1181 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1182
1183 return 0;
1184}
1185
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001186static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001191 int rc;
1192
1193 rc = check_dev(dev);
1194 if (rc < 0)
1195 return rc;
1196
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001197 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198
1199 /* Adjusts width/height, if needed */
1200 f.fmt.pix.width = dev->width;
1201 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001202 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204 /* set new image size */
1205 dev->width = f.fmt.pix.width;
1206 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001207 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1208
1209 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001210 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001211
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001212 return 0;
1213}
1214
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001215static int vidioc_g_parm(struct file *file, void *priv,
1216 struct v4l2_streamparm *p)
1217{
1218 struct em28xx_fh *fh = priv;
1219 struct em28xx *dev = fh->dev;
1220 int rc = 0;
1221
1222 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223 return -EINVAL;
1224
1225 if (dev->board.is_webcam)
1226 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1227 video, g_parm, p);
1228 else
1229 v4l2_video_std_frame_period(dev->norm,
1230 &p->parm.capture.timeperframe);
1231
1232 return rc;
1233}
1234
1235static int vidioc_s_parm(struct file *file, void *priv,
1236 struct v4l2_streamparm *p)
1237{
1238 struct em28xx_fh *fh = priv;
1239 struct em28xx *dev = fh->dev;
1240
1241 if (!dev->board.is_webcam)
1242 return -EINVAL;
1243
1244 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1245 return -EINVAL;
1246
1247 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1248}
1249
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001250static const char *iname[] = {
1251 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1252 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1253 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1254 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1255 [EM28XX_VMUX_SVIDEO] = "S-Video",
1256 [EM28XX_VMUX_TELEVISION] = "Television",
1257 [EM28XX_VMUX_CABLE] = "Cable TV",
1258 [EM28XX_VMUX_DVB] = "DVB",
1259 [EM28XX_VMUX_DEBUG] = "for debug only",
1260};
1261
1262static int vidioc_enum_input(struct file *file, void *priv,
1263 struct v4l2_input *i)
1264{
1265 struct em28xx_fh *fh = priv;
1266 struct em28xx *dev = fh->dev;
1267 unsigned int n;
1268
1269 n = i->index;
1270 if (n >= MAX_EM28XX_INPUT)
1271 return -EINVAL;
1272 if (0 == INPUT(n)->type)
1273 return -EINVAL;
1274
1275 i->index = n;
1276 i->type = V4L2_INPUT_TYPE_CAMERA;
1277
1278 strcpy(i->name, iname[INPUT(n)->type]);
1279
1280 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1281 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1282 i->type = V4L2_INPUT_TYPE_TUNER;
1283
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001284 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001285
1286 return 0;
1287}
1288
1289static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1290{
1291 struct em28xx_fh *fh = priv;
1292 struct em28xx *dev = fh->dev;
1293
1294 *i = dev->ctl_input;
1295
1296 return 0;
1297}
1298
1299static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1300{
1301 struct em28xx_fh *fh = priv;
1302 struct em28xx *dev = fh->dev;
1303 int rc;
1304
1305 rc = check_dev(dev);
1306 if (rc < 0)
1307 return rc;
1308
1309 if (i >= MAX_EM28XX_INPUT)
1310 return -EINVAL;
1311 if (0 == INPUT(i)->type)
1312 return -EINVAL;
1313
Ezequiel García96371fc2012-03-23 18:09:34 -03001314 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001315 return 0;
1316}
1317
1318static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1319{
1320 struct em28xx_fh *fh = priv;
1321 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001322
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001323 if (!dev->audio_mode.has_audio)
1324 return -EINVAL;
1325
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001326 switch (a->index) {
1327 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001329 break;
1330 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001332 break;
1333 case EM28XX_AMUX_VIDEO2:
1334 strcpy(a->name, "Television alt");
1335 break;
1336 case EM28XX_AMUX_PHONE:
1337 strcpy(a->name, "Phone");
1338 break;
1339 case EM28XX_AMUX_MIC:
1340 strcpy(a->name, "Mic");
1341 break;
1342 case EM28XX_AMUX_CD:
1343 strcpy(a->name, "CD");
1344 break;
1345 case EM28XX_AMUX_AUX:
1346 strcpy(a->name, "Aux");
1347 break;
1348 case EM28XX_AMUX_PCM_OUT:
1349 strcpy(a->name, "PCM");
1350 break;
1351 default:
1352 return -EINVAL;
1353 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001354
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001355 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001357
1358 return 0;
1359}
1360
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001361static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362{
1363 struct em28xx_fh *fh = priv;
1364 struct em28xx *dev = fh->dev;
1365
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001366
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001367 if (!dev->audio_mode.has_audio)
1368 return -EINVAL;
1369
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001370 if (a->index >= MAX_EM28XX_INPUT)
1371 return -EINVAL;
1372 if (0 == INPUT(a->index)->type)
1373 return -EINVAL;
1374
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001375 dev->ctl_ainput = INPUT(a->index)->amux;
1376 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001377
1378 if (!dev->ctl_aoutput)
1379 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001380
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381 return 0;
1382}
1383
1384static int vidioc_queryctrl(struct file *file, void *priv,
1385 struct v4l2_queryctrl *qc)
1386{
1387 struct em28xx_fh *fh = priv;
1388 struct em28xx *dev = fh->dev;
1389 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001390 int rc;
1391
1392 rc = check_dev(dev);
1393 if (rc < 0)
1394 return rc;
1395
1396 memset(qc, 0, sizeof(*qc));
1397
1398 qc->id = id;
1399
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001400 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001401 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1402 rc = ac97_queryctrl(qc);
1403 if (!rc)
1404 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001405 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001406
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001407 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001408 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001409
1410 if (qc->type)
1411 return 0;
1412 else
1413 return -EINVAL;
1414}
1415
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001416/*
1417 * FIXME: This is an indirect way to check if a control exists at a
1418 * subdev. Instead of that hack, maybe the better would be to change all
1419 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1420 */
1421static int check_subdev_ctrl(struct em28xx *dev, int id)
1422{
1423 struct v4l2_queryctrl qc;
1424
1425 memset(&qc, 0, sizeof(qc));
1426 qc.id = id;
1427
1428 /* enumerate V4L2 device controls */
1429 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1430
1431 if (qc.type)
1432 return 0;
1433 else
1434 return -EINVAL;
1435}
1436
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001437static int vidioc_g_ctrl(struct file *file, void *priv,
1438 struct v4l2_control *ctrl)
1439{
1440 struct em28xx_fh *fh = priv;
1441 struct em28xx *dev = fh->dev;
1442 int rc;
1443
1444 rc = check_dev(dev);
1445 if (rc < 0)
1446 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001447 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001448
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001449 /* Set an AC97 control */
1450 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1451 rc = ac97_get_ctrl(dev, ctrl);
1452 else
1453 rc = 1;
1454
1455 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1456 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001457 if (check_subdev_ctrl(dev, ctrl->id))
1458 return -EINVAL;
1459
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001460 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001461 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001462 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001463
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464 return rc;
1465}
1466
1467static int vidioc_s_ctrl(struct file *file, void *priv,
1468 struct v4l2_control *ctrl)
1469{
1470 struct em28xx_fh *fh = priv;
1471 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001472 int rc;
1473
1474 rc = check_dev(dev);
1475 if (rc < 0)
1476 return rc;
1477
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001478 /* Set an AC97 control */
1479 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1480 rc = ac97_set_ctrl(dev, ctrl);
1481 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001483
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001484 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001485 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001486 rc = check_subdev_ctrl(dev, ctrl->id);
1487 if (!rc)
1488 v4l2_device_call_all(&dev->v4l2_dev, 0,
1489 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001490 /*
1491 * In the case of non-AC97 volume controls, we still need
1492 * to do some setups at em28xx, in order to mute/unmute
1493 * and to adjust audio volume. However, the value ranges
1494 * should be checked by the corresponding V4L subdriver.
1495 */
1496 switch (ctrl->id) {
1497 case V4L2_CID_AUDIO_MUTE:
1498 dev->mute = ctrl->value;
1499 rc = em28xx_audio_analog_set(dev);
1500 break;
1501 case V4L2_CID_AUDIO_VOLUME:
1502 dev->volume = ctrl->value;
1503 rc = em28xx_audio_analog_set(dev);
1504 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001505 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001506 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001507}
1508
1509static int vidioc_g_tuner(struct file *file, void *priv,
1510 struct v4l2_tuner *t)
1511{
1512 struct em28xx_fh *fh = priv;
1513 struct em28xx *dev = fh->dev;
1514 int rc;
1515
1516 rc = check_dev(dev);
1517 if (rc < 0)
1518 return rc;
1519
1520 if (0 != t->index)
1521 return -EINVAL;
1522
1523 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001524 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001525
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001526 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001527 return 0;
1528}
1529
1530static int vidioc_s_tuner(struct file *file, void *priv,
1531 struct v4l2_tuner *t)
1532{
1533 struct em28xx_fh *fh = priv;
1534 struct em28xx *dev = fh->dev;
1535 int rc;
1536
1537 rc = check_dev(dev);
1538 if (rc < 0)
1539 return rc;
1540
1541 if (0 != t->index)
1542 return -EINVAL;
1543
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001544 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001545 return 0;
1546}
1547
1548static int vidioc_g_frequency(struct file *file, void *priv,
1549 struct v4l2_frequency *f)
1550{
1551 struct em28xx_fh *fh = priv;
1552 struct em28xx *dev = fh->dev;
1553
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001554 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001556 return 0;
1557}
1558
1559static int vidioc_s_frequency(struct file *file, void *priv,
1560 struct v4l2_frequency *f)
1561{
1562 struct em28xx_fh *fh = priv;
1563 struct em28xx *dev = fh->dev;
1564 int rc;
1565
1566 rc = check_dev(dev);
1567 if (rc < 0)
1568 return rc;
1569
1570 if (0 != f->tuner)
1571 return -EINVAL;
1572
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001573 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1574 return -EINVAL;
1575 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001576 return -EINVAL;
1577
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001578 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001579 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001580
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001581 return 0;
1582}
1583
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001584#ifdef CONFIG_VIDEO_ADV_DEBUG
1585static int em28xx_reg_len(int reg)
1586{
1587 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001588 case EM28XX_R40_AC97LSB:
1589 case EM28XX_R30_HSCALELOW:
1590 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001591 return 2;
1592 default:
1593 return 1;
1594 }
1595}
1596
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001597static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001598 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001599{
1600 struct em28xx_fh *fh = priv;
1601 struct em28xx *dev = fh->dev;
1602
1603 chip->ident = V4L2_IDENT_NONE;
1604 chip->revision = 0;
1605
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001606 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001607
1608 return 0;
1609}
1610
1611
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001612static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001613 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001614{
1615 struct em28xx_fh *fh = priv;
1616 struct em28xx *dev = fh->dev;
1617 int ret;
1618
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001619 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001620 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001621 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001622 if (ret < 0)
1623 return ret;
1624
1625 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001626 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001627 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001628 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001629 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001630 return 0;
1631 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001632 /* TODO: is this correct? */
1633 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1634 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001635 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001636 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001637 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001638 }
1639
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001640 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001641 reg->size = em28xx_reg_len(reg->reg);
1642 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001644
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001645 if (ret < 0)
1646 return ret;
1647
1648 reg->val = ret;
1649 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001650 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001651 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1652 reg->reg, (char *)&val, 2);
1653 if (ret < 0)
1654 return ret;
1655
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001656 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001657 }
1658
1659 return 0;
1660}
1661
1662static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001663 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001664{
1665 struct em28xx_fh *fh = priv;
1666 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001667 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001668
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001669 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001670 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001671 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001672 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001673 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001674 return 0;
1675 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001676 /* TODO: is this correct? */
1677 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1678 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001679 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001680 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001681 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001682 }
1683
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001684 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001685 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001686
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001687 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001688 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001689}
1690#endif
1691
1692
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001693static int vidioc_cropcap(struct file *file, void *priv,
1694 struct v4l2_cropcap *cc)
1695{
1696 struct em28xx_fh *fh = priv;
1697 struct em28xx *dev = fh->dev;
1698
1699 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1700 return -EINVAL;
1701
1702 cc->bounds.left = 0;
1703 cc->bounds.top = 0;
1704 cc->bounds.width = dev->width;
1705 cc->bounds.height = dev->height;
1706 cc->defrect = cc->bounds;
1707 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1708 cc->pixelaspect.denominator = 59;
1709
1710 return 0;
1711}
1712
1713static int vidioc_streamon(struct file *file, void *priv,
1714 enum v4l2_buf_type type)
1715{
1716 struct em28xx_fh *fh = priv;
1717 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001718 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001719
1720 rc = check_dev(dev);
1721 if (rc < 0)
1722 return rc;
1723
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001724 if (unlikely(type != fh->type))
1725 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001726
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001727 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1728 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001729
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001730 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001731 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001732
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001733 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1734 rc = videobuf_streamon(&fh->vb_vidq);
1735 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1736 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001737
1738 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001739}
1740
1741static int vidioc_streamoff(struct file *file, void *priv,
1742 enum v4l2_buf_type type)
1743{
1744 struct em28xx_fh *fh = priv;
1745 struct em28xx *dev = fh->dev;
1746 int rc;
1747
1748 rc = check_dev(dev);
1749 if (rc < 0)
1750 return rc;
1751
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001752 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1753 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001754 return -EINVAL;
1755 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001756 return -EINVAL;
1757
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001758 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1759 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001760
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001761 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001762 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1763 videobuf_streamoff(&fh->vb_vidq);
1764 res_free(fh, EM28XX_RESOURCE_VIDEO);
1765 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001766 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001767 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1768 videobuf_streamoff(&fh->vb_vbiq);
1769 res_free(fh, EM28XX_RESOURCE_VBI);
1770 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001771 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001772
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773 return 0;
1774}
1775
1776static int vidioc_querycap(struct file *file, void *priv,
1777 struct v4l2_capability *cap)
1778{
1779 struct em28xx_fh *fh = priv;
1780 struct em28xx *dev = fh->dev;
1781
1782 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1783 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001784 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001786 cap->capabilities =
1787 V4L2_CAP_SLICED_VBI_CAPTURE |
1788 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001789 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1790
Devin Heitmueller04146142009-09-11 00:08:44 -03001791 if (dev->vbi_dev)
1792 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1793
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001794 if (dev->audio_mode.has_audio)
1795 cap->capabilities |= V4L2_CAP_AUDIO;
1796
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001797 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001798 cap->capabilities |= V4L2_CAP_TUNER;
1799
1800 return 0;
1801}
1802
Hans Verkuil78b526a2008-05-28 12:16:41 -03001803static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001804 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001805{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001806 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001807 return -EINVAL;
1808
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001809 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1810 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001811
1812 return 0;
1813}
1814
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001815static int vidioc_enum_framesizes(struct file *file, void *priv,
1816 struct v4l2_frmsizeenum *fsize)
1817{
1818 struct em28xx_fh *fh = priv;
1819 struct em28xx *dev = fh->dev;
1820 struct em28xx_fmt *fmt;
1821 unsigned int maxw = norm_maxw(dev);
1822 unsigned int maxh = norm_maxh(dev);
1823
1824 fmt = format_by_fourcc(fsize->pixel_format);
1825 if (!fmt) {
1826 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1827 fsize->pixel_format);
1828 return -EINVAL;
1829 }
1830
1831 if (dev->board.is_em2800) {
1832 if (fsize->index > 1)
1833 return -EINVAL;
1834 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1835 fsize->discrete.width = maxw / (1 + fsize->index);
1836 fsize->discrete.height = maxh / (1 + fsize->index);
1837 return 0;
1838 }
1839
1840 if (fsize->index != 0)
1841 return -EINVAL;
1842
1843 /* Report a continuous range */
1844 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1845 fsize->stepwise.min_width = 48;
1846 fsize->stepwise.min_height = 32;
1847 fsize->stepwise.max_width = maxw;
1848 fsize->stepwise.max_height = maxh;
1849 fsize->stepwise.step_width = 1;
1850 fsize->stepwise.step_height = 1;
1851 return 0;
1852}
1853
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001854/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001855static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001856 struct v4l2_format *f)
1857{
1858 struct em28xx_fh *fh = priv;
1859 struct em28xx *dev = fh->dev;
1860 int rc;
1861
1862 rc = check_dev(dev);
1863 if (rc < 0)
1864 return rc;
1865
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001866 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001867 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001868
1869 if (f->fmt.sliced.service_set == 0)
1870 rc = -EINVAL;
1871
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001872 return rc;
1873}
1874
Hans Verkuil78b526a2008-05-28 12:16:41 -03001875static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001876 struct v4l2_format *f)
1877{
1878 struct em28xx_fh *fh = priv;
1879 struct em28xx *dev = fh->dev;
1880 int rc;
1881
1882 rc = check_dev(dev);
1883 if (rc < 0)
1884 return rc;
1885
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001886 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001887
1888 if (f->fmt.sliced.service_set == 0)
1889 return -EINVAL;
1890
1891 return 0;
1892}
1893
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001894/* RAW VBI ioctls */
1895
1896static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1897 struct v4l2_format *format)
1898{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001899 struct em28xx_fh *fh = priv;
1900 struct em28xx *dev = fh->dev;
1901
1902 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001903 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1904 format->fmt.vbi.offset = 0;
1905 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001906 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1907 format->fmt.vbi.count[0] = dev->vbi_height;
1908 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001909
1910 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001911 if (dev->norm & V4L2_STD_525_60) {
1912 /* NTSC */
1913 format->fmt.vbi.start[0] = 10;
1914 format->fmt.vbi.start[1] = 273;
1915 } else if (dev->norm & V4L2_STD_625_50) {
1916 /* PAL */
1917 format->fmt.vbi.start[0] = 6;
1918 format->fmt.vbi.start[1] = 318;
1919 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001920
1921 return 0;
1922}
1923
1924static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1925 struct v4l2_format *format)
1926{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001927 struct em28xx_fh *fh = priv;
1928 struct em28xx *dev = fh->dev;
1929
1930 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001931 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1932 format->fmt.vbi.offset = 0;
1933 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001934 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1935 format->fmt.vbi.count[0] = dev->vbi_height;
1936 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001937
1938 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001939 if (dev->norm & V4L2_STD_525_60) {
1940 /* NTSC */
1941 format->fmt.vbi.start[0] = 10;
1942 format->fmt.vbi.start[1] = 273;
1943 } else if (dev->norm & V4L2_STD_625_50) {
1944 /* PAL */
1945 format->fmt.vbi.start[0] = 6;
1946 format->fmt.vbi.start[1] = 318;
1947 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001948
1949 return 0;
1950}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001951
1952static int vidioc_reqbufs(struct file *file, void *priv,
1953 struct v4l2_requestbuffers *rb)
1954{
1955 struct em28xx_fh *fh = priv;
1956 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001957 int rc;
1958
1959 rc = check_dev(dev);
1960 if (rc < 0)
1961 return rc;
1962
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001963 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1964 return videobuf_reqbufs(&fh->vb_vidq, rb);
1965 else
1966 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001967}
1968
1969static int vidioc_querybuf(struct file *file, void *priv,
1970 struct v4l2_buffer *b)
1971{
1972 struct em28xx_fh *fh = priv;
1973 struct em28xx *dev = fh->dev;
1974 int rc;
1975
1976 rc = check_dev(dev);
1977 if (rc < 0)
1978 return rc;
1979
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001980 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1981 return videobuf_querybuf(&fh->vb_vidq, b);
1982 else {
1983 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1984 the videobuf framework, but we probably shouldn't be
1985 returning a buffer larger than that which was asked for.
1986 At a minimum, it causes a crash in zvbi since it does
1987 a memcpy based on the source buffer length */
1988 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001989 b->length = dev->vbi_width * dev->vbi_height * 2;
1990
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001991 return result;
1992 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001993}
1994
1995static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1996{
1997 struct em28xx_fh *fh = priv;
1998 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001999 int rc;
2000
2001 rc = check_dev(dev);
2002 if (rc < 0)
2003 return rc;
2004
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002005 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2006 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002007 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002008 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002009}
2010
2011static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2012{
2013 struct em28xx_fh *fh = priv;
2014 struct em28xx *dev = fh->dev;
2015 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002016
2017 rc = check_dev(dev);
2018 if (rc < 0)
2019 return rc;
2020
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002021 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2022 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2023 O_NONBLOCK);
2024 else
2025 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2026 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002027}
2028
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002029/* ----------------------------------------------------------- */
2030/* RADIO ESPECIFIC IOCTLS */
2031/* ----------------------------------------------------------- */
2032
2033static int radio_querycap(struct file *file, void *priv,
2034 struct v4l2_capability *cap)
2035{
2036 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2037
2038 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2039 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002040 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002041
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002042 cap->capabilities = V4L2_CAP_TUNER;
2043 return 0;
2044}
2045
2046static int radio_g_tuner(struct file *file, void *priv,
2047 struct v4l2_tuner *t)
2048{
2049 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2050
2051 if (unlikely(t->index > 0))
2052 return -EINVAL;
2053
2054 strcpy(t->name, "Radio");
2055 t->type = V4L2_TUNER_RADIO;
2056
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002057 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002058
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002059 return 0;
2060}
2061
2062static int radio_enum_input(struct file *file, void *priv,
2063 struct v4l2_input *i)
2064{
2065 if (i->index != 0)
2066 return -EINVAL;
2067 strcpy(i->name, "Radio");
2068 i->type = V4L2_INPUT_TYPE_TUNER;
2069
2070 return 0;
2071}
2072
2073static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2074{
2075 if (unlikely(a->index))
2076 return -EINVAL;
2077
2078 strcpy(a->name, "Radio");
2079 return 0;
2080}
2081
2082static int radio_s_tuner(struct file *file, void *priv,
2083 struct v4l2_tuner *t)
2084{
2085 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2086
2087 if (0 != t->index)
2088 return -EINVAL;
2089
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002090 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002091
2092 return 0;
2093}
2094
2095static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002096 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002097{
2098 return 0;
2099}
2100
2101static int radio_s_input(struct file *file, void *fh, unsigned int i)
2102{
2103 return 0;
2104}
2105
2106static int radio_queryctrl(struct file *file, void *priv,
2107 struct v4l2_queryctrl *qc)
2108{
2109 int i;
2110
2111 if (qc->id < V4L2_CID_BASE ||
2112 qc->id >= V4L2_CID_LASTP1)
2113 return -EINVAL;
2114
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002115 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2116 if (qc->id && qc->id == ac97_qctrl[i].id) {
2117 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002118 return 0;
2119 }
2120 }
2121
2122 return -EINVAL;
2123}
2124
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002125/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002126 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127 * inits the device and starts isoc transfer
2128 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002129static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002130{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002131 int errCode = 0, radio = 0;
2132 struct video_device *vdev = video_devdata(filp);
2133 struct em28xx *dev = video_drvdata(filp);
2134 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002135 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002136 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002137
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002138 switch (vdev->vfl_type) {
2139 case VFL_TYPE_GRABBER:
2140 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2141 break;
2142 case VFL_TYPE_VBI:
2143 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2144 break;
2145 case VFL_TYPE_RADIO:
2146 radio = 1;
2147 break;
2148 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002149
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002150 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2151 video_device_node_name(vdev), v4l2_type_names[fh_type],
2152 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154
Hans Verkuil876cb142012-06-23 08:12:47 -03002155 if (mutex_lock_interruptible(&dev->lock))
2156 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002157 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002158 if (!fh) {
2159 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002160 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002161 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002163 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002164 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002165 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002166 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002167
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002168 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002169 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002170 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002171 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002173 /* Needed, since GPIO might have disabled power of
2174 some i2c device
2175 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002176 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002177
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002178 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002179 if (fh->radio) {
2180 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002181 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002182 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183
2184 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002186 if (dev->progressive)
2187 field = V4L2_FIELD_NONE;
2188 else
2189 field = V4L2_FIELD_INTERLACED;
2190
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002191 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2192 NULL, &dev->slock,
2193 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002194 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002195
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002196 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2197 NULL, &dev->slock,
2198 V4L2_BUF_TYPE_VBI_CAPTURE,
2199 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002200 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002201 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002202
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203 return errCode;
2204}
2205
2206/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002207 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002208 * unregisters the v4l2,i2c and usb devices
2209 * called when the device gets disconected or at module unload
2210*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002211void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002214 /*FIXME: I2C IR should be disconnected */
2215
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002216 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002217 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002218 video_unregister_device(dev->radio_dev);
2219 else
2220 video_device_release(dev->radio_dev);
2221 dev->radio_dev = NULL;
2222 }
2223 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002224 em28xx_info("V4L2 device %s deregistered\n",
2225 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002226 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002227 video_unregister_device(dev->vbi_dev);
2228 else
2229 video_device_release(dev->vbi_dev);
2230 dev->vbi_dev = NULL;
2231 }
2232 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002233 em28xx_info("V4L2 device %s deregistered\n",
2234 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002235 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002236 video_unregister_device(dev->vdev);
2237 else
2238 video_device_release(dev->vdev);
2239 dev->vdev = NULL;
2240 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241}
2242
2243/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002244 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002245 * stops streaming and deallocates all resources allocated by the v4l2
2246 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002248static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002249{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002250 struct em28xx_fh *fh = filp->private_data;
2251 struct em28xx *dev = fh->dev;
2252 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002253
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002254 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255
Hans Verkuil876cb142012-06-23 08:12:47 -03002256 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002257 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002258 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002259 res_free(fh, EM28XX_RESOURCE_VIDEO);
2260 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002261
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002262 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2263 videobuf_stop(&fh->vb_vbiq);
2264 res_free(fh, EM28XX_RESOURCE_VBI);
2265 }
2266
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002267 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002268 /* the device is already disconnect,
2269 free the remaining resources */
2270 if (dev->state & DEV_DISCONNECTED) {
2271 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03002272 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002273 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002274 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002275 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002276 return 0;
2277 }
2278
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002279 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002280 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002281
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002282 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002283 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002284 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002285
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002286 /* set alternate 0 */
2287 dev->alt = 0;
2288 em28xx_videodbg("setting alternate 0\n");
2289 errCode = usb_set_interface(dev->udev, 0, 0);
2290 if (errCode < 0) {
2291 em28xx_errdev("cannot change alternate number to "
2292 "0 (error=%i)\n", errCode);
2293 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002294 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002295
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002296 videobuf_mmap_free(&fh->vb_vidq);
2297 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002298 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002299 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002300 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002301 return 0;
2302}
2303
2304/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002305 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002306 * will allocate buffers when called for the first time
2307 */
2308static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002309em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002310 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002312 struct em28xx_fh *fh = filp->private_data;
2313 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002314 int rc;
2315
2316 rc = check_dev(dev);
2317 if (rc < 0)
2318 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002319
Hans Verkuil876cb142012-06-23 08:12:47 -03002320 if (mutex_lock_interruptible(&dev->lock))
2321 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002322 /* FIXME: read() is not prepared to allow changing the video
2323 resolution while streaming. Seems a bug at em28xx_set_fmt
2324 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002325
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002326 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002327 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002328 rc = -EBUSY;
2329 else
2330 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002331 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002332 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002333 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002334 rc = -EBUSY;
2335 else
2336 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002337 filp->f_flags & O_NONBLOCK);
2338 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002339 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002340
Hans Verkuil876cb142012-06-23 08:12:47 -03002341 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002342}
2343
2344/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002345 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002346 * will allocate buffers when called for the first time
2347 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002348static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002349{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002350 struct em28xx_fh *fh = filp->private_data;
2351 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002352 int rc;
2353
2354 rc = check_dev(dev);
2355 if (rc < 0)
2356 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002357
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002358 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2359 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2360 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002361 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002362 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2363 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2364 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002365 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002366 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002367 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002368 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002369}
2370
Hans Verkuil876cb142012-06-23 08:12:47 -03002371static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2372{
2373 struct em28xx_fh *fh = filp->private_data;
2374 struct em28xx *dev = fh->dev;
2375 unsigned int res;
2376
2377 mutex_lock(&dev->lock);
2378 res = em28xx_poll(filp, wait);
2379 mutex_unlock(&dev->lock);
2380 return res;
2381}
2382
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002383/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002384 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002385 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002386static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002387{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002388 struct em28xx_fh *fh = filp->private_data;
2389 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002390 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002391
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002392 rc = check_dev(dev);
2393 if (rc < 0)
2394 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002395
Hans Verkuil876cb142012-06-23 08:12:47 -03002396 if (mutex_lock_interruptible(&dev->lock))
2397 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002398 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2399 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2400 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2401 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002402 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002403
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002404 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2405 (unsigned long)vma->vm_start,
2406 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2407 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002408
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002409 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002410}
2411
Hans Verkuilbec43662008-12-30 06:58:20 -03002412static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002413 .owner = THIS_MODULE,
2414 .open = em28xx_v4l2_open,
2415 .release = em28xx_v4l2_close,
2416 .read = em28xx_v4l2_read,
2417 .poll = em28xx_v4l2_poll,
2418 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002419 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420};
2421
Hans Verkuila3998102008-07-21 02:57:38 -03002422static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002423 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002424 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2425 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2426 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2427 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002428 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2429 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002430 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002431 .vidioc_g_audio = vidioc_g_audio,
2432 .vidioc_s_audio = vidioc_s_audio,
2433 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002434 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2435 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2436 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002437
2438 .vidioc_reqbufs = vidioc_reqbufs,
2439 .vidioc_querybuf = vidioc_querybuf,
2440 .vidioc_qbuf = vidioc_qbuf,
2441 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002442 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002443 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002444 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002445 .vidioc_g_parm = vidioc_g_parm,
2446 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002447 .vidioc_enum_input = vidioc_enum_input,
2448 .vidioc_g_input = vidioc_g_input,
2449 .vidioc_s_input = vidioc_s_input,
2450 .vidioc_queryctrl = vidioc_queryctrl,
2451 .vidioc_g_ctrl = vidioc_g_ctrl,
2452 .vidioc_s_ctrl = vidioc_s_ctrl,
2453 .vidioc_streamon = vidioc_streamon,
2454 .vidioc_streamoff = vidioc_streamoff,
2455 .vidioc_g_tuner = vidioc_g_tuner,
2456 .vidioc_s_tuner = vidioc_s_tuner,
2457 .vidioc_g_frequency = vidioc_g_frequency,
2458 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002459#ifdef CONFIG_VIDEO_ADV_DEBUG
2460 .vidioc_g_register = vidioc_g_register,
2461 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002462 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002463#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002464};
2465
2466static const struct video_device em28xx_video_template = {
2467 .fops = &em28xx_v4l_fops,
2468 .release = video_device_release,
2469 .ioctl_ops = &video_ioctl_ops,
2470
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002471 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002472 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002473};
2474
Hans Verkuilbec43662008-12-30 06:58:20 -03002475static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002476 .owner = THIS_MODULE,
2477 .open = em28xx_v4l2_open,
2478 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002479 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002480};
2481
2482static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002483 .vidioc_querycap = radio_querycap,
2484 .vidioc_g_tuner = radio_g_tuner,
2485 .vidioc_enum_input = radio_enum_input,
2486 .vidioc_g_audio = radio_g_audio,
2487 .vidioc_s_tuner = radio_s_tuner,
2488 .vidioc_s_audio = radio_s_audio,
2489 .vidioc_s_input = radio_s_input,
2490 .vidioc_queryctrl = radio_queryctrl,
2491 .vidioc_g_ctrl = vidioc_g_ctrl,
2492 .vidioc_s_ctrl = vidioc_s_ctrl,
2493 .vidioc_g_frequency = vidioc_g_frequency,
2494 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002495#ifdef CONFIG_VIDEO_ADV_DEBUG
2496 .vidioc_g_register = vidioc_g_register,
2497 .vidioc_s_register = vidioc_s_register,
2498#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002499};
2500
Hans Verkuila3998102008-07-21 02:57:38 -03002501static struct video_device em28xx_radio_template = {
2502 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002503 .fops = &radio_fops,
2504 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002505};
2506
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002507/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002508
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002509
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002510
Adrian Bunk532fe652008-01-28 22:10:48 -03002511static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002512 const struct video_device *template,
2513 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002514{
2515 struct video_device *vfd;
2516
2517 vfd = video_device_alloc();
2518 if (NULL == vfd)
2519 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002520
2521 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002522 vfd->v4l2_dev = &dev->v4l2_dev;
2523 vfd->release = video_device_release;
2524 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002525 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002526
2527 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2528 dev->name, type_name);
2529
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002530 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002531 return vfd;
2532}
2533
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002534int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002535{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002536 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002537 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002538 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002539
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002540 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2541 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002542
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002543 /* set default norm */
2544 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002545 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002546 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002547
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002548 /* Analog specific initialization */
2549 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002550
2551 maxw = norm_maxw(dev);
2552 /* MaxPacketSize for em2800 is too small to capture at full resolution
2553 * use half of maxw as the scaler can only scale to 50% */
2554 if (dev->board.is_em2800)
2555 maxw /= 2;
2556
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002557 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002558 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002559
Ezequiel García96371fc2012-03-23 18:09:34 -03002560 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002561
2562 /* Audio defaults */
2563 dev->mute = 1;
2564 dev->volume = 0x1f;
2565
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002566/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002567 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2568 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2569 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002570
2571 em28xx_set_outfmt(dev);
2572 em28xx_colorlevels_set_default(dev);
2573 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002574
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002575 /* allocate and fill video video_device struct */
2576 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2577 if (!dev->vdev) {
2578 em28xx_errdev("cannot allocate video_device.\n");
2579 return -ENODEV;
2580 }
2581
2582 /* register v4l2 video video_device */
2583 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2584 video_nr[dev->devno]);
2585 if (ret) {
2586 em28xx_errdev("unable to register video device (error=%i).\n",
2587 ret);
2588 return ret;
2589 }
2590
2591 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002592 if (em28xx_vbi_supported(dev) == 1) {
2593 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2594 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002595
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002596 /* register v4l2 vbi video_device */
2597 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2598 vbi_nr[dev->devno]);
2599 if (ret < 0) {
2600 em28xx_errdev("unable to register vbi device\n");
2601 return ret;
2602 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002603 }
2604
2605 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002606 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2607 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002608 if (!dev->radio_dev) {
2609 em28xx_errdev("cannot allocate video_device.\n");
2610 return -ENODEV;
2611 }
2612 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2613 radio_nr[dev->devno]);
2614 if (ret < 0) {
2615 em28xx_errdev("can't register radio device\n");
2616 return ret;
2617 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002618 em28xx_info("Registered radio device as %s\n",
2619 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002620 }
2621
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002622 em28xx_info("V4L2 video device registered as %s\n",
2623 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002624
2625 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002626 em28xx_info("V4L2 VBI device registered as %s\n",
2627 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002628
2629 return 0;
2630}