blob: 6bb0b1d74e36beefcaf6d9d2068b9e5ed1d6ff96 [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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030052
53#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030075MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080076
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084MODULE_PARM_DESC(video_nr, "video device numbers");
85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080087
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030088static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030089module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080091
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030092/* supported video standards */
93static struct em28xx_fmt format[] = {
94 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030095 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030096 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030098 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300100 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300101 .fourcc = V4L2_PIX_FMT_RGB565,
102 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300103 .reg = EM28XX_OUTFMT_RGB_16_656,
104 }, {
105 .name = "8 bpp Bayer BGBG..GRGR",
106 .fourcc = V4L2_PIX_FMT_SBGGR8,
107 .depth = 8,
108 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
109 }, {
110 .name = "8 bpp Bayer GRGR..BGBG",
111 .fourcc = V4L2_PIX_FMT_SGRBG8,
112 .depth = 8,
113 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
114 }, {
115 .name = "8 bpp Bayer GBGB..RGRG",
116 .fourcc = V4L2_PIX_FMT_SGBRG8,
117 .depth = 8,
118 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
119 }, {
120 .name = "12 bpp YUV411",
121 .fourcc = V4L2_PIX_FMT_YUV411P,
122 .depth = 12,
123 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300124 },
125};
126
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800127/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200128/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300129static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800130 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300138 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300139 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300151/* ------------------------------------------------------------------
152 DMA and thread functions
153 ------------------------------------------------------------------*/
154
155/*
156 * Announces that a buffer were filled and request the next
157 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 struct em28xx_dmaqueue *dma_q,
160 struct em28xx_buffer *buf)
161{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 /* Advice that buffer was filled */
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300166 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300167
Frank Schaefer74209dc2012-11-08 14:11:37 -0300168 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300169
170 list_del(&buf->vb.queue);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300183 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300184
Frank Schaefer74209dc2012-11-08 14:11:37 -0300185 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300186
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189}
190
191/*
192 * Identify the buffer header type and properly handles
193 */
194static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf,
197 unsigned char *p,
198 unsigned char *outp, unsigned long len)
199{
200 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300201 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
204 if (dma_q->pos + len > buf->vb.size)
205 len = buf->vb.size - dma_q->pos;
206
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 startread = p;
208 remain = len;
209
Frank Schaeferc02ec712012-11-08 14:11:33 -0300210 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300212 else /* interlaced mode, even nr. of lines */
213 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300214
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300215 linesdone = dma_q->pos / bytesperline;
216 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300217
218 if (dev->progressive)
219 offset = linesdone * bytesperline + currlinedone;
220 else
221 offset = linesdone * bytesperline * 2 + currlinedone;
222
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300224 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 lencopy = lencopy > remain ? remain : lencopy;
226
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300228 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229 ((char *)startwrite + lencopy) -
230 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300231 remain = (char *)outp + buf->vb.size - (char *)startwrite;
232 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300233 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300234 if (lencopy <= 0)
235 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300236 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300237
238 remain -= lencopy;
239
240 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300241 if (dev->progressive)
242 startwrite += lencopy;
243 else
244 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300245 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300246 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247 lencopy = remain;
248 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300249 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300250
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300251 if ((char *)startwrite + lencopy > (char *)outp +
252 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300253 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
254 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300255 ((char *)startwrite + lencopy) -
256 ((char *)outp + buf->vb.size));
257 lencopy = remain = (char *)outp + buf->vb.size -
258 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300259 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300260 if (lencopy <= 0)
261 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300262
263 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300264
265 remain -= lencopy;
266 }
267
268 dma_q->pos += len;
269}
270
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300271static void em28xx_copy_vbi(struct em28xx *dev,
272 struct em28xx_dmaqueue *dma_q,
273 struct em28xx_buffer *buf,
274 unsigned char *p,
275 unsigned char *outp, unsigned long len)
276{
277 void *startwrite, *startread;
278 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300279 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300280
281 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300282 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300283 return;
284 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300285 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300286
287 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300288 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300289 return;
290 }
291 if (buf == NULL) {
292 return;
293 }
294 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300295 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300296 return;
297 }
298 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300299 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300300 return;
301 }
302
303 if (dma_q->pos + len > buf->vb.size)
304 len = buf->vb.size - dma_q->pos;
305
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300306 startread = p;
307
308 startwrite = outp + dma_q->pos;
309 offset = dma_q->pos;
310
311 /* Make sure the bottom field populates the second half of the frame */
312 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300313 startwrite += bytesperline * dev->vbi_height;
314 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300315 }
316
317 memcpy(startwrite, startread, len);
318 dma_q->pos += len;
319}
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 int packet, int status)
323{
324 char *errmsg = "Unknown";
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327 case -ENOENT:
328 errmsg = "unlinked synchronuously";
329 break;
330 case -ECONNRESET:
331 errmsg = "unlinked asynchronuously";
332 break;
333 case -ENOSR:
334 errmsg = "Buffer error (overrun)";
335 break;
336 case -EPIPE:
337 errmsg = "Stalled (device not responding)";
338 break;
339 case -EOVERFLOW:
340 errmsg = "Babble (bad cable?)";
341 break;
342 case -EPROTO:
343 errmsg = "Bit-stuff error (bad cable?)";
344 break;
345 case -EILSEQ:
346 errmsg = "CRC/Timeout (could be anything)";
347 break;
348 case -ETIME:
349 errmsg = "Device does not respond";
350 break;
351 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300352 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300353 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
354 } else {
355 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 packet, status, errmsg);
357 }
358}
359
360/*
361 * video-buf generic routine to get the next available buffer
362 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300363static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364 struct em28xx_buffer **buf)
365{
366 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300367 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300369 if (list_empty(&dma_q->active)) {
370 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300371 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300372 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300373 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300374 }
375
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300376 /* Get the next buffer */
377 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
378
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300379 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 outp = videobuf_to_vmalloc(&(*buf)->vb);
381 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300382
Frank Schaefer74209dc2012-11-08 14:11:37 -0300383 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384
385 return;
386}
387
388/*
389 * video-buf generic routine to get the next available VBI buffer
390 */
391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 struct em28xx_buffer **buf)
393{
394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 char *outp;
396
397 if (list_empty(&dma_q->active)) {
398 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300399 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300400 *buf = NULL;
401 return;
402 }
403
404 /* Get the next buffer */
405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300407 outp = videobuf_to_vmalloc(&(*buf)->vb);
408 memset(outp, 0x00, (*buf)->vb.size);
409
Frank Schaefer74209dc2012-11-08 14:11:37 -0300410 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300411
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300412 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300413}
414
415/*
416 * Controls the isoc copy of each urb packet
417 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300418static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300419{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300420 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300421 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300422 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300423 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300424 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425
426 if (!dev)
427 return 0;
428
429 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
430 return 0;
431
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300432 if (urb->status < 0)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300433 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300434
Frank Schaefer74209dc2012-11-08 14:11:37 -0300435 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300436 if (buf != NULL)
437 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300438
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439 for (i = 0; i < urb->number_of_packets; i++) {
440 int status = urb->iso_frame_desc[i].status;
441
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300442 if (status < 0) {
443 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444 if (urb->iso_frame_desc[i].status != -EPROTO)
445 continue;
446 }
447
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300448 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300449
450 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300451 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452 continue;
453 }
454 if (urb->iso_frame_desc[i].actual_length >
455 dev->max_pkt_size) {
456 em28xx_isocdbg("packet bigger than packet size");
457 continue;
458 }
459
460 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461
462 /* FIXME: incomplete buffer checks where removed to make
463 logic simpler. Impacts of those changes should be evaluated
464 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300465 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
466 em28xx_isocdbg("VBI HEADER!!!\n");
467 /* FIXME: Should add vbi copy */
468 continue;
469 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300470 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300471 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300472 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300474 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300475 if (buf != NULL)
476 buffer_filled(dev, dma_q, buf);
477 get_next_buf(dma_q, &buf);
478 if (buf == NULL)
479 outp = NULL;
480 else
481 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300482 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300483
484 if (buf != NULL) {
485 if (p[2] & 1)
486 buf->top_field = 0;
487 else
488 buf->top_field = 1;
489 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300490
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300493 if (buf != NULL) {
494 if (p[0] != 0x88 && p[0] != 0x22) {
495 em28xx_isocdbg("frame is not complete\n");
496 len += 4;
497 } else {
498 p += 4;
499 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300500 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300501 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300502 }
503 return rc;
504}
505
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300506/* Version of isoc handler that takes into account a mixture of video and
507 VBI data */
508static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
509{
510 struct em28xx_buffer *buf, *vbi_buf;
511 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300512 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300513 unsigned char *outp = NULL;
514 unsigned char *vbioutp = NULL;
515 int i, len = 0, rc = 1;
516 unsigned char *p;
517 int vbi_size;
518
519 if (!dev)
520 return 0;
521
522 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
523 return 0;
524
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300525 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300526 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300527
Frank Schaefer74209dc2012-11-08 14:11:37 -0300528 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300529 if (buf != NULL)
530 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300531
Frank Schaefer74209dc2012-11-08 14:11:37 -0300532 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300533 if (vbi_buf != NULL)
534 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
535
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300536 for (i = 0; i < urb->number_of_packets; i++) {
537 int status = urb->iso_frame_desc[i].status;
538
539 if (status < 0) {
540 print_err_status(dev, i, status);
541 if (urb->iso_frame_desc[i].status != -EPROTO)
542 continue;
543 }
544
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300545 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300546 if (urb->iso_frame_desc[i].actual_length <= 0) {
547 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
548 continue;
549 }
550 if (urb->iso_frame_desc[i].actual_length >
551 dev->max_pkt_size) {
552 em28xx_isocdbg("packet bigger than packet size");
553 continue;
554 }
555
556 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
557
558 /* capture type 0 = vbi start
559 capture type 1 = video start
560 capture type 2 = video in progress */
561 if (p[0] == 0x33 && p[1] == 0x95) {
562 dev->capture_type = 0;
563 dev->vbi_read = 0;
564 em28xx_isocdbg("VBI START HEADER!!!\n");
565 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300566 p += 4;
567 len -= 4;
568 } else if (p[0] == 0x88 && p[1] == 0x88 &&
569 p[2] == 0x88 && p[3] == 0x88) {
570 /* continuation */
571 p += 4;
572 len -= 4;
573 } else if (p[0] == 0x22 && p[1] == 0x5a) {
574 /* start video */
575 p += 4;
576 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300577 }
578
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300579 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300580
581 if (dev->capture_type == 0) {
582 if (dev->vbi_read >= vbi_size) {
583 /* We've already read all the VBI data, so
584 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300585 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300586 } else if ((dev->vbi_read + len) < vbi_size) {
587 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300588 if (dev->vbi_read == 0 &&
589 (!(dev->cur_field & 1))) {
590 /* Brand new frame */
591 if (vbi_buf != NULL)
592 vbi_buffer_filled(dev,
593 vbi_dma_q,
594 vbi_buf);
595 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
596 if (vbi_buf == NULL)
597 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300598 else
599 vbioutp = videobuf_to_vmalloc(
600 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300601 }
602
603 if (dev->vbi_read == 0) {
604 vbi_dma_q->pos = 0;
605 if (vbi_buf != NULL) {
606 if (dev->cur_field & 1)
607 vbi_buf->top_field = 0;
608 else
609 vbi_buf->top_field = 1;
610 }
611 }
612
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300613 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300614 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
615 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300616 } else {
617 /* Some of this frame is VBI data and some is
618 video data */
619 int vbi_data_len = vbi_size - dev->vbi_read;
620 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300621 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
622 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300623 dev->capture_type = 1;
624 p += vbi_data_len;
625 len -= vbi_data_len;
626 }
627 }
628
629 if (dev->capture_type == 1) {
630 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300631 if (dev->progressive || !(dev->cur_field & 1)) {
632 if (buf != NULL)
633 buffer_filled(dev, dma_q, buf);
634 get_next_buf(dma_q, &buf);
635 if (buf == NULL)
636 outp = NULL;
637 else
638 outp = videobuf_to_vmalloc(&buf->vb);
639 }
640 if (buf != NULL) {
641 if (dev->cur_field & 1)
642 buf->top_field = 0;
643 else
644 buf->top_field = 1;
645 }
646
647 dma_q->pos = 0;
648 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300649
650 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300651 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300652 p[2] == 0x88 && p[3] == 0x88) {
653 p += 4;
654 len -= 4;
655 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300656 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300657 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
658 p[2], len, (p[2] & 1) ?
659 "odd" : "even");
660 p += 4;
661 len -= 4;
662 }
663
664 if (len > 0)
665 em28xx_copy_video(dev, dma_q, buf, p, outp,
666 len);
667 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300668 }
669 return rc;
670}
671
672
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300673/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300674 Videobuf operations
675 ------------------------------------------------------------------*/
676
677static int
678buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
679{
680 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300681 struct em28xx *dev = fh->dev;
682 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300683
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300684 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
685 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300686
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300687 if (0 == *count)
688 *count = EM28XX_DEF_BUF;
689
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300690 if (*count < EM28XX_MIN_BUF)
691 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300692
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300693 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300694 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300695 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300696 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300697
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300698 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300699
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300700 return 0;
701}
702
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300703/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300704static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
705{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300706 struct em28xx_fh *fh = vq->priv_data;
707 struct em28xx *dev = fh->dev;
708 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300709 if (in_interrupt())
710 BUG();
711
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300712 /* We used to wait for the buffer to finish here, but this didn't work
713 because, as we were keeping the state as VIDEOBUF_QUEUED,
714 videobuf_queue_cancel marked it as finished for us.
715 (Also, it could wedge forever if the hardware was misconfigured.)
716
717 This should be safe; by the time we get here, the buffer isn't
718 queued anymore. If we ever start marking the buffers as
719 VIDEOBUF_ACTIVE, it won't be, though.
720 */
721 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300722 if (dev->usb_ctl.vid_buf == buf)
723 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300724 spin_unlock_irqrestore(&dev->slock, flags);
725
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300726 videobuf_vmalloc_free(&buf->vb);
727 buf->vb.state = VIDEOBUF_NEEDS_INIT;
728}
729
730static int
731buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
732 enum v4l2_field field)
733{
734 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300735 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300736 struct em28xx *dev = fh->dev;
737 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300738
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300739 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
740 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741
742 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
743 return -EINVAL;
744
Brandon Philips05612972008-04-13 14:57:01 -0300745 buf->vb.width = dev->width;
746 buf->vb.height = dev->height;
747 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300748
749 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300750 rc = videobuf_iolock(vq, &buf->vb, NULL);
751 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300752 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300753 }
754
Frank Schaefer74209dc2012-11-08 14:11:37 -0300755 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300756 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300757
758 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300759 if (em28xx_vbi_supported(dev) == 1)
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300760 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
761 EM28XX_NUM_BUFS,
762 dev->max_pkt_size,
763 EM28XX_NUM_ISOC_PACKETS,
764 em28xx_isoc_copy_vbi);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300765 else
Frank Schaefer057ca0d2012-11-08 14:11:42 -0300766 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE, 0,
767 EM28XX_NUM_BUFS,
768 dev->max_pkt_size,
769 EM28XX_NUM_ISOC_PACKETS,
770 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300771 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300772 goto fail;
773 }
774
775 buf->vb.state = VIDEOBUF_PREPARED;
776 return 0;
777
778fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300779 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300780 return rc;
781}
782
783static void
784buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
785{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300786 struct em28xx_buffer *buf = container_of(vb,
787 struct em28xx_buffer,
788 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300789 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300790 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300791 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300792
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300793 buf->vb.state = VIDEOBUF_QUEUED;
794 list_add_tail(&buf->vb.queue, &vidq->active);
795
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300796}
797
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300798static void buffer_release(struct videobuf_queue *vq,
799 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300800{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300801 struct em28xx_buffer *buf = container_of(vb,
802 struct em28xx_buffer,
803 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300804 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300805 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300807 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300808
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300809 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810}
811
812static struct videobuf_queue_ops em28xx_video_qops = {
813 .buf_setup = buffer_setup,
814 .buf_prepare = buffer_prepare,
815 .buf_queue = buffer_queue,
816 .buf_release = buffer_release,
817};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800818
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300819/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800820
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800821static void video_mux(struct em28xx *dev, int index)
822{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800823 dev->ctl_input = index;
824 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300825 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800826
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300827 if (!dev->ctl_aoutput)
828 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
829
Hans Verkuil5325b422009-04-02 11:26:22 -0300830 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
831 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800832
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300833 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300834 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300835 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
836 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300837 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300838 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300839 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
840 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800841 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300842
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300843 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300844 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
845 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300846 }
847
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300848 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800849}
850
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300851/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300852static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300853{
854 struct em28xx *dev = fh->dev;
855
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300856 if (fh->resources & bit)
857 /* have it already allocated */
858 return 1;
859
860 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300861 if (dev->resources & bit) {
862 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300863 return 0;
864 }
865 /* it's free, grab it */
866 fh->resources |= bit;
867 dev->resources |= bit;
868 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869 return 1;
870}
871
872static int res_check(struct em28xx_fh *fh, unsigned int bit)
873{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300874 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300875}
876
877static int res_locked(struct em28xx *dev, unsigned int bit)
878{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300879 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300880}
881
882static void res_free(struct em28xx_fh *fh, unsigned int bits)
883{
884 struct em28xx *dev = fh->dev;
885
886 BUG_ON((fh->resources & bits) != bits);
887
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300888 fh->resources &= ~bits;
889 dev->resources &= ~bits;
890 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300891}
892
893static int get_ressource(struct em28xx_fh *fh)
894{
895 switch (fh->type) {
896 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
897 return EM28XX_RESOURCE_VIDEO;
898 case V4L2_BUF_TYPE_VBI_CAPTURE:
899 return EM28XX_RESOURCE_VBI;
900 default:
901 BUG();
902 return 0;
903 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300904}
905
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800906/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300907 * ac97_queryctrl()
908 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300909 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300910static int ac97_queryctrl(struct v4l2_queryctrl *qc)
911{
912 int i;
913
914 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
915 if (qc->id && qc->id == ac97_qctrl[i].id) {
916 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
917 return 0;
918 }
919 }
920
921 /* Control is not ac97 related */
922 return 1;
923}
924
925/*
926 * ac97_get_ctrl()
927 * return the current values for ac97 mute and volume
928 */
929static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300930{
931 switch (ctrl->id) {
932 case V4L2_CID_AUDIO_MUTE:
933 ctrl->value = dev->mute;
934 return 0;
935 case V4L2_CID_AUDIO_VOLUME:
936 ctrl->value = dev->volume;
937 return 0;
938 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300939 /* Control is not ac97 related */
940 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941 }
942}
943
944/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300945 * ac97_set_ctrl()
946 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300948static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300949{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300950 int i;
951
952 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
953 if (ctrl->id == ac97_qctrl[i].id)
954 goto handle;
955
956 /* Announce that hasn't handle it */
957 return 1;
958
959handle:
960 if (ctrl->value < ac97_qctrl[i].minimum ||
961 ctrl->value > ac97_qctrl[i].maximum)
962 return -ERANGE;
963
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964 switch (ctrl->id) {
965 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300966 dev->mute = ctrl->value;
967 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968 case V4L2_CID_AUDIO_VOLUME:
969 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300970 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300971 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300972
973 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974}
975
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300976static int check_dev(struct em28xx *dev)
977{
978 if (dev->state & DEV_DISCONNECTED) {
979 em28xx_errdev("v4l2 ioctl: device not present\n");
980 return -ENODEV;
981 }
982
983 if (dev->state & DEV_MISCONFIGURED) {
984 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
985 "close and open it again\n");
986 return -EIO;
987 }
988 return 0;
989}
990
991static void get_scale(struct em28xx *dev,
992 unsigned int width, unsigned int height,
993 unsigned int *hscale, unsigned int *vscale)
994{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300995 unsigned int maxw = norm_maxw(dev);
996 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997
998 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
999 if (*hscale >= 0x4000)
1000 *hscale = 0x3fff;
1001
1002 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1003 if (*vscale >= 0x4000)
1004 *vscale = 0x3fff;
1005}
1006
1007/* ------------------------------------------------------------------
1008 IOCTL vidioc handling
1009 ------------------------------------------------------------------*/
1010
Hans Verkuil78b526a2008-05-28 12:16:41 -03001011static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012 struct v4l2_format *f)
1013{
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
1016
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001017 f->fmt.pix.width = dev->width;
1018 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001019 f->fmt.pix.pixelformat = dev->format->fourcc;
1020 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001021 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001022 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1023
1024 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001025 if (dev->progressive)
1026 f->fmt.pix.field = V4L2_FIELD_NONE;
1027 else
1028 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001029 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030 return 0;
1031}
1032
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001033static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1034{
1035 unsigned int i;
1036
1037 for (i = 0; i < ARRAY_SIZE(format); i++)
1038 if (format[i].fourcc == fourcc)
1039 return &format[i];
1040
1041 return NULL;
1042}
1043
Hans Verkuil78b526a2008-05-28 12:16:41 -03001044static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001045 struct v4l2_format *f)
1046{
1047 struct em28xx_fh *fh = priv;
1048 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001049 unsigned int width = f->fmt.pix.width;
1050 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001051 unsigned int maxw = norm_maxw(dev);
1052 unsigned int maxh = norm_maxh(dev);
1053 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001054 struct em28xx_fmt *fmt;
1055
1056 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1057 if (!fmt) {
1058 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1059 f->fmt.pix.pixelformat);
1060 return -EINVAL;
1061 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001062
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001063 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001065 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1066 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001067 /* MaxPacketSize for em2800 is too small to capture at full resolution
1068 * use half of maxw as the scaler can only scale to 50% */
1069 if (width == maxw && height == maxh)
1070 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001071 } else {
1072 /* width must even because of the YUYV format
1073 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001074 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1075 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001076 }
1077
1078 get_scale(dev, width, height, &hscale, &vscale);
1079
1080 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1081 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1082
1083 f->fmt.pix.width = width;
1084 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001085 f->fmt.pix.pixelformat = fmt->fourcc;
1086 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1087 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001088 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001089 if (dev->progressive)
1090 f->fmt.pix.field = V4L2_FIELD_NONE;
1091 else
1092 f->fmt.pix.field = dev->interlaced ?
1093 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001094
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095 return 0;
1096}
1097
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001098static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1099 unsigned width, unsigned height)
1100{
1101 struct em28xx_fmt *fmt;
1102
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001103 fmt = format_by_fourcc(fourcc);
1104 if (!fmt)
1105 return -EINVAL;
1106
1107 dev->format = fmt;
1108 dev->width = width;
1109 dev->height = height;
1110
1111 /* set new image size */
1112 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1113
1114 em28xx_set_alternate(dev);
1115 em28xx_resolution_set(dev);
1116
1117 return 0;
1118}
1119
Hans Verkuil78b526a2008-05-28 12:16:41 -03001120static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001121 struct v4l2_format *f)
1122{
1123 struct em28xx_fh *fh = priv;
1124 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001125 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001126
1127 rc = check_dev(dev);
1128 if (rc < 0)
1129 return rc;
1130
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001131 vidioc_try_fmt_vid_cap(file, priv, f);
1132
Brandon Philips05612972008-04-13 14:57:01 -03001133 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1134 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001135 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001136 }
1137
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001138 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001139 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001140}
1141
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001142static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1143{
1144 struct em28xx_fh *fh = priv;
1145 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001146 int rc;
1147
1148 rc = check_dev(dev);
1149 if (rc < 0)
1150 return rc;
1151
1152 *norm = dev->norm;
1153
1154 return 0;
1155}
1156
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001157static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1158{
1159 struct em28xx_fh *fh = priv;
1160 struct em28xx *dev = fh->dev;
1161 int rc;
1162
1163 rc = check_dev(dev);
1164 if (rc < 0)
1165 return rc;
1166
1167 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1168
1169 return 0;
1170}
1171
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001172static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001173{
1174 struct em28xx_fh *fh = priv;
1175 struct em28xx *dev = fh->dev;
1176 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001177 int rc;
1178
1179 rc = check_dev(dev);
1180 if (rc < 0)
1181 return rc;
1182
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001183 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184
1185 /* Adjusts width/height, if needed */
1186 f.fmt.pix.width = dev->width;
1187 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001188 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001189
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001190 /* set new image size */
1191 dev->width = f.fmt.pix.width;
1192 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001193 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1194
1195 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001196 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198 return 0;
1199}
1200
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001201static int vidioc_g_parm(struct file *file, void *priv,
1202 struct v4l2_streamparm *p)
1203{
1204 struct em28xx_fh *fh = priv;
1205 struct em28xx *dev = fh->dev;
1206 int rc = 0;
1207
1208 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1209 return -EINVAL;
1210
1211 if (dev->board.is_webcam)
1212 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1213 video, g_parm, p);
1214 else
1215 v4l2_video_std_frame_period(dev->norm,
1216 &p->parm.capture.timeperframe);
1217
1218 return rc;
1219}
1220
1221static int vidioc_s_parm(struct file *file, void *priv,
1222 struct v4l2_streamparm *p)
1223{
1224 struct em28xx_fh *fh = priv;
1225 struct em28xx *dev = fh->dev;
1226
1227 if (!dev->board.is_webcam)
1228 return -EINVAL;
1229
1230 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1231 return -EINVAL;
1232
1233 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1234}
1235
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001236static const char *iname[] = {
1237 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1238 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1239 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1240 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1241 [EM28XX_VMUX_SVIDEO] = "S-Video",
1242 [EM28XX_VMUX_TELEVISION] = "Television",
1243 [EM28XX_VMUX_CABLE] = "Cable TV",
1244 [EM28XX_VMUX_DVB] = "DVB",
1245 [EM28XX_VMUX_DEBUG] = "for debug only",
1246};
1247
1248static int vidioc_enum_input(struct file *file, void *priv,
1249 struct v4l2_input *i)
1250{
1251 struct em28xx_fh *fh = priv;
1252 struct em28xx *dev = fh->dev;
1253 unsigned int n;
1254
1255 n = i->index;
1256 if (n >= MAX_EM28XX_INPUT)
1257 return -EINVAL;
1258 if (0 == INPUT(n)->type)
1259 return -EINVAL;
1260
1261 i->index = n;
1262 i->type = V4L2_INPUT_TYPE_CAMERA;
1263
1264 strcpy(i->name, iname[INPUT(n)->type]);
1265
1266 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1267 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1268 i->type = V4L2_INPUT_TYPE_TUNER;
1269
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001270 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001271
1272 return 0;
1273}
1274
1275static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1276{
1277 struct em28xx_fh *fh = priv;
1278 struct em28xx *dev = fh->dev;
1279
1280 *i = dev->ctl_input;
1281
1282 return 0;
1283}
1284
1285static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1286{
1287 struct em28xx_fh *fh = priv;
1288 struct em28xx *dev = fh->dev;
1289 int rc;
1290
1291 rc = check_dev(dev);
1292 if (rc < 0)
1293 return rc;
1294
1295 if (i >= MAX_EM28XX_INPUT)
1296 return -EINVAL;
1297 if (0 == INPUT(i)->type)
1298 return -EINVAL;
1299
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03001300 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001301 return 0;
1302}
1303
1304static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1305{
1306 struct em28xx_fh *fh = priv;
1307 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001308
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001309 if (!dev->audio_mode.has_audio)
1310 return -EINVAL;
1311
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001312 switch (a->index) {
1313 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001314 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001315 break;
1316 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001317 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001318 break;
1319 case EM28XX_AMUX_VIDEO2:
1320 strcpy(a->name, "Television alt");
1321 break;
1322 case EM28XX_AMUX_PHONE:
1323 strcpy(a->name, "Phone");
1324 break;
1325 case EM28XX_AMUX_MIC:
1326 strcpy(a->name, "Mic");
1327 break;
1328 case EM28XX_AMUX_CD:
1329 strcpy(a->name, "CD");
1330 break;
1331 case EM28XX_AMUX_AUX:
1332 strcpy(a->name, "Aux");
1333 break;
1334 case EM28XX_AMUX_PCM_OUT:
1335 strcpy(a->name, "PCM");
1336 break;
1337 default:
1338 return -EINVAL;
1339 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001340
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001341 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001342 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001343
1344 return 0;
1345}
1346
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001347static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348{
1349 struct em28xx_fh *fh = priv;
1350 struct em28xx *dev = fh->dev;
1351
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001352
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001353 if (!dev->audio_mode.has_audio)
1354 return -EINVAL;
1355
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001356 if (a->index >= MAX_EM28XX_INPUT)
1357 return -EINVAL;
1358 if (0 == INPUT(a->index)->type)
1359 return -EINVAL;
1360
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001361 dev->ctl_ainput = INPUT(a->index)->amux;
1362 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001363
1364 if (!dev->ctl_aoutput)
1365 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001366
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367 return 0;
1368}
1369
1370static int vidioc_queryctrl(struct file *file, void *priv,
1371 struct v4l2_queryctrl *qc)
1372{
1373 struct em28xx_fh *fh = priv;
1374 struct em28xx *dev = fh->dev;
1375 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001376 int rc;
1377
1378 rc = check_dev(dev);
1379 if (rc < 0)
1380 return rc;
1381
1382 memset(qc, 0, sizeof(*qc));
1383
1384 qc->id = id;
1385
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001386 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001387 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1388 rc = ac97_queryctrl(qc);
1389 if (!rc)
1390 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001391 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001392
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001393 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001394 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395
1396 if (qc->type)
1397 return 0;
1398 else
1399 return -EINVAL;
1400}
1401
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001402/*
1403 * FIXME: This is an indirect way to check if a control exists at a
1404 * subdev. Instead of that hack, maybe the better would be to change all
1405 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1406 */
1407static int check_subdev_ctrl(struct em28xx *dev, int id)
1408{
1409 struct v4l2_queryctrl qc;
1410
1411 memset(&qc, 0, sizeof(qc));
1412 qc.id = id;
1413
1414 /* enumerate V4L2 device controls */
1415 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1416
1417 if (qc.type)
1418 return 0;
1419 else
1420 return -EINVAL;
1421}
1422
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001423static int vidioc_g_ctrl(struct file *file, void *priv,
1424 struct v4l2_control *ctrl)
1425{
1426 struct em28xx_fh *fh = priv;
1427 struct em28xx *dev = fh->dev;
1428 int rc;
1429
1430 rc = check_dev(dev);
1431 if (rc < 0)
1432 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001433 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001434
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001435 /* Set an AC97 control */
1436 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1437 rc = ac97_get_ctrl(dev, ctrl);
1438 else
1439 rc = 1;
1440
1441 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1442 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001443 if (check_subdev_ctrl(dev, ctrl->id))
1444 return -EINVAL;
1445
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001446 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001447 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001448 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001449
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001450 return rc;
1451}
1452
1453static int vidioc_s_ctrl(struct file *file, void *priv,
1454 struct v4l2_control *ctrl)
1455{
1456 struct em28xx_fh *fh = priv;
1457 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001458 int rc;
1459
1460 rc = check_dev(dev);
1461 if (rc < 0)
1462 return rc;
1463
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001464 /* Set an AC97 control */
1465 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1466 rc = ac97_set_ctrl(dev, ctrl);
1467 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001468 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001469
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001470 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001471 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001472 rc = check_subdev_ctrl(dev, ctrl->id);
1473 if (!rc)
1474 v4l2_device_call_all(&dev->v4l2_dev, 0,
1475 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001476 /*
1477 * In the case of non-AC97 volume controls, we still need
1478 * to do some setups at em28xx, in order to mute/unmute
1479 * and to adjust audio volume. However, the value ranges
1480 * should be checked by the corresponding V4L subdriver.
1481 */
1482 switch (ctrl->id) {
1483 case V4L2_CID_AUDIO_MUTE:
1484 dev->mute = ctrl->value;
1485 rc = em28xx_audio_analog_set(dev);
1486 break;
1487 case V4L2_CID_AUDIO_VOLUME:
1488 dev->volume = ctrl->value;
1489 rc = em28xx_audio_analog_set(dev);
1490 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001491 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001492 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001493}
1494
1495static int vidioc_g_tuner(struct file *file, void *priv,
1496 struct v4l2_tuner *t)
1497{
1498 struct em28xx_fh *fh = priv;
1499 struct em28xx *dev = fh->dev;
1500 int rc;
1501
1502 rc = check_dev(dev);
1503 if (rc < 0)
1504 return rc;
1505
1506 if (0 != t->index)
1507 return -EINVAL;
1508
1509 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001510 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001511
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001512 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001513 return 0;
1514}
1515
1516static int vidioc_s_tuner(struct file *file, void *priv,
1517 struct v4l2_tuner *t)
1518{
1519 struct em28xx_fh *fh = priv;
1520 struct em28xx *dev = fh->dev;
1521 int rc;
1522
1523 rc = check_dev(dev);
1524 if (rc < 0)
1525 return rc;
1526
1527 if (0 != t->index)
1528 return -EINVAL;
1529
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001530 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001531 return 0;
1532}
1533
1534static int vidioc_g_frequency(struct file *file, void *priv,
1535 struct v4l2_frequency *f)
1536{
1537 struct em28xx_fh *fh = priv;
1538 struct em28xx *dev = fh->dev;
1539
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001540 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001541 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001542 return 0;
1543}
1544
1545static int vidioc_s_frequency(struct file *file, void *priv,
1546 struct v4l2_frequency *f)
1547{
1548 struct em28xx_fh *fh = priv;
1549 struct em28xx *dev = fh->dev;
1550 int rc;
1551
1552 rc = check_dev(dev);
1553 if (rc < 0)
1554 return rc;
1555
1556 if (0 != f->tuner)
1557 return -EINVAL;
1558
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001559 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1560 return -EINVAL;
1561 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001562 return -EINVAL;
1563
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001564 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001565 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001566
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001567 return 0;
1568}
1569
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001570#ifdef CONFIG_VIDEO_ADV_DEBUG
1571static int em28xx_reg_len(int reg)
1572{
1573 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001574 case EM28XX_R40_AC97LSB:
1575 case EM28XX_R30_HSCALELOW:
1576 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001577 return 2;
1578 default:
1579 return 1;
1580 }
1581}
1582
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001583static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001584 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001585{
1586 struct em28xx_fh *fh = priv;
1587 struct em28xx *dev = fh->dev;
1588
1589 chip->ident = V4L2_IDENT_NONE;
1590 chip->revision = 0;
1591
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001592 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001593
1594 return 0;
1595}
1596
1597
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001598static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001599 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001600{
1601 struct em28xx_fh *fh = priv;
1602 struct em28xx *dev = fh->dev;
1603 int ret;
1604
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001605 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001606 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001607 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001608 if (ret < 0)
1609 return ret;
1610
1611 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001612 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001613 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001614 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001615 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001616 return 0;
1617 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001618 /* TODO: is this correct? */
1619 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1620 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001621 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001622 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001623 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001624 }
1625
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001626 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001627 reg->size = em28xx_reg_len(reg->reg);
1628 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001629 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001630
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001631 if (ret < 0)
1632 return ret;
1633
1634 reg->val = ret;
1635 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001636 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001637 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1638 reg->reg, (char *)&val, 2);
1639 if (ret < 0)
1640 return ret;
1641
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001642 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643 }
1644
1645 return 0;
1646}
1647
1648static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001649 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001650{
1651 struct em28xx_fh *fh = priv;
1652 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001653 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001654
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001655 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001656 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001657 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001658 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001659 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001660 return 0;
1661 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001662 /* TODO: is this correct? */
1663 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1664 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001665 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001666 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001667 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001668 }
1669
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001670 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001671 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001672
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001673 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001674 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001675}
1676#endif
1677
1678
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001679static int vidioc_cropcap(struct file *file, void *priv,
1680 struct v4l2_cropcap *cc)
1681{
1682 struct em28xx_fh *fh = priv;
1683 struct em28xx *dev = fh->dev;
1684
1685 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1686 return -EINVAL;
1687
1688 cc->bounds.left = 0;
1689 cc->bounds.top = 0;
1690 cc->bounds.width = dev->width;
1691 cc->bounds.height = dev->height;
1692 cc->defrect = cc->bounds;
1693 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1694 cc->pixelaspect.denominator = 59;
1695
1696 return 0;
1697}
1698
1699static int vidioc_streamon(struct file *file, void *priv,
1700 enum v4l2_buf_type type)
1701{
1702 struct em28xx_fh *fh = priv;
1703 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001704 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001705
1706 rc = check_dev(dev);
1707 if (rc < 0)
1708 return rc;
1709
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001710 if (unlikely(type != fh->type))
1711 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001712
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001713 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1714 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001715
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001716 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001717 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001718
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1720 rc = videobuf_streamon(&fh->vb_vidq);
1721 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1722 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001723
1724 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001725}
1726
1727static int vidioc_streamoff(struct file *file, void *priv,
1728 enum v4l2_buf_type type)
1729{
1730 struct em28xx_fh *fh = priv;
1731 struct em28xx *dev = fh->dev;
1732 int rc;
1733
1734 rc = check_dev(dev);
1735 if (rc < 0)
1736 return rc;
1737
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001738 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1739 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001740 return -EINVAL;
1741 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001742 return -EINVAL;
1743
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001744 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1745 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001746
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001747 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001748 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1749 videobuf_streamoff(&fh->vb_vidq);
1750 res_free(fh, EM28XX_RESOURCE_VIDEO);
1751 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001752 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001753 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1754 videobuf_streamoff(&fh->vb_vbiq);
1755 res_free(fh, EM28XX_RESOURCE_VBI);
1756 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001757 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001758
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001759 return 0;
1760}
1761
1762static int vidioc_querycap(struct file *file, void *priv,
1763 struct v4l2_capability *cap)
1764{
1765 struct em28xx_fh *fh = priv;
1766 struct em28xx *dev = fh->dev;
1767
1768 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1769 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001770 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001771
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001772 cap->capabilities =
1773 V4L2_CAP_SLICED_VBI_CAPTURE |
1774 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1776
Devin Heitmueller04146142009-09-11 00:08:44 -03001777 if (dev->vbi_dev)
1778 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1779
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001780 if (dev->audio_mode.has_audio)
1781 cap->capabilities |= V4L2_CAP_AUDIO;
1782
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001783 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 cap->capabilities |= V4L2_CAP_TUNER;
1785
1786 return 0;
1787}
1788
Hans Verkuil78b526a2008-05-28 12:16:41 -03001789static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001790 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001792 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793 return -EINVAL;
1794
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001795 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1796 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001797
1798 return 0;
1799}
1800
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001801static int vidioc_enum_framesizes(struct file *file, void *priv,
1802 struct v4l2_frmsizeenum *fsize)
1803{
1804 struct em28xx_fh *fh = priv;
1805 struct em28xx *dev = fh->dev;
1806 struct em28xx_fmt *fmt;
1807 unsigned int maxw = norm_maxw(dev);
1808 unsigned int maxh = norm_maxh(dev);
1809
1810 fmt = format_by_fourcc(fsize->pixel_format);
1811 if (!fmt) {
1812 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1813 fsize->pixel_format);
1814 return -EINVAL;
1815 }
1816
1817 if (dev->board.is_em2800) {
1818 if (fsize->index > 1)
1819 return -EINVAL;
1820 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1821 fsize->discrete.width = maxw / (1 + fsize->index);
1822 fsize->discrete.height = maxh / (1 + fsize->index);
1823 return 0;
1824 }
1825
1826 if (fsize->index != 0)
1827 return -EINVAL;
1828
1829 /* Report a continuous range */
1830 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1831 fsize->stepwise.min_width = 48;
1832 fsize->stepwise.min_height = 32;
1833 fsize->stepwise.max_width = maxw;
1834 fsize->stepwise.max_height = maxh;
1835 fsize->stepwise.step_width = 1;
1836 fsize->stepwise.step_height = 1;
1837 return 0;
1838}
1839
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001840/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001841static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001842 struct v4l2_format *f)
1843{
1844 struct em28xx_fh *fh = priv;
1845 struct em28xx *dev = fh->dev;
1846 int rc;
1847
1848 rc = check_dev(dev);
1849 if (rc < 0)
1850 return rc;
1851
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001852 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001853 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001854
1855 if (f->fmt.sliced.service_set == 0)
1856 rc = -EINVAL;
1857
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001858 return rc;
1859}
1860
Hans Verkuil78b526a2008-05-28 12:16:41 -03001861static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001862 struct v4l2_format *f)
1863{
1864 struct em28xx_fh *fh = priv;
1865 struct em28xx *dev = fh->dev;
1866 int rc;
1867
1868 rc = check_dev(dev);
1869 if (rc < 0)
1870 return rc;
1871
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001872 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001873
1874 if (f->fmt.sliced.service_set == 0)
1875 return -EINVAL;
1876
1877 return 0;
1878}
1879
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001880/* RAW VBI ioctls */
1881
1882static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1883 struct v4l2_format *format)
1884{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001885 struct em28xx_fh *fh = priv;
1886 struct em28xx *dev = fh->dev;
1887
1888 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001889 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1890 format->fmt.vbi.offset = 0;
1891 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001892 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1893 format->fmt.vbi.count[0] = dev->vbi_height;
1894 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001895
1896 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001897 if (dev->norm & V4L2_STD_525_60) {
1898 /* NTSC */
1899 format->fmt.vbi.start[0] = 10;
1900 format->fmt.vbi.start[1] = 273;
1901 } else if (dev->norm & V4L2_STD_625_50) {
1902 /* PAL */
1903 format->fmt.vbi.start[0] = 6;
1904 format->fmt.vbi.start[1] = 318;
1905 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001906
1907 return 0;
1908}
1909
1910static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1911 struct v4l2_format *format)
1912{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001913 struct em28xx_fh *fh = priv;
1914 struct em28xx *dev = fh->dev;
1915
1916 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001917 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1918 format->fmt.vbi.offset = 0;
1919 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001920 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1921 format->fmt.vbi.count[0] = dev->vbi_height;
1922 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001923
1924 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001925 if (dev->norm & V4L2_STD_525_60) {
1926 /* NTSC */
1927 format->fmt.vbi.start[0] = 10;
1928 format->fmt.vbi.start[1] = 273;
1929 } else if (dev->norm & V4L2_STD_625_50) {
1930 /* PAL */
1931 format->fmt.vbi.start[0] = 6;
1932 format->fmt.vbi.start[1] = 318;
1933 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001934
1935 return 0;
1936}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001937
1938static int vidioc_reqbufs(struct file *file, void *priv,
1939 struct v4l2_requestbuffers *rb)
1940{
1941 struct em28xx_fh *fh = priv;
1942 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001943 int rc;
1944
1945 rc = check_dev(dev);
1946 if (rc < 0)
1947 return rc;
1948
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001949 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1950 return videobuf_reqbufs(&fh->vb_vidq, rb);
1951 else
1952 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001953}
1954
1955static int vidioc_querybuf(struct file *file, void *priv,
1956 struct v4l2_buffer *b)
1957{
1958 struct em28xx_fh *fh = priv;
1959 struct em28xx *dev = fh->dev;
1960 int rc;
1961
1962 rc = check_dev(dev);
1963 if (rc < 0)
1964 return rc;
1965
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001966 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1967 return videobuf_querybuf(&fh->vb_vidq, b);
1968 else {
1969 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1970 the videobuf framework, but we probably shouldn't be
1971 returning a buffer larger than that which was asked for.
1972 At a minimum, it causes a crash in zvbi since it does
1973 a memcpy based on the source buffer length */
1974 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001975 b->length = dev->vbi_width * dev->vbi_height * 2;
1976
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001977 return result;
1978 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001979}
1980
1981static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1982{
1983 struct em28xx_fh *fh = priv;
1984 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001985 int rc;
1986
1987 rc = check_dev(dev);
1988 if (rc < 0)
1989 return rc;
1990
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001991 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1992 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001993 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001994 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001995}
1996
1997static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1998{
1999 struct em28xx_fh *fh = priv;
2000 struct em28xx *dev = fh->dev;
2001 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002002
2003 rc = check_dev(dev);
2004 if (rc < 0)
2005 return rc;
2006
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002007 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2008 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2009 O_NONBLOCK);
2010 else
2011 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2012 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002013}
2014
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002015/* ----------------------------------------------------------- */
2016/* RADIO ESPECIFIC IOCTLS */
2017/* ----------------------------------------------------------- */
2018
2019static int radio_querycap(struct file *file, void *priv,
2020 struct v4l2_capability *cap)
2021{
2022 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2023
2024 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2025 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002026 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002027
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002028 cap->capabilities = V4L2_CAP_TUNER;
2029 return 0;
2030}
2031
2032static int radio_g_tuner(struct file *file, void *priv,
2033 struct v4l2_tuner *t)
2034{
2035 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2036
2037 if (unlikely(t->index > 0))
2038 return -EINVAL;
2039
2040 strcpy(t->name, "Radio");
2041 t->type = V4L2_TUNER_RADIO;
2042
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002043 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002044
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002045 return 0;
2046}
2047
2048static int radio_enum_input(struct file *file, void *priv,
2049 struct v4l2_input *i)
2050{
2051 if (i->index != 0)
2052 return -EINVAL;
2053 strcpy(i->name, "Radio");
2054 i->type = V4L2_INPUT_TYPE_TUNER;
2055
2056 return 0;
2057}
2058
2059static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2060{
2061 if (unlikely(a->index))
2062 return -EINVAL;
2063
2064 strcpy(a->name, "Radio");
2065 return 0;
2066}
2067
2068static int radio_s_tuner(struct file *file, void *priv,
2069 struct v4l2_tuner *t)
2070{
2071 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2072
2073 if (0 != t->index)
2074 return -EINVAL;
2075
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002076 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002077
2078 return 0;
2079}
2080
2081static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002082 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002083{
2084 return 0;
2085}
2086
2087static int radio_s_input(struct file *file, void *fh, unsigned int i)
2088{
2089 return 0;
2090}
2091
2092static int radio_queryctrl(struct file *file, void *priv,
2093 struct v4l2_queryctrl *qc)
2094{
2095 int i;
2096
2097 if (qc->id < V4L2_CID_BASE ||
2098 qc->id >= V4L2_CID_LASTP1)
2099 return -EINVAL;
2100
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002101 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2102 if (qc->id && qc->id == ac97_qctrl[i].id) {
2103 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002104 return 0;
2105 }
2106 }
2107
2108 return -EINVAL;
2109}
2110
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002111/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002112 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002113 * inits the device and starts isoc transfer
2114 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002115static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002117 int errCode = 0, radio = 0;
2118 struct video_device *vdev = video_devdata(filp);
2119 struct em28xx *dev = video_drvdata(filp);
2120 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002121 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002122 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002123
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002124 switch (vdev->vfl_type) {
2125 case VFL_TYPE_GRABBER:
2126 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2127 break;
2128 case VFL_TYPE_VBI:
2129 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2130 break;
2131 case VFL_TYPE_RADIO:
2132 radio = 1;
2133 break;
2134 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002135
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002136 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2137 video_device_node_name(vdev), v4l2_type_names[fh_type],
2138 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002140
Hans Verkuil876cb142012-06-23 08:12:47 -03002141 if (mutex_lock_interruptible(&dev->lock))
2142 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002143 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002144 if (!fh) {
2145 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002146 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002147 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002148 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002149 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002150 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002151 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002152 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002154 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002155 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002156 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002157 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002159 /* Needed, since GPIO might have disabled power of
2160 some i2c device
2161 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002162 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002163
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002164 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002165 if (fh->radio) {
2166 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002167 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002168 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002169
2170 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002172 if (dev->progressive)
2173 field = V4L2_FIELD_NONE;
2174 else
2175 field = V4L2_FIELD_INTERLACED;
2176
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002177 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2178 NULL, &dev->slock,
2179 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002180 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002181
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002182 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2183 NULL, &dev->slock,
2184 V4L2_BUF_TYPE_VBI_CAPTURE,
2185 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002186 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002187 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002188
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189 return errCode;
2190}
2191
2192/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002193 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002194 * unregisters the v4l2,i2c and usb devices
2195 * called when the device gets disconected or at module unload
2196*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002197void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002199
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002200 /*FIXME: I2C IR should be disconnected */
2201
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002202 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002203 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002204 video_unregister_device(dev->radio_dev);
2205 else
2206 video_device_release(dev->radio_dev);
2207 dev->radio_dev = NULL;
2208 }
2209 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002210 em28xx_info("V4L2 device %s deregistered\n",
2211 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002212 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002213 video_unregister_device(dev->vbi_dev);
2214 else
2215 video_device_release(dev->vbi_dev);
2216 dev->vbi_dev = NULL;
2217 }
2218 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002219 em28xx_info("V4L2 device %s deregistered\n",
2220 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002221 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002222 video_unregister_device(dev->vdev);
2223 else
2224 video_device_release(dev->vdev);
2225 dev->vdev = NULL;
2226 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227}
2228
2229/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002230 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002231 * stops streaming and deallocates all resources allocated by the v4l2
2232 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002234static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002235{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002236 struct em28xx_fh *fh = filp->private_data;
2237 struct em28xx *dev = fh->dev;
2238 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002240 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241
Hans Verkuil876cb142012-06-23 08:12:47 -03002242 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002243 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002244 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002245 res_free(fh, EM28XX_RESOURCE_VIDEO);
2246 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002248 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2249 videobuf_stop(&fh->vb_vbiq);
2250 res_free(fh, EM28XX_RESOURCE_VBI);
2251 }
2252
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002253 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002254 /* the device is already disconnect,
2255 free the remaining resources */
2256 if (dev->state & DEV_DISCONNECTED) {
2257 em28xx_release_resources(dev);
Chris Rankin16e3d2f2011-09-04 15:38:14 -03002258 kfree(dev->alt_max_pkt_size);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002259 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002260 kfree(dev);
Ezequiel GarcĂ­adedb8cb2012-05-05 16:13:22 -03002261 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002262 return 0;
2263 }
2264
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002265 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002266 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002267
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002268 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002269 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002270 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002271
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002272 /* set alternate 0 */
2273 dev->alt = 0;
2274 em28xx_videodbg("setting alternate 0\n");
2275 errCode = usb_set_interface(dev->udev, 0, 0);
2276 if (errCode < 0) {
2277 em28xx_errdev("cannot change alternate number to "
2278 "0 (error=%i)\n", errCode);
2279 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002281
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002282 videobuf_mmap_free(&fh->vb_vidq);
2283 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002284 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002285 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002286 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287 return 0;
2288}
2289
2290/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002292 * will allocate buffers when called for the first time
2293 */
2294static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002295em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002296 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002298 struct em28xx_fh *fh = filp->private_data;
2299 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002300 int rc;
2301
2302 rc = check_dev(dev);
2303 if (rc < 0)
2304 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305
Hans Verkuil876cb142012-06-23 08:12:47 -03002306 if (mutex_lock_interruptible(&dev->lock))
2307 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002308 /* FIXME: read() is not prepared to allow changing the video
2309 resolution while streaming. Seems a bug at em28xx_set_fmt
2310 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002311
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002312 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002313 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002314 rc = -EBUSY;
2315 else
2316 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002317 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002318 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002319 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002320 rc = -EBUSY;
2321 else
2322 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002323 filp->f_flags & O_NONBLOCK);
2324 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002325 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002326
Hans Verkuil876cb142012-06-23 08:12:47 -03002327 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002328}
2329
2330/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002331 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002332 * will allocate buffers when called for the first time
2333 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002334static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002335{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002336 struct em28xx_fh *fh = filp->private_data;
2337 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002338 int rc;
2339
2340 rc = check_dev(dev);
2341 if (rc < 0)
2342 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002344 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2345 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2346 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002347 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002348 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2349 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2350 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002351 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002352 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002353 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002354 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002355}
2356
Hans Verkuil876cb142012-06-23 08:12:47 -03002357static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2358{
2359 struct em28xx_fh *fh = filp->private_data;
2360 struct em28xx *dev = fh->dev;
2361 unsigned int res;
2362
2363 mutex_lock(&dev->lock);
2364 res = em28xx_poll(filp, wait);
2365 mutex_unlock(&dev->lock);
2366 return res;
2367}
2368
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002369/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002370 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002371 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002372static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002373{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002374 struct em28xx_fh *fh = filp->private_data;
2375 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002376 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002377
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002378 rc = check_dev(dev);
2379 if (rc < 0)
2380 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002381
Hans Verkuil876cb142012-06-23 08:12:47 -03002382 if (mutex_lock_interruptible(&dev->lock))
2383 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002384 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2385 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2386 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2387 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002388 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002389
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002390 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2391 (unsigned long)vma->vm_start,
2392 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2393 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002394
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002395 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002396}
2397
Hans Verkuilbec43662008-12-30 06:58:20 -03002398static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002399 .owner = THIS_MODULE,
2400 .open = em28xx_v4l2_open,
2401 .release = em28xx_v4l2_close,
2402 .read = em28xx_v4l2_read,
2403 .poll = em28xx_v4l2_poll,
2404 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002405 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002406};
2407
Hans Verkuila3998102008-07-21 02:57:38 -03002408static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002409 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002410 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2411 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2412 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2413 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002414 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2415 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002416 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002417 .vidioc_g_audio = vidioc_g_audio,
2418 .vidioc_s_audio = vidioc_s_audio,
2419 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002420 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2421 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2422 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002423
2424 .vidioc_reqbufs = vidioc_reqbufs,
2425 .vidioc_querybuf = vidioc_querybuf,
2426 .vidioc_qbuf = vidioc_qbuf,
2427 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002428 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002429 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002430 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002431 .vidioc_g_parm = vidioc_g_parm,
2432 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002433 .vidioc_enum_input = vidioc_enum_input,
2434 .vidioc_g_input = vidioc_g_input,
2435 .vidioc_s_input = vidioc_s_input,
2436 .vidioc_queryctrl = vidioc_queryctrl,
2437 .vidioc_g_ctrl = vidioc_g_ctrl,
2438 .vidioc_s_ctrl = vidioc_s_ctrl,
2439 .vidioc_streamon = vidioc_streamon,
2440 .vidioc_streamoff = vidioc_streamoff,
2441 .vidioc_g_tuner = vidioc_g_tuner,
2442 .vidioc_s_tuner = vidioc_s_tuner,
2443 .vidioc_g_frequency = vidioc_g_frequency,
2444 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002445#ifdef CONFIG_VIDEO_ADV_DEBUG
2446 .vidioc_g_register = vidioc_g_register,
2447 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002448 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002449#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002450};
2451
2452static const struct video_device em28xx_video_template = {
2453 .fops = &em28xx_v4l_fops,
2454 .release = video_device_release,
2455 .ioctl_ops = &video_ioctl_ops,
2456
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002457 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002458 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002459};
2460
Hans Verkuilbec43662008-12-30 06:58:20 -03002461static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002462 .owner = THIS_MODULE,
2463 .open = em28xx_v4l2_open,
2464 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002465 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002466};
2467
2468static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002469 .vidioc_querycap = radio_querycap,
2470 .vidioc_g_tuner = radio_g_tuner,
2471 .vidioc_enum_input = radio_enum_input,
2472 .vidioc_g_audio = radio_g_audio,
2473 .vidioc_s_tuner = radio_s_tuner,
2474 .vidioc_s_audio = radio_s_audio,
2475 .vidioc_s_input = radio_s_input,
2476 .vidioc_queryctrl = radio_queryctrl,
2477 .vidioc_g_ctrl = vidioc_g_ctrl,
2478 .vidioc_s_ctrl = vidioc_s_ctrl,
2479 .vidioc_g_frequency = vidioc_g_frequency,
2480 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002481#ifdef CONFIG_VIDEO_ADV_DEBUG
2482 .vidioc_g_register = vidioc_g_register,
2483 .vidioc_s_register = vidioc_s_register,
2484#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002485};
2486
Hans Verkuila3998102008-07-21 02:57:38 -03002487static struct video_device em28xx_radio_template = {
2488 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002489 .fops = &radio_fops,
2490 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002491};
2492
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002493/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002494
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002495
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002496
Adrian Bunk532fe652008-01-28 22:10:48 -03002497static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002498 const struct video_device *template,
2499 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002500{
2501 struct video_device *vfd;
2502
2503 vfd = video_device_alloc();
2504 if (NULL == vfd)
2505 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002506
2507 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002508 vfd->v4l2_dev = &dev->v4l2_dev;
2509 vfd->release = video_device_release;
2510 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002511 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002512
2513 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2514 dev->name, type_name);
2515
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002516 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002517 return vfd;
2518}
2519
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002520int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002521{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002522 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002523 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002524 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002525
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002526 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2527 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002528
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002529 /* set default norm */
2530 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002531 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002532 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002533
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002534 /* Analog specific initialization */
2535 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002536
2537 maxw = norm_maxw(dev);
2538 /* MaxPacketSize for em2800 is too small to capture at full resolution
2539 * use half of maxw as the scaler can only scale to 50% */
2540 if (dev->board.is_em2800)
2541 maxw /= 2;
2542
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002543 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002544 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002545
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03002546 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002547
2548 /* Audio defaults */
2549 dev->mute = 1;
2550 dev->volume = 0x1f;
2551
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002552/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002553 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2554 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2555 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002556
2557 em28xx_set_outfmt(dev);
2558 em28xx_colorlevels_set_default(dev);
2559 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002560
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002561 /* allocate and fill video video_device struct */
2562 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2563 if (!dev->vdev) {
2564 em28xx_errdev("cannot allocate video_device.\n");
2565 return -ENODEV;
2566 }
2567
2568 /* register v4l2 video video_device */
2569 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2570 video_nr[dev->devno]);
2571 if (ret) {
2572 em28xx_errdev("unable to register video device (error=%i).\n",
2573 ret);
2574 return ret;
2575 }
2576
2577 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002578 if (em28xx_vbi_supported(dev) == 1) {
2579 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2580 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002581
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002582 /* register v4l2 vbi video_device */
2583 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2584 vbi_nr[dev->devno]);
2585 if (ret < 0) {
2586 em28xx_errdev("unable to register vbi device\n");
2587 return ret;
2588 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002589 }
2590
2591 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002592 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2593 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002594 if (!dev->radio_dev) {
2595 em28xx_errdev("cannot allocate video_device.\n");
2596 return -ENODEV;
2597 }
2598 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2599 radio_nr[dev->devno]);
2600 if (ret < 0) {
2601 em28xx_errdev("can't register radio device\n");
2602 return ret;
2603 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002604 em28xx_info("Registered radio device as %s\n",
2605 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002606 }
2607
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002608 em28xx_info("V4L2 video device registered as %s\n",
2609 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002610
2611 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002612 em28xx_info("V4L2 VBI device registered as %s\n",
2613 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002614
2615 return 0;
2616}