blob: acdb4340399f42bd002d6cb4be6cd8ae7936e1d1 [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
912 rc = check_dev(dev);
913 if (rc < 0)
914 return rc;
915
916 *norm = dev->norm;
917
918 return 0;
919}
920
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300921static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
922{
923 struct em28xx_fh *fh = priv;
924 struct em28xx *dev = fh->dev;
925 int rc;
926
927 rc = check_dev(dev);
928 if (rc < 0)
929 return rc;
930
931 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
932
933 return 0;
934}
935
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300936static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300937{
938 struct em28xx_fh *fh = priv;
939 struct em28xx *dev = fh->dev;
940 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941 int rc;
942
943 rc = check_dev(dev);
944 if (rc < 0)
945 return rc;
946
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300947 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300948
949 /* Adjusts width/height, if needed */
950 f.fmt.pix.width = dev->width;
951 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300952 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300953
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300954 /* set new image size */
955 dev->width = f.fmt.pix.width;
956 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300957 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
958
959 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300960 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 return 0;
963}
964
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -0300965static int vidioc_g_parm(struct file *file, void *priv,
966 struct v4l2_streamparm *p)
967{
968 struct em28xx_fh *fh = priv;
969 struct em28xx *dev = fh->dev;
970 int rc = 0;
971
972 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
973 return -EINVAL;
974
975 if (dev->board.is_webcam)
976 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
977 video, g_parm, p);
978 else
979 v4l2_video_std_frame_period(dev->norm,
980 &p->parm.capture.timeperframe);
981
982 return rc;
983}
984
985static int vidioc_s_parm(struct file *file, void *priv,
986 struct v4l2_streamparm *p)
987{
988 struct em28xx_fh *fh = priv;
989 struct em28xx *dev = fh->dev;
990
991 if (!dev->board.is_webcam)
992 return -EINVAL;
993
994 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
995 return -EINVAL;
996
997 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
998}
999
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001000static const char *iname[] = {
1001 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1002 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1003 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1004 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1005 [EM28XX_VMUX_SVIDEO] = "S-Video",
1006 [EM28XX_VMUX_TELEVISION] = "Television",
1007 [EM28XX_VMUX_CABLE] = "Cable TV",
1008 [EM28XX_VMUX_DVB] = "DVB",
1009 [EM28XX_VMUX_DEBUG] = "for debug only",
1010};
1011
1012static int vidioc_enum_input(struct file *file, void *priv,
1013 struct v4l2_input *i)
1014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 unsigned int n;
1018
1019 n = i->index;
1020 if (n >= MAX_EM28XX_INPUT)
1021 return -EINVAL;
1022 if (0 == INPUT(n)->type)
1023 return -EINVAL;
1024
1025 i->index = n;
1026 i->type = V4L2_INPUT_TYPE_CAMERA;
1027
1028 strcpy(i->name, iname[INPUT(n)->type]);
1029
1030 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1031 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1032 i->type = V4L2_INPUT_TYPE_TUNER;
1033
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001034 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035
1036 return 0;
1037}
1038
1039static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1040{
1041 struct em28xx_fh *fh = priv;
1042 struct em28xx *dev = fh->dev;
1043
1044 *i = dev->ctl_input;
1045
1046 return 0;
1047}
1048
1049static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1050{
1051 struct em28xx_fh *fh = priv;
1052 struct em28xx *dev = fh->dev;
1053 int rc;
1054
1055 rc = check_dev(dev);
1056 if (rc < 0)
1057 return rc;
1058
1059 if (i >= MAX_EM28XX_INPUT)
1060 return -EINVAL;
1061 if (0 == INPUT(i)->type)
1062 return -EINVAL;
1063
Ezequiel García96371fc2012-03-23 18:09:34 -03001064 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001065 return 0;
1066}
1067
1068static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1069{
1070 struct em28xx_fh *fh = priv;
1071 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001072
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001073 if (!dev->audio_mode.has_audio)
1074 return -EINVAL;
1075
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001076 switch (a->index) {
1077 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001079 break;
1080 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001081 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001082 break;
1083 case EM28XX_AMUX_VIDEO2:
1084 strcpy(a->name, "Television alt");
1085 break;
1086 case EM28XX_AMUX_PHONE:
1087 strcpy(a->name, "Phone");
1088 break;
1089 case EM28XX_AMUX_MIC:
1090 strcpy(a->name, "Mic");
1091 break;
1092 case EM28XX_AMUX_CD:
1093 strcpy(a->name, "CD");
1094 break;
1095 case EM28XX_AMUX_AUX:
1096 strcpy(a->name, "Aux");
1097 break;
1098 case EM28XX_AMUX_PCM_OUT:
1099 strcpy(a->name, "PCM");
1100 break;
1101 default:
1102 return -EINVAL;
1103 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001104
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001105 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001106 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001107
1108 return 0;
1109}
1110
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001111static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001112{
1113 struct em28xx_fh *fh = priv;
1114 struct em28xx *dev = fh->dev;
1115
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001116
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001117 if (!dev->audio_mode.has_audio)
1118 return -EINVAL;
1119
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001120 if (a->index >= MAX_EM28XX_INPUT)
1121 return -EINVAL;
1122 if (0 == INPUT(a->index)->type)
1123 return -EINVAL;
1124
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001125 dev->ctl_ainput = INPUT(a->index)->amux;
1126 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001127
1128 if (!dev->ctl_aoutput)
1129 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001130
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001131 return 0;
1132}
1133
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134static int vidioc_g_tuner(struct file *file, void *priv,
1135 struct v4l2_tuner *t)
1136{
1137 struct em28xx_fh *fh = priv;
1138 struct em28xx *dev = fh->dev;
1139 int rc;
1140
1141 rc = check_dev(dev);
1142 if (rc < 0)
1143 return rc;
1144
1145 if (0 != t->index)
1146 return -EINVAL;
1147
1148 strcpy(t->name, "Tuner");
1149
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001150 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001151 return 0;
1152}
1153
1154static int vidioc_s_tuner(struct file *file, void *priv,
1155 struct v4l2_tuner *t)
1156{
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 int rc;
1160
1161 rc = check_dev(dev);
1162 if (rc < 0)
1163 return rc;
1164
1165 if (0 != t->index)
1166 return -EINVAL;
1167
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001168 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169 return 0;
1170}
1171
1172static int vidioc_g_frequency(struct file *file, void *priv,
1173 struct v4l2_frequency *f)
1174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1177
Hans Verkuil20deebf2012-09-06 10:07:25 -03001178 if (0 != f->tuner)
1179 return -EINVAL;
1180
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001182 return 0;
1183}
1184
1185static int vidioc_s_frequency(struct file *file, void *priv,
1186 struct v4l2_frequency *f)
1187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190 int rc;
1191
1192 rc = check_dev(dev);
1193 if (rc < 0)
1194 return rc;
1195
1196 if (0 != f->tuner)
1197 return -EINVAL;
1198
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001199 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuil20deebf2012-09-06 10:07:25 -03001200 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1201 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001202
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203 return 0;
1204}
1205
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001206#ifdef CONFIG_VIDEO_ADV_DEBUG
1207static int em28xx_reg_len(int reg)
1208{
1209 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001210 case EM28XX_R40_AC97LSB:
1211 case EM28XX_R30_HSCALELOW:
1212 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001213 return 2;
1214 default:
1215 return 1;
1216 }
1217}
1218
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001219static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001220 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001221{
1222 struct em28xx_fh *fh = priv;
1223 struct em28xx *dev = fh->dev;
1224
1225 chip->ident = V4L2_IDENT_NONE;
1226 chip->revision = 0;
Hans Verkuil319a55f2012-09-06 09:53:08 -03001227 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1228 if (v4l2_chip_match_host(&chip->match))
1229 chip->ident = V4L2_IDENT_NONE;
1230 return 0;
1231 }
1232 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1233 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1234 return -EINVAL;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001235
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001236 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001237
1238 return 0;
1239}
1240
1241
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001242static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001243 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001244{
1245 struct em28xx_fh *fh = priv;
1246 struct em28xx *dev = fh->dev;
1247 int ret;
1248
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001249 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001250 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001251 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001252 if (ret < 0)
1253 return ret;
1254
1255 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001256 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001257 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001258 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001259 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001260 return 0;
1261 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001262 /* TODO: is this correct? */
1263 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1264 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001265 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001266 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001267 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001268 }
1269
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001270 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001271 reg->size = em28xx_reg_len(reg->reg);
1272 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001273 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001274
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001275 if (ret < 0)
1276 return ret;
1277
1278 reg->val = ret;
1279 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001280 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001281 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1282 reg->reg, (char *)&val, 2);
1283 if (ret < 0)
1284 return ret;
1285
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001286 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001287 }
1288
1289 return 0;
1290}
1291
1292static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001293 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001294{
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001297 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001298
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001299 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001300 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001301 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001302 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001303 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001304 return 0;
1305 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001306 /* TODO: is this correct? */
1307 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1308 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001309 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001310 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001311 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001312 }
1313
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001314 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001315 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001316
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001317 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001318 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001319}
1320#endif
1321
1322
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001323static int vidioc_cropcap(struct file *file, void *priv,
1324 struct v4l2_cropcap *cc)
1325{
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
1328
1329 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1330 return -EINVAL;
1331
1332 cc->bounds.left = 0;
1333 cc->bounds.top = 0;
1334 cc->bounds.width = dev->width;
1335 cc->bounds.height = dev->height;
1336 cc->defrect = cc->bounds;
1337 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1338 cc->pixelaspect.denominator = 59;
1339
1340 return 0;
1341}
1342
1343static int vidioc_streamon(struct file *file, void *priv,
1344 enum v4l2_buf_type type)
1345{
1346 struct em28xx_fh *fh = priv;
1347 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001348 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001349
1350 rc = check_dev(dev);
1351 if (rc < 0)
1352 return rc;
1353
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001354 if (unlikely(type != fh->type))
1355 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001357 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1358 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001359
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001360 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001361 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001362
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001363 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1364 rc = videobuf_streamon(&fh->vb_vidq);
1365 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1366 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001367
1368 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001369}
1370
1371static int vidioc_streamoff(struct file *file, void *priv,
1372 enum v4l2_buf_type type)
1373{
1374 struct em28xx_fh *fh = priv;
1375 struct em28xx *dev = fh->dev;
1376 int rc;
1377
1378 rc = check_dev(dev);
1379 if (rc < 0)
1380 return rc;
1381
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001382 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1383 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001384 return -EINVAL;
1385 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001386 return -EINVAL;
1387
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001388 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1389 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001390
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001391 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001392 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1393 videobuf_streamoff(&fh->vb_vidq);
1394 res_free(fh, EM28XX_RESOURCE_VIDEO);
1395 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001396 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001397 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1398 videobuf_streamoff(&fh->vb_vbiq);
1399 res_free(fh, EM28XX_RESOURCE_VBI);
1400 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001401 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001402
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001403 return 0;
1404}
1405
1406static int vidioc_querycap(struct file *file, void *priv,
1407 struct v4l2_capability *cap)
1408{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001409 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001410 struct em28xx_fh *fh = priv;
1411 struct em28xx *dev = fh->dev;
1412
1413 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1414 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001415 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001416
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001417 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1418 cap->device_caps = V4L2_CAP_READWRITE |
1419 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1420 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1421 cap->device_caps = V4L2_CAP_RADIO;
1422 else
1423 cap->device_caps = V4L2_CAP_READWRITE |
1424 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001425
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001426 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001427 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001428
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001429 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001430 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001431
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001432 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1433 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1434 if (dev->vbi_dev)
1435 cap->capabilities |=
1436 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
1437 if (dev->radio_dev)
1438 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 return 0;
1440}
1441
Hans Verkuil78b526a2008-05-28 12:16:41 -03001442static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001443 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001444{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001445 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001446 return -EINVAL;
1447
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001448 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1449 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001450
1451 return 0;
1452}
1453
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001454static int vidioc_enum_framesizes(struct file *file, void *priv,
1455 struct v4l2_frmsizeenum *fsize)
1456{
1457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
1459 struct em28xx_fmt *fmt;
1460 unsigned int maxw = norm_maxw(dev);
1461 unsigned int maxh = norm_maxh(dev);
1462
1463 fmt = format_by_fourcc(fsize->pixel_format);
1464 if (!fmt) {
1465 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1466 fsize->pixel_format);
1467 return -EINVAL;
1468 }
1469
1470 if (dev->board.is_em2800) {
1471 if (fsize->index > 1)
1472 return -EINVAL;
1473 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1474 fsize->discrete.width = maxw / (1 + fsize->index);
1475 fsize->discrete.height = maxh / (1 + fsize->index);
1476 return 0;
1477 }
1478
1479 if (fsize->index != 0)
1480 return -EINVAL;
1481
1482 /* Report a continuous range */
1483 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1484 fsize->stepwise.min_width = 48;
1485 fsize->stepwise.min_height = 32;
1486 fsize->stepwise.max_width = maxw;
1487 fsize->stepwise.max_height = maxh;
1488 fsize->stepwise.step_width = 1;
1489 fsize->stepwise.step_height = 1;
1490 return 0;
1491}
1492
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001493/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001494static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001495 struct v4l2_format *f)
1496{
1497 struct em28xx_fh *fh = priv;
1498 struct em28xx *dev = fh->dev;
1499 int rc;
1500
1501 rc = check_dev(dev);
1502 if (rc < 0)
1503 return rc;
1504
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001505 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001506 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001507
1508 if (f->fmt.sliced.service_set == 0)
1509 rc = -EINVAL;
1510
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001511 return rc;
1512}
1513
Hans Verkuil78b526a2008-05-28 12:16:41 -03001514static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001515 struct v4l2_format *f)
1516{
1517 struct em28xx_fh *fh = priv;
1518 struct em28xx *dev = fh->dev;
1519 int rc;
1520
1521 rc = check_dev(dev);
1522 if (rc < 0)
1523 return rc;
1524
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001525 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001526
1527 if (f->fmt.sliced.service_set == 0)
1528 return -EINVAL;
1529
1530 return 0;
1531}
1532
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001533/* RAW VBI ioctls */
1534
1535static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1536 struct v4l2_format *format)
1537{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001538 struct em28xx_fh *fh = priv;
1539 struct em28xx *dev = fh->dev;
1540
1541 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001542 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1543 format->fmt.vbi.offset = 0;
1544 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001545 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1546 format->fmt.vbi.count[0] = dev->vbi_height;
1547 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001548
1549 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001550 if (dev->norm & V4L2_STD_525_60) {
1551 /* NTSC */
1552 format->fmt.vbi.start[0] = 10;
1553 format->fmt.vbi.start[1] = 273;
1554 } else if (dev->norm & V4L2_STD_625_50) {
1555 /* PAL */
1556 format->fmt.vbi.start[0] = 6;
1557 format->fmt.vbi.start[1] = 318;
1558 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001559
1560 return 0;
1561}
1562
1563static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1564 struct v4l2_format *format)
1565{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001566 struct em28xx_fh *fh = priv;
1567 struct em28xx *dev = fh->dev;
1568
1569 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001570 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1571 format->fmt.vbi.offset = 0;
1572 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001573 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1574 format->fmt.vbi.count[0] = dev->vbi_height;
1575 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001576
1577 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001578 if (dev->norm & V4L2_STD_525_60) {
1579 /* NTSC */
1580 format->fmt.vbi.start[0] = 10;
1581 format->fmt.vbi.start[1] = 273;
1582 } else if (dev->norm & V4L2_STD_625_50) {
1583 /* PAL */
1584 format->fmt.vbi.start[0] = 6;
1585 format->fmt.vbi.start[1] = 318;
1586 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001587
1588 return 0;
1589}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001590
1591static int vidioc_reqbufs(struct file *file, void *priv,
1592 struct v4l2_requestbuffers *rb)
1593{
1594 struct em28xx_fh *fh = priv;
1595 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001596 int rc;
1597
1598 rc = check_dev(dev);
1599 if (rc < 0)
1600 return rc;
1601
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001602 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1603 return videobuf_reqbufs(&fh->vb_vidq, rb);
1604 else
1605 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001606}
1607
1608static int vidioc_querybuf(struct file *file, void *priv,
1609 struct v4l2_buffer *b)
1610{
1611 struct em28xx_fh *fh = priv;
1612 struct em28xx *dev = fh->dev;
1613 int rc;
1614
1615 rc = check_dev(dev);
1616 if (rc < 0)
1617 return rc;
1618
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001619 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1620 return videobuf_querybuf(&fh->vb_vidq, b);
1621 else {
1622 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1623 the videobuf framework, but we probably shouldn't be
1624 returning a buffer larger than that which was asked for.
1625 At a minimum, it causes a crash in zvbi since it does
1626 a memcpy based on the source buffer length */
1627 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001628 b->length = dev->vbi_width * dev->vbi_height * 2;
1629
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001630 return result;
1631 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001632}
1633
1634static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1635{
1636 struct em28xx_fh *fh = priv;
1637 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001638 int rc;
1639
1640 rc = check_dev(dev);
1641 if (rc < 0)
1642 return rc;
1643
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001644 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1645 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001646 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001647 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001648}
1649
1650static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1651{
1652 struct em28xx_fh *fh = priv;
1653 struct em28xx *dev = fh->dev;
1654 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001655
1656 rc = check_dev(dev);
1657 if (rc < 0)
1658 return rc;
1659
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001660 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1661 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1662 O_NONBLOCK);
1663 else
1664 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1665 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001666}
1667
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001668/* ----------------------------------------------------------- */
1669/* RADIO ESPECIFIC IOCTLS */
1670/* ----------------------------------------------------------- */
1671
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001672static int radio_g_tuner(struct file *file, void *priv,
1673 struct v4l2_tuner *t)
1674{
1675 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1676
1677 if (unlikely(t->index > 0))
1678 return -EINVAL;
1679
1680 strcpy(t->name, "Radio");
1681 t->type = V4L2_TUNER_RADIO;
1682
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001683 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001684
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001685 return 0;
1686}
1687
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001688static int radio_s_tuner(struct file *file, void *priv,
1689 struct v4l2_tuner *t)
1690{
1691 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1692
1693 if (0 != t->index)
1694 return -EINVAL;
1695
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001696 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001697
1698 return 0;
1699}
1700
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001701/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001702 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001703 * inits the device and starts isoc transfer
1704 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001705static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001707 int errCode = 0, radio = 0;
1708 struct video_device *vdev = video_devdata(filp);
1709 struct em28xx *dev = video_drvdata(filp);
1710 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001712 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001713
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001714 switch (vdev->vfl_type) {
1715 case VFL_TYPE_GRABBER:
1716 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1717 break;
1718 case VFL_TYPE_VBI:
1719 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1720 break;
1721 case VFL_TYPE_RADIO:
1722 radio = 1;
1723 break;
1724 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001725
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001726 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1727 video_device_node_name(vdev), v4l2_type_names[fh_type],
1728 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001729
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Hans Verkuil876cb142012-06-23 08:12:47 -03001731 if (mutex_lock_interruptible(&dev->lock))
1732 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001733 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001734 if (!fh) {
1735 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001736 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001737 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001739 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001740 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001741 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001742 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001743 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001744
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001745 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001746 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001747 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001748 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001749
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001750 /* Needed, since GPIO might have disabled power of
1751 some i2c device
1752 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001753 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001754
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001755 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001756 if (fh->radio) {
1757 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001758 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001759 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
1761 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001762
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001763 if (dev->progressive)
1764 field = V4L2_FIELD_NONE;
1765 else
1766 field = V4L2_FIELD_INTERLACED;
1767
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001768 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1769 NULL, &dev->slock,
1770 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001771 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001772
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001773 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1774 NULL, &dev->slock,
1775 V4L2_BUF_TYPE_VBI_CAPTURE,
1776 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001777 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03001778 mutex_unlock(&dev->lock);
Hans Verkuil69a61642012-09-07 05:52:40 -03001779 v4l2_fh_add(&fh->fh);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001780
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781 return errCode;
1782}
1783
1784/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001785 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001786 * unregisters the v4l2,i2c and usb devices
1787 * called when the device gets disconected or at module unload
1788*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001789void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001791
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001792 /*FIXME: I2C IR should be disconnected */
1793
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001794 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001795 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001796 video_unregister_device(dev->radio_dev);
1797 else
1798 video_device_release(dev->radio_dev);
1799 dev->radio_dev = NULL;
1800 }
1801 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001802 em28xx_info("V4L2 device %s deregistered\n",
1803 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001804 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001805 video_unregister_device(dev->vbi_dev);
1806 else
1807 video_device_release(dev->vbi_dev);
1808 dev->vbi_dev = NULL;
1809 }
1810 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001811 em28xx_info("V4L2 device %s deregistered\n",
1812 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001813 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001814 video_unregister_device(dev->vdev);
1815 else
1816 video_device_release(dev->vdev);
1817 dev->vdev = NULL;
1818 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001819}
1820
1821/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001822 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001823 * stops streaming and deallocates all resources allocated by the v4l2
1824 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001826static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001827{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001828 struct em28xx_fh *fh = filp->private_data;
1829 struct em28xx *dev = fh->dev;
1830 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001831
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001832 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001833
Hans Verkuil876cb142012-06-23 08:12:47 -03001834 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001835 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001836 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001837 res_free(fh, EM28XX_RESOURCE_VIDEO);
1838 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001839
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001840 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1841 videobuf_stop(&fh->vb_vbiq);
1842 res_free(fh, EM28XX_RESOURCE_VBI);
1843 }
1844
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001845 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001846 /* the device is already disconnect,
1847 free the remaining resources */
1848 if (dev->state & DEV_DISCONNECTED) {
1849 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001850 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001851 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001852 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03001853 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001854 return 0;
1855 }
1856
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001857 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001858 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001859
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001860 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03001861 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001862 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001863
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001864 /* set alternate 0 */
1865 dev->alt = 0;
1866 em28xx_videodbg("setting alternate 0\n");
1867 errCode = usb_set_interface(dev->udev, 0, 0);
1868 if (errCode < 0) {
1869 em28xx_errdev("cannot change alternate number to "
1870 "0 (error=%i)\n", errCode);
1871 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001872 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001873 v4l2_fh_del(&fh->fh);
1874 v4l2_fh_exit(&fh->fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001875
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001876 videobuf_mmap_free(&fh->vb_vidq);
1877 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001878 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001879 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001880 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001881 return 0;
1882}
1883
1884/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001885 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886 * will allocate buffers when called for the first time
1887 */
1888static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001889em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001890 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001891{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001892 struct em28xx_fh *fh = filp->private_data;
1893 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001894 int rc;
1895
1896 rc = check_dev(dev);
1897 if (rc < 0)
1898 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001899
Hans Verkuil876cb142012-06-23 08:12:47 -03001900 if (mutex_lock_interruptible(&dev->lock))
1901 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001902 /* FIXME: read() is not prepared to allow changing the video
1903 resolution while streaming. Seems a bug at em28xx_set_fmt
1904 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001905
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001906 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001907 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03001908 rc = -EBUSY;
1909 else
1910 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001911 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03001912 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001913 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03001914 rc = -EBUSY;
1915 else
1916 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001917 filp->f_flags & O_NONBLOCK);
1918 }
Hans Verkuil876cb142012-06-23 08:12:47 -03001919 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001920
Hans Verkuil876cb142012-06-23 08:12:47 -03001921 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001922}
1923
1924/*
Hans Verkuil876cb142012-06-23 08:12:47 -03001925 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001926 * will allocate buffers when called for the first time
1927 */
Hans Verkuil876cb142012-06-23 08:12:47 -03001928static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001929{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001930 struct em28xx_fh *fh = filp->private_data;
Hans Verkuil50fdf402012-09-07 06:10:12 -03001931 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001932 struct em28xx *dev = fh->dev;
Hans Verkuil50fdf402012-09-07 06:10:12 -03001933 unsigned int res = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001934 int rc;
1935
1936 rc = check_dev(dev);
1937 if (rc < 0)
Hans Verkuil50fdf402012-09-07 06:10:12 -03001938 return DEFAULT_POLLMASK;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001939
Hans Verkuil50fdf402012-09-07 06:10:12 -03001940 if (v4l2_event_pending(&fh->fh))
1941 res = POLLPRI;
1942 else if (req_events & POLLPRI)
1943 poll_wait(filp, &fh->fh.wait, wait);
1944
1945 if (req_events & (POLLIN | POLLRDNORM)) {
1946 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1947 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
1948 return res | POLLERR;
1949 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1950 }
1951 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1952 if (!res_get(fh, EM28XX_RESOURCE_VBI))
1953 return res | POLLERR;
1954 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1955 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001956 }
Hans Verkuil50fdf402012-09-07 06:10:12 -03001957 return res;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001958}
1959
Hans Verkuil876cb142012-06-23 08:12:47 -03001960static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1961{
1962 struct em28xx_fh *fh = filp->private_data;
1963 struct em28xx *dev = fh->dev;
1964 unsigned int res;
1965
1966 mutex_lock(&dev->lock);
1967 res = em28xx_poll(filp, wait);
1968 mutex_unlock(&dev->lock);
1969 return res;
1970}
1971
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001972/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001973 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001974 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001975static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001976{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001977 struct em28xx_fh *fh = filp->private_data;
1978 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001979 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001980
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001981 rc = check_dev(dev);
1982 if (rc < 0)
1983 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001984
Hans Verkuil876cb142012-06-23 08:12:47 -03001985 if (mutex_lock_interruptible(&dev->lock))
1986 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03001987 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1988 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1989 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1990 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03001991 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001992
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001993 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1994 (unsigned long)vma->vm_start,
1995 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1996 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001997
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001998 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001999}
2000
Hans Verkuilbec43662008-12-30 06:58:20 -03002001static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002002 .owner = THIS_MODULE,
2003 .open = em28xx_v4l2_open,
2004 .release = em28xx_v4l2_close,
2005 .read = em28xx_v4l2_read,
2006 .poll = em28xx_v4l2_poll,
2007 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002008 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002009};
2010
Hans Verkuila3998102008-07-21 02:57:38 -03002011static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002013 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2014 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2015 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2016 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002017 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2018 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002019 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002020 .vidioc_g_audio = vidioc_g_audio,
2021 .vidioc_s_audio = vidioc_s_audio,
2022 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002023 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2024 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2025 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002026
2027 .vidioc_reqbufs = vidioc_reqbufs,
2028 .vidioc_querybuf = vidioc_querybuf,
2029 .vidioc_qbuf = vidioc_qbuf,
2030 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002031 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002032 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002033 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002034 .vidioc_g_parm = vidioc_g_parm,
2035 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002036 .vidioc_enum_input = vidioc_enum_input,
2037 .vidioc_g_input = vidioc_g_input,
2038 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002039 .vidioc_streamon = vidioc_streamon,
2040 .vidioc_streamoff = vidioc_streamoff,
2041 .vidioc_g_tuner = vidioc_g_tuner,
2042 .vidioc_s_tuner = vidioc_s_tuner,
2043 .vidioc_g_frequency = vidioc_g_frequency,
2044 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03002045 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2046 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002047#ifdef CONFIG_VIDEO_ADV_DEBUG
2048 .vidioc_g_register = vidioc_g_register,
2049 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002050 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002051#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002052};
2053
2054static const struct video_device em28xx_video_template = {
2055 .fops = &em28xx_v4l_fops,
2056 .release = video_device_release,
2057 .ioctl_ops = &video_ioctl_ops,
2058
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002059 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002060 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002061};
2062
Hans Verkuilbec43662008-12-30 06:58:20 -03002063static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002064 .owner = THIS_MODULE,
2065 .open = em28xx_v4l2_open,
2066 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002067 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002068};
2069
2070static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03002071 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002072 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002073 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002074 .vidioc_g_frequency = vidioc_g_frequency,
2075 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03002076 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2077 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002078#ifdef CONFIG_VIDEO_ADV_DEBUG
2079 .vidioc_g_register = vidioc_g_register,
2080 .vidioc_s_register = vidioc_s_register,
2081#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002082};
2083
Hans Verkuila3998102008-07-21 02:57:38 -03002084static struct video_device em28xx_radio_template = {
2085 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002086 .fops = &radio_fops,
2087 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002088};
2089
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002090/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002092
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002093
Adrian Bunk532fe652008-01-28 22:10:48 -03002094static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002095 const struct video_device *template,
2096 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002097{
2098 struct video_device *vfd;
2099
2100 vfd = video_device_alloc();
2101 if (NULL == vfd)
2102 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002103
2104 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002105 vfd->v4l2_dev = &dev->v4l2_dev;
2106 vfd->release = video_device_release;
2107 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002108 vfd->lock = &dev->lock;
Hans Verkuil69a61642012-09-07 05:52:40 -03002109 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002110
2111 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2112 dev->name, type_name);
2113
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002114 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002115 return vfd;
2116}
2117
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002118int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002119{
Hans Verkuil081b9452012-09-07 05:43:59 -03002120 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002121 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002122 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002123
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002124 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2125 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002126
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002127 /* set default norm */
2128 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002129 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002130 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002131
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002132 /* Analog specific initialization */
2133 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002134
2135 maxw = norm_maxw(dev);
2136 /* MaxPacketSize for em2800 is too small to capture at full resolution
2137 * use half of maxw as the scaler can only scale to 50% */
2138 if (dev->board.is_em2800)
2139 maxw /= 2;
2140
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002141 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002142 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002143
Ezequiel García96371fc2012-03-23 18:09:34 -03002144 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002145
2146 /* Audio defaults */
2147 dev->mute = 1;
2148 dev->volume = 0x1f;
2149
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002150/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002151 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2152 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2153 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002154
2155 em28xx_set_outfmt(dev);
2156 em28xx_colorlevels_set_default(dev);
2157 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002158
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002159 /* allocate and fill video video_device struct */
2160 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2161 if (!dev->vdev) {
2162 em28xx_errdev("cannot allocate video_device.\n");
2163 return -ENODEV;
2164 }
2165
2166 /* register v4l2 video video_device */
2167 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2168 video_nr[dev->devno]);
2169 if (ret) {
2170 em28xx_errdev("unable to register video device (error=%i).\n",
2171 ret);
2172 return ret;
2173 }
2174
2175 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002176 if (em28xx_vbi_supported(dev) == 1) {
2177 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2178 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002179
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002180 /* register v4l2 vbi video_device */
2181 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2182 vbi_nr[dev->devno]);
2183 if (ret < 0) {
2184 em28xx_errdev("unable to register vbi device\n");
2185 return ret;
2186 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002187 }
2188
2189 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002190 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2191 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002192 if (!dev->radio_dev) {
2193 em28xx_errdev("cannot allocate video_device.\n");
2194 return -ENODEV;
2195 }
2196 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2197 radio_nr[dev->devno]);
2198 if (ret < 0) {
2199 em28xx_errdev("can't register radio device\n");
2200 return ret;
2201 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002202 em28xx_info("Registered radio device as %s\n",
2203 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002204 }
2205
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002206 em28xx_info("V4L2 video device registered as %s\n",
2207 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002208
2209 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002210 em28xx_info("V4L2 VBI device registered as %s\n",
2211 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002212
2213 return 0;
2214}