blob: 971046861f7ff1946e4800b73d977a646187b9ae [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Hans Verkuil50fdf402012-09-07 06:10:12 -030043#include <media/v4l2-event.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030044#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030045#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030046#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
49 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030050 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080052
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080053#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030054
55#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080057#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080058 if (video_debug) \
59 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030060 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080061
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030063module_param(isoc_debug, int, 0644);
64MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030065
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030066#define em28xx_isocdbg(fmt, arg...) \
67do {\
68 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030069 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030070 dev->name, __func__ , ##arg); \
71 } \
72 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030073
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080074MODULE_AUTHOR(DRIVER_AUTHOR);
75MODULE_DESCRIPTION(DRIVER_DESC);
76MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030077MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080078
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030080static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020083module_param_array(video_nr, int, NULL, 0444);
84module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030086MODULE_PARM_DESC(video_nr, "video device numbers");
87MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080089
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030090static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030091module_param(video_debug, int, 0644);
92MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080093
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030094/* supported video standards */
95static struct em28xx_fmt format[] = {
96 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030097 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030098 .fourcc = V4L2_PIX_FMT_YUYV,
99 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -0300100 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300101 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300102 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300103 .fourcc = V4L2_PIX_FMT_RGB565,
104 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300105 .reg = EM28XX_OUTFMT_RGB_16_656,
106 }, {
107 .name = "8 bpp Bayer BGBG..GRGR",
108 .fourcc = V4L2_PIX_FMT_SBGGR8,
109 .depth = 8,
110 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
111 }, {
112 .name = "8 bpp Bayer GRGR..BGBG",
113 .fourcc = V4L2_PIX_FMT_SGRBG8,
114 .depth = 8,
115 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
116 }, {
117 .name = "8 bpp Bayer GBGB..RGRG",
118 .fourcc = V4L2_PIX_FMT_SGBRG8,
119 .depth = 8,
120 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
121 }, {
122 .name = "12 bpp YUV411",
123 .fourcc = V4L2_PIX_FMT_YUV411P,
124 .depth = 12,
125 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300126 },
127};
128
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300129/* ------------------------------------------------------------------
130 DMA and thread functions
131 ------------------------------------------------------------------*/
132
133/*
Frank Schaefer948a49a2012-12-08 11:31:25 -0300134 * Finish the current buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300135 */
Frank Schaefer948a49a2012-12-08 11:31:25 -0300136static inline void finish_buffer(struct em28xx *dev,
137 struct em28xx_buffer *buf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300138{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
140 buf->vb.state = VIDEOBUF_DONE;
141 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300142 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
145}
146
147/*
Frank Schaefer36016a32012-12-08 11:31:32 -0300148 * Copy picture data from USB buffer to videobuf buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300149 */
150static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300151 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300152 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300153 unsigned long len)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300154{
155 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300157 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158
Frank Schaefer87325332012-12-08 11:31:27 -0300159 if (buf->pos + len > buf->vb.size)
160 len = buf->vb.size - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161
Frank Schaefer36016a32012-12-08 11:31:32 -0300162 startread = usb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 remain = len;
164
Frank Schaeferc02ec712012-11-08 14:11:33 -0300165 if (dev->progressive || buf->top_field)
Frank Schaefer36016a32012-12-08 11:31:32 -0300166 fieldstart = buf->vb_buf;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300167 else /* interlaced mode, even nr. of lines */
Frank Schaefer36016a32012-12-08 11:31:32 -0300168 fieldstart = buf->vb_buf + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169
Frank Schaefer87325332012-12-08 11:31:27 -0300170 linesdone = buf->pos / bytesperline;
171 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300172
173 if (dev->progressive)
174 offset = linesdone * bytesperline + currlinedone;
175 else
176 offset = linesdone * bytesperline * 2 + currlinedone;
177
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300179 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300180 lencopy = lencopy > remain ? remain : lencopy;
181
Frank Schaefer36016a32012-12-08 11:31:32 -0300182 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300183 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Frank Schaefer36016a32012-12-08 11:31:32 -0300184 ((char *)startwrite + lencopy) -
185 ((char *)buf->vb_buf + buf->vb.size));
186 remain = (char *)buf->vb_buf + buf->vb.size -
187 (char *)startwrite;
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300188 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300189 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300190 if (lencopy <= 0)
191 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300192 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300193
194 remain -= lencopy;
195
196 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300197 if (dev->progressive)
198 startwrite += lencopy;
199 else
200 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203 lencopy = remain;
204 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300205 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206
Frank Schaefer36016a32012-12-08 11:31:32 -0300207 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300208 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300209 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
210 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300211 ((char *)startwrite + lencopy) -
Frank Schaefer36016a32012-12-08 11:31:32 -0300212 ((char *)buf->vb_buf + buf->vb.size));
213 lencopy = remain = (char *)buf->vb_buf + buf->vb.size -
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300214 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300216 if (lencopy <= 0)
217 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300218
219 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300220
221 remain -= lencopy;
222 }
223
Frank Schaefer87325332012-12-08 11:31:27 -0300224 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225}
226
Frank Schaefer36016a32012-12-08 11:31:32 -0300227/*
228 * Copy VBI data from USB buffer to videobuf buffer
229 */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300230static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300231 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300232 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300233 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300234{
Frank Schaefer36016a32012-12-08 11:31:32 -0300235 unsigned int offset;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300236
Frank Schaefer87325332012-12-08 11:31:27 -0300237 if (buf->pos + len > buf->vb.size)
238 len = buf->vb.size - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300239
Frank Schaefer87325332012-12-08 11:31:27 -0300240 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300241 /* Make sure the bottom field populates the second half of the frame */
Frank Schaefer36016a32012-12-08 11:31:32 -0300242 if (buf->top_field == 0)
243 offset += dev->vbi_width * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300244
Frank Schaefer36016a32012-12-08 11:31:32 -0300245 memcpy(buf->vb_buf + offset, usb_buf, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300246 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300247}
248
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300249static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300250 int packet, int status)
251{
252 char *errmsg = "Unknown";
253
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300254 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300255 case -ENOENT:
256 errmsg = "unlinked synchronuously";
257 break;
258 case -ECONNRESET:
259 errmsg = "unlinked asynchronuously";
260 break;
261 case -ENOSR:
262 errmsg = "Buffer error (overrun)";
263 break;
264 case -EPIPE:
265 errmsg = "Stalled (device not responding)";
266 break;
267 case -EOVERFLOW:
268 errmsg = "Babble (bad cable?)";
269 break;
270 case -EPROTO:
271 errmsg = "Bit-stuff error (bad cable?)";
272 break;
273 case -EILSEQ:
274 errmsg = "CRC/Timeout (could be anything)";
275 break;
276 case -ETIME:
277 errmsg = "Device does not respond";
278 break;
279 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300280 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300281 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
282 } else {
283 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
284 packet, status, errmsg);
285 }
286}
287
288/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300289 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300290 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300291static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
292 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300293{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300294 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300295 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300296
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300297 if (list_empty(&dma_q->active)) {
298 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300299 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300300 }
301
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300302 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300303 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300304 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300305 outp = videobuf_to_vmalloc(&buf->vb);
306 memset(outp, 0, buf->vb.size);
Frank Schaefer87325332012-12-08 11:31:27 -0300307 buf->pos = 0;
Frank Schaefer4078d622012-12-08 11:31:29 -0300308 buf->vb_buf = outp;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300309
Frank Schaefer24a6d842012-12-08 11:31:24 -0300310 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300311}
312
Frank Schaefere04c00d2012-12-08 11:31:30 -0300313/*
314 * Finish the current buffer if completed and prepare for the next field
315 */
316static struct em28xx_buffer *
317finish_field_prepare_next(struct em28xx *dev,
318 struct em28xx_buffer *buf,
319 struct em28xx_dmaqueue *dma_q)
320{
321 if (dev->progressive || dev->top_field) { /* Brand new frame */
322 if (buf != NULL)
323 finish_buffer(dev, buf);
324 buf = get_next_buf(dev, dma_q);
325 }
326 if (buf != NULL) {
327 buf->top_field = dev->top_field;
328 buf->pos = 0;
329 }
330
331 return buf;
332}
333
Frank Schaefer227b7c92012-12-08 11:31:31 -0300334/*
335 * Process data packet according to the em2710/em2750/em28xx frame data format
336 */
337static inline void process_frame_data_em28xx(struct em28xx *dev,
338 unsigned char *data_pkt,
339 unsigned int data_len)
340{
341 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
342 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
343 struct em28xx_dmaqueue *dma_q = &dev->vidq;
344 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
345
346 /* capture type 0 = vbi start
347 capture type 1 = vbi in progress
348 capture type 2 = video start
349 capture type 3 = video in progress */
350 if (data_len >= 4) {
351 /* NOTE: Headers are always 4 bytes and
352 * never split across packets */
353 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
354 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
355 /* Continuation */
356 data_pkt += 4;
357 data_len -= 4;
358 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
359 /* Field start (VBI mode) */
360 dev->capture_type = 0;
361 dev->vbi_read = 0;
362 em28xx_isocdbg("VBI START HEADER !!!\n");
363 dev->top_field = !(data_pkt[2] & 1);
364 data_pkt += 4;
365 data_len -= 4;
366 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
367 /* Field start (VBI disabled) */
368 dev->capture_type = 2;
369 em28xx_isocdbg("VIDEO START HEADER !!!\n");
370 dev->top_field = !(data_pkt[2] & 1);
371 data_pkt += 4;
372 data_len -= 4;
373 }
374 }
375 /* NOTE: With bulk transfers, intermediate data packets
376 * have no continuation header */
377
378 if (dev->capture_type == 0) {
379 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
380 dev->usb_ctl.vbi_buf = vbi_buf;
381 dev->capture_type = 1;
382 }
383
384 if (dev->capture_type == 1) {
385 int vbi_size = dev->vbi_width * dev->vbi_height;
386 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
387 (vbi_size - dev->vbi_read) : data_len;
388
389 /* Copy VBI data */
390 if (vbi_buf != NULL)
391 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
392 dev->vbi_read += vbi_data_len;
393
394 if (vbi_data_len < data_len) {
395 /* Continue with copying video data */
396 dev->capture_type = 2;
397 data_pkt += vbi_data_len;
398 data_len -= vbi_data_len;
399 }
400 }
401
402 if (dev->capture_type == 2) {
403 buf = finish_field_prepare_next(dev, buf, dma_q);
404 dev->usb_ctl.vid_buf = buf;
405 dev->capture_type = 3;
406 }
407
408 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
409 em28xx_copy_video(dev, buf, data_pkt, data_len);
410}
411
Frank Schaefer960da932012-11-25 06:37:37 -0300412/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300413static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300415 int xfer_bulk, num_packets, i;
416 unsigned char *usb_data_pkt;
417 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300418
419 if (!dev)
420 return 0;
421
422 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
423 return 0;
424
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300425 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300426 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300427
Frank Schaefer4601cc32012-11-08 14:11:46 -0300428 xfer_bulk = usb_pipebulk(urb->pipe);
429
Frank Schaefer4601cc32012-11-08 14:11:46 -0300430 if (xfer_bulk) /* bulk */
431 num_packets = 1;
432 else /* isoc */
433 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300434
Frank Schaefer4601cc32012-11-08 14:11:46 -0300435 for (i = 0; i < num_packets; i++) {
436 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300437 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300438
Frank Schaefer227b7c92012-12-08 11:31:31 -0300439 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300440 } else { /* isoc */
441 if (urb->iso_frame_desc[i].status < 0) {
442 print_err_status(dev, i,
443 urb->iso_frame_desc[i].status);
444 if (urb->iso_frame_desc[i].status != -EPROTO)
445 continue;
446 }
447
Frank Schaefer227b7c92012-12-08 11:31:31 -0300448 usb_data_len = urb->iso_frame_desc[i].actual_length;
449 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300450 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300451 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300452 }
453
Frank Schaefer227b7c92012-12-08 11:31:31 -0300454 usb_data_pkt = urb->transfer_buffer +
455 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300456 }
457
Frank Schaefer227b7c92012-12-08 11:31:31 -0300458 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300459 /* NOTE: happens very often with isoc transfers */
460 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300461 continue;
462 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300463
Frank Schaefer227b7c92012-12-08 11:31:31 -0300464 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300465 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300466 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300467}
468
469
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300470/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300471 Videobuf operations
472 ------------------------------------------------------------------*/
473
474static int
475buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
476{
477 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300478 struct em28xx *dev = fh->dev;
479 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300480
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300481 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
482 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300483
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484 if (0 == *count)
485 *count = EM28XX_DEF_BUF;
486
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300487 if (*count < EM28XX_MIN_BUF)
488 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300490 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300491 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300492 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300493 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300494
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300495 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300496
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497 return 0;
498}
499
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300500/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300501static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
502{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300503 struct em28xx_fh *fh = vq->priv_data;
504 struct em28xx *dev = fh->dev;
505 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300506 if (in_interrupt())
507 BUG();
508
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300509 /* We used to wait for the buffer to finish here, but this didn't work
510 because, as we were keeping the state as VIDEOBUF_QUEUED,
511 videobuf_queue_cancel marked it as finished for us.
512 (Also, it could wedge forever if the hardware was misconfigured.)
513
514 This should be safe; by the time we get here, the buffer isn't
515 queued anymore. If we ever start marking the buffers as
516 VIDEOBUF_ACTIVE, it won't be, though.
517 */
518 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300519 if (dev->usb_ctl.vid_buf == buf)
520 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300521 spin_unlock_irqrestore(&dev->slock, flags);
522
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300523 videobuf_vmalloc_free(&buf->vb);
524 buf->vb.state = VIDEOBUF_NEEDS_INIT;
525}
526
527static int
528buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
529 enum v4l2_field field)
530{
531 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300532 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300533 struct em28xx *dev = fh->dev;
534 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300535
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300536 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
537 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300538
539 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
540 return -EINVAL;
541
Brandon Philips05612972008-04-13 14:57:01 -0300542 buf->vb.width = dev->width;
543 buf->vb.height = dev->height;
544 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300545
546 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300547 rc = videobuf_iolock(vq, &buf->vb, NULL);
548 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300549 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300550 }
551
Frank Schaefer74209dc2012-11-08 14:11:37 -0300552 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300553 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300554
555 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300556 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300557 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
558 dev->analog_xfer_bulk,
559 EM28XX_NUM_BUFS,
560 dev->max_pkt_size,
561 dev->packet_multiplier,
562 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300563 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300564 goto fail;
565 }
566
567 buf->vb.state = VIDEOBUF_PREPARED;
568 return 0;
569
570fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300571 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300572 return rc;
573}
574
575static void
576buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
577{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300578 struct em28xx_buffer *buf = container_of(vb,
579 struct em28xx_buffer,
580 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300581 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300582 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300583 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300584
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300585 buf->vb.state = VIDEOBUF_QUEUED;
586 list_add_tail(&buf->vb.queue, &vidq->active);
587
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300588}
589
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300590static void buffer_release(struct videobuf_queue *vq,
591 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300592{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300593 struct em28xx_buffer *buf = container_of(vb,
594 struct em28xx_buffer,
595 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300596 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300597 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300598
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300599 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300600
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300601 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300602}
603
604static struct videobuf_queue_ops em28xx_video_qops = {
605 .buf_setup = buffer_setup,
606 .buf_prepare = buffer_prepare,
607 .buf_queue = buffer_queue,
608 .buf_release = buffer_release,
609};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800610
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300611/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800612
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800613static void video_mux(struct em28xx *dev, int index)
614{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800615 dev->ctl_input = index;
616 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300617 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800618
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300619 if (!dev->ctl_aoutput)
620 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
621
Hans Verkuil5325b422009-04-02 11:26:22 -0300622 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
623 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800624
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300625 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300626 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300627 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
628 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300629 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300630 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300631 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
632 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800633 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300634
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300635 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300636 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
637 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300638 }
639
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300640 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800641}
642
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300643/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300644static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300645{
646 struct em28xx *dev = fh->dev;
647
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300648 if (fh->resources & bit)
649 /* have it already allocated */
650 return 1;
651
652 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300653 if (dev->resources & bit) {
654 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300655 return 0;
656 }
657 /* it's free, grab it */
658 fh->resources |= bit;
659 dev->resources |= bit;
660 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300661 return 1;
662}
663
664static int res_check(struct em28xx_fh *fh, unsigned int bit)
665{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300666 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300667}
668
669static int res_locked(struct em28xx *dev, unsigned int bit)
670{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300671 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300672}
673
674static void res_free(struct em28xx_fh *fh, unsigned int bits)
675{
676 struct em28xx *dev = fh->dev;
677
678 BUG_ON((fh->resources & bits) != bits);
679
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300680 fh->resources &= ~bits;
681 dev->resources &= ~bits;
682 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300683}
684
685static int get_ressource(struct em28xx_fh *fh)
686{
687 switch (fh->type) {
688 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
689 return EM28XX_RESOURCE_VIDEO;
690 case V4L2_BUF_TYPE_VBI_CAPTURE:
691 return EM28XX_RESOURCE_VBI;
692 default:
693 BUG();
694 return 0;
695 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300696}
697
Hans Verkuil081b9452012-09-07 05:43:59 -0300698void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300699{
Hans Verkuil081b9452012-09-07 05:43:59 -0300700 struct em28xx *dev = priv;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300701
Hans Verkuil081b9452012-09-07 05:43:59 -0300702 /*
703 * In the case of non-AC97 volume controls, we still need
704 * to do some setups at em28xx, in order to mute/unmute
705 * and to adjust audio volume. However, the value ranges
706 * should be checked by the corresponding V4L subdriver.
707 */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300708 switch (ctrl->id) {
709 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil081b9452012-09-07 05:43:59 -0300710 dev->mute = ctrl->val;
711 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300712 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300713 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil081b9452012-09-07 05:43:59 -0300714 dev->volume = ctrl->val;
715 em28xx_audio_analog_set(dev);
716 break;
717 }
718}
719
720static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
721{
722 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
723
724 switch (ctrl->id) {
725 case V4L2_CID_AUDIO_MUTE:
726 dev->mute = ctrl->val;
727 break;
728 case V4L2_CID_AUDIO_VOLUME:
729 dev->volume = ctrl->val;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300730 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300731 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300732
733 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300734}
735
Hans Verkuil081b9452012-09-07 05:43:59 -0300736const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
737 .s_ctrl = em28xx_s_ctrl,
738};
739
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300740static int check_dev(struct em28xx *dev)
741{
742 if (dev->state & DEV_DISCONNECTED) {
743 em28xx_errdev("v4l2 ioctl: device not present\n");
744 return -ENODEV;
745 }
746
747 if (dev->state & DEV_MISCONFIGURED) {
748 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
749 "close and open it again\n");
750 return -EIO;
751 }
752 return 0;
753}
754
755static void get_scale(struct em28xx *dev,
756 unsigned int width, unsigned int height,
757 unsigned int *hscale, unsigned int *vscale)
758{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300759 unsigned int maxw = norm_maxw(dev);
760 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300761
762 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
763 if (*hscale >= 0x4000)
764 *hscale = 0x3fff;
765
766 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
767 if (*vscale >= 0x4000)
768 *vscale = 0x3fff;
769}
770
771/* ------------------------------------------------------------------
772 IOCTL vidioc handling
773 ------------------------------------------------------------------*/
774
Hans Verkuil78b526a2008-05-28 12:16:41 -0300775static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300776 struct v4l2_format *f)
777{
778 struct em28xx_fh *fh = priv;
779 struct em28xx *dev = fh->dev;
780
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300781 f->fmt.pix.width = dev->width;
782 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300783 f->fmt.pix.pixelformat = dev->format->fourcc;
784 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300785 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300786 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
787
788 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300789 if (dev->progressive)
790 f->fmt.pix.field = V4L2_FIELD_NONE;
791 else
792 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300794 return 0;
795}
796
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300797static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
798{
799 unsigned int i;
800
801 for (i = 0; i < ARRAY_SIZE(format); i++)
802 if (format[i].fourcc == fourcc)
803 return &format[i];
804
805 return NULL;
806}
807
Hans Verkuil78b526a2008-05-28 12:16:41 -0300808static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 struct v4l2_format *f)
810{
811 struct em28xx_fh *fh = priv;
812 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300813 unsigned int width = f->fmt.pix.width;
814 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300815 unsigned int maxw = norm_maxw(dev);
816 unsigned int maxh = norm_maxh(dev);
817 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300818 struct em28xx_fmt *fmt;
819
820 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
821 if (!fmt) {
822 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
823 f->fmt.pix.pixelformat);
824 return -EINVAL;
825 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300827 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300828 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300829 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
830 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300831 /* MaxPacketSize for em2800 is too small to capture at full resolution
832 * use half of maxw as the scaler can only scale to 50% */
833 if (width == maxw && height == maxh)
834 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300835 } else {
836 /* width must even because of the YUYV format
837 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300838 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
839 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300840 }
841
842 get_scale(dev, width, height, &hscale, &vscale);
843
844 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
845 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
846
847 f->fmt.pix.width = width;
848 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300849 f->fmt.pix.pixelformat = fmt->fourcc;
850 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
851 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300852 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300853 if (dev->progressive)
854 f->fmt.pix.field = V4L2_FIELD_NONE;
855 else
856 f->fmt.pix.field = dev->interlaced ?
857 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300858
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300859 return 0;
860}
861
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300862static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
863 unsigned width, unsigned height)
864{
865 struct em28xx_fmt *fmt;
866
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300867 fmt = format_by_fourcc(fourcc);
868 if (!fmt)
869 return -EINVAL;
870
871 dev->format = fmt;
872 dev->width = width;
873 dev->height = height;
874
875 /* set new image size */
876 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
877
878 em28xx_set_alternate(dev);
879 em28xx_resolution_set(dev);
880
881 return 0;
882}
883
Hans Verkuil78b526a2008-05-28 12:16:41 -0300884static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300885 struct v4l2_format *f)
886{
887 struct em28xx_fh *fh = priv;
888 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300889 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300890
891 rc = check_dev(dev);
892 if (rc < 0)
893 return rc;
894
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300895 vidioc_try_fmt_vid_cap(file, priv, f);
896
Brandon Philips05612972008-04-13 14:57:01 -0300897 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
898 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300899 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300900 }
901
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300902 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300903 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300904}
905
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300906static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
907{
908 struct em28xx_fh *fh = priv;
909 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300910 int rc;
911
Hans Verkuild8c95c02012-09-07 07:31:54 -0300912 if (dev->board.is_webcam)
913 return -ENOTTY;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300914 rc = check_dev(dev);
915 if (rc < 0)
916 return rc;
917
918 *norm = dev->norm;
919
920 return 0;
921}
922
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300923static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
924{
925 struct em28xx_fh *fh = priv;
926 struct em28xx *dev = fh->dev;
927 int rc;
928
Hans Verkuild8c95c02012-09-07 07:31:54 -0300929 if (dev->board.is_webcam)
930 return -ENOTTY;
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300931 rc = check_dev(dev);
932 if (rc < 0)
933 return rc;
934
935 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
936
937 return 0;
938}
939
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300940static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941{
942 struct em28xx_fh *fh = priv;
943 struct em28xx *dev = fh->dev;
944 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300945 int rc;
946
Hans Verkuild8c95c02012-09-07 07:31:54 -0300947 if (dev->board.is_webcam)
948 return -ENOTTY;
949 if (*norm == dev->norm)
950 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300951 rc = check_dev(dev);
952 if (rc < 0)
953 return rc;
954
Hans Verkuild8c95c02012-09-07 07:31:54 -0300955 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
956 em28xx_errdev("%s queue busy\n", __func__);
957 return -EBUSY;
958 }
959
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300960 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961
962 /* Adjusts width/height, if needed */
Hans Verkuild8c95c02012-09-07 07:31:54 -0300963 f.fmt.pix.width = 720;
964 f.fmt.pix.height = (*norm & V4L2_STD_525_60) ? 480 : 576;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300965 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967 /* set new image size */
968 dev->width = f.fmt.pix.width;
969 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300970 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
971
972 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300973 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300975 return 0;
976}
977
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -0300978static int vidioc_g_parm(struct file *file, void *priv,
979 struct v4l2_streamparm *p)
980{
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
983 int rc = 0;
984
985 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
986 return -EINVAL;
987
Hans Verkuil86ff7f12012-09-07 06:16:03 -0300988 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -0300989 if (dev->board.is_webcam)
990 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
991 video, g_parm, p);
992 else
993 v4l2_video_std_frame_period(dev->norm,
994 &p->parm.capture.timeperframe);
995
996 return rc;
997}
998
999static int vidioc_s_parm(struct file *file, void *priv,
1000 struct v4l2_streamparm *p)
1001{
1002 struct em28xx_fh *fh = priv;
1003 struct em28xx *dev = fh->dev;
1004
1005 if (!dev->board.is_webcam)
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001006 return -ENOTTY;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001007
1008 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1009 return -EINVAL;
1010
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001011 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001012 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1013}
1014
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001015static const char *iname[] = {
1016 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1017 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1018 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1019 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1020 [EM28XX_VMUX_SVIDEO] = "S-Video",
1021 [EM28XX_VMUX_TELEVISION] = "Television",
1022 [EM28XX_VMUX_CABLE] = "Cable TV",
1023 [EM28XX_VMUX_DVB] = "DVB",
1024 [EM28XX_VMUX_DEBUG] = "for debug only",
1025};
1026
1027static int vidioc_enum_input(struct file *file, void *priv,
1028 struct v4l2_input *i)
1029{
1030 struct em28xx_fh *fh = priv;
1031 struct em28xx *dev = fh->dev;
1032 unsigned int n;
1033
1034 n = i->index;
1035 if (n >= MAX_EM28XX_INPUT)
1036 return -EINVAL;
1037 if (0 == INPUT(n)->type)
1038 return -EINVAL;
1039
1040 i->index = n;
1041 i->type = V4L2_INPUT_TYPE_CAMERA;
1042
1043 strcpy(i->name, iname[INPUT(n)->type]);
1044
1045 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1046 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1047 i->type = V4L2_INPUT_TYPE_TUNER;
1048
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001049 i->std = dev->vdev->tvnorms;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001050 /* webcams do not have the STD API */
1051 if (dev->board.is_webcam)
1052 i->capabilities = 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001053
1054 return 0;
1055}
1056
1057static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1058{
1059 struct em28xx_fh *fh = priv;
1060 struct em28xx *dev = fh->dev;
1061
1062 *i = dev->ctl_input;
1063
1064 return 0;
1065}
1066
1067static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1068{
1069 struct em28xx_fh *fh = priv;
1070 struct em28xx *dev = fh->dev;
1071 int rc;
1072
1073 rc = check_dev(dev);
1074 if (rc < 0)
1075 return rc;
1076
1077 if (i >= MAX_EM28XX_INPUT)
1078 return -EINVAL;
1079 if (0 == INPUT(i)->type)
1080 return -EINVAL;
1081
Ezequiel García96371fc2012-03-23 18:09:34 -03001082 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001083 return 0;
1084}
1085
1086static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1087{
1088 struct em28xx_fh *fh = priv;
1089 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001091 if (!dev->audio_mode.has_audio)
1092 return -EINVAL;
1093
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001094 switch (a->index) {
1095 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001097 break;
1098 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001099 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001100 break;
1101 case EM28XX_AMUX_VIDEO2:
1102 strcpy(a->name, "Television alt");
1103 break;
1104 case EM28XX_AMUX_PHONE:
1105 strcpy(a->name, "Phone");
1106 break;
1107 case EM28XX_AMUX_MIC:
1108 strcpy(a->name, "Mic");
1109 break;
1110 case EM28XX_AMUX_CD:
1111 strcpy(a->name, "CD");
1112 break;
1113 case EM28XX_AMUX_AUX:
1114 strcpy(a->name, "Aux");
1115 break;
1116 case EM28XX_AMUX_PCM_OUT:
1117 strcpy(a->name, "PCM");
1118 break;
1119 default:
1120 return -EINVAL;
1121 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001122
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001123 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001124 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001125
1126 return 0;
1127}
1128
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001129static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001130{
1131 struct em28xx_fh *fh = priv;
1132 struct em28xx *dev = fh->dev;
1133
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001134
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001135 if (!dev->audio_mode.has_audio)
1136 return -EINVAL;
1137
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001138 if (a->index >= MAX_EM28XX_INPUT)
1139 return -EINVAL;
1140 if (0 == INPUT(a->index)->type)
1141 return -EINVAL;
1142
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001143 dev->ctl_ainput = INPUT(a->index)->amux;
1144 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001145
1146 if (!dev->ctl_aoutput)
1147 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001148
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001149 return 0;
1150}
1151
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001152static int vidioc_g_tuner(struct file *file, void *priv,
1153 struct v4l2_tuner *t)
1154{
1155 struct em28xx_fh *fh = priv;
1156 struct em28xx *dev = fh->dev;
1157 int rc;
1158
1159 rc = check_dev(dev);
1160 if (rc < 0)
1161 return rc;
1162
1163 if (0 != t->index)
1164 return -EINVAL;
1165
1166 strcpy(t->name, "Tuner");
1167
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001168 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169 return 0;
1170}
1171
1172static int vidioc_s_tuner(struct file *file, void *priv,
1173 struct v4l2_tuner *t)
1174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1177 int rc;
1178
1179 rc = check_dev(dev);
1180 if (rc < 0)
1181 return rc;
1182
1183 if (0 != t->index)
1184 return -EINVAL;
1185
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001186 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001187 return 0;
1188}
1189
1190static int vidioc_g_frequency(struct file *file, void *priv,
1191 struct v4l2_frequency *f)
1192{
1193 struct em28xx_fh *fh = priv;
1194 struct em28xx *dev = fh->dev;
1195
Hans Verkuil20deebf2012-09-06 10:07:25 -03001196 if (0 != f->tuner)
1197 return -EINVAL;
1198
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001200 return 0;
1201}
1202
1203static int vidioc_s_frequency(struct file *file, void *priv,
1204 struct v4l2_frequency *f)
1205{
1206 struct em28xx_fh *fh = priv;
1207 struct em28xx *dev = fh->dev;
1208 int rc;
1209
1210 rc = check_dev(dev);
1211 if (rc < 0)
1212 return rc;
1213
1214 if (0 != f->tuner)
1215 return -EINVAL;
1216
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001217 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuil20deebf2012-09-06 10:07:25 -03001218 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1219 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001220
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001221 return 0;
1222}
1223
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001224#ifdef CONFIG_VIDEO_ADV_DEBUG
1225static int em28xx_reg_len(int reg)
1226{
1227 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001228 case EM28XX_R40_AC97LSB:
1229 case EM28XX_R30_HSCALELOW:
1230 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001231 return 2;
1232 default:
1233 return 1;
1234 }
1235}
1236
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001237static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001238 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001239{
1240 struct em28xx_fh *fh = priv;
1241 struct em28xx *dev = fh->dev;
1242
1243 chip->ident = V4L2_IDENT_NONE;
1244 chip->revision = 0;
Hans Verkuil319a55f2012-09-06 09:53:08 -03001245 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1246 if (v4l2_chip_match_host(&chip->match))
1247 chip->ident = V4L2_IDENT_NONE;
1248 return 0;
1249 }
1250 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1251 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1252 return -EINVAL;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001253
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001254 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001255
1256 return 0;
1257}
1258
1259
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001260static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001261 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001262{
1263 struct em28xx_fh *fh = priv;
1264 struct em28xx *dev = fh->dev;
1265 int ret;
1266
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001267 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001268 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001269 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001270 if (ret < 0)
1271 return ret;
1272
1273 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001274 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001275 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001276 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001277 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001278 return 0;
1279 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001280 /* TODO: is this correct? */
1281 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1282 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001283 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001284 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001285 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001286 }
1287
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001288 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001289 reg->size = em28xx_reg_len(reg->reg);
1290 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001291 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001292
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001293 if (ret < 0)
1294 return ret;
1295
1296 reg->val = ret;
1297 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001298 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001299 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1300 reg->reg, (char *)&val, 2);
1301 if (ret < 0)
1302 return ret;
1303
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001304 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001305 }
1306
1307 return 0;
1308}
1309
1310static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001311 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001315 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001316
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001317 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001318 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001319 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001320 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001321 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001322 return 0;
1323 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001324 /* TODO: is this correct? */
1325 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1326 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001327 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001328 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001329 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001330 }
1331
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001332 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001333 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001334
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001335 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001336 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001337}
1338#endif
1339
1340
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001341static int vidioc_cropcap(struct file *file, void *priv,
1342 struct v4l2_cropcap *cc)
1343{
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
1346
1347 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1348 return -EINVAL;
1349
1350 cc->bounds.left = 0;
1351 cc->bounds.top = 0;
1352 cc->bounds.width = dev->width;
1353 cc->bounds.height = dev->height;
1354 cc->defrect = cc->bounds;
1355 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1356 cc->pixelaspect.denominator = 59;
1357
1358 return 0;
1359}
1360
1361static int vidioc_streamon(struct file *file, void *priv,
1362 enum v4l2_buf_type type)
1363{
1364 struct em28xx_fh *fh = priv;
1365 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001366 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367
1368 rc = check_dev(dev);
1369 if (rc < 0)
1370 return rc;
1371
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001372 if (unlikely(type != fh->type))
1373 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001375 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1376 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001377
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001378 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001379 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001380
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001381 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1382 rc = videobuf_streamon(&fh->vb_vidq);
1383 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1384 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001385
1386 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387}
1388
1389static int vidioc_streamoff(struct file *file, void *priv,
1390 enum v4l2_buf_type type)
1391{
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1394 int rc;
1395
1396 rc = check_dev(dev);
1397 if (rc < 0)
1398 return rc;
1399
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001400 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1401 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001402 return -EINVAL;
1403 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001404 return -EINVAL;
1405
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001406 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1407 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001408
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001409 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001410 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1411 videobuf_streamoff(&fh->vb_vidq);
1412 res_free(fh, EM28XX_RESOURCE_VIDEO);
1413 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001414 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001415 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1416 videobuf_streamoff(&fh->vb_vbiq);
1417 res_free(fh, EM28XX_RESOURCE_VBI);
1418 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001419 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001420
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421 return 0;
1422}
1423
1424static int vidioc_querycap(struct file *file, void *priv,
1425 struct v4l2_capability *cap)
1426{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001427 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001428 struct em28xx_fh *fh = priv;
1429 struct em28xx *dev = fh->dev;
1430
1431 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1432 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001433 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001434
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001435 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1436 cap->device_caps = V4L2_CAP_READWRITE |
1437 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1438 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1439 cap->device_caps = V4L2_CAP_RADIO;
1440 else
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001441 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001442
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001443 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001444 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001445
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001446 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001447 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001448
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001449 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1450 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1451 if (dev->vbi_dev)
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001452 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001453 if (dev->radio_dev)
1454 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001455 return 0;
1456}
1457
Hans Verkuil78b526a2008-05-28 12:16:41 -03001458static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001459 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001460{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001461 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462 return -EINVAL;
1463
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001464 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1465 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466
1467 return 0;
1468}
1469
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001470static int vidioc_enum_framesizes(struct file *file, void *priv,
1471 struct v4l2_frmsizeenum *fsize)
1472{
1473 struct em28xx_fh *fh = priv;
1474 struct em28xx *dev = fh->dev;
1475 struct em28xx_fmt *fmt;
1476 unsigned int maxw = norm_maxw(dev);
1477 unsigned int maxh = norm_maxh(dev);
1478
1479 fmt = format_by_fourcc(fsize->pixel_format);
1480 if (!fmt) {
1481 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1482 fsize->pixel_format);
1483 return -EINVAL;
1484 }
1485
1486 if (dev->board.is_em2800) {
1487 if (fsize->index > 1)
1488 return -EINVAL;
1489 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1490 fsize->discrete.width = maxw / (1 + fsize->index);
1491 fsize->discrete.height = maxh / (1 + fsize->index);
1492 return 0;
1493 }
1494
1495 if (fsize->index != 0)
1496 return -EINVAL;
1497
1498 /* Report a continuous range */
1499 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1500 fsize->stepwise.min_width = 48;
1501 fsize->stepwise.min_height = 32;
1502 fsize->stepwise.max_width = maxw;
1503 fsize->stepwise.max_height = maxh;
1504 fsize->stepwise.step_width = 1;
1505 fsize->stepwise.step_height = 1;
1506 return 0;
1507}
1508
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001509/* RAW VBI ioctls */
1510
1511static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1512 struct v4l2_format *format)
1513{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001514 struct em28xx_fh *fh = priv;
1515 struct em28xx *dev = fh->dev;
1516
1517 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001518 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1519 format->fmt.vbi.offset = 0;
1520 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001521 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1522 format->fmt.vbi.count[0] = dev->vbi_height;
1523 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001524
1525 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001526 if (dev->norm & V4L2_STD_525_60) {
1527 /* NTSC */
1528 format->fmt.vbi.start[0] = 10;
1529 format->fmt.vbi.start[1] = 273;
1530 } else if (dev->norm & V4L2_STD_625_50) {
1531 /* PAL */
1532 format->fmt.vbi.start[0] = 6;
1533 format->fmt.vbi.start[1] = 318;
1534 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001535
1536 return 0;
1537}
1538
1539static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1540 struct v4l2_format *format)
1541{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001542 struct em28xx_fh *fh = priv;
1543 struct em28xx *dev = fh->dev;
1544
1545 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001546 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1547 format->fmt.vbi.offset = 0;
1548 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001549 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1550 format->fmt.vbi.count[0] = dev->vbi_height;
1551 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001552
1553 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001554 if (dev->norm & V4L2_STD_525_60) {
1555 /* NTSC */
1556 format->fmt.vbi.start[0] = 10;
1557 format->fmt.vbi.start[1] = 273;
1558 } else if (dev->norm & V4L2_STD_625_50) {
1559 /* PAL */
1560 format->fmt.vbi.start[0] = 6;
1561 format->fmt.vbi.start[1] = 318;
1562 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001563
1564 return 0;
1565}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001566
1567static int vidioc_reqbufs(struct file *file, void *priv,
1568 struct v4l2_requestbuffers *rb)
1569{
1570 struct em28xx_fh *fh = priv;
1571 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001572 int rc;
1573
1574 rc = check_dev(dev);
1575 if (rc < 0)
1576 return rc;
1577
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001578 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1579 return videobuf_reqbufs(&fh->vb_vidq, rb);
1580 else
1581 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582}
1583
1584static int vidioc_querybuf(struct file *file, void *priv,
1585 struct v4l2_buffer *b)
1586{
1587 struct em28xx_fh *fh = priv;
1588 struct em28xx *dev = fh->dev;
1589 int rc;
1590
1591 rc = check_dev(dev);
1592 if (rc < 0)
1593 return rc;
1594
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001595 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1596 return videobuf_querybuf(&fh->vb_vidq, b);
1597 else {
1598 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1599 the videobuf framework, but we probably shouldn't be
1600 returning a buffer larger than that which was asked for.
1601 At a minimum, it causes a crash in zvbi since it does
1602 a memcpy based on the source buffer length */
1603 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001604 b->length = dev->vbi_width * dev->vbi_height * 2;
1605
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001606 return result;
1607 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001608}
1609
1610static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1611{
1612 struct em28xx_fh *fh = priv;
1613 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001614 int rc;
1615
1616 rc = check_dev(dev);
1617 if (rc < 0)
1618 return rc;
1619
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001620 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1621 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001622 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001623 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001624}
1625
1626static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1627{
1628 struct em28xx_fh *fh = priv;
1629 struct em28xx *dev = fh->dev;
1630 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001631
1632 rc = check_dev(dev);
1633 if (rc < 0)
1634 return rc;
1635
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001636 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1637 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1638 O_NONBLOCK);
1639 else
1640 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1641 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001642}
1643
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001644/* ----------------------------------------------------------- */
1645/* RADIO ESPECIFIC IOCTLS */
1646/* ----------------------------------------------------------- */
1647
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001648static int radio_g_tuner(struct file *file, void *priv,
1649 struct v4l2_tuner *t)
1650{
1651 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1652
1653 if (unlikely(t->index > 0))
1654 return -EINVAL;
1655
1656 strcpy(t->name, "Radio");
1657 t->type = V4L2_TUNER_RADIO;
1658
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001659 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001660
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001661 return 0;
1662}
1663
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001664static int radio_s_tuner(struct file *file, void *priv,
1665 struct v4l2_tuner *t)
1666{
1667 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1668
1669 if (0 != t->index)
1670 return -EINVAL;
1671
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001672 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001673
1674 return 0;
1675}
1676
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001677/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001678 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001679 * inits the device and starts isoc transfer
1680 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001681static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001683 int errCode = 0, radio = 0;
1684 struct video_device *vdev = video_devdata(filp);
1685 struct em28xx *dev = video_drvdata(filp);
1686 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001687 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001688 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001689
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001690 switch (vdev->vfl_type) {
1691 case VFL_TYPE_GRABBER:
1692 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1693 break;
1694 case VFL_TYPE_VBI:
1695 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1696 break;
1697 case VFL_TYPE_RADIO:
1698 radio = 1;
1699 break;
1700 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001701
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001702 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1703 video_device_node_name(vdev), v4l2_type_names[fh_type],
1704 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001705
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706
Hans Verkuil876cb142012-06-23 08:12:47 -03001707 if (mutex_lock_interruptible(&dev->lock))
1708 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001709 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001710 if (!fh) {
1711 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001712 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001713 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001715 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001716 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001717 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001718 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001719 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001720
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001721 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001722 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001723 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001724 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001726 /* Needed, since GPIO might have disabled power of
1727 some i2c device
1728 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001729 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001730
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001731 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001732 if (fh->radio) {
1733 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001734 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001735 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
1737 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001739 if (dev->progressive)
1740 field = V4L2_FIELD_NONE;
1741 else
1742 field = V4L2_FIELD_INTERLACED;
1743
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001744 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1745 NULL, &dev->slock,
1746 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001747 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001748
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001749 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1750 NULL, &dev->slock,
1751 V4L2_BUF_TYPE_VBI_CAPTURE,
1752 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001753 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03001754 mutex_unlock(&dev->lock);
Hans Verkuil69a61642012-09-07 05:52:40 -03001755 v4l2_fh_add(&fh->fh);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001756
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757 return errCode;
1758}
1759
1760/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001761 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001762 * unregisters the v4l2,i2c and usb devices
1763 * called when the device gets disconected or at module unload
1764*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001765void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001767
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001768 /*FIXME: I2C IR should be disconnected */
1769
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001770 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001771 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001772 video_unregister_device(dev->radio_dev);
1773 else
1774 video_device_release(dev->radio_dev);
1775 dev->radio_dev = NULL;
1776 }
1777 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001778 em28xx_info("V4L2 device %s deregistered\n",
1779 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001780 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001781 video_unregister_device(dev->vbi_dev);
1782 else
1783 video_device_release(dev->vbi_dev);
1784 dev->vbi_dev = NULL;
1785 }
1786 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001787 em28xx_info("V4L2 device %s deregistered\n",
1788 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001789 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001790 video_unregister_device(dev->vdev);
1791 else
1792 video_device_release(dev->vdev);
1793 dev->vdev = NULL;
1794 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795}
1796
1797/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001798 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001799 * stops streaming and deallocates all resources allocated by the v4l2
1800 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001801 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001802static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001804 struct em28xx_fh *fh = filp->private_data;
1805 struct em28xx *dev = fh->dev;
1806 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001807
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001808 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001809
Hans Verkuil876cb142012-06-23 08:12:47 -03001810 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001811 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001812 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001813 res_free(fh, EM28XX_RESOURCE_VIDEO);
1814 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001815
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001816 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1817 videobuf_stop(&fh->vb_vbiq);
1818 res_free(fh, EM28XX_RESOURCE_VBI);
1819 }
1820
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001821 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001822 /* the device is already disconnect,
1823 free the remaining resources */
1824 if (dev->state & DEV_DISCONNECTED) {
1825 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001826 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001827 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001828 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03001829 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001830 return 0;
1831 }
1832
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001833 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001834 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001835
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001836 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03001837 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001838 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001839
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001840 /* set alternate 0 */
1841 dev->alt = 0;
1842 em28xx_videodbg("setting alternate 0\n");
1843 errCode = usb_set_interface(dev->udev, 0, 0);
1844 if (errCode < 0) {
1845 em28xx_errdev("cannot change alternate number to "
1846 "0 (error=%i)\n", errCode);
1847 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001848 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001849 v4l2_fh_del(&fh->fh);
1850 v4l2_fh_exit(&fh->fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001851
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001852 videobuf_mmap_free(&fh->vb_vidq);
1853 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001854 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001855 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001856 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001857 return 0;
1858}
1859
1860/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001861 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001862 * will allocate buffers when called for the first time
1863 */
1864static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001865em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001866 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001867{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001868 struct em28xx_fh *fh = filp->private_data;
1869 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001870 int rc;
1871
1872 rc = check_dev(dev);
1873 if (rc < 0)
1874 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001875
Hans Verkuil876cb142012-06-23 08:12:47 -03001876 if (mutex_lock_interruptible(&dev->lock))
1877 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001878 /* FIXME: read() is not prepared to allow changing the video
1879 resolution while streaming. Seems a bug at em28xx_set_fmt
1880 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001881
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001882 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001883 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03001884 rc = -EBUSY;
1885 else
1886 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001887 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03001888 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001889 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03001890 rc = -EBUSY;
1891 else
1892 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001893 filp->f_flags & O_NONBLOCK);
1894 }
Hans Verkuil876cb142012-06-23 08:12:47 -03001895 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001896
Hans Verkuil876cb142012-06-23 08:12:47 -03001897 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001898}
1899
1900/*
Hans Verkuil876cb142012-06-23 08:12:47 -03001901 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902 * will allocate buffers when called for the first time
1903 */
Hans Verkuil876cb142012-06-23 08:12:47 -03001904static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001905{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001906 struct em28xx_fh *fh = filp->private_data;
Hans Verkuil50fdf402012-09-07 06:10:12 -03001907 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001908 struct em28xx *dev = fh->dev;
Hans Verkuil50fdf402012-09-07 06:10:12 -03001909 unsigned int res = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001910 int rc;
1911
1912 rc = check_dev(dev);
1913 if (rc < 0)
Hans Verkuil50fdf402012-09-07 06:10:12 -03001914 return DEFAULT_POLLMASK;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001915
Hans Verkuil50fdf402012-09-07 06:10:12 -03001916 if (v4l2_event_pending(&fh->fh))
1917 res = POLLPRI;
1918 else if (req_events & POLLPRI)
1919 poll_wait(filp, &fh->fh.wait, wait);
1920
1921 if (req_events & (POLLIN | POLLRDNORM)) {
1922 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1923 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
1924 return res | POLLERR;
1925 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1926 }
1927 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1928 if (!res_get(fh, EM28XX_RESOURCE_VBI))
1929 return res | POLLERR;
1930 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1931 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001932 }
Hans Verkuil50fdf402012-09-07 06:10:12 -03001933 return res;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001934}
1935
Hans Verkuil876cb142012-06-23 08:12:47 -03001936static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1937{
1938 struct em28xx_fh *fh = filp->private_data;
1939 struct em28xx *dev = fh->dev;
1940 unsigned int res;
1941
1942 mutex_lock(&dev->lock);
1943 res = em28xx_poll(filp, wait);
1944 mutex_unlock(&dev->lock);
1945 return res;
1946}
1947
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001948/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001949 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001950 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001951static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001952{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001953 struct em28xx_fh *fh = filp->private_data;
1954 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001955 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001956
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001957 rc = check_dev(dev);
1958 if (rc < 0)
1959 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001960
Hans Verkuil876cb142012-06-23 08:12:47 -03001961 if (mutex_lock_interruptible(&dev->lock))
1962 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03001963 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1964 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1965 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1966 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03001967 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001968
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001969 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1970 (unsigned long)vma->vm_start,
1971 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1972 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001973
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001974 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001975}
1976
Hans Verkuilbec43662008-12-30 06:58:20 -03001977static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001978 .owner = THIS_MODULE,
1979 .open = em28xx_v4l2_open,
1980 .release = em28xx_v4l2_close,
1981 .read = em28xx_v4l2_read,
1982 .poll = em28xx_v4l2_poll,
1983 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001984 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001985};
1986
Hans Verkuila3998102008-07-21 02:57:38 -03001987static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001988 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001989 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1990 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1991 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1992 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001993 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1994 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001995 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001996 .vidioc_g_audio = vidioc_g_audio,
1997 .vidioc_s_audio = vidioc_s_audio,
1998 .vidioc_cropcap = vidioc_cropcap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001999
2000 .vidioc_reqbufs = vidioc_reqbufs,
2001 .vidioc_querybuf = vidioc_querybuf,
2002 .vidioc_qbuf = vidioc_qbuf,
2003 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002004 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002005 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002006 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002007 .vidioc_g_parm = vidioc_g_parm,
2008 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002009 .vidioc_enum_input = vidioc_enum_input,
2010 .vidioc_g_input = vidioc_g_input,
2011 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012 .vidioc_streamon = vidioc_streamon,
2013 .vidioc_streamoff = vidioc_streamoff,
2014 .vidioc_g_tuner = vidioc_g_tuner,
2015 .vidioc_s_tuner = vidioc_s_tuner,
2016 .vidioc_g_frequency = vidioc_g_frequency,
2017 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03002018 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2019 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002020#ifdef CONFIG_VIDEO_ADV_DEBUG
2021 .vidioc_g_register = vidioc_g_register,
2022 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002023 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002024#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002025};
2026
2027static const struct video_device em28xx_video_template = {
2028 .fops = &em28xx_v4l_fops,
2029 .release = video_device_release,
2030 .ioctl_ops = &video_ioctl_ops,
2031
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002032 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002033};
2034
Hans Verkuilbec43662008-12-30 06:58:20 -03002035static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002036 .owner = THIS_MODULE,
2037 .open = em28xx_v4l2_open,
2038 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002039 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002040};
2041
2042static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03002043 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002044 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002045 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002046 .vidioc_g_frequency = vidioc_g_frequency,
2047 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03002048 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2049 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002050#ifdef CONFIG_VIDEO_ADV_DEBUG
2051 .vidioc_g_register = vidioc_g_register,
2052 .vidioc_s_register = vidioc_s_register,
2053#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002054};
2055
Hans Verkuila3998102008-07-21 02:57:38 -03002056static struct video_device em28xx_radio_template = {
2057 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002058 .fops = &radio_fops,
2059 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002060};
2061
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002062/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002063
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002064
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002065
Adrian Bunk532fe652008-01-28 22:10:48 -03002066static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002067 const struct video_device *template,
2068 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002069{
2070 struct video_device *vfd;
2071
2072 vfd = video_device_alloc();
2073 if (NULL == vfd)
2074 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002075
2076 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002077 vfd->v4l2_dev = &dev->v4l2_dev;
2078 vfd->release = video_device_release;
2079 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002080 vfd->lock = &dev->lock;
Hans Verkuil69a61642012-09-07 05:52:40 -03002081 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuild8c95c02012-09-07 07:31:54 -03002082 if (dev->board.is_webcam)
2083 vfd->tvnorms = 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002084
2085 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2086 dev->name, type_name);
2087
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002088 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002089 return vfd;
2090}
2091
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002092int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002093{
Hans Verkuil081b9452012-09-07 05:43:59 -03002094 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002095 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002096 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002097
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002098 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2099 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002100
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002101 /* set default norm */
Hans Verkuild8c95c02012-09-07 07:31:54 -03002102 dev->norm = V4L2_STD_PAL;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002103 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002104 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002105
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002106 /* Analog specific initialization */
2107 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002108
2109 maxw = norm_maxw(dev);
2110 /* MaxPacketSize for em2800 is too small to capture at full resolution
2111 * use half of maxw as the scaler can only scale to 50% */
2112 if (dev->board.is_em2800)
2113 maxw /= 2;
2114
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002115 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002116 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002117
Ezequiel García96371fc2012-03-23 18:09:34 -03002118 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002119
2120 /* Audio defaults */
2121 dev->mute = 1;
2122 dev->volume = 0x1f;
2123
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002124/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002125 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2126 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2127 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002128
2129 em28xx_set_outfmt(dev);
2130 em28xx_colorlevels_set_default(dev);
2131 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002132
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002133 /* allocate and fill video video_device struct */
2134 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2135 if (!dev->vdev) {
2136 em28xx_errdev("cannot allocate video_device.\n");
2137 return -ENODEV;
2138 }
2139
2140 /* register v4l2 video video_device */
2141 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2142 video_nr[dev->devno]);
2143 if (ret) {
2144 em28xx_errdev("unable to register video device (error=%i).\n",
2145 ret);
2146 return ret;
2147 }
2148
2149 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002150 if (em28xx_vbi_supported(dev) == 1) {
2151 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2152 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002153
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002154 /* register v4l2 vbi video_device */
2155 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2156 vbi_nr[dev->devno]);
2157 if (ret < 0) {
2158 em28xx_errdev("unable to register vbi device\n");
2159 return ret;
2160 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002161 }
2162
2163 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002164 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2165 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002166 if (!dev->radio_dev) {
2167 em28xx_errdev("cannot allocate video_device.\n");
2168 return -ENODEV;
2169 }
2170 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2171 radio_nr[dev->devno]);
2172 if (ret < 0) {
2173 em28xx_errdev("can't register radio device\n");
2174 return ret;
2175 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002176 em28xx_info("Registered radio device as %s\n",
2177 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002178 }
2179
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002180 em28xx_info("V4L2 video device registered as %s\n",
2181 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002182
2183 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002184 em28xx_info("V4L2 VBI device registered as %s\n",
2185 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002186
2187 return 0;
2188}