blob: 87161eea257eba4b2ab6b9c1a7f251a85366df82 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
54#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
86MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080088
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030089static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030090module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080092
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030093/* supported video standards */
94static struct em28xx_fmt format[] = {
95 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030096 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030099 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300102 .fourcc = V4L2_PIX_FMT_RGB565,
103 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300104 .reg = EM28XX_OUTFMT_RGB_16_656,
105 }, {
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .depth = 8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
110 }, {
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
113 .depth = 8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
115 }, {
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
120 }, {
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
123 .depth = 12,
124 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300125 },
126};
127
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300130static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800131 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200132 .id = V4L2_CID_AUDIO_VOLUME,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Volume",
135 .minimum = 0x0,
136 .maximum = 0x1f,
137 .step = 0x1,
138 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300139 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300140 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200141 .id = V4L2_CID_AUDIO_MUTE,
142 .type = V4L2_CTRL_TYPE_BOOLEAN,
143 .name = "Mute",
144 .minimum = 0,
145 .maximum = 1,
146 .step = 1,
147 .default_value = 1,
148 .flags = 0,
149 }
150};
151
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300152/* ------------------------------------------------------------------
153 DMA and thread functions
154 ------------------------------------------------------------------*/
155
156/*
157 * Announces that a buffer were filled and request the next
158 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300159static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
162{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300167 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer74209dc2012-11-08 14:11:37 -0300169 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
175static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
178{
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300184 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300185
Frank Schaefer74209dc2012-11-08 14:11:37 -0300186 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
190}
191
192/*
193 * Identify the buffer header type and properly handles
194 */
195static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
198 unsigned char *p,
199 unsigned char *outp, unsigned long len)
200{
201 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300208 startread = p;
209 remain = len;
210
Frank Schaeferc02ec712012-11-08 14:11:33 -0300211 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300218
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
221 else
222 offset = linesdone * bytesperline * 2 + currlinedone;
223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = lencopy > remain ? remain : lencopy;
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300234 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300235 if (lencopy <= 0)
236 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300238
239 remain -= lencopy;
240
241 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300242 if (dev->progressive)
243 startwrite += lencopy;
244 else
245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300372 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
379
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300380 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300383
Frank Schaefer74209dc2012-11-08 14:11:37 -0300384 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385
386 return;
387}
388
389/*
390 * video-buf generic routine to get the next available VBI buffer
391 */
392static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
394{
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396 char *outp;
397
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300400 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300401 *buf = NULL;
402 return;
403 }
404
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
410
Frank Schaefer74209dc2012-11-08 14:11:37 -0300411 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414}
415
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300416/* Processes and copies the URB data content to a frame buffer queue */
417static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300418{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300419 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300420 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300421 int xfer_bulk, num_packets, i, rc = 1;
422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424
425 if (!dev)
426 return 0;
427
428 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
429 return 0;
430
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300431 if (urb->status < 0)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300433
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300434 xfer_bulk = usb_pipebulk(urb->pipe);
435
Frank Schaefer74209dc2012-11-08 14:11:37 -0300436 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300437 if (buf != NULL)
438 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300439
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300440 if (xfer_bulk) /* bulk */
441 num_packets = 1;
442 else /* isoc */
443 num_packets = urb->number_of_packets;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300445 for (i = 0; i < num_packets; i++) {
446 if (xfer_bulk) { /* bulk */
447 actual_length = urb->actual_length;
448
449 p = urb->transfer_buffer;
450 } else { /* isoc */
451 if (urb->iso_frame_desc[i].status < 0) {
452 print_err_status(dev, i,
453 urb->iso_frame_desc[i].status);
454 if (urb->iso_frame_desc[i].status != -EPROTO)
455 continue;
456 }
457
458 actual_length = urb->iso_frame_desc[i].actual_length;
459 if (actual_length > dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 continue;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300462 }
463
464 p = urb->transfer_buffer +
465 urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 }
467
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300468 if (actual_length <= 0) {
469 /* NOTE: happens very often with isoc transfers */
470 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300471 continue;
472 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473
474 /* FIXME: incomplete buffer checks where removed to make
475 logic simpler. Impacts of those changes should be evaluated
476 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300477 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478 em28xx_isocdbg("VBI HEADER!!!\n");
479 /* FIXME: Should add vbi copy */
480 continue;
481 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300482 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300483 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300484 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300486 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300487 if (buf != NULL)
488 buffer_filled(dev, dma_q, buf);
489 get_next_buf(dma_q, &buf);
490 if (buf == NULL)
491 outp = NULL;
492 else
493 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300494 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300495
496 if (buf != NULL) {
497 if (p[2] & 1)
498 buf->top_field = 0;
499 else
500 buf->top_field = 1;
501 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300502
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300505 if (buf != NULL) {
506 if (p[0] != 0x88 && p[0] != 0x22) {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300507 /* NOTE: no intermediate data packet header
508 * 88 88 88 88 when using bulk transfers */
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300509 em28xx_isocdbg("frame is not complete\n");
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300510 len = actual_length;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300511 } else {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300512 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300513 p += 4;
514 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300515 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300516 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300517 }
518 return rc;
519}
520
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300521/* Version of isoc handler that takes into account a mixture of video and
522 VBI data */
523static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
524{
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;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300528 unsigned char *outp = NULL;
529 unsigned char *vbioutp = NULL;
530 int i, len = 0, rc = 1;
531 unsigned char *p;
532 int vbi_size;
533
534 if (!dev)
535 return 0;
536
537 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
538 return 0;
539
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300540 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300541 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300542
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
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300551 for (i = 0; i < urb->number_of_packets; i++) {
552 int status = urb->iso_frame_desc[i].status;
553
554 if (status < 0) {
555 print_err_status(dev, i, status);
556 if (urb->iso_frame_desc[i].status != -EPROTO)
557 continue;
558 }
559
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300560 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300561 if (urb->iso_frame_desc[i].actual_length <= 0) {
562 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
563 continue;
564 }
565 if (urb->iso_frame_desc[i].actual_length >
566 dev->max_pkt_size) {
567 em28xx_isocdbg("packet bigger than packet size");
568 continue;
569 }
570
571 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
572
573 /* capture type 0 = vbi start
574 capture type 1 = video start
575 capture type 2 = video in progress */
576 if (p[0] == 0x33 && p[1] == 0x95) {
577 dev->capture_type = 0;
578 dev->vbi_read = 0;
579 em28xx_isocdbg("VBI START HEADER!!!\n");
580 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300581 p += 4;
582 len -= 4;
583 } else if (p[0] == 0x88 && p[1] == 0x88 &&
584 p[2] == 0x88 && p[3] == 0x88) {
585 /* continuation */
586 p += 4;
587 len -= 4;
588 } else if (p[0] == 0x22 && p[1] == 0x5a) {
589 /* start video */
590 p += 4;
591 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300592 }
593
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300594 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300595
596 if (dev->capture_type == 0) {
597 if (dev->vbi_read >= vbi_size) {
598 /* We've already read all the VBI data, so
599 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300600 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300601 } else if ((dev->vbi_read + len) < vbi_size) {
602 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300603 if (dev->vbi_read == 0 &&
604 (!(dev->cur_field & 1))) {
605 /* Brand new frame */
606 if (vbi_buf != NULL)
607 vbi_buffer_filled(dev,
608 vbi_dma_q,
609 vbi_buf);
610 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
611 if (vbi_buf == NULL)
612 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300613 else
614 vbioutp = videobuf_to_vmalloc(
615 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300616 }
617
618 if (dev->vbi_read == 0) {
619 vbi_dma_q->pos = 0;
620 if (vbi_buf != NULL) {
621 if (dev->cur_field & 1)
622 vbi_buf->top_field = 0;
623 else
624 vbi_buf->top_field = 1;
625 }
626 }
627
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300628 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300631 } else {
632 /* Some of this frame is VBI data and some is
633 video data */
634 int vbi_data_len = vbi_size - dev->vbi_read;
635 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300636 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
637 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300638 dev->capture_type = 1;
639 p += vbi_data_len;
640 len -= vbi_data_len;
641 }
642 }
643
644 if (dev->capture_type == 1) {
645 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300646 if (dev->progressive || !(dev->cur_field & 1)) {
647 if (buf != NULL)
648 buffer_filled(dev, dma_q, buf);
649 get_next_buf(dma_q, &buf);
650 if (buf == NULL)
651 outp = NULL;
652 else
653 outp = videobuf_to_vmalloc(&buf->vb);
654 }
655 if (buf != NULL) {
656 if (dev->cur_field & 1)
657 buf->top_field = 0;
658 else
659 buf->top_field = 1;
660 }
661
662 dma_q->pos = 0;
663 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300664
665 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300666 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300667 p[2] == 0x88 && p[3] == 0x88) {
668 p += 4;
669 len -= 4;
670 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300671 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300672 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
673 p[2], len, (p[2] & 1) ?
674 "odd" : "even");
675 p += 4;
676 len -= 4;
677 }
678
679 if (len > 0)
680 em28xx_copy_video(dev, dma_q, buf, p, outp,
681 len);
682 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300683 }
684 return rc;
685}
686
687
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300688/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689 Videobuf operations
690 ------------------------------------------------------------------*/
691
692static int
693buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
694{
695 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300696 struct em28xx *dev = fh->dev;
697 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300699 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
700 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300701
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300702 if (0 == *count)
703 *count = EM28XX_DEF_BUF;
704
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300705 if (*count < EM28XX_MIN_BUF)
706 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300707
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300708 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300709 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300710 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300711 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300712
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300713 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300714
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 return 0;
716}
717
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300718/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300719static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
720{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300721 struct em28xx_fh *fh = vq->priv_data;
722 struct em28xx *dev = fh->dev;
723 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300724 if (in_interrupt())
725 BUG();
726
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300727 /* We used to wait for the buffer to finish here, but this didn't work
728 because, as we were keeping the state as VIDEOBUF_QUEUED,
729 videobuf_queue_cancel marked it as finished for us.
730 (Also, it could wedge forever if the hardware was misconfigured.)
731
732 This should be safe; by the time we get here, the buffer isn't
733 queued anymore. If we ever start marking the buffers as
734 VIDEOBUF_ACTIVE, it won't be, though.
735 */
736 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300737 if (dev->usb_ctl.vid_buf == buf)
738 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300739 spin_unlock_irqrestore(&dev->slock, flags);
740
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 videobuf_vmalloc_free(&buf->vb);
742 buf->vb.state = VIDEOBUF_NEEDS_INIT;
743}
744
745static int
746buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
747 enum v4l2_field field)
748{
749 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300750 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300751 struct em28xx *dev = fh->dev;
752 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300753
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300754 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
755 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756
757 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
758 return -EINVAL;
759
Brandon Philips05612972008-04-13 14:57:01 -0300760 buf->vb.width = dev->width;
761 buf->vb.height = dev->height;
762 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763
764 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300765 rc = videobuf_iolock(vq, &buf->vb, NULL);
766 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300768 }
769
Frank Schaefer74209dc2012-11-08 14:11:37 -0300770 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300771 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300772
773 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300774 if (em28xx_vbi_supported(dev) == 1)
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300775 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
776 EM28XX_NUM_BUFS,
777 dev->max_pkt_size,
778 EM28XX_NUM_ISOC_PACKETS,
779 em28xx_isoc_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300780 else
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300781 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
782 EM28XX_NUM_BUFS,
783 dev->max_pkt_size,
784 EM28XX_NUM_ISOC_PACKETS,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300785 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300786 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300787 goto fail;
788 }
789
790 buf->vb.state = VIDEOBUF_PREPARED;
791 return 0;
792
793fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300794 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795 return rc;
796}
797
798static void
799buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
800{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300801 struct em28xx_buffer *buf = container_of(vb,
802 struct em28xx_buffer,
803 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300804 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300805 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300807
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300808 buf->vb.state = VIDEOBUF_QUEUED;
809 list_add_tail(&buf->vb.queue, &vidq->active);
810
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300811}
812
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300813static void buffer_release(struct videobuf_queue *vq,
814 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300815{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300816 struct em28xx_buffer *buf = container_of(vb,
817 struct em28xx_buffer,
818 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300819 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300820 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300821
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300822 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300823
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300824 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300825}
826
827static struct videobuf_queue_ops em28xx_video_qops = {
828 .buf_setup = buffer_setup,
829 .buf_prepare = buffer_prepare,
830 .buf_queue = buffer_queue,
831 .buf_release = buffer_release,
832};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800833
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300834/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800835
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800836static void video_mux(struct em28xx *dev, int index)
837{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800838 dev->ctl_input = index;
839 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300840 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800841
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300842 if (!dev->ctl_aoutput)
843 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
844
Hans Verkuil5325b422009-04-02 11:26:22 -0300845 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
846 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800847
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300848 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300849 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300850 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
851 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300852 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300853 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300854 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
855 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800856 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300857
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300858 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300859 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
860 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300861 }
862
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300863 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800864}
865
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300866/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300867static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300868{
869 struct em28xx *dev = fh->dev;
870
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300871 if (fh->resources & bit)
872 /* have it already allocated */
873 return 1;
874
875 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300876 if (dev->resources & bit) {
877 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300878 return 0;
879 }
880 /* it's free, grab it */
881 fh->resources |= bit;
882 dev->resources |= bit;
883 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300884 return 1;
885}
886
887static int res_check(struct em28xx_fh *fh, unsigned int bit)
888{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300889 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300890}
891
892static int res_locked(struct em28xx *dev, unsigned int bit)
893{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300894 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300895}
896
897static void res_free(struct em28xx_fh *fh, unsigned int bits)
898{
899 struct em28xx *dev = fh->dev;
900
901 BUG_ON((fh->resources & bits) != bits);
902
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300903 fh->resources &= ~bits;
904 dev->resources &= ~bits;
905 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300906}
907
908static int get_ressource(struct em28xx_fh *fh)
909{
910 switch (fh->type) {
911 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
912 return EM28XX_RESOURCE_VIDEO;
913 case V4L2_BUF_TYPE_VBI_CAPTURE:
914 return EM28XX_RESOURCE_VBI;
915 default:
916 BUG();
917 return 0;
918 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300919}
920
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800921/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300922 * ac97_queryctrl()
923 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300924 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300925static int ac97_queryctrl(struct v4l2_queryctrl *qc)
926{
927 int i;
928
929 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
930 if (qc->id && qc->id == ac97_qctrl[i].id) {
931 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
932 return 0;
933 }
934 }
935
936 /* Control is not ac97 related */
937 return 1;
938}
939
940/*
941 * ac97_get_ctrl()
942 * return the current values for ac97 mute and volume
943 */
944static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300945{
946 switch (ctrl->id) {
947 case V4L2_CID_AUDIO_MUTE:
948 ctrl->value = dev->mute;
949 return 0;
950 case V4L2_CID_AUDIO_VOLUME:
951 ctrl->value = dev->volume;
952 return 0;
953 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300954 /* Control is not ac97 related */
955 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 }
957}
958
959/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300960 * ac97_set_ctrl()
961 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300963static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300965 int i;
966
967 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
968 if (ctrl->id == ac97_qctrl[i].id)
969 goto handle;
970
971 /* Announce that hasn't handle it */
972 return 1;
973
974handle:
975 if (ctrl->value < ac97_qctrl[i].minimum ||
976 ctrl->value > ac97_qctrl[i].maximum)
977 return -ERANGE;
978
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979 switch (ctrl->id) {
980 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300981 dev->mute = ctrl->value;
982 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300983 case V4L2_CID_AUDIO_VOLUME:
984 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300985 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300987
988 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300989}
990
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991static int check_dev(struct em28xx *dev)
992{
993 if (dev->state & DEV_DISCONNECTED) {
994 em28xx_errdev("v4l2 ioctl: device not present\n");
995 return -ENODEV;
996 }
997
998 if (dev->state & DEV_MISCONFIGURED) {
999 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1000 "close and open it again\n");
1001 return -EIO;
1002 }
1003 return 0;
1004}
1005
1006static void get_scale(struct em28xx *dev,
1007 unsigned int width, unsigned int height,
1008 unsigned int *hscale, unsigned int *vscale)
1009{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001010 unsigned int maxw = norm_maxw(dev);
1011 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012
1013 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1014 if (*hscale >= 0x4000)
1015 *hscale = 0x3fff;
1016
1017 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1018 if (*vscale >= 0x4000)
1019 *vscale = 0x3fff;
1020}
1021
1022/* ------------------------------------------------------------------
1023 IOCTL vidioc handling
1024 ------------------------------------------------------------------*/
1025
Hans Verkuil78b526a2008-05-28 12:16:41 -03001026static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001027 struct v4l2_format *f)
1028{
1029 struct em28xx_fh *fh = priv;
1030 struct em28xx *dev = fh->dev;
1031
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001032 f->fmt.pix.width = dev->width;
1033 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001034 f->fmt.pix.pixelformat = dev->format->fourcc;
1035 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001036 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001037 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1038
1039 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001040 if (dev->progressive)
1041 f->fmt.pix.field = V4L2_FIELD_NONE;
1042 else
1043 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001045 return 0;
1046}
1047
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001048static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1049{
1050 unsigned int i;
1051
1052 for (i = 0; i < ARRAY_SIZE(format); i++)
1053 if (format[i].fourcc == fourcc)
1054 return &format[i];
1055
1056 return NULL;
1057}
1058
Hans Verkuil78b526a2008-05-28 12:16:41 -03001059static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001060 struct v4l2_format *f)
1061{
1062 struct em28xx_fh *fh = priv;
1063 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001064 unsigned int width = f->fmt.pix.width;
1065 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001066 unsigned int maxw = norm_maxw(dev);
1067 unsigned int maxh = norm_maxh(dev);
1068 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001069 struct em28xx_fmt *fmt;
1070
1071 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1072 if (!fmt) {
1073 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1074 f->fmt.pix.pixelformat);
1075 return -EINVAL;
1076 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001078 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001079 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001080 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1081 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001082 /* MaxPacketSize for em2800 is too small to capture at full resolution
1083 * use half of maxw as the scaler can only scale to 50% */
1084 if (width == maxw && height == maxh)
1085 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001086 } else {
1087 /* width must even because of the YUYV format
1088 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001089 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1090 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001091 }
1092
1093 get_scale(dev, width, height, &hscale, &vscale);
1094
1095 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1096 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1097
1098 f->fmt.pix.width = width;
1099 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001100 f->fmt.pix.pixelformat = fmt->fourcc;
1101 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1102 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001103 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001104 if (dev->progressive)
1105 f->fmt.pix.field = V4L2_FIELD_NONE;
1106 else
1107 f->fmt.pix.field = dev->interlaced ?
1108 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001109
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001110 return 0;
1111}
1112
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001113static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1114 unsigned width, unsigned height)
1115{
1116 struct em28xx_fmt *fmt;
1117
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001118 fmt = format_by_fourcc(fourcc);
1119 if (!fmt)
1120 return -EINVAL;
1121
1122 dev->format = fmt;
1123 dev->width = width;
1124 dev->height = height;
1125
1126 /* set new image size */
1127 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1128
1129 em28xx_set_alternate(dev);
1130 em28xx_resolution_set(dev);
1131
1132 return 0;
1133}
1134
Hans Verkuil78b526a2008-05-28 12:16:41 -03001135static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001136 struct v4l2_format *f)
1137{
1138 struct em28xx_fh *fh = priv;
1139 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001140 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141
1142 rc = check_dev(dev);
1143 if (rc < 0)
1144 return rc;
1145
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001146 vidioc_try_fmt_vid_cap(file, priv, f);
1147
Brandon Philips05612972008-04-13 14:57:01 -03001148 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1149 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001150 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001151 }
1152
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001153 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001154 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001155}
1156
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001157static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1158{
1159 struct em28xx_fh *fh = priv;
1160 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001161 int rc;
1162
1163 rc = check_dev(dev);
1164 if (rc < 0)
1165 return rc;
1166
1167 *norm = dev->norm;
1168
1169 return 0;
1170}
1171
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001172static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1173{
1174 struct em28xx_fh *fh = priv;
1175 struct em28xx *dev = fh->dev;
1176 int rc;
1177
1178 rc = check_dev(dev);
1179 if (rc < 0)
1180 return rc;
1181
1182 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1183
1184 return 0;
1185}
1186
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001187static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001188{
1189 struct em28xx_fh *fh = priv;
1190 struct em28xx *dev = fh->dev;
1191 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001192 int rc;
1193
1194 rc = check_dev(dev);
1195 if (rc < 0)
1196 return rc;
1197
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001198 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199
1200 /* Adjusts width/height, if needed */
1201 f.fmt.pix.width = dev->width;
1202 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001203 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001205 /* set new image size */
1206 dev->width = f.fmt.pix.width;
1207 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001208 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1209
1210 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001211 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001212
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001213 return 0;
1214}
1215
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001216static int vidioc_g_parm(struct file *file, void *priv,
1217 struct v4l2_streamparm *p)
1218{
1219 struct em28xx_fh *fh = priv;
1220 struct em28xx *dev = fh->dev;
1221 int rc = 0;
1222
1223 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1224 return -EINVAL;
1225
1226 if (dev->board.is_webcam)
1227 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1228 video, g_parm, p);
1229 else
1230 v4l2_video_std_frame_period(dev->norm,
1231 &p->parm.capture.timeperframe);
1232
1233 return rc;
1234}
1235
1236static int vidioc_s_parm(struct file *file, void *priv,
1237 struct v4l2_streamparm *p)
1238{
1239 struct em28xx_fh *fh = priv;
1240 struct em28xx *dev = fh->dev;
1241
1242 if (!dev->board.is_webcam)
1243 return -EINVAL;
1244
1245 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1246 return -EINVAL;
1247
1248 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1249}
1250
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001251static const char *iname[] = {
1252 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1253 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1254 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1255 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1256 [EM28XX_VMUX_SVIDEO] = "S-Video",
1257 [EM28XX_VMUX_TELEVISION] = "Television",
1258 [EM28XX_VMUX_CABLE] = "Cable TV",
1259 [EM28XX_VMUX_DVB] = "DVB",
1260 [EM28XX_VMUX_DEBUG] = "for debug only",
1261};
1262
1263static int vidioc_enum_input(struct file *file, void *priv,
1264 struct v4l2_input *i)
1265{
1266 struct em28xx_fh *fh = priv;
1267 struct em28xx *dev = fh->dev;
1268 unsigned int n;
1269
1270 n = i->index;
1271 if (n >= MAX_EM28XX_INPUT)
1272 return -EINVAL;
1273 if (0 == INPUT(n)->type)
1274 return -EINVAL;
1275
1276 i->index = n;
1277 i->type = V4L2_INPUT_TYPE_CAMERA;
1278
1279 strcpy(i->name, iname[INPUT(n)->type]);
1280
1281 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1282 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1283 i->type = V4L2_INPUT_TYPE_TUNER;
1284
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001285 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001286
1287 return 0;
1288}
1289
1290static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1291{
1292 struct em28xx_fh *fh = priv;
1293 struct em28xx *dev = fh->dev;
1294
1295 *i = dev->ctl_input;
1296
1297 return 0;
1298}
1299
1300static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1301{
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
1304 int rc;
1305
1306 rc = check_dev(dev);
1307 if (rc < 0)
1308 return rc;
1309
1310 if (i >= MAX_EM28XX_INPUT)
1311 return -EINVAL;
1312 if (0 == INPUT(i)->type)
1313 return -EINVAL;
1314
Ezequiel García96371fc2012-03-23 18:09:34 -03001315 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001316 return 0;
1317}
1318
1319static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1320{
1321 struct em28xx_fh *fh = priv;
1322 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001323
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001324 if (!dev->audio_mode.has_audio)
1325 return -EINVAL;
1326
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001327 switch (a->index) {
1328 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001329 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001330 break;
1331 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001333 break;
1334 case EM28XX_AMUX_VIDEO2:
1335 strcpy(a->name, "Television alt");
1336 break;
1337 case EM28XX_AMUX_PHONE:
1338 strcpy(a->name, "Phone");
1339 break;
1340 case EM28XX_AMUX_MIC:
1341 strcpy(a->name, "Mic");
1342 break;
1343 case EM28XX_AMUX_CD:
1344 strcpy(a->name, "CD");
1345 break;
1346 case EM28XX_AMUX_AUX:
1347 strcpy(a->name, "Aux");
1348 break;
1349 case EM28XX_AMUX_PCM_OUT:
1350 strcpy(a->name, "PCM");
1351 break;
1352 default:
1353 return -EINVAL;
1354 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001355
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001356 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001357 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001358
1359 return 0;
1360}
1361
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001362static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363{
1364 struct em28xx_fh *fh = priv;
1365 struct em28xx *dev = fh->dev;
1366
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001367
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001368 if (!dev->audio_mode.has_audio)
1369 return -EINVAL;
1370
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001371 if (a->index >= MAX_EM28XX_INPUT)
1372 return -EINVAL;
1373 if (0 == INPUT(a->index)->type)
1374 return -EINVAL;
1375
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001376 dev->ctl_ainput = INPUT(a->index)->amux;
1377 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001378
1379 if (!dev->ctl_aoutput)
1380 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001381
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 return 0;
1383}
1384
1385static int vidioc_queryctrl(struct file *file, void *priv,
1386 struct v4l2_queryctrl *qc)
1387{
1388 struct em28xx_fh *fh = priv;
1389 struct em28xx *dev = fh->dev;
1390 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001391 int rc;
1392
1393 rc = check_dev(dev);
1394 if (rc < 0)
1395 return rc;
1396
1397 memset(qc, 0, sizeof(*qc));
1398
1399 qc->id = id;
1400
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001401 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001402 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1403 rc = ac97_queryctrl(qc);
1404 if (!rc)
1405 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001406 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001407
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001408 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001409 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001410
1411 if (qc->type)
1412 return 0;
1413 else
1414 return -EINVAL;
1415}
1416
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001417/*
1418 * FIXME: This is an indirect way to check if a control exists at a
1419 * subdev. Instead of that hack, maybe the better would be to change all
1420 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1421 */
1422static int check_subdev_ctrl(struct em28xx *dev, int id)
1423{
1424 struct v4l2_queryctrl qc;
1425
1426 memset(&qc, 0, sizeof(qc));
1427 qc.id = id;
1428
1429 /* enumerate V4L2 device controls */
1430 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1431
1432 if (qc.type)
1433 return 0;
1434 else
1435 return -EINVAL;
1436}
1437
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001438static int vidioc_g_ctrl(struct file *file, void *priv,
1439 struct v4l2_control *ctrl)
1440{
1441 struct em28xx_fh *fh = priv;
1442 struct em28xx *dev = fh->dev;
1443 int rc;
1444
1445 rc = check_dev(dev);
1446 if (rc < 0)
1447 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001448 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001449
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001450 /* Set an AC97 control */
1451 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1452 rc = ac97_get_ctrl(dev, ctrl);
1453 else
1454 rc = 1;
1455
1456 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1457 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001458 if (check_subdev_ctrl(dev, ctrl->id))
1459 return -EINVAL;
1460
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001461 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001462 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001463 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001465 return rc;
1466}
1467
1468static int vidioc_s_ctrl(struct file *file, void *priv,
1469 struct v4l2_control *ctrl)
1470{
1471 struct em28xx_fh *fh = priv;
1472 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473 int rc;
1474
1475 rc = check_dev(dev);
1476 if (rc < 0)
1477 return rc;
1478
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001479 /* Set an AC97 control */
1480 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1481 rc = ac97_set_ctrl(dev, ctrl);
1482 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001483 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001484
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001485 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001486 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001487 rc = check_subdev_ctrl(dev, ctrl->id);
1488 if (!rc)
1489 v4l2_device_call_all(&dev->v4l2_dev, 0,
1490 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001491 /*
1492 * In the case of non-AC97 volume controls, we still need
1493 * to do some setups at em28xx, in order to mute/unmute
1494 * and to adjust audio volume. However, the value ranges
1495 * should be checked by the corresponding V4L subdriver.
1496 */
1497 switch (ctrl->id) {
1498 case V4L2_CID_AUDIO_MUTE:
1499 dev->mute = ctrl->value;
1500 rc = em28xx_audio_analog_set(dev);
1501 break;
1502 case V4L2_CID_AUDIO_VOLUME:
1503 dev->volume = ctrl->value;
1504 rc = em28xx_audio_analog_set(dev);
1505 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001506 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001507 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001508}
1509
1510static int vidioc_g_tuner(struct file *file, void *priv,
1511 struct v4l2_tuner *t)
1512{
1513 struct em28xx_fh *fh = priv;
1514 struct em28xx *dev = fh->dev;
1515 int rc;
1516
1517 rc = check_dev(dev);
1518 if (rc < 0)
1519 return rc;
1520
1521 if (0 != t->index)
1522 return -EINVAL;
1523
1524 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001525 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001526
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001527 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001528 return 0;
1529}
1530
1531static int vidioc_s_tuner(struct file *file, void *priv,
1532 struct v4l2_tuner *t)
1533{
1534 struct em28xx_fh *fh = priv;
1535 struct em28xx *dev = fh->dev;
1536 int rc;
1537
1538 rc = check_dev(dev);
1539 if (rc < 0)
1540 return rc;
1541
1542 if (0 != t->index)
1543 return -EINVAL;
1544
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001545 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546 return 0;
1547}
1548
1549static int vidioc_g_frequency(struct file *file, void *priv,
1550 struct v4l2_frequency *f)
1551{
1552 struct em28xx_fh *fh = priv;
1553 struct em28xx *dev = fh->dev;
1554
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001555 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001556 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001557 return 0;
1558}
1559
1560static int vidioc_s_frequency(struct file *file, void *priv,
1561 struct v4l2_frequency *f)
1562{
1563 struct em28xx_fh *fh = priv;
1564 struct em28xx *dev = fh->dev;
1565 int rc;
1566
1567 rc = check_dev(dev);
1568 if (rc < 0)
1569 return rc;
1570
1571 if (0 != f->tuner)
1572 return -EINVAL;
1573
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001574 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1575 return -EINVAL;
1576 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001577 return -EINVAL;
1578
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001579 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001580 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001581
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582 return 0;
1583}
1584
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001585#ifdef CONFIG_VIDEO_ADV_DEBUG
1586static int em28xx_reg_len(int reg)
1587{
1588 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001589 case EM28XX_R40_AC97LSB:
1590 case EM28XX_R30_HSCALELOW:
1591 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001592 return 2;
1593 default:
1594 return 1;
1595 }
1596}
1597
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001598static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001599 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001600{
1601 struct em28xx_fh *fh = priv;
1602 struct em28xx *dev = fh->dev;
1603
1604 chip->ident = V4L2_IDENT_NONE;
1605 chip->revision = 0;
1606
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001607 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001608
1609 return 0;
1610}
1611
1612
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001613static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001614 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001615{
1616 struct em28xx_fh *fh = priv;
1617 struct em28xx *dev = fh->dev;
1618 int ret;
1619
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001620 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001621 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001622 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001623 if (ret < 0)
1624 return ret;
1625
1626 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001627 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001628 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001629 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001630 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001631 return 0;
1632 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001633 /* TODO: is this correct? */
1634 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1635 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001636 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001637 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001638 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001639 }
1640
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001641 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001642 reg->size = em28xx_reg_len(reg->reg);
1643 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001644 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001645
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001646 if (ret < 0)
1647 return ret;
1648
1649 reg->val = ret;
1650 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001651 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001652 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1653 reg->reg, (char *)&val, 2);
1654 if (ret < 0)
1655 return ret;
1656
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001657 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001658 }
1659
1660 return 0;
1661}
1662
1663static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001664 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001665{
1666 struct em28xx_fh *fh = priv;
1667 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001668 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001669
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001670 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001671 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001672 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001673 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001674 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001675 return 0;
1676 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001677 /* TODO: is this correct? */
1678 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1679 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001680 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001681 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001682 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001683 }
1684
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001685 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001686 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001687
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001688 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001689 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001690}
1691#endif
1692
1693
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001694static int vidioc_cropcap(struct file *file, void *priv,
1695 struct v4l2_cropcap *cc)
1696{
1697 struct em28xx_fh *fh = priv;
1698 struct em28xx *dev = fh->dev;
1699
1700 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1701 return -EINVAL;
1702
1703 cc->bounds.left = 0;
1704 cc->bounds.top = 0;
1705 cc->bounds.width = dev->width;
1706 cc->bounds.height = dev->height;
1707 cc->defrect = cc->bounds;
1708 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1709 cc->pixelaspect.denominator = 59;
1710
1711 return 0;
1712}
1713
1714static int vidioc_streamon(struct file *file, void *priv,
1715 enum v4l2_buf_type type)
1716{
1717 struct em28xx_fh *fh = priv;
1718 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001720
1721 rc = check_dev(dev);
1722 if (rc < 0)
1723 return rc;
1724
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001725 if (unlikely(type != fh->type))
1726 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001727
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001728 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1729 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001730
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001731 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001732 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001733
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001734 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1735 rc = videobuf_streamon(&fh->vb_vidq);
1736 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1737 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001738
1739 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001740}
1741
1742static int vidioc_streamoff(struct file *file, void *priv,
1743 enum v4l2_buf_type type)
1744{
1745 struct em28xx_fh *fh = priv;
1746 struct em28xx *dev = fh->dev;
1747 int rc;
1748
1749 rc = check_dev(dev);
1750 if (rc < 0)
1751 return rc;
1752
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001753 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1754 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001755 return -EINVAL;
1756 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001757 return -EINVAL;
1758
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001759 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1760 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001761
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001762 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001763 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1764 videobuf_streamoff(&fh->vb_vidq);
1765 res_free(fh, EM28XX_RESOURCE_VIDEO);
1766 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001767 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001768 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1769 videobuf_streamoff(&fh->vb_vbiq);
1770 res_free(fh, EM28XX_RESOURCE_VBI);
1771 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001772 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001774 return 0;
1775}
1776
1777static int vidioc_querycap(struct file *file, void *priv,
1778 struct v4l2_capability *cap)
1779{
1780 struct em28xx_fh *fh = priv;
1781 struct em28xx *dev = fh->dev;
1782
1783 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1784 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001785 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001786
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001787 cap->capabilities =
1788 V4L2_CAP_SLICED_VBI_CAPTURE |
1789 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001790 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1791
Devin Heitmueller04146142009-09-11 00:08:44 -03001792 if (dev->vbi_dev)
1793 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1794
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001795 if (dev->audio_mode.has_audio)
1796 cap->capabilities |= V4L2_CAP_AUDIO;
1797
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001798 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001799 cap->capabilities |= V4L2_CAP_TUNER;
1800
1801 return 0;
1802}
1803
Hans Verkuil78b526a2008-05-28 12:16:41 -03001804static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001805 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001806{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001807 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001808 return -EINVAL;
1809
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001810 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1811 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001812
1813 return 0;
1814}
1815
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001816static int vidioc_enum_framesizes(struct file *file, void *priv,
1817 struct v4l2_frmsizeenum *fsize)
1818{
1819 struct em28xx_fh *fh = priv;
1820 struct em28xx *dev = fh->dev;
1821 struct em28xx_fmt *fmt;
1822 unsigned int maxw = norm_maxw(dev);
1823 unsigned int maxh = norm_maxh(dev);
1824
1825 fmt = format_by_fourcc(fsize->pixel_format);
1826 if (!fmt) {
1827 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1828 fsize->pixel_format);
1829 return -EINVAL;
1830 }
1831
1832 if (dev->board.is_em2800) {
1833 if (fsize->index > 1)
1834 return -EINVAL;
1835 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1836 fsize->discrete.width = maxw / (1 + fsize->index);
1837 fsize->discrete.height = maxh / (1 + fsize->index);
1838 return 0;
1839 }
1840
1841 if (fsize->index != 0)
1842 return -EINVAL;
1843
1844 /* Report a continuous range */
1845 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1846 fsize->stepwise.min_width = 48;
1847 fsize->stepwise.min_height = 32;
1848 fsize->stepwise.max_width = maxw;
1849 fsize->stepwise.max_height = maxh;
1850 fsize->stepwise.step_width = 1;
1851 fsize->stepwise.step_height = 1;
1852 return 0;
1853}
1854
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001855/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001856static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001857 struct v4l2_format *f)
1858{
1859 struct em28xx_fh *fh = priv;
1860 struct em28xx *dev = fh->dev;
1861 int rc;
1862
1863 rc = check_dev(dev);
1864 if (rc < 0)
1865 return rc;
1866
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001867 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001868 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001869
1870 if (f->fmt.sliced.service_set == 0)
1871 rc = -EINVAL;
1872
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001873 return rc;
1874}
1875
Hans Verkuil78b526a2008-05-28 12:16:41 -03001876static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001877 struct v4l2_format *f)
1878{
1879 struct em28xx_fh *fh = priv;
1880 struct em28xx *dev = fh->dev;
1881 int rc;
1882
1883 rc = check_dev(dev);
1884 if (rc < 0)
1885 return rc;
1886
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001887 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001888
1889 if (f->fmt.sliced.service_set == 0)
1890 return -EINVAL;
1891
1892 return 0;
1893}
1894
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001895/* RAW VBI ioctls */
1896
1897static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1898 struct v4l2_format *format)
1899{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001900 struct em28xx_fh *fh = priv;
1901 struct em28xx *dev = fh->dev;
1902
1903 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001904 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1905 format->fmt.vbi.offset = 0;
1906 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001907 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1908 format->fmt.vbi.count[0] = dev->vbi_height;
1909 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001910
1911 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001912 if (dev->norm & V4L2_STD_525_60) {
1913 /* NTSC */
1914 format->fmt.vbi.start[0] = 10;
1915 format->fmt.vbi.start[1] = 273;
1916 } else if (dev->norm & V4L2_STD_625_50) {
1917 /* PAL */
1918 format->fmt.vbi.start[0] = 6;
1919 format->fmt.vbi.start[1] = 318;
1920 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001921
1922 return 0;
1923}
1924
1925static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1926 struct v4l2_format *format)
1927{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001928 struct em28xx_fh *fh = priv;
1929 struct em28xx *dev = fh->dev;
1930
1931 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001932 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1933 format->fmt.vbi.offset = 0;
1934 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001935 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1936 format->fmt.vbi.count[0] = dev->vbi_height;
1937 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001938
1939 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001940 if (dev->norm & V4L2_STD_525_60) {
1941 /* NTSC */
1942 format->fmt.vbi.start[0] = 10;
1943 format->fmt.vbi.start[1] = 273;
1944 } else if (dev->norm & V4L2_STD_625_50) {
1945 /* PAL */
1946 format->fmt.vbi.start[0] = 6;
1947 format->fmt.vbi.start[1] = 318;
1948 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001949
1950 return 0;
1951}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001952
1953static int vidioc_reqbufs(struct file *file, void *priv,
1954 struct v4l2_requestbuffers *rb)
1955{
1956 struct em28xx_fh *fh = priv;
1957 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001958 int rc;
1959
1960 rc = check_dev(dev);
1961 if (rc < 0)
1962 return rc;
1963
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001964 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1965 return videobuf_reqbufs(&fh->vb_vidq, rb);
1966 else
1967 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001968}
1969
1970static int vidioc_querybuf(struct file *file, void *priv,
1971 struct v4l2_buffer *b)
1972{
1973 struct em28xx_fh *fh = priv;
1974 struct em28xx *dev = fh->dev;
1975 int rc;
1976
1977 rc = check_dev(dev);
1978 if (rc < 0)
1979 return rc;
1980
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001981 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1982 return videobuf_querybuf(&fh->vb_vidq, b);
1983 else {
1984 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1985 the videobuf framework, but we probably shouldn't be
1986 returning a buffer larger than that which was asked for.
1987 At a minimum, it causes a crash in zvbi since it does
1988 a memcpy based on the source buffer length */
1989 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001990 b->length = dev->vbi_width * dev->vbi_height * 2;
1991
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001992 return result;
1993 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001994}
1995
1996static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1997{
1998 struct em28xx_fh *fh = priv;
1999 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002000 int rc;
2001
2002 rc = check_dev(dev);
2003 if (rc < 0)
2004 return rc;
2005
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002006 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2007 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002008 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002009 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002010}
2011
2012static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2013{
2014 struct em28xx_fh *fh = priv;
2015 struct em28xx *dev = fh->dev;
2016 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002017
2018 rc = check_dev(dev);
2019 if (rc < 0)
2020 return rc;
2021
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002022 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2023 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2024 O_NONBLOCK);
2025 else
2026 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2027 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002028}
2029
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002030/* ----------------------------------------------------------- */
2031/* RADIO ESPECIFIC IOCTLS */
2032/* ----------------------------------------------------------- */
2033
2034static int radio_querycap(struct file *file, void *priv,
2035 struct v4l2_capability *cap)
2036{
2037 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2038
2039 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2040 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002041 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002042
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002043 cap->capabilities = V4L2_CAP_TUNER;
2044 return 0;
2045}
2046
2047static int radio_g_tuner(struct file *file, void *priv,
2048 struct v4l2_tuner *t)
2049{
2050 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2051
2052 if (unlikely(t->index > 0))
2053 return -EINVAL;
2054
2055 strcpy(t->name, "Radio");
2056 t->type = V4L2_TUNER_RADIO;
2057
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002058 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002059
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002060 return 0;
2061}
2062
2063static int radio_enum_input(struct file *file, void *priv,
2064 struct v4l2_input *i)
2065{
2066 if (i->index != 0)
2067 return -EINVAL;
2068 strcpy(i->name, "Radio");
2069 i->type = V4L2_INPUT_TYPE_TUNER;
2070
2071 return 0;
2072}
2073
2074static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2075{
2076 if (unlikely(a->index))
2077 return -EINVAL;
2078
2079 strcpy(a->name, "Radio");
2080 return 0;
2081}
2082
2083static int radio_s_tuner(struct file *file, void *priv,
2084 struct v4l2_tuner *t)
2085{
2086 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2087
2088 if (0 != t->index)
2089 return -EINVAL;
2090
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002091 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002092
2093 return 0;
2094}
2095
2096static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b2012-09-04 11:59:31 -03002097 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002098{
2099 return 0;
2100}
2101
2102static int radio_s_input(struct file *file, void *fh, unsigned int i)
2103{
2104 return 0;
2105}
2106
2107static int radio_queryctrl(struct file *file, void *priv,
2108 struct v4l2_queryctrl *qc)
2109{
2110 int i;
2111
2112 if (qc->id < V4L2_CID_BASE ||
2113 qc->id >= V4L2_CID_LASTP1)
2114 return -EINVAL;
2115
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002116 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2117 if (qc->id && qc->id == ac97_qctrl[i].id) {
2118 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002119 return 0;
2120 }
2121 }
2122
2123 return -EINVAL;
2124}
2125
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002126/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002127 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128 * inits the device and starts isoc transfer
2129 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002130static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002131{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002132 int errCode = 0, radio = 0;
2133 struct video_device *vdev = video_devdata(filp);
2134 struct em28xx *dev = video_drvdata(filp);
2135 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002136 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002137 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002138
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002139 switch (vdev->vfl_type) {
2140 case VFL_TYPE_GRABBER:
2141 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2142 break;
2143 case VFL_TYPE_VBI:
2144 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2145 break;
2146 case VFL_TYPE_RADIO:
2147 radio = 1;
2148 break;
2149 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002150
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002151 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2152 video_device_node_name(vdev), v4l2_type_names[fh_type],
2153 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002155
Hans Verkuil876cb142012-06-23 08:12:47 -03002156 if (mutex_lock_interruptible(&dev->lock))
2157 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002158 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002159 if (!fh) {
2160 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002161 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002162 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002163 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002164 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002165 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002166 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002167 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002168
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002169 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002170 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002171 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002172 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002174 /* Needed, since GPIO might have disabled power of
2175 some i2c device
2176 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002177 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002178
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002179 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002180 if (fh->radio) {
2181 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002182 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002183 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184
2185 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002187 if (dev->progressive)
2188 field = V4L2_FIELD_NONE;
2189 else
2190 field = V4L2_FIELD_INTERLACED;
2191
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002192 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2193 NULL, &dev->slock,
2194 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002195 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002196
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002197 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2198 NULL, &dev->slock,
2199 V4L2_BUF_TYPE_VBI_CAPTURE,
2200 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002201 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002202 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002203
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002204 return errCode;
2205}
2206
2207/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002208 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209 * unregisters the v4l2,i2c and usb devices
2210 * called when the device gets disconected or at module unload
2211*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002212void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002215 /*FIXME: I2C IR should be disconnected */
2216
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002217 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002218 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002219 video_unregister_device(dev->radio_dev);
2220 else
2221 video_device_release(dev->radio_dev);
2222 dev->radio_dev = NULL;
2223 }
2224 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002225 em28xx_info("V4L2 device %s deregistered\n",
2226 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002227 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002228 video_unregister_device(dev->vbi_dev);
2229 else
2230 video_device_release(dev->vbi_dev);
2231 dev->vbi_dev = NULL;
2232 }
2233 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002234 em28xx_info("V4L2 device %s deregistered\n",
2235 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002236 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002237 video_unregister_device(dev->vdev);
2238 else
2239 video_device_release(dev->vdev);
2240 dev->vdev = NULL;
2241 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242}
2243
2244/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002245 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002246 * stops streaming and deallocates all resources allocated by the v4l2
2247 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002248 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002249static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002250{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002251 struct em28xx_fh *fh = filp->private_data;
2252 struct em28xx *dev = fh->dev;
2253 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002255 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002256
Hans Verkuil876cb142012-06-23 08:12:47 -03002257 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002258 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002259 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002260 res_free(fh, EM28XX_RESOURCE_VIDEO);
2261 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002263 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2264 videobuf_stop(&fh->vb_vbiq);
2265 res_free(fh, EM28XX_RESOURCE_VBI);
2266 }
2267
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002268 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002269 /* the device is already disconnect,
2270 free the remaining resources */
2271 if (dev->state & DEV_DISCONNECTED) {
2272 em28xx_release_resources(dev);
Chris Rankin16e3d2f2011-09-04 15:38:14 -03002273 kfree(dev->alt_max_pkt_size);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002274 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002275 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002276 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002277 return 0;
2278 }
2279
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002280 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002281 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002282
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002283 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002284 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002285 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002286
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002287 /* set alternate 0 */
2288 dev->alt = 0;
2289 em28xx_videodbg("setting alternate 0\n");
2290 errCode = usb_set_interface(dev->udev, 0, 0);
2291 if (errCode < 0) {
2292 em28xx_errdev("cannot change alternate number to "
2293 "0 (error=%i)\n", errCode);
2294 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002295 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002296
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002297 videobuf_mmap_free(&fh->vb_vidq);
2298 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002299 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002300 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002301 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002302 return 0;
2303}
2304
2305/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002306 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002307 * will allocate buffers when called for the first time
2308 */
2309static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002310em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002311 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002312{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002313 struct em28xx_fh *fh = filp->private_data;
2314 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002315 int rc;
2316
2317 rc = check_dev(dev);
2318 if (rc < 0)
2319 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002320
Hans Verkuil876cb142012-06-23 08:12:47 -03002321 if (mutex_lock_interruptible(&dev->lock))
2322 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002323 /* FIXME: read() is not prepared to allow changing the video
2324 resolution while streaming. Seems a bug at em28xx_set_fmt
2325 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002326
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002327 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002328 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002329 rc = -EBUSY;
2330 else
2331 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002332 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002333 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002334 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002335 rc = -EBUSY;
2336 else
2337 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002338 filp->f_flags & O_NONBLOCK);
2339 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002340 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002341
Hans Verkuil876cb142012-06-23 08:12:47 -03002342 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343}
2344
2345/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002346 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002347 * will allocate buffers when called for the first time
2348 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002349static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002350{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002351 struct em28xx_fh *fh = filp->private_data;
2352 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002353 int rc;
2354
2355 rc = check_dev(dev);
2356 if (rc < 0)
2357 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002358
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002359 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2360 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2361 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002362 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002363 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2364 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2365 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002366 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002367 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002368 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002369 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002370}
2371
Hans Verkuil876cb142012-06-23 08:12:47 -03002372static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2373{
2374 struct em28xx_fh *fh = filp->private_data;
2375 struct em28xx *dev = fh->dev;
2376 unsigned int res;
2377
2378 mutex_lock(&dev->lock);
2379 res = em28xx_poll(filp, wait);
2380 mutex_unlock(&dev->lock);
2381 return res;
2382}
2383
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002384/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002385 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002387static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002388{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002389 struct em28xx_fh *fh = filp->private_data;
2390 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002391 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002392
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002393 rc = check_dev(dev);
2394 if (rc < 0)
2395 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002396
Hans Verkuil876cb142012-06-23 08:12:47 -03002397 if (mutex_lock_interruptible(&dev->lock))
2398 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002399 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2400 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2401 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2402 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002403 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002404
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002405 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2406 (unsigned long)vma->vm_start,
2407 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2408 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002409
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002410 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411}
2412
Hans Verkuilbec43662008-12-30 06:58:20 -03002413static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002414 .owner = THIS_MODULE,
2415 .open = em28xx_v4l2_open,
2416 .release = em28xx_v4l2_close,
2417 .read = em28xx_v4l2_read,
2418 .poll = em28xx_v4l2_poll,
2419 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002420 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002421};
2422
Hans Verkuila3998102008-07-21 02:57:38 -03002423static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002424 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002425 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2426 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2427 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2428 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002429 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2430 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002431 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002432 .vidioc_g_audio = vidioc_g_audio,
2433 .vidioc_s_audio = vidioc_s_audio,
2434 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002435 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2436 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2437 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002438
2439 .vidioc_reqbufs = vidioc_reqbufs,
2440 .vidioc_querybuf = vidioc_querybuf,
2441 .vidioc_qbuf = vidioc_qbuf,
2442 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002443 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002444 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002445 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002446 .vidioc_g_parm = vidioc_g_parm,
2447 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002448 .vidioc_enum_input = vidioc_enum_input,
2449 .vidioc_g_input = vidioc_g_input,
2450 .vidioc_s_input = vidioc_s_input,
2451 .vidioc_queryctrl = vidioc_queryctrl,
2452 .vidioc_g_ctrl = vidioc_g_ctrl,
2453 .vidioc_s_ctrl = vidioc_s_ctrl,
2454 .vidioc_streamon = vidioc_streamon,
2455 .vidioc_streamoff = vidioc_streamoff,
2456 .vidioc_g_tuner = vidioc_g_tuner,
2457 .vidioc_s_tuner = vidioc_s_tuner,
2458 .vidioc_g_frequency = vidioc_g_frequency,
2459 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002460#ifdef CONFIG_VIDEO_ADV_DEBUG
2461 .vidioc_g_register = vidioc_g_register,
2462 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002463 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002464#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002465};
2466
2467static const struct video_device em28xx_video_template = {
2468 .fops = &em28xx_v4l_fops,
2469 .release = video_device_release,
2470 .ioctl_ops = &video_ioctl_ops,
2471
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002472 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002473 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002474};
2475
Hans Verkuilbec43662008-12-30 06:58:20 -03002476static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002477 .owner = THIS_MODULE,
2478 .open = em28xx_v4l2_open,
2479 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002480 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002481};
2482
2483static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002484 .vidioc_querycap = radio_querycap,
2485 .vidioc_g_tuner = radio_g_tuner,
2486 .vidioc_enum_input = radio_enum_input,
2487 .vidioc_g_audio = radio_g_audio,
2488 .vidioc_s_tuner = radio_s_tuner,
2489 .vidioc_s_audio = radio_s_audio,
2490 .vidioc_s_input = radio_s_input,
2491 .vidioc_queryctrl = radio_queryctrl,
2492 .vidioc_g_ctrl = vidioc_g_ctrl,
2493 .vidioc_s_ctrl = vidioc_s_ctrl,
2494 .vidioc_g_frequency = vidioc_g_frequency,
2495 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002496#ifdef CONFIG_VIDEO_ADV_DEBUG
2497 .vidioc_g_register = vidioc_g_register,
2498 .vidioc_s_register = vidioc_s_register,
2499#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002500};
2501
Hans Verkuila3998102008-07-21 02:57:38 -03002502static struct video_device em28xx_radio_template = {
2503 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002504 .fops = &radio_fops,
2505 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002506};
2507
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002508/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002509
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002510
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002511
Adrian Bunk532fe652008-01-28 22:10:48 -03002512static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002513 const struct video_device *template,
2514 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002515{
2516 struct video_device *vfd;
2517
2518 vfd = video_device_alloc();
2519 if (NULL == vfd)
2520 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002521
2522 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002523 vfd->v4l2_dev = &dev->v4l2_dev;
2524 vfd->release = video_device_release;
2525 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002526 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002527
2528 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2529 dev->name, type_name);
2530
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002531 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002532 return vfd;
2533}
2534
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002535int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002536{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002537 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002538 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002539 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002540
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002541 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2542 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002543
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002544 /* set default norm */
2545 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002546 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002547 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002548
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002549 /* Analog specific initialization */
2550 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002551
2552 maxw = norm_maxw(dev);
2553 /* MaxPacketSize for em2800 is too small to capture at full resolution
2554 * use half of maxw as the scaler can only scale to 50% */
2555 if (dev->board.is_em2800)
2556 maxw /= 2;
2557
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002558 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002559 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002560
Ezequiel García96371fc2012-03-23 18:09:34 -03002561 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002562
2563 /* Audio defaults */
2564 dev->mute = 1;
2565 dev->volume = 0x1f;
2566
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002567/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002568 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2569 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2570 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002571
2572 em28xx_set_outfmt(dev);
2573 em28xx_colorlevels_set_default(dev);
2574 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002575
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002576 /* allocate and fill video video_device struct */
2577 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2578 if (!dev->vdev) {
2579 em28xx_errdev("cannot allocate video_device.\n");
2580 return -ENODEV;
2581 }
2582
2583 /* register v4l2 video video_device */
2584 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2585 video_nr[dev->devno]);
2586 if (ret) {
2587 em28xx_errdev("unable to register video device (error=%i).\n",
2588 ret);
2589 return ret;
2590 }
2591
2592 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002593 if (em28xx_vbi_supported(dev) == 1) {
2594 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2595 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002596
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002597 /* register v4l2 vbi video_device */
2598 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2599 vbi_nr[dev->devno]);
2600 if (ret < 0) {
2601 em28xx_errdev("unable to register vbi device\n");
2602 return ret;
2603 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002604 }
2605
2606 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002607 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2608 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002609 if (!dev->radio_dev) {
2610 em28xx_errdev("cannot allocate video_device.\n");
2611 return -ENODEV;
2612 }
2613 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2614 radio_nr[dev->devno]);
2615 if (ret < 0) {
2616 em28xx_errdev("can't register radio device\n");
2617 return ret;
2618 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002619 em28xx_info("Registered radio device as %s\n",
2620 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002621 }
2622
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002623 em28xx_info("V4L2 video device registered as %s\n",
2624 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002625
2626 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002627 em28xx_info("V4L2 VBI device registered as %s\n",
2628 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002629
2630 return 0;
2631}