blob: df294f3858ea8cac4b93da2b612c61b657572b1b [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
54#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
86MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080088
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030089static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030090module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080092
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030093/* supported video standards */
94static struct em28xx_fmt format[] = {
95 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030096 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030099 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300102 .fourcc = V4L2_PIX_FMT_RGB565,
103 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300104 .reg = EM28XX_OUTFMT_RGB_16_656,
105 }, {
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .depth = 8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
110 }, {
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
113 .depth = 8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
115 }, {
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
120 }, {
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
123 .depth = 12,
124 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300125 },
126};
127
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300130static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800131 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200132 .id = V4L2_CID_AUDIO_VOLUME,
133 .type = V4L2_CTRL_TYPE_INTEGER,
134 .name = "Volume",
135 .minimum = 0x0,
136 .maximum = 0x1f,
137 .step = 0x1,
138 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300139 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300140 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200141 .id = V4L2_CID_AUDIO_MUTE,
142 .type = V4L2_CTRL_TYPE_BOOLEAN,
143 .name = "Mute",
144 .minimum = 0,
145 .maximum = 1,
146 .step = 1,
147 .default_value = 1,
148 .flags = 0,
149 }
150};
151
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300152/* ------------------------------------------------------------------
153 DMA and thread functions
154 ------------------------------------------------------------------*/
155
156/*
157 * Announces that a buffer were filled and request the next
158 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300159static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf)
162{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 /* Advice that buffer was filled */
164 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
165 buf->vb.state = VIDEOBUF_DONE;
166 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300167 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer74209dc2012-11-08 14:11:37 -0300169 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
175static inline void vbi_buffer_filled(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf)
178{
179 /* Advice that buffer was filled */
180 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
181
182 buf->vb.state = VIDEOBUF_DONE;
183 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300184 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300185
Frank Schaefer74209dc2012-11-08 14:11:37 -0300186 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 list_del(&buf->vb.queue);
189 wake_up(&buf->vb.done);
190}
191
192/*
193 * Identify the buffer header type and properly handles
194 */
195static void em28xx_copy_video(struct em28xx *dev,
196 struct em28xx_dmaqueue *dma_q,
197 struct em28xx_buffer *buf,
198 unsigned char *p,
199 unsigned char *outp, unsigned long len)
200{
201 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 if (dma_q->pos + len > buf->vb.size)
206 len = buf->vb.size - dma_q->pos;
207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300208 startread = p;
209 remain = len;
210
Frank Schaeferc02ec712012-11-08 14:11:33 -0300211 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300213 else /* interlaced mode, even nr. of lines */
214 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 linesdone = dma_q->pos / bytesperline;
217 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300218
219 if (dev->progressive)
220 offset = linesdone * bytesperline + currlinedone;
221 else
222 offset = linesdone * bytesperline * 2 + currlinedone;
223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 lencopy = lencopy > remain ? remain : lencopy;
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300229 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 ((char *)startwrite + lencopy) -
231 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 remain = (char *)outp + buf->vb.size - (char *)startwrite;
233 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300234 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300235 if (lencopy <= 0)
236 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300238
239 remain -= lencopy;
240
241 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300242 if (dev->progressive)
243 startwrite += lencopy;
244 else
245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300372 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
379
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300380 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300383
Frank Schaefer74209dc2012-11-08 14:11:37 -0300384 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385
386 return;
387}
388
389/*
390 * video-buf generic routine to get the next available VBI buffer
391 */
392static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
394{
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396 char *outp;
397
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300400 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300401 *buf = NULL;
402 return;
403 }
404
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
410
Frank Schaefer74209dc2012-11-08 14:11:37 -0300411 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414}
415
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300416/* Processes and copies the URB data content to a frame buffer queue */
417static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300418{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300419 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300420 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300421 int xfer_bulk, num_packets, i, rc = 1;
422 unsigned int actual_length, len = 0;
423 unsigned char *p, *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424
425 if (!dev)
426 return 0;
427
428 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
429 return 0;
430
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300431 if (urb->status < 0)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300433
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300434 xfer_bulk = usb_pipebulk(urb->pipe);
435
Frank Schaefer74209dc2012-11-08 14:11:37 -0300436 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300437 if (buf != NULL)
438 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300439
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300440 if (xfer_bulk) /* bulk */
441 num_packets = 1;
442 else /* isoc */
443 num_packets = urb->number_of_packets;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300445 for (i = 0; i < num_packets; i++) {
446 if (xfer_bulk) { /* bulk */
447 actual_length = urb->actual_length;
448
449 p = urb->transfer_buffer;
450 } else { /* isoc */
451 if (urb->iso_frame_desc[i].status < 0) {
452 print_err_status(dev, i,
453 urb->iso_frame_desc[i].status);
454 if (urb->iso_frame_desc[i].status != -EPROTO)
455 continue;
456 }
457
458 actual_length = urb->iso_frame_desc[i].actual_length;
459 if (actual_length > dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 continue;
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300462 }
463
464 p = urb->transfer_buffer +
465 urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 }
467
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300468 if (actual_length <= 0) {
469 /* NOTE: happens very often with isoc transfers */
470 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300471 continue;
472 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473
474 /* FIXME: incomplete buffer checks where removed to make
475 logic simpler. Impacts of those changes should be evaluated
476 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300477 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
478 em28xx_isocdbg("VBI HEADER!!!\n");
479 /* FIXME: Should add vbi copy */
480 continue;
481 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300482 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300483 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300484 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300486 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300487 if (buf != NULL)
488 buffer_filled(dev, dma_q, buf);
489 get_next_buf(dma_q, &buf);
490 if (buf == NULL)
491 outp = NULL;
492 else
493 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300494 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300495
496 if (buf != NULL) {
497 if (p[2] & 1)
498 buf->top_field = 0;
499 else
500 buf->top_field = 1;
501 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300502
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300505 if (buf != NULL) {
506 if (p[0] != 0x88 && p[0] != 0x22) {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300507 /* NOTE: no intermediate data packet header
508 * 88 88 88 88 when using bulk transfers */
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300509 em28xx_isocdbg("frame is not complete\n");
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300510 len = actual_length;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300511 } else {
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300512 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300513 p += 4;
514 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300515 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300516 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300517 }
518 return rc;
519}
520
Frank Schaefer4601cc32012-11-08 14:11:46 -0300521/* Version of the urb data handler that takes into account a mixture of
522 video and VBI data */
523static inline int em28xx_urb_data_copy_vbi(struct em28xx *dev, struct urb *urb)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300524{
525 struct em28xx_buffer *buf, *vbi_buf;
526 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300527 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300528 int xfer_bulk, vbi_size, num_packets, i, rc = 1;
529 unsigned int actual_length, len = 0;
530 unsigned char *p, *outp = NULL, *vbioutp = NULL;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300531
532 if (!dev)
533 return 0;
534
535 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
536 return 0;
537
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300538 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300539 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300540
Frank Schaefer4601cc32012-11-08 14:11:46 -0300541 xfer_bulk = usb_pipebulk(urb->pipe);
542
Frank Schaefer74209dc2012-11-08 14:11:37 -0300543 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300544 if (buf != NULL)
545 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300546
Frank Schaefer74209dc2012-11-08 14:11:37 -0300547 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300548 if (vbi_buf != NULL)
549 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
550
Frank Schaefer4601cc32012-11-08 14:11:46 -0300551 if (xfer_bulk) /* bulk */
552 num_packets = 1;
553 else /* isoc */
554 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300555
Frank Schaefer4601cc32012-11-08 14:11:46 -0300556 for (i = 0; i < num_packets; i++) {
557 if (xfer_bulk) { /* bulk */
558 actual_length = urb->actual_length;
559
560 p = urb->transfer_buffer;
561 } else { /* isoc */
562 if (urb->iso_frame_desc[i].status < 0) {
563 print_err_status(dev, i,
564 urb->iso_frame_desc[i].status);
565 if (urb->iso_frame_desc[i].status != -EPROTO)
566 continue;
567 }
568
569 actual_length = urb->iso_frame_desc[i].actual_length;
570 if (actual_length > dev->max_pkt_size) {
571 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300572 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300573 }
574
575 p = urb->transfer_buffer +
576 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300577 }
578
Frank Schaefer4601cc32012-11-08 14:11:46 -0300579 if (actual_length <= 0) {
580 /* NOTE: happens very often with isoc transfers */
581 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300582 continue;
583 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300584
585 /* capture type 0 = vbi start
586 capture type 1 = video start
587 capture type 2 = video in progress */
588 if (p[0] == 0x33 && p[1] == 0x95) {
589 dev->capture_type = 0;
590 dev->vbi_read = 0;
591 em28xx_isocdbg("VBI START HEADER!!!\n");
592 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300593 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300594 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300595 } else if (p[0] == 0x88 && p[1] == 0x88 &&
596 p[2] == 0x88 && p[3] == 0x88) {
597 /* continuation */
598 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300599 len = actual_length - 4;
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300600 } else if (p[0] == 0x22 && p[1] == 0x5a) {
601 /* start video */
602 p += 4;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300603 len = actual_length - 4;
604 } else {
605 /* NOTE: With bulk transfers, intermediate data packets
606 * have no continuation header */
607 len = actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300608 }
609
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300610 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300611
612 if (dev->capture_type == 0) {
613 if (dev->vbi_read >= vbi_size) {
614 /* We've already read all the VBI data, so
615 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300616 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300617 } else if ((dev->vbi_read + len) < vbi_size) {
618 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300619 if (dev->vbi_read == 0 &&
620 (!(dev->cur_field & 1))) {
621 /* Brand new frame */
622 if (vbi_buf != NULL)
623 vbi_buffer_filled(dev,
624 vbi_dma_q,
625 vbi_buf);
626 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
627 if (vbi_buf == NULL)
628 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300629 else
630 vbioutp = videobuf_to_vmalloc(
631 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300632 }
633
634 if (dev->vbi_read == 0) {
635 vbi_dma_q->pos = 0;
636 if (vbi_buf != NULL) {
637 if (dev->cur_field & 1)
638 vbi_buf->top_field = 0;
639 else
640 vbi_buf->top_field = 1;
641 }
642 }
643
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300644 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300645 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
646 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300647 } else {
648 /* Some of this frame is VBI data and some is
649 video data */
650 int vbi_data_len = vbi_size - dev->vbi_read;
651 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300652 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
653 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300654 dev->capture_type = 1;
655 p += vbi_data_len;
656 len -= vbi_data_len;
657 }
658 }
659
660 if (dev->capture_type == 1) {
661 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300662 if (dev->progressive || !(dev->cur_field & 1)) {
663 if (buf != NULL)
664 buffer_filled(dev, dma_q, buf);
665 get_next_buf(dma_q, &buf);
666 if (buf == NULL)
667 outp = NULL;
668 else
669 outp = videobuf_to_vmalloc(&buf->vb);
670 }
671 if (buf != NULL) {
672 if (dev->cur_field & 1)
673 buf->top_field = 0;
674 else
675 buf->top_field = 1;
676 }
677
678 dma_q->pos = 0;
679 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300680
681 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300682 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300683 p[2] == 0x88 && p[3] == 0x88) {
684 p += 4;
685 len -= 4;
686 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300687 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300688 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
689 p[2], len, (p[2] & 1) ?
690 "odd" : "even");
691 p += 4;
692 len -= 4;
693 }
694
695 if (len > 0)
696 em28xx_copy_video(dev, dma_q, buf, p, outp,
697 len);
698 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300699 }
700 return rc;
701}
702
703
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300704/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300705 Videobuf operations
706 ------------------------------------------------------------------*/
707
708static int
709buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
710{
711 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300712 struct em28xx *dev = fh->dev;
713 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300714
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300715 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
716 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300717
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300718 if (0 == *count)
719 *count = EM28XX_DEF_BUF;
720
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300721 if (*count < EM28XX_MIN_BUF)
722 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300723
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300724 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300725 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300726 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300727 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300728
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300729 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300730
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300731 return 0;
732}
733
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300734/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300735static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
736{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300737 struct em28xx_fh *fh = vq->priv_data;
738 struct em28xx *dev = fh->dev;
739 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300740 if (in_interrupt())
741 BUG();
742
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300743 /* We used to wait for the buffer to finish here, but this didn't work
744 because, as we were keeping the state as VIDEOBUF_QUEUED,
745 videobuf_queue_cancel marked it as finished for us.
746 (Also, it could wedge forever if the hardware was misconfigured.)
747
748 This should be safe; by the time we get here, the buffer isn't
749 queued anymore. If we ever start marking the buffers as
750 VIDEOBUF_ACTIVE, it won't be, though.
751 */
752 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300753 if (dev->usb_ctl.vid_buf == buf)
754 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300755 spin_unlock_irqrestore(&dev->slock, flags);
756
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300757 videobuf_vmalloc_free(&buf->vb);
758 buf->vb.state = VIDEOBUF_NEEDS_INIT;
759}
760
761static int
762buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
763 enum v4l2_field field)
764{
765 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300766 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 struct em28xx *dev = fh->dev;
768 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300769
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300770 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
771 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300772
773 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
774 return -EINVAL;
775
Brandon Philips05612972008-04-13 14:57:01 -0300776 buf->vb.width = dev->width;
777 buf->vb.height = dev->height;
778 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300779
780 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300781 rc = videobuf_iolock(vq, &buf->vb, NULL);
782 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300784 }
785
Frank Schaefer74209dc2012-11-08 14:11:37 -0300786 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300787 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300788
789 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300790 if (em28xx_vbi_supported(dev) == 1)
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300791 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
792 EM28XX_NUM_BUFS,
793 dev->max_pkt_size,
794 EM28XX_NUM_ISOC_PACKETS,
Frank Schaefer4601cc32012-11-08 14:11:46 -0300795 em28xx_urb_data_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300796 else
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300797 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
798 EM28XX_NUM_BUFS,
799 dev->max_pkt_size,
800 EM28XX_NUM_ISOC_PACKETS,
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300801 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300802 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300803 goto fail;
804 }
805
806 buf->vb.state = VIDEOBUF_PREPARED;
807 return 0;
808
809fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300810 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300811 return rc;
812}
813
814static void
815buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
816{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300817 struct em28xx_buffer *buf = container_of(vb,
818 struct em28xx_buffer,
819 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300820 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300821 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300822 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300823
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300824 buf->vb.state = VIDEOBUF_QUEUED;
825 list_add_tail(&buf->vb.queue, &vidq->active);
826
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300827}
828
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300829static void buffer_release(struct videobuf_queue *vq,
830 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300831{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300832 struct em28xx_buffer *buf = container_of(vb,
833 struct em28xx_buffer,
834 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300835 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300836 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300837
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300838 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300839
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300840 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300841}
842
843static struct videobuf_queue_ops em28xx_video_qops = {
844 .buf_setup = buffer_setup,
845 .buf_prepare = buffer_prepare,
846 .buf_queue = buffer_queue,
847 .buf_release = buffer_release,
848};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800849
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300850/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800851
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800852static void video_mux(struct em28xx *dev, int index)
853{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800854 dev->ctl_input = index;
855 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300856 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800857
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300858 if (!dev->ctl_aoutput)
859 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
860
Hans Verkuil5325b422009-04-02 11:26:22 -0300861 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
862 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800863
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300864 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300865 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300866 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
867 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300868 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300869 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300870 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
871 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800872 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300873
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300874 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300875 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
876 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300877 }
878
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300879 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800880}
881
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300882/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300883static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300884{
885 struct em28xx *dev = fh->dev;
886
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300887 if (fh->resources & bit)
888 /* have it already allocated */
889 return 1;
890
891 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300892 if (dev->resources & bit) {
893 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300894 return 0;
895 }
896 /* it's free, grab it */
897 fh->resources |= bit;
898 dev->resources |= bit;
899 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300900 return 1;
901}
902
903static int res_check(struct em28xx_fh *fh, unsigned int bit)
904{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300905 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300906}
907
908static int res_locked(struct em28xx *dev, unsigned int bit)
909{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300910 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300911}
912
913static void res_free(struct em28xx_fh *fh, unsigned int bits)
914{
915 struct em28xx *dev = fh->dev;
916
917 BUG_ON((fh->resources & bits) != bits);
918
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300919 fh->resources &= ~bits;
920 dev->resources &= ~bits;
921 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300922}
923
924static int get_ressource(struct em28xx_fh *fh)
925{
926 switch (fh->type) {
927 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
928 return EM28XX_RESOURCE_VIDEO;
929 case V4L2_BUF_TYPE_VBI_CAPTURE:
930 return EM28XX_RESOURCE_VBI;
931 default:
932 BUG();
933 return 0;
934 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300935}
936
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800937/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300938 * ac97_queryctrl()
939 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300940 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300941static int ac97_queryctrl(struct v4l2_queryctrl *qc)
942{
943 int i;
944
945 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
946 if (qc->id && qc->id == ac97_qctrl[i].id) {
947 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
948 return 0;
949 }
950 }
951
952 /* Control is not ac97 related */
953 return 1;
954}
955
956/*
957 * ac97_get_ctrl()
958 * return the current values for ac97 mute and volume
959 */
960static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961{
962 switch (ctrl->id) {
963 case V4L2_CID_AUDIO_MUTE:
964 ctrl->value = dev->mute;
965 return 0;
966 case V4L2_CID_AUDIO_VOLUME:
967 ctrl->value = dev->volume;
968 return 0;
969 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300970 /* Control is not ac97 related */
971 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300972 }
973}
974
975/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300976 * ac97_set_ctrl()
977 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300978 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300979static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300981 int i;
982
983 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
984 if (ctrl->id == ac97_qctrl[i].id)
985 goto handle;
986
987 /* Announce that hasn't handle it */
988 return 1;
989
990handle:
991 if (ctrl->value < ac97_qctrl[i].minimum ||
992 ctrl->value > ac97_qctrl[i].maximum)
993 return -ERANGE;
994
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300995 switch (ctrl->id) {
996 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300997 dev->mute = ctrl->value;
998 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300999 case V4L2_CID_AUDIO_VOLUME:
1000 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001001 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001002 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001003
1004 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005}
1006
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007static int check_dev(struct em28xx *dev)
1008{
1009 if (dev->state & DEV_DISCONNECTED) {
1010 em28xx_errdev("v4l2 ioctl: device not present\n");
1011 return -ENODEV;
1012 }
1013
1014 if (dev->state & DEV_MISCONFIGURED) {
1015 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
1016 "close and open it again\n");
1017 return -EIO;
1018 }
1019 return 0;
1020}
1021
1022static void get_scale(struct em28xx *dev,
1023 unsigned int width, unsigned int height,
1024 unsigned int *hscale, unsigned int *vscale)
1025{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001026 unsigned int maxw = norm_maxw(dev);
1027 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028
1029 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1030 if (*hscale >= 0x4000)
1031 *hscale = 0x3fff;
1032
1033 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1034 if (*vscale >= 0x4000)
1035 *vscale = 0x3fff;
1036}
1037
1038/* ------------------------------------------------------------------
1039 IOCTL vidioc handling
1040 ------------------------------------------------------------------*/
1041
Hans Verkuil78b526a2008-05-28 12:16:41 -03001042static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043 struct v4l2_format *f)
1044{
1045 struct em28xx_fh *fh = priv;
1046 struct em28xx *dev = fh->dev;
1047
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001048 f->fmt.pix.width = dev->width;
1049 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001050 f->fmt.pix.pixelformat = dev->format->fourcc;
1051 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001052 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001053 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1054
1055 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001056 if (dev->progressive)
1057 f->fmt.pix.field = V4L2_FIELD_NONE;
1058 else
1059 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001060 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001061 return 0;
1062}
1063
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001064static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1065{
1066 unsigned int i;
1067
1068 for (i = 0; i < ARRAY_SIZE(format); i++)
1069 if (format[i].fourcc == fourcc)
1070 return &format[i];
1071
1072 return NULL;
1073}
1074
Hans Verkuil78b526a2008-05-28 12:16:41 -03001075static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001076 struct v4l2_format *f)
1077{
1078 struct em28xx_fh *fh = priv;
1079 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001080 unsigned int width = f->fmt.pix.width;
1081 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082 unsigned int maxw = norm_maxw(dev);
1083 unsigned int maxh = norm_maxh(dev);
1084 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001085 struct em28xx_fmt *fmt;
1086
1087 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1088 if (!fmt) {
1089 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1090 f->fmt.pix.pixelformat);
1091 return -EINVAL;
1092 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001094 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001096 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1097 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001098 /* MaxPacketSize for em2800 is too small to capture at full resolution
1099 * use half of maxw as the scaler can only scale to 50% */
1100 if (width == maxw && height == maxh)
1101 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001102 } else {
1103 /* width must even because of the YUYV format
1104 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001105 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1106 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001107 }
1108
1109 get_scale(dev, width, height, &hscale, &vscale);
1110
1111 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1112 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1113
1114 f->fmt.pix.width = width;
1115 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001116 f->fmt.pix.pixelformat = fmt->fourcc;
1117 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1118 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001119 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001120 if (dev->progressive)
1121 f->fmt.pix.field = V4L2_FIELD_NONE;
1122 else
1123 f->fmt.pix.field = dev->interlaced ?
1124 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001125
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001126 return 0;
1127}
1128
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001129static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1130 unsigned width, unsigned height)
1131{
1132 struct em28xx_fmt *fmt;
1133
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001134 fmt = format_by_fourcc(fourcc);
1135 if (!fmt)
1136 return -EINVAL;
1137
1138 dev->format = fmt;
1139 dev->width = width;
1140 dev->height = height;
1141
1142 /* set new image size */
1143 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1144
1145 em28xx_set_alternate(dev);
1146 em28xx_resolution_set(dev);
1147
1148 return 0;
1149}
1150
Hans Verkuil78b526a2008-05-28 12:16:41 -03001151static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001152 struct v4l2_format *f)
1153{
1154 struct em28xx_fh *fh = priv;
1155 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001156 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157
1158 rc = check_dev(dev);
1159 if (rc < 0)
1160 return rc;
1161
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001162 vidioc_try_fmt_vid_cap(file, priv, f);
1163
Brandon Philips05612972008-04-13 14:57:01 -03001164 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1165 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001166 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001167 }
1168
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001169 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001170 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001171}
1172
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001173static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001177 int rc;
1178
1179 rc = check_dev(dev);
1180 if (rc < 0)
1181 return rc;
1182
1183 *norm = dev->norm;
1184
1185 return 0;
1186}
1187
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001188static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1189{
1190 struct em28xx_fh *fh = priv;
1191 struct em28xx *dev = fh->dev;
1192 int rc;
1193
1194 rc = check_dev(dev);
1195 if (rc < 0)
1196 return rc;
1197
1198 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1199
1200 return 0;
1201}
1202
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001203static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204{
1205 struct em28xx_fh *fh = priv;
1206 struct em28xx *dev = fh->dev;
1207 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001208 int rc;
1209
1210 rc = check_dev(dev);
1211 if (rc < 0)
1212 return rc;
1213
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001214 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001215
1216 /* Adjusts width/height, if needed */
1217 f.fmt.pix.width = dev->width;
1218 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001219 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001220
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001221 /* set new image size */
1222 dev->width = f.fmt.pix.width;
1223 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001224 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1225
1226 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001227 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001228
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001229 return 0;
1230}
1231
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001232static int vidioc_g_parm(struct file *file, void *priv,
1233 struct v4l2_streamparm *p)
1234{
1235 struct em28xx_fh *fh = priv;
1236 struct em28xx *dev = fh->dev;
1237 int rc = 0;
1238
1239 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1240 return -EINVAL;
1241
1242 if (dev->board.is_webcam)
1243 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1244 video, g_parm, p);
1245 else
1246 v4l2_video_std_frame_period(dev->norm,
1247 &p->parm.capture.timeperframe);
1248
1249 return rc;
1250}
1251
1252static int vidioc_s_parm(struct file *file, void *priv,
1253 struct v4l2_streamparm *p)
1254{
1255 struct em28xx_fh *fh = priv;
1256 struct em28xx *dev = fh->dev;
1257
1258 if (!dev->board.is_webcam)
1259 return -EINVAL;
1260
1261 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1262 return -EINVAL;
1263
1264 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1265}
1266
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001267static const char *iname[] = {
1268 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1269 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1270 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1271 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1272 [EM28XX_VMUX_SVIDEO] = "S-Video",
1273 [EM28XX_VMUX_TELEVISION] = "Television",
1274 [EM28XX_VMUX_CABLE] = "Cable TV",
1275 [EM28XX_VMUX_DVB] = "DVB",
1276 [EM28XX_VMUX_DEBUG] = "for debug only",
1277};
1278
1279static int vidioc_enum_input(struct file *file, void *priv,
1280 struct v4l2_input *i)
1281{
1282 struct em28xx_fh *fh = priv;
1283 struct em28xx *dev = fh->dev;
1284 unsigned int n;
1285
1286 n = i->index;
1287 if (n >= MAX_EM28XX_INPUT)
1288 return -EINVAL;
1289 if (0 == INPUT(n)->type)
1290 return -EINVAL;
1291
1292 i->index = n;
1293 i->type = V4L2_INPUT_TYPE_CAMERA;
1294
1295 strcpy(i->name, iname[INPUT(n)->type]);
1296
1297 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1298 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1299 i->type = V4L2_INPUT_TYPE_TUNER;
1300
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001301 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001302
1303 return 0;
1304}
1305
1306static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1307{
1308 struct em28xx_fh *fh = priv;
1309 struct em28xx *dev = fh->dev;
1310
1311 *i = dev->ctl_input;
1312
1313 return 0;
1314}
1315
1316static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1317{
1318 struct em28xx_fh *fh = priv;
1319 struct em28xx *dev = fh->dev;
1320 int rc;
1321
1322 rc = check_dev(dev);
1323 if (rc < 0)
1324 return rc;
1325
1326 if (i >= MAX_EM28XX_INPUT)
1327 return -EINVAL;
1328 if (0 == INPUT(i)->type)
1329 return -EINVAL;
1330
Ezequiel García96371fc2012-03-23 18:09:34 -03001331 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332 return 0;
1333}
1334
1335static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1336{
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001339
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001340 if (!dev->audio_mode.has_audio)
1341 return -EINVAL;
1342
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001343 switch (a->index) {
1344 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001345 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001346 break;
1347 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001349 break;
1350 case EM28XX_AMUX_VIDEO2:
1351 strcpy(a->name, "Television alt");
1352 break;
1353 case EM28XX_AMUX_PHONE:
1354 strcpy(a->name, "Phone");
1355 break;
1356 case EM28XX_AMUX_MIC:
1357 strcpy(a->name, "Mic");
1358 break;
1359 case EM28XX_AMUX_CD:
1360 strcpy(a->name, "CD");
1361 break;
1362 case EM28XX_AMUX_AUX:
1363 strcpy(a->name, "Aux");
1364 break;
1365 case EM28XX_AMUX_PCM_OUT:
1366 strcpy(a->name, "PCM");
1367 break;
1368 default:
1369 return -EINVAL;
1370 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001371
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001372 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001373 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374
1375 return 0;
1376}
1377
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001378static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001379{
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
1382
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001383
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001384 if (!dev->audio_mode.has_audio)
1385 return -EINVAL;
1386
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001387 if (a->index >= MAX_EM28XX_INPUT)
1388 return -EINVAL;
1389 if (0 == INPUT(a->index)->type)
1390 return -EINVAL;
1391
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001392 dev->ctl_ainput = INPUT(a->index)->amux;
1393 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001394
1395 if (!dev->ctl_aoutput)
1396 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001397
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001398 return 0;
1399}
1400
1401static int vidioc_queryctrl(struct file *file, void *priv,
1402 struct v4l2_queryctrl *qc)
1403{
1404 struct em28xx_fh *fh = priv;
1405 struct em28xx *dev = fh->dev;
1406 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407 int rc;
1408
1409 rc = check_dev(dev);
1410 if (rc < 0)
1411 return rc;
1412
1413 memset(qc, 0, sizeof(*qc));
1414
1415 qc->id = id;
1416
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001417 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001418 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1419 rc = ac97_queryctrl(qc);
1420 if (!rc)
1421 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001422 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001423
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001424 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001425 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001426
1427 if (qc->type)
1428 return 0;
1429 else
1430 return -EINVAL;
1431}
1432
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001433/*
1434 * FIXME: This is an indirect way to check if a control exists at a
1435 * subdev. Instead of that hack, maybe the better would be to change all
1436 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1437 */
1438static int check_subdev_ctrl(struct em28xx *dev, int id)
1439{
1440 struct v4l2_queryctrl qc;
1441
1442 memset(&qc, 0, sizeof(qc));
1443 qc.id = id;
1444
1445 /* enumerate V4L2 device controls */
1446 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1447
1448 if (qc.type)
1449 return 0;
1450 else
1451 return -EINVAL;
1452}
1453
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001454static int vidioc_g_ctrl(struct file *file, void *priv,
1455 struct v4l2_control *ctrl)
1456{
1457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
1459 int rc;
1460
1461 rc = check_dev(dev);
1462 if (rc < 0)
1463 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001464 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001465
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001466 /* Set an AC97 control */
1467 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1468 rc = ac97_get_ctrl(dev, ctrl);
1469 else
1470 rc = 1;
1471
1472 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1473 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001474 if (check_subdev_ctrl(dev, ctrl->id))
1475 return -EINVAL;
1476
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001477 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001478 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001479 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001480
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001481 return rc;
1482}
1483
1484static int vidioc_s_ctrl(struct file *file, void *priv,
1485 struct v4l2_control *ctrl)
1486{
1487 struct em28xx_fh *fh = priv;
1488 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001489 int rc;
1490
1491 rc = check_dev(dev);
1492 if (rc < 0)
1493 return rc;
1494
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001495 /* Set an AC97 control */
1496 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1497 rc = ac97_set_ctrl(dev, ctrl);
1498 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001499 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001500
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001501 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001502 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001503 rc = check_subdev_ctrl(dev, ctrl->id);
1504 if (!rc)
1505 v4l2_device_call_all(&dev->v4l2_dev, 0,
1506 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001507 /*
1508 * In the case of non-AC97 volume controls, we still need
1509 * to do some setups at em28xx, in order to mute/unmute
1510 * and to adjust audio volume. However, the value ranges
1511 * should be checked by the corresponding V4L subdriver.
1512 */
1513 switch (ctrl->id) {
1514 case V4L2_CID_AUDIO_MUTE:
1515 dev->mute = ctrl->value;
1516 rc = em28xx_audio_analog_set(dev);
1517 break;
1518 case V4L2_CID_AUDIO_VOLUME:
1519 dev->volume = ctrl->value;
1520 rc = em28xx_audio_analog_set(dev);
1521 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001522 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001523 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001524}
1525
1526static int vidioc_g_tuner(struct file *file, void *priv,
1527 struct v4l2_tuner *t)
1528{
1529 struct em28xx_fh *fh = priv;
1530 struct em28xx *dev = fh->dev;
1531 int rc;
1532
1533 rc = check_dev(dev);
1534 if (rc < 0)
1535 return rc;
1536
1537 if (0 != t->index)
1538 return -EINVAL;
1539
1540 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001541 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001542
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001543 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001544 return 0;
1545}
1546
1547static int vidioc_s_tuner(struct file *file, void *priv,
1548 struct v4l2_tuner *t)
1549{
1550 struct em28xx_fh *fh = priv;
1551 struct em28xx *dev = fh->dev;
1552 int rc;
1553
1554 rc = check_dev(dev);
1555 if (rc < 0)
1556 return rc;
1557
1558 if (0 != t->index)
1559 return -EINVAL;
1560
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001561 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001562 return 0;
1563}
1564
1565static int vidioc_g_frequency(struct file *file, void *priv,
1566 struct v4l2_frequency *f)
1567{
1568 struct em28xx_fh *fh = priv;
1569 struct em28xx *dev = fh->dev;
1570
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001571 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001572 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001573 return 0;
1574}
1575
1576static int vidioc_s_frequency(struct file *file, void *priv,
1577 struct v4l2_frequency *f)
1578{
1579 struct em28xx_fh *fh = priv;
1580 struct em28xx *dev = fh->dev;
1581 int rc;
1582
1583 rc = check_dev(dev);
1584 if (rc < 0)
1585 return rc;
1586
1587 if (0 != f->tuner)
1588 return -EINVAL;
1589
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001590 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1591 return -EINVAL;
1592 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001593 return -EINVAL;
1594
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001596 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001597
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001598 return 0;
1599}
1600
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001601#ifdef CONFIG_VIDEO_ADV_DEBUG
1602static int em28xx_reg_len(int reg)
1603{
1604 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001605 case EM28XX_R40_AC97LSB:
1606 case EM28XX_R30_HSCALELOW:
1607 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001608 return 2;
1609 default:
1610 return 1;
1611 }
1612}
1613
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001614static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001615 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001616{
1617 struct em28xx_fh *fh = priv;
1618 struct em28xx *dev = fh->dev;
1619
1620 chip->ident = V4L2_IDENT_NONE;
1621 chip->revision = 0;
1622
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001623 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001624
1625 return 0;
1626}
1627
1628
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001629static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001630 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001631{
1632 struct em28xx_fh *fh = priv;
1633 struct em28xx *dev = fh->dev;
1634 int ret;
1635
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001636 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001637 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001638 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001639 if (ret < 0)
1640 return ret;
1641
1642 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001643 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001644 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001645 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001646 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001647 return 0;
1648 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001649 /* TODO: is this correct? */
1650 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1651 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001652 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001653 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001654 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001655 }
1656
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001657 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001658 reg->size = em28xx_reg_len(reg->reg);
1659 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001660 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001661
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001662 if (ret < 0)
1663 return ret;
1664
1665 reg->val = ret;
1666 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001667 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001668 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1669 reg->reg, (char *)&val, 2);
1670 if (ret < 0)
1671 return ret;
1672
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001673 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001674 }
1675
1676 return 0;
1677}
1678
1679static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001680 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001681{
1682 struct em28xx_fh *fh = priv;
1683 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001684 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001685
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001686 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001687 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001688 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001689 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001690 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001691 return 0;
1692 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001693 /* TODO: is this correct? */
1694 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1695 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001696 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001697 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001698 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001699 }
1700
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001701 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001702 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001703
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001704 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001705 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001706}
1707#endif
1708
1709
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001710static int vidioc_cropcap(struct file *file, void *priv,
1711 struct v4l2_cropcap *cc)
1712{
1713 struct em28xx_fh *fh = priv;
1714 struct em28xx *dev = fh->dev;
1715
1716 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1717 return -EINVAL;
1718
1719 cc->bounds.left = 0;
1720 cc->bounds.top = 0;
1721 cc->bounds.width = dev->width;
1722 cc->bounds.height = dev->height;
1723 cc->defrect = cc->bounds;
1724 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1725 cc->pixelaspect.denominator = 59;
1726
1727 return 0;
1728}
1729
1730static int vidioc_streamon(struct file *file, void *priv,
1731 enum v4l2_buf_type type)
1732{
1733 struct em28xx_fh *fh = priv;
1734 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001735 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001736
1737 rc = check_dev(dev);
1738 if (rc < 0)
1739 return rc;
1740
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001741 if (unlikely(type != fh->type))
1742 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001743
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001744 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1745 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001746
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001747 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001748 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001749
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001750 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1751 rc = videobuf_streamon(&fh->vb_vidq);
1752 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1753 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001754
1755 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001756}
1757
1758static int vidioc_streamoff(struct file *file, void *priv,
1759 enum v4l2_buf_type type)
1760{
1761 struct em28xx_fh *fh = priv;
1762 struct em28xx *dev = fh->dev;
1763 int rc;
1764
1765 rc = check_dev(dev);
1766 if (rc < 0)
1767 return rc;
1768
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001769 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1770 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001771 return -EINVAL;
1772 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773 return -EINVAL;
1774
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001775 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1776 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001777
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001778 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001779 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1780 videobuf_streamoff(&fh->vb_vidq);
1781 res_free(fh, EM28XX_RESOURCE_VIDEO);
1782 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001783 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001784 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1785 videobuf_streamoff(&fh->vb_vbiq);
1786 res_free(fh, EM28XX_RESOURCE_VBI);
1787 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001788 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001789
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001790 return 0;
1791}
1792
1793static int vidioc_querycap(struct file *file, void *priv,
1794 struct v4l2_capability *cap)
1795{
1796 struct em28xx_fh *fh = priv;
1797 struct em28xx *dev = fh->dev;
1798
1799 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1800 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001801 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001802
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001803 cap->capabilities =
1804 V4L2_CAP_SLICED_VBI_CAPTURE |
1805 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001806 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1807
Devin Heitmueller04146142009-09-11 00:08:44 -03001808 if (dev->vbi_dev)
1809 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1810
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001811 if (dev->audio_mode.has_audio)
1812 cap->capabilities |= V4L2_CAP_AUDIO;
1813
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001814 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001815 cap->capabilities |= V4L2_CAP_TUNER;
1816
1817 return 0;
1818}
1819
Hans Verkuil78b526a2008-05-28 12:16:41 -03001820static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001821 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001822{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001823 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824 return -EINVAL;
1825
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001826 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1827 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001828
1829 return 0;
1830}
1831
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001832static int vidioc_enum_framesizes(struct file *file, void *priv,
1833 struct v4l2_frmsizeenum *fsize)
1834{
1835 struct em28xx_fh *fh = priv;
1836 struct em28xx *dev = fh->dev;
1837 struct em28xx_fmt *fmt;
1838 unsigned int maxw = norm_maxw(dev);
1839 unsigned int maxh = norm_maxh(dev);
1840
1841 fmt = format_by_fourcc(fsize->pixel_format);
1842 if (!fmt) {
1843 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1844 fsize->pixel_format);
1845 return -EINVAL;
1846 }
1847
1848 if (dev->board.is_em2800) {
1849 if (fsize->index > 1)
1850 return -EINVAL;
1851 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1852 fsize->discrete.width = maxw / (1 + fsize->index);
1853 fsize->discrete.height = maxh / (1 + fsize->index);
1854 return 0;
1855 }
1856
1857 if (fsize->index != 0)
1858 return -EINVAL;
1859
1860 /* Report a continuous range */
1861 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1862 fsize->stepwise.min_width = 48;
1863 fsize->stepwise.min_height = 32;
1864 fsize->stepwise.max_width = maxw;
1865 fsize->stepwise.max_height = maxh;
1866 fsize->stepwise.step_width = 1;
1867 fsize->stepwise.step_height = 1;
1868 return 0;
1869}
1870
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001871/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001872static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001873 struct v4l2_format *f)
1874{
1875 struct em28xx_fh *fh = priv;
1876 struct em28xx *dev = fh->dev;
1877 int rc;
1878
1879 rc = check_dev(dev);
1880 if (rc < 0)
1881 return rc;
1882
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001883 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001884 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001885
1886 if (f->fmt.sliced.service_set == 0)
1887 rc = -EINVAL;
1888
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001889 return rc;
1890}
1891
Hans Verkuil78b526a2008-05-28 12:16:41 -03001892static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001893 struct v4l2_format *f)
1894{
1895 struct em28xx_fh *fh = priv;
1896 struct em28xx *dev = fh->dev;
1897 int rc;
1898
1899 rc = check_dev(dev);
1900 if (rc < 0)
1901 return rc;
1902
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001903 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001904
1905 if (f->fmt.sliced.service_set == 0)
1906 return -EINVAL;
1907
1908 return 0;
1909}
1910
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001911/* RAW VBI ioctls */
1912
1913static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1914 struct v4l2_format *format)
1915{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001916 struct em28xx_fh *fh = priv;
1917 struct em28xx *dev = fh->dev;
1918
1919 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001920 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1921 format->fmt.vbi.offset = 0;
1922 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001923 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1924 format->fmt.vbi.count[0] = dev->vbi_height;
1925 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001926
1927 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001928 if (dev->norm & V4L2_STD_525_60) {
1929 /* NTSC */
1930 format->fmt.vbi.start[0] = 10;
1931 format->fmt.vbi.start[1] = 273;
1932 } else if (dev->norm & V4L2_STD_625_50) {
1933 /* PAL */
1934 format->fmt.vbi.start[0] = 6;
1935 format->fmt.vbi.start[1] = 318;
1936 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001937
1938 return 0;
1939}
1940
1941static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1942 struct v4l2_format *format)
1943{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001944 struct em28xx_fh *fh = priv;
1945 struct em28xx *dev = fh->dev;
1946
1947 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001948 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1949 format->fmt.vbi.offset = 0;
1950 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001951 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1952 format->fmt.vbi.count[0] = dev->vbi_height;
1953 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001954
1955 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001956 if (dev->norm & V4L2_STD_525_60) {
1957 /* NTSC */
1958 format->fmt.vbi.start[0] = 10;
1959 format->fmt.vbi.start[1] = 273;
1960 } else if (dev->norm & V4L2_STD_625_50) {
1961 /* PAL */
1962 format->fmt.vbi.start[0] = 6;
1963 format->fmt.vbi.start[1] = 318;
1964 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001965
1966 return 0;
1967}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001968
1969static int vidioc_reqbufs(struct file *file, void *priv,
1970 struct v4l2_requestbuffers *rb)
1971{
1972 struct em28xx_fh *fh = priv;
1973 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001974 int rc;
1975
1976 rc = check_dev(dev);
1977 if (rc < 0)
1978 return rc;
1979
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001980 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1981 return videobuf_reqbufs(&fh->vb_vidq, rb);
1982 else
1983 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001984}
1985
1986static int vidioc_querybuf(struct file *file, void *priv,
1987 struct v4l2_buffer *b)
1988{
1989 struct em28xx_fh *fh = priv;
1990 struct em28xx *dev = fh->dev;
1991 int rc;
1992
1993 rc = check_dev(dev);
1994 if (rc < 0)
1995 return rc;
1996
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001997 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1998 return videobuf_querybuf(&fh->vb_vidq, b);
1999 else {
2000 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
2001 the videobuf framework, but we probably shouldn't be
2002 returning a buffer larger than that which was asked for.
2003 At a minimum, it causes a crash in zvbi since it does
2004 a memcpy based on the source buffer length */
2005 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03002006 b->length = dev->vbi_width * dev->vbi_height * 2;
2007
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002008 return result;
2009 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002010}
2011
2012static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2013{
2014 struct em28xx_fh *fh = priv;
2015 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002016 int rc;
2017
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_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002024 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002025 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002026}
2027
2028static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2029{
2030 struct em28xx_fh *fh = priv;
2031 struct em28xx *dev = fh->dev;
2032 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002033
2034 rc = check_dev(dev);
2035 if (rc < 0)
2036 return rc;
2037
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002038 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2039 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2040 O_NONBLOCK);
2041 else
2042 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2043 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002044}
2045
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002046/* ----------------------------------------------------------- */
2047/* RADIO ESPECIFIC IOCTLS */
2048/* ----------------------------------------------------------- */
2049
2050static int radio_querycap(struct file *file, void *priv,
2051 struct v4l2_capability *cap)
2052{
2053 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2054
2055 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2056 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002057 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002058
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002059 cap->capabilities = V4L2_CAP_TUNER;
2060 return 0;
2061}
2062
2063static int radio_g_tuner(struct file *file, void *priv,
2064 struct v4l2_tuner *t)
2065{
2066 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2067
2068 if (unlikely(t->index > 0))
2069 return -EINVAL;
2070
2071 strcpy(t->name, "Radio");
2072 t->type = V4L2_TUNER_RADIO;
2073
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002074 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002075
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002076 return 0;
2077}
2078
2079static int radio_enum_input(struct file *file, void *priv,
2080 struct v4l2_input *i)
2081{
2082 if (i->index != 0)
2083 return -EINVAL;
2084 strcpy(i->name, "Radio");
2085 i->type = V4L2_INPUT_TYPE_TUNER;
2086
2087 return 0;
2088}
2089
2090static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2091{
2092 if (unlikely(a->index))
2093 return -EINVAL;
2094
2095 strcpy(a->name, "Radio");
2096 return 0;
2097}
2098
2099static int radio_s_tuner(struct file *file, void *priv,
2100 struct v4l2_tuner *t)
2101{
2102 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2103
2104 if (0 != t->index)
2105 return -EINVAL;
2106
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002107 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002108
2109 return 0;
2110}
2111
2112static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002113 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002114{
2115 return 0;
2116}
2117
2118static int radio_s_input(struct file *file, void *fh, unsigned int i)
2119{
2120 return 0;
2121}
2122
2123static int radio_queryctrl(struct file *file, void *priv,
2124 struct v4l2_queryctrl *qc)
2125{
2126 int i;
2127
2128 if (qc->id < V4L2_CID_BASE ||
2129 qc->id >= V4L2_CID_LASTP1)
2130 return -EINVAL;
2131
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002132 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2133 if (qc->id && qc->id == ac97_qctrl[i].id) {
2134 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002135 return 0;
2136 }
2137 }
2138
2139 return -EINVAL;
2140}
2141
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002142/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002143 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002144 * inits the device and starts isoc transfer
2145 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002146static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002147{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002148 int errCode = 0, radio = 0;
2149 struct video_device *vdev = video_devdata(filp);
2150 struct em28xx *dev = video_drvdata(filp);
2151 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002152 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002153 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002154
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002155 switch (vdev->vfl_type) {
2156 case VFL_TYPE_GRABBER:
2157 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2158 break;
2159 case VFL_TYPE_VBI:
2160 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2161 break;
2162 case VFL_TYPE_RADIO:
2163 radio = 1;
2164 break;
2165 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002166
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002167 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2168 video_device_node_name(vdev), v4l2_type_names[fh_type],
2169 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002170
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171
Hans Verkuil876cb142012-06-23 08:12:47 -03002172 if (mutex_lock_interruptible(&dev->lock))
2173 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002174 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002175 if (!fh) {
2176 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002177 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002178 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002179 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002180 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002181 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002182 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002183 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002185 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002186 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002187 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002188 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002190 /* Needed, since GPIO might have disabled power of
2191 some i2c device
2192 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002193 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002194
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002195 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002196 if (fh->radio) {
2197 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002198 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002199 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002200
2201 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002203 if (dev->progressive)
2204 field = V4L2_FIELD_NONE;
2205 else
2206 field = V4L2_FIELD_INTERLACED;
2207
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002208 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2209 NULL, &dev->slock,
2210 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002211 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002212
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002213 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2214 NULL, &dev->slock,
2215 V4L2_BUF_TYPE_VBI_CAPTURE,
2216 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002217 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002218 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002219
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002220 return errCode;
2221}
2222
2223/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002224 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002225 * unregisters the v4l2,i2c and usb devices
2226 * called when the device gets disconected or at module unload
2227*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002228void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002229{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002230
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002231 /*FIXME: I2C IR should be disconnected */
2232
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002233 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002234 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002235 video_unregister_device(dev->radio_dev);
2236 else
2237 video_device_release(dev->radio_dev);
2238 dev->radio_dev = NULL;
2239 }
2240 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002241 em28xx_info("V4L2 device %s deregistered\n",
2242 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002243 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002244 video_unregister_device(dev->vbi_dev);
2245 else
2246 video_device_release(dev->vbi_dev);
2247 dev->vbi_dev = NULL;
2248 }
2249 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002250 em28xx_info("V4L2 device %s deregistered\n",
2251 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002252 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002253 video_unregister_device(dev->vdev);
2254 else
2255 video_device_release(dev->vdev);
2256 dev->vdev = NULL;
2257 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002258}
2259
2260/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002261 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002262 * stops streaming and deallocates all resources allocated by the v4l2
2263 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002265static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002266{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002267 struct em28xx_fh *fh = filp->private_data;
2268 struct em28xx *dev = fh->dev;
2269 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002271 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002272
Hans Verkuil876cb142012-06-23 08:12:47 -03002273 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002274 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002275 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002276 res_free(fh, EM28XX_RESOURCE_VIDEO);
2277 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002278
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002279 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2280 videobuf_stop(&fh->vb_vbiq);
2281 res_free(fh, EM28XX_RESOURCE_VBI);
2282 }
2283
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002284 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002285 /* the device is already disconnect,
2286 free the remaining resources */
2287 if (dev->state & DEV_DISCONNECTED) {
2288 em28xx_release_resources(dev);
Chris Rankin16e3d2f2011-09-04 15:38:14 -03002289 kfree(dev->alt_max_pkt_size);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002290 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002291 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03002292 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002293 return 0;
2294 }
2295
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002296 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002297 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002298
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002299 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002300 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002301 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002302
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002303 /* set alternate 0 */
2304 dev->alt = 0;
2305 em28xx_videodbg("setting alternate 0\n");
2306 errCode = usb_set_interface(dev->udev, 0, 0);
2307 if (errCode < 0) {
2308 em28xx_errdev("cannot change alternate number to "
2309 "0 (error=%i)\n", errCode);
2310 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002312
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002313 videobuf_mmap_free(&fh->vb_vidq);
2314 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002315 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002316 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002317 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002318 return 0;
2319}
2320
2321/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002322 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002323 * will allocate buffers when called for the first time
2324 */
2325static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002326em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002327 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002328{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002329 struct em28xx_fh *fh = filp->private_data;
2330 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002331 int rc;
2332
2333 rc = check_dev(dev);
2334 if (rc < 0)
2335 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002336
Hans Verkuil876cb142012-06-23 08:12:47 -03002337 if (mutex_lock_interruptible(&dev->lock))
2338 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002339 /* FIXME: read() is not prepared to allow changing the video
2340 resolution while streaming. Seems a bug at em28xx_set_fmt
2341 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002342
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002343 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002344 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002345 rc = -EBUSY;
2346 else
2347 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002348 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002349 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002350 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002351 rc = -EBUSY;
2352 else
2353 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002354 filp->f_flags & O_NONBLOCK);
2355 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002356 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002357
Hans Verkuil876cb142012-06-23 08:12:47 -03002358 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002359}
2360
2361/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002362 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002363 * will allocate buffers when called for the first time
2364 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002365static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002366{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002367 struct em28xx_fh *fh = filp->private_data;
2368 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002369 int rc;
2370
2371 rc = check_dev(dev);
2372 if (rc < 0)
2373 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002374
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002375 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2376 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2377 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002378 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002379 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2380 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2381 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002382 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002383 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002384 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002385 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386}
2387
Hans Verkuil876cb142012-06-23 08:12:47 -03002388static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2389{
2390 struct em28xx_fh *fh = filp->private_data;
2391 struct em28xx *dev = fh->dev;
2392 unsigned int res;
2393
2394 mutex_lock(&dev->lock);
2395 res = em28xx_poll(filp, wait);
2396 mutex_unlock(&dev->lock);
2397 return res;
2398}
2399
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002400/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002401 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002403static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002404{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002405 struct em28xx_fh *fh = filp->private_data;
2406 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002407 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002408
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002409 rc = check_dev(dev);
2410 if (rc < 0)
2411 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002412
Hans Verkuil876cb142012-06-23 08:12:47 -03002413 if (mutex_lock_interruptible(&dev->lock))
2414 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002415 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2416 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2417 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2418 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002419 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002421 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2422 (unsigned long)vma->vm_start,
2423 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2424 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002425
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002426 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002427}
2428
Hans Verkuilbec43662008-12-30 06:58:20 -03002429static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002430 .owner = THIS_MODULE,
2431 .open = em28xx_v4l2_open,
2432 .release = em28xx_v4l2_close,
2433 .read = em28xx_v4l2_read,
2434 .poll = em28xx_v4l2_poll,
2435 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002436 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002437};
2438
Hans Verkuila3998102008-07-21 02:57:38 -03002439static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002440 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002441 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2442 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2443 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2444 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002445 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2446 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002447 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002448 .vidioc_g_audio = vidioc_g_audio,
2449 .vidioc_s_audio = vidioc_s_audio,
2450 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002451 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2452 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2453 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002454
2455 .vidioc_reqbufs = vidioc_reqbufs,
2456 .vidioc_querybuf = vidioc_querybuf,
2457 .vidioc_qbuf = vidioc_qbuf,
2458 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002459 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002460 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002461 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002462 .vidioc_g_parm = vidioc_g_parm,
2463 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002464 .vidioc_enum_input = vidioc_enum_input,
2465 .vidioc_g_input = vidioc_g_input,
2466 .vidioc_s_input = vidioc_s_input,
2467 .vidioc_queryctrl = vidioc_queryctrl,
2468 .vidioc_g_ctrl = vidioc_g_ctrl,
2469 .vidioc_s_ctrl = vidioc_s_ctrl,
2470 .vidioc_streamon = vidioc_streamon,
2471 .vidioc_streamoff = vidioc_streamoff,
2472 .vidioc_g_tuner = vidioc_g_tuner,
2473 .vidioc_s_tuner = vidioc_s_tuner,
2474 .vidioc_g_frequency = vidioc_g_frequency,
2475 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002476#ifdef CONFIG_VIDEO_ADV_DEBUG
2477 .vidioc_g_register = vidioc_g_register,
2478 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002479 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002480#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002481};
2482
2483static const struct video_device em28xx_video_template = {
2484 .fops = &em28xx_v4l_fops,
2485 .release = video_device_release,
2486 .ioctl_ops = &video_ioctl_ops,
2487
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002488 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002489 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002490};
2491
Hans Verkuilbec43662008-12-30 06:58:20 -03002492static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002493 .owner = THIS_MODULE,
2494 .open = em28xx_v4l2_open,
2495 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002496 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002497};
2498
2499static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002500 .vidioc_querycap = radio_querycap,
2501 .vidioc_g_tuner = radio_g_tuner,
2502 .vidioc_enum_input = radio_enum_input,
2503 .vidioc_g_audio = radio_g_audio,
2504 .vidioc_s_tuner = radio_s_tuner,
2505 .vidioc_s_audio = radio_s_audio,
2506 .vidioc_s_input = radio_s_input,
2507 .vidioc_queryctrl = radio_queryctrl,
2508 .vidioc_g_ctrl = vidioc_g_ctrl,
2509 .vidioc_s_ctrl = vidioc_s_ctrl,
2510 .vidioc_g_frequency = vidioc_g_frequency,
2511 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002512#ifdef CONFIG_VIDEO_ADV_DEBUG
2513 .vidioc_g_register = vidioc_g_register,
2514 .vidioc_s_register = vidioc_s_register,
2515#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002516};
2517
Hans Verkuila3998102008-07-21 02:57:38 -03002518static struct video_device em28xx_radio_template = {
2519 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002520 .fops = &radio_fops,
2521 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002522};
2523
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002524/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002525
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002526
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002527
Adrian Bunk532fe652008-01-28 22:10:48 -03002528static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002529 const struct video_device *template,
2530 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002531{
2532 struct video_device *vfd;
2533
2534 vfd = video_device_alloc();
2535 if (NULL == vfd)
2536 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002537
2538 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002539 vfd->v4l2_dev = &dev->v4l2_dev;
2540 vfd->release = video_device_release;
2541 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002542 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002543
2544 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2545 dev->name, type_name);
2546
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002547 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002548 return vfd;
2549}
2550
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002551int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002552{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002553 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002554 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002555 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002556
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002557 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2558 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002559
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002560 /* set default norm */
2561 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002562 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002563 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002564
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002565 /* Analog specific initialization */
2566 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002567
2568 maxw = norm_maxw(dev);
2569 /* MaxPacketSize for em2800 is too small to capture at full resolution
2570 * use half of maxw as the scaler can only scale to 50% */
2571 if (dev->board.is_em2800)
2572 maxw /= 2;
2573
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002574 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002575 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002576
Ezequiel García96371fc2012-03-23 18:09:34 -03002577 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002578
2579 /* Audio defaults */
2580 dev->mute = 1;
2581 dev->volume = 0x1f;
2582
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002583/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002584 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2585 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2586 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002587
2588 em28xx_set_outfmt(dev);
2589 em28xx_colorlevels_set_default(dev);
2590 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002591
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002592 /* allocate and fill video video_device struct */
2593 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2594 if (!dev->vdev) {
2595 em28xx_errdev("cannot allocate video_device.\n");
2596 return -ENODEV;
2597 }
2598
2599 /* register v4l2 video video_device */
2600 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2601 video_nr[dev->devno]);
2602 if (ret) {
2603 em28xx_errdev("unable to register video device (error=%i).\n",
2604 ret);
2605 return ret;
2606 }
2607
2608 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002609 if (em28xx_vbi_supported(dev) == 1) {
2610 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2611 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002612
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002613 /* register v4l2 vbi video_device */
2614 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2615 vbi_nr[dev->devno]);
2616 if (ret < 0) {
2617 em28xx_errdev("unable to register vbi device\n");
2618 return ret;
2619 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002620 }
2621
2622 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002623 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2624 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002625 if (!dev->radio_dev) {
2626 em28xx_errdev("cannot allocate video_device.\n");
2627 return -ENODEV;
2628 }
2629 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2630 radio_nr[dev->devno]);
2631 if (ret < 0) {
2632 em28xx_errdev("can't register radio device\n");
2633 return ret;
2634 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002635 em28xx_info("Registered radio device as %s\n",
2636 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002637 }
2638
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002639 em28xx_info("V4L2 video device registered as %s\n",
2640 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002641
2642 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002643 em28xx_info("V4L2 VBI device registered as %s\n",
2644 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002645
2646 return 0;
2647}