blob: 1a577ed8ea0ca0163e9203e9db0a143ccd65a743 [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>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
Frank Schaefer43a20d72013-02-11 14:07:55 -030054#define EM28XX_VERSION "0.2.0"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Frank Schaefere507e0e2013-03-26 13:38:38 -030078
79#define EM25XX_FRMDATAHDR_BYTE1 0x02
80#define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
81#define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
82#define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
83#define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
84 EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
85 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
86
87
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -030088static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
89static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
90static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030091
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020092module_param_array(video_nr, int, NULL, 0444);
93module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030094module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030095MODULE_PARM_DESC(video_nr, "video device numbers");
96MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
97MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080098
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030099static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300100module_param(video_debug, int, 0644);
101MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800102
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300103/* supported video standards */
104static struct em28xx_fmt format[] = {
105 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300106 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300107 .fourcc = V4L2_PIX_FMT_YUYV,
108 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -0300109 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300110 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300111 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300112 .fourcc = V4L2_PIX_FMT_RGB565,
113 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300114 .reg = EM28XX_OUTFMT_RGB_16_656,
115 }, {
116 .name = "8 bpp Bayer BGBG..GRGR",
117 .fourcc = V4L2_PIX_FMT_SBGGR8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
120 }, {
121 .name = "8 bpp Bayer GRGR..BGBG",
122 .fourcc = V4L2_PIX_FMT_SGRBG8,
123 .depth = 8,
124 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
125 }, {
126 .name = "8 bpp Bayer GBGB..RGRG",
127 .fourcc = V4L2_PIX_FMT_SGBRG8,
128 .depth = 8,
129 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
130 }, {
131 .name = "12 bpp YUV411",
132 .fourcc = V4L2_PIX_FMT_YUV411P,
133 .depth = 12,
134 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300135 },
136};
137
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300138/* ------------------------------------------------------------------
139 DMA and thread functions
140 ------------------------------------------------------------------*/
141
142/*
Frank Schaefer948a49a2012-12-08 11:31:25 -0300143 * Finish the current buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300144 */
Frank Schaefer948a49a2012-12-08 11:31:25 -0300145static inline void finish_buffer(struct em28xx *dev,
146 struct em28xx_buffer *buf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300147{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300148 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
149
150 buf->vb.v4l2_buf.sequence = dev->field_count++;
151 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
152 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
153
154 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300155}
156
157/*
Frank Schaefer36016a32012-12-08 11:31:32 -0300158 * Copy picture data from USB buffer to videobuf buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 */
160static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300162 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300163 unsigned long len)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300164{
165 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300166 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300167 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300169 if (buf->pos + len > buf->length)
170 len = buf->length - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300171
Frank Schaefer36016a32012-12-08 11:31:32 -0300172 startread = usb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300173 remain = len;
174
Frank Schaeferc02ec712012-11-08 14:11:33 -0300175 if (dev->progressive || buf->top_field)
Frank Schaefer36016a32012-12-08 11:31:32 -0300176 fieldstart = buf->vb_buf;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300177 else /* interlaced mode, even nr. of lines */
Frank Schaefer36016a32012-12-08 11:31:32 -0300178 fieldstart = buf->vb_buf + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300179
Frank Schaefer87325332012-12-08 11:31:27 -0300180 linesdone = buf->pos / bytesperline;
181 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300182
183 if (dev->progressive)
184 offset = linesdone * bytesperline + currlinedone;
185 else
186 offset = linesdone * bytesperline * 2 + currlinedone;
187
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300189 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300190 lencopy = lencopy > remain ? remain : lencopy;
191
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300192 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300193 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Frank Schaefer36016a32012-12-08 11:31:32 -0300194 ((char *)startwrite + lencopy) -
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300195 ((char *)buf->vb_buf + buf->length));
196 remain = (char *)buf->vb_buf + buf->length -
Frank Schaefer36016a32012-12-08 11:31:32 -0300197 (char *)startwrite;
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300198 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300199 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300200 if (lencopy <= 0)
201 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300202 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
204 remain -= lencopy;
205
206 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300207 if (dev->progressive)
208 startwrite += lencopy;
209 else
210 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300212 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300213 lencopy = remain;
214 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300215 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
Frank Schaefer36016a32012-12-08 11:31:32 -0300217 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300218 buf->length) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300219 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
220 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300221 ((char *)startwrite + lencopy) -
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300222 ((char *)buf->vb_buf + buf->length));
223 lencopy = remain = (char *)buf->vb_buf + buf->length -
224 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300225 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300226 if (lencopy <= 0)
227 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300228
229 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230
231 remain -= lencopy;
232 }
233
Frank Schaefer87325332012-12-08 11:31:27 -0300234 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300235}
236
Frank Schaefer36016a32012-12-08 11:31:32 -0300237/*
238 * Copy VBI data from USB buffer to videobuf buffer
239 */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300240static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300241 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300242 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300243 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300244{
Frank Schaefer36016a32012-12-08 11:31:32 -0300245 unsigned int offset;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300246
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300247 if (buf->pos + len > buf->length)
248 len = buf->length - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300249
Frank Schaefer87325332012-12-08 11:31:27 -0300250 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300251 /* Make sure the bottom field populates the second half of the frame */
Frank Schaefer36016a32012-12-08 11:31:32 -0300252 if (buf->top_field == 0)
253 offset += dev->vbi_width * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300254
Frank Schaefer36016a32012-12-08 11:31:32 -0300255 memcpy(buf->vb_buf + offset, usb_buf, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300256 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300257}
258
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300259static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300260 int packet, int status)
261{
262 char *errmsg = "Unknown";
263
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300264 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265 case -ENOENT:
266 errmsg = "unlinked synchronuously";
267 break;
268 case -ECONNRESET:
269 errmsg = "unlinked asynchronuously";
270 break;
271 case -ENOSR:
272 errmsg = "Buffer error (overrun)";
273 break;
274 case -EPIPE:
275 errmsg = "Stalled (device not responding)";
276 break;
277 case -EOVERFLOW:
278 errmsg = "Babble (bad cable?)";
279 break;
280 case -EPROTO:
281 errmsg = "Bit-stuff error (bad cable?)";
282 break;
283 case -EILSEQ:
284 errmsg = "CRC/Timeout (could be anything)";
285 break;
286 case -ETIME:
287 errmsg = "Device does not respond";
288 break;
289 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300290 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300291 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
292 } else {
293 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
294 packet, status, errmsg);
295 }
296}
297
298/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300299 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300301static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
302 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300303{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300304 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300305
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300306 if (list_empty(&dma_q->active)) {
307 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300308 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300309 }
310
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300311 /* Get the next buffer */
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300312 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300313 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300314 list_del(&buf->list);
Frank Schaefer87325332012-12-08 11:31:27 -0300315 buf->pos = 0;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300316 buf->vb_buf = buf->mem;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300317
Frank Schaefer24a6d842012-12-08 11:31:24 -0300318 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300319}
320
Frank Schaefere04c00d2012-12-08 11:31:30 -0300321/*
322 * Finish the current buffer if completed and prepare for the next field
323 */
324static struct em28xx_buffer *
325finish_field_prepare_next(struct em28xx *dev,
326 struct em28xx_buffer *buf,
327 struct em28xx_dmaqueue *dma_q)
328{
329 if (dev->progressive || dev->top_field) { /* Brand new frame */
330 if (buf != NULL)
331 finish_buffer(dev, buf);
332 buf = get_next_buf(dev, dma_q);
333 }
334 if (buf != NULL) {
335 buf->top_field = dev->top_field;
336 buf->pos = 0;
337 }
338
339 return buf;
340}
341
Frank Schaefer227b7c92012-12-08 11:31:31 -0300342/*
343 * Process data packet according to the em2710/em2750/em28xx frame data format
344 */
345static inline void process_frame_data_em28xx(struct em28xx *dev,
346 unsigned char *data_pkt,
347 unsigned int data_len)
348{
349 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
350 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
351 struct em28xx_dmaqueue *dma_q = &dev->vidq;
352 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
353
354 /* capture type 0 = vbi start
355 capture type 1 = vbi in progress
356 capture type 2 = video start
357 capture type 3 = video in progress */
358 if (data_len >= 4) {
359 /* NOTE: Headers are always 4 bytes and
360 * never split across packets */
361 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
362 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
363 /* Continuation */
364 data_pkt += 4;
365 data_len -= 4;
366 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
367 /* Field start (VBI mode) */
368 dev->capture_type = 0;
369 dev->vbi_read = 0;
370 em28xx_isocdbg("VBI START HEADER !!!\n");
371 dev->top_field = !(data_pkt[2] & 1);
372 data_pkt += 4;
373 data_len -= 4;
374 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
375 /* Field start (VBI disabled) */
376 dev->capture_type = 2;
377 em28xx_isocdbg("VIDEO START HEADER !!!\n");
378 dev->top_field = !(data_pkt[2] & 1);
379 data_pkt += 4;
380 data_len -= 4;
381 }
382 }
383 /* NOTE: With bulk transfers, intermediate data packets
384 * have no continuation header */
385
386 if (dev->capture_type == 0) {
387 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
388 dev->usb_ctl.vbi_buf = vbi_buf;
389 dev->capture_type = 1;
390 }
391
392 if (dev->capture_type == 1) {
393 int vbi_size = dev->vbi_width * dev->vbi_height;
394 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
395 (vbi_size - dev->vbi_read) : data_len;
396
397 /* Copy VBI data */
398 if (vbi_buf != NULL)
399 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
400 dev->vbi_read += vbi_data_len;
401
402 if (vbi_data_len < data_len) {
403 /* Continue with copying video data */
404 dev->capture_type = 2;
405 data_pkt += vbi_data_len;
406 data_len -= vbi_data_len;
407 }
408 }
409
410 if (dev->capture_type == 2) {
411 buf = finish_field_prepare_next(dev, buf, dma_q);
412 dev->usb_ctl.vid_buf = buf;
413 dev->capture_type = 3;
414 }
415
416 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
417 em28xx_copy_video(dev, buf, data_pkt, data_len);
418}
419
Frank Schaefere507e0e2013-03-26 13:38:38 -0300420/*
421 * Process data packet according to the em25xx/em276x/7x/8x frame data format
422 */
423static inline void process_frame_data_em25xx(struct em28xx *dev,
424 unsigned char *data_pkt,
425 unsigned int data_len)
426{
427 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
428 struct em28xx_dmaqueue *dmaq = &dev->vidq;
429 bool frame_end = 0;
430
431 /* Check for header */
432 /* NOTE: at least with bulk transfers, only the first packet
433 * has a header and has always set the FRAME_END bit */
434 if (data_len >= 2) { /* em25xx header is only 2 bytes long */
435 if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
436 ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
437 dev->top_field = !(data_pkt[1] &
438 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
439 frame_end = data_pkt[1] &
440 EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
441 data_pkt += 2;
442 data_len -= 2;
443 }
444
445 /* Finish field and prepare next (BULK only) */
446 if (dev->analog_xfer_bulk && frame_end) {
447 buf = finish_field_prepare_next(dev, buf, dmaq);
448 dev->usb_ctl.vid_buf = buf;
449 }
450 /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
451 * we COULD already prepare a buffer here to avoid skipping the
452 * first frame.
453 */
454 }
455
456 /* Copy data */
457 if (buf != NULL && data_len > 0)
458 em28xx_copy_video(dev, buf, data_pkt, data_len);
459
460 /* Finish frame (ISOC only) => avoids lag of 1 frame */
461 if (!dev->analog_xfer_bulk && frame_end) {
462 buf = finish_field_prepare_next(dev, buf, dmaq);
463 dev->usb_ctl.vid_buf = buf;
464 }
465
466 /* NOTE: Tested with USB bulk transfers only !
467 * The wording in the datasheet suggests that isoc might work different.
468 * The current code assumes that with isoc transfers each packet has a
469 * header like with the other em28xx devices.
470 */
471 /* NOTE: Support for interlaced mode is pure theory. It has not been
472 * tested and it is unknown if these devices actually support it. */
473 /* NOTE: No VBI support yet (these chips likely do not support VBI). */
474}
475
Frank Schaefer960da932012-11-25 06:37:37 -0300476/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300477static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300478{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300479 int xfer_bulk, num_packets, i;
480 unsigned char *usb_data_pkt;
481 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300482
483 if (!dev)
484 return 0;
485
Frank Schaefer2665c292012-12-27 19:02:43 -0300486 if (dev->disconnected)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300487 return 0;
488
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300489 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300490 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300491
Frank Schaefer4601cc32012-11-08 14:11:46 -0300492 xfer_bulk = usb_pipebulk(urb->pipe);
493
Frank Schaefer4601cc32012-11-08 14:11:46 -0300494 if (xfer_bulk) /* bulk */
495 num_packets = 1;
496 else /* isoc */
497 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300498
Frank Schaefer4601cc32012-11-08 14:11:46 -0300499 for (i = 0; i < num_packets; i++) {
500 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300501 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300502
Frank Schaefer227b7c92012-12-08 11:31:31 -0300503 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300504 } else { /* isoc */
505 if (urb->iso_frame_desc[i].status < 0) {
506 print_err_status(dev, i,
507 urb->iso_frame_desc[i].status);
508 if (urb->iso_frame_desc[i].status != -EPROTO)
509 continue;
510 }
511
Frank Schaefer227b7c92012-12-08 11:31:31 -0300512 usb_data_len = urb->iso_frame_desc[i].actual_length;
513 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300514 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300515 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300516 }
517
Frank Schaefer227b7c92012-12-08 11:31:31 -0300518 usb_data_pkt = urb->transfer_buffer +
519 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300520 }
521
Frank Schaefer227b7c92012-12-08 11:31:31 -0300522 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300523 /* NOTE: happens very often with isoc transfers */
524 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300525 continue;
526 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300527
Frank Schaefere507e0e2013-03-26 13:38:38 -0300528 if (dev->is_em25xx)
529 process_frame_data_em25xx(dev,
530 usb_data_pkt, usb_data_len);
531 else
532 process_frame_data_em28xx(dev,
533 usb_data_pkt, usb_data_len);
534
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300535 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300536 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300537}
538
539
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300540static int get_ressource(enum v4l2_buf_type f_type)
541{
542 switch (f_type) {
543 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
544 return EM28XX_RESOURCE_VIDEO;
545 case V4L2_BUF_TYPE_VBI_CAPTURE:
546 return EM28XX_RESOURCE_VBI;
547 default:
548 BUG();
549 return 0;
550 }
551}
552
553/* Usage lock check functions */
554static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
555{
556 int res_type = get_ressource(f_type);
557
558 /* is it free? */
559 if (dev->resources & res_type) {
560 /* no, someone else uses it */
561 return -EBUSY;
562 }
563
564 /* it's free, grab it */
565 dev->resources |= res_type;
566 em28xx_videodbg("res: get %d\n", res_type);
567 return 0;
568}
569
570static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
571{
572 int res_type = get_ressource(f_type);
573
574 dev->resources &= ~res_type;
575 em28xx_videodbg("res: put %d\n", res_type);
576}
577
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300578/* ------------------------------------------------------------------
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300579 Videobuf2 operations
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300580 ------------------------------------------------------------------*/
581
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300582static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
583 unsigned int *nbuffers, unsigned int *nplanes,
584 unsigned int sizes[], void *alloc_ctxs[])
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300585{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300586 struct em28xx *dev = vb2_get_drv_priv(vq);
587 unsigned long size;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300588
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300589 if (fmt)
590 size = fmt->fmt.pix.sizeimage;
591 else
592 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300593
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300594 if (size == 0)
595 return -EINVAL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300596
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300597 if (0 == *nbuffers)
598 *nbuffers = 32;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300599
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300600 *nplanes = 1;
601 sizes[0] = size;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300602
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300603 return 0;
604}
605
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300606static int
607buffer_prepare(struct vb2_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300608{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300609 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
610 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
611 unsigned long size;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300612
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300613 em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300614
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300615 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300616
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300617 if (vb2_plane_size(vb, 0) < size) {
618 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
619 __func__, vb2_plane_size(vb, 0), size);
620 return -EINVAL;
621 }
622 vb2_set_plane_payload(&buf->vb, 0, size);
623
624 return 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300625}
626
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300627int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300628{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300629 struct em28xx *dev = vb2_get_drv_priv(vq);
630 struct v4l2_frequency f;
631 int rc = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300633 em28xx_videodbg("%s\n", __func__);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300634
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300635 /* Make sure streaming is not already in progress for this type
636 of filehandle (e.g. video, vbi) */
637 rc = res_get(dev, vq->type);
638 if (rc)
639 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300640
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300641 if (dev->streaming_users++ == 0) {
642 /* First active streaming user, so allocate all the URBs */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300643
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300644 /* Allocate the USB bandwidth */
645 em28xx_set_alternate(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300646
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300647 /* Needed, since GPIO might have disabled power of
648 some i2c device
649 */
650 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300651
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300652 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300653 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
654 dev->analog_xfer_bulk,
655 EM28XX_NUM_BUFS,
656 dev->max_pkt_size,
657 dev->packet_multiplier,
658 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300659 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300660 goto fail;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300661
662 /*
663 * djh: it's not clear whether this code is still needed. I'm
664 * leaving it in here for now entirely out of concern for
665 * backward compatibility (the old code did it)
666 */
667
668 /* Ask tuner to go to analog or radio mode */
669 memset(&f, 0, sizeof(f));
670 f.frequency = dev->ctl_freq;
671 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
672 f.type = V4L2_TUNER_RADIO;
673 else
674 f.type = V4L2_TUNER_ANALOG_TV;
675 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300676 }
677
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300678fail:
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679 return rc;
680}
681
Mauro Carvalho Chehab3a799c22013-01-05 01:27:21 -0200682static int em28xx_stop_streaming(struct vb2_queue *vq)
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300683{
684 struct em28xx *dev = vb2_get_drv_priv(vq);
685 struct em28xx_dmaqueue *vidq = &dev->vidq;
686 unsigned long flags = 0;
687
688 em28xx_videodbg("%s\n", __func__);
689
690 res_free(dev, vq->type);
691
692 if (dev->streaming_users-- == 1) {
693 /* Last active user, so shutdown all the URBS */
694 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
695 }
696
697 spin_lock_irqsave(&dev->slock, flags);
698 while (!list_empty(&vidq->active)) {
699 struct em28xx_buffer *buf;
700 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
701 list_del(&buf->list);
702 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
703 }
704 dev->usb_ctl.vid_buf = NULL;
705 spin_unlock_irqrestore(&dev->slock, flags);
706
707 return 0;
708}
709
710int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
711{
712 struct em28xx *dev = vb2_get_drv_priv(vq);
713 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
714 unsigned long flags = 0;
715
716 em28xx_videodbg("%s\n", __func__);
717
718 res_free(dev, vq->type);
719
720 if (dev->streaming_users-- == 1) {
721 /* Last active user, so shutdown all the URBS */
722 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
723 }
724
725 spin_lock_irqsave(&dev->slock, flags);
726 while (!list_empty(&vbiq->active)) {
727 struct em28xx_buffer *buf;
728 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
729 list_del(&buf->list);
730 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
731 }
732 dev->usb_ctl.vbi_buf = NULL;
733 spin_unlock_irqrestore(&dev->slock, flags);
734
735 return 0;
736}
737
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300738static void
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300739buffer_queue(struct vb2_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300740{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300741 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
742 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
743 struct em28xx_dmaqueue *vidq = &dev->vidq;
744 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300745
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300746 em28xx_videodbg("%s\n", __func__);
747 buf->mem = vb2_plane_vaddr(vb, 0);
748 buf->length = vb2_plane_size(vb, 0);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300749
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300750 spin_lock_irqsave(&dev->slock, flags);
751 list_add_tail(&buf->list, &vidq->active);
752 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300753}
754
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300755static struct vb2_ops em28xx_video_qops = {
756 .queue_setup = queue_setup,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300757 .buf_prepare = buffer_prepare,
758 .buf_queue = buffer_queue,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300759 .start_streaming = em28xx_start_analog_streaming,
760 .stop_streaming = em28xx_stop_streaming,
761 .wait_prepare = vb2_ops_wait_prepare,
762 .wait_finish = vb2_ops_wait_finish,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800764
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300765int em28xx_vb2_setup(struct em28xx *dev)
766{
767 int rc;
768 struct vb2_queue *q;
769
770 /* Setup Videobuf2 for Video capture */
771 q = &dev->vb_vidq;
772 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabef85cd92013-01-06 00:34:22 -0200773 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Frank Schaefer195281d2013-03-09 06:53:01 -0300774 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300775 q->drv_priv = dev;
776 q->buf_struct_size = sizeof(struct em28xx_buffer);
777 q->ops = &em28xx_video_qops;
778 q->mem_ops = &vb2_vmalloc_memops;
779
780 rc = vb2_queue_init(q);
781 if (rc < 0)
782 return rc;
783
784 /* Setup Videobuf2 for VBI capture */
785 q = &dev->vb_vbiq;
786 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
787 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
Frank Schaefer195281d2013-03-09 06:53:01 -0300788 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300789 q->drv_priv = dev;
790 q->buf_struct_size = sizeof(struct em28xx_buffer);
791 q->ops = &em28xx_vbi_qops;
792 q->mem_ops = &vb2_vmalloc_memops;
793
794 rc = vb2_queue_init(q);
795 if (rc < 0)
796 return rc;
797
798 return 0;
799}
800
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300801/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800802
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800803static void video_mux(struct em28xx *dev, int index)
804{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800805 dev->ctl_input = index;
806 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300807 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800808
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300809 if (!dev->ctl_aoutput)
810 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
811
Hans Verkuil5325b422009-04-02 11:26:22 -0300812 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
813 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800814
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300815 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300816 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300817 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
818 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300819 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300820 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300821 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
822 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800823 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300824
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300825 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300826 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
827 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300828 }
829
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300830 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800831}
832
Hans Verkuil081b9452012-09-07 05:43:59 -0300833void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300834{
Hans Verkuil081b9452012-09-07 05:43:59 -0300835 struct em28xx *dev = priv;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300836
Hans Verkuil081b9452012-09-07 05:43:59 -0300837 /*
838 * In the case of non-AC97 volume controls, we still need
839 * to do some setups at em28xx, in order to mute/unmute
840 * and to adjust audio volume. However, the value ranges
841 * should be checked by the corresponding V4L subdriver.
842 */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300843 switch (ctrl->id) {
844 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil081b9452012-09-07 05:43:59 -0300845 dev->mute = ctrl->val;
846 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300847 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300848 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil081b9452012-09-07 05:43:59 -0300849 dev->volume = ctrl->val;
850 em28xx_audio_analog_set(dev);
851 break;
852 }
853}
854
855static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
856{
857 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300858 int ret = -EINVAL;
Hans Verkuil081b9452012-09-07 05:43:59 -0300859
860 switch (ctrl->id) {
861 case V4L2_CID_AUDIO_MUTE:
862 dev->mute = ctrl->val;
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300863 ret = em28xx_audio_analog_set(dev);
Hans Verkuil081b9452012-09-07 05:43:59 -0300864 break;
865 case V4L2_CID_AUDIO_VOLUME:
866 dev->volume = ctrl->val;
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300867 ret = em28xx_audio_analog_set(dev);
868 break;
869 case V4L2_CID_CONTRAST:
870 ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
871 break;
872 case V4L2_CID_BRIGHTNESS:
873 ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
874 break;
875 case V4L2_CID_SATURATION:
876 ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
877 break;
878 case V4L2_CID_BLUE_BALANCE:
879 ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
880 break;
881 case V4L2_CID_RED_BALANCE:
882 ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
883 break;
884 case V4L2_CID_SHARPNESS:
885 ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300886 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300887 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300888
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300889 return (ret < 0) ? ret : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300890}
891
Hans Verkuil081b9452012-09-07 05:43:59 -0300892const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
893 .s_ctrl = em28xx_s_ctrl,
894};
895
Frank Schaefer6b09a212013-02-10 16:05:12 -0300896static void size_to_scale(struct em28xx *dev,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300897 unsigned int width, unsigned int height,
898 unsigned int *hscale, unsigned int *vscale)
899{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300900 unsigned int maxw = norm_maxw(dev);
901 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300902
903 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
Frank Schaefer81685322013-02-10 16:05:11 -0300904 if (*hscale > EM28XX_HVSCALE_MAX)
905 *hscale = EM28XX_HVSCALE_MAX;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300906
907 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
Frank Schaefer81685322013-02-10 16:05:11 -0300908 if (*vscale > EM28XX_HVSCALE_MAX)
909 *vscale = EM28XX_HVSCALE_MAX;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910}
911
Frank Schaeferb8374132013-02-10 16:05:13 -0300912static void scale_to_size(struct em28xx *dev,
913 unsigned int hscale, unsigned int vscale,
914 unsigned int *width, unsigned int *height)
915{
916 unsigned int maxw = norm_maxw(dev);
917 unsigned int maxh = norm_maxh(dev);
918
919 *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
920 *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
921}
922
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300923/* ------------------------------------------------------------------
924 IOCTL vidioc handling
925 ------------------------------------------------------------------*/
926
Hans Verkuil78b526a2008-05-28 12:16:41 -0300927static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300928 struct v4l2_format *f)
929{
930 struct em28xx_fh *fh = priv;
931 struct em28xx *dev = fh->dev;
932
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933 f->fmt.pix.width = dev->width;
934 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300935 f->fmt.pix.pixelformat = dev->format->fourcc;
936 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300937 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
939
940 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300941 if (dev->progressive)
942 f->fmt.pix.field = V4L2_FIELD_NONE;
943 else
944 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300945 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300946 return 0;
947}
948
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300949static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
950{
951 unsigned int i;
952
953 for (i = 0; i < ARRAY_SIZE(format); i++)
954 if (format[i].fourcc == fourcc)
955 return &format[i];
956
957 return NULL;
958}
959
Hans Verkuil78b526a2008-05-28 12:16:41 -0300960static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961 struct v4l2_format *f)
962{
963 struct em28xx_fh *fh = priv;
964 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300965 unsigned int width = f->fmt.pix.width;
966 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967 unsigned int maxw = norm_maxw(dev);
968 unsigned int maxh = norm_maxh(dev);
969 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300970 struct em28xx_fmt *fmt;
971
972 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
973 if (!fmt) {
974 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
975 f->fmt.pix.pixelformat);
976 return -EINVAL;
977 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300978
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300979 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300981 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
982 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300983 /*
984 * MaxPacketSize for em2800 is too small to capture at full
985 * resolution use half of maxw as the scaler can only scale
986 * to 50%
987 */
Sascha Sommer1020d132012-01-08 16:54:28 -0300988 if (width == maxw && height == maxh)
989 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300990 } else {
991 /* width must even because of the YUYV format
992 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300993 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
994 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300995 }
996
Frank Schaefer6b09a212013-02-10 16:05:12 -0300997 size_to_scale(dev, width, height, &hscale, &vscale);
Hans Verkuil46f85972013-03-30 05:31:42 -0300998 scale_to_size(dev, hscale, vscale, &width, &height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300999
1000 f->fmt.pix.width = width;
1001 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001002 f->fmt.pix.pixelformat = fmt->fourcc;
Hans Verkuile6066db2013-02-06 08:14:47 -03001003 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001004 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001006 if (dev->progressive)
1007 f->fmt.pix.field = V4L2_FIELD_NONE;
1008 else
1009 f->fmt.pix.field = dev->interlaced ?
1010 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001011
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012 return 0;
1013}
1014
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001015static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1016 unsigned width, unsigned height)
1017{
1018 struct em28xx_fmt *fmt;
1019
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001020 fmt = format_by_fourcc(fourcc);
1021 if (!fmt)
1022 return -EINVAL;
1023
1024 dev->format = fmt;
1025 dev->width = width;
1026 dev->height = height;
1027
1028 /* set new image size */
Frank Schaefer6b09a212013-02-10 16:05:12 -03001029 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001030
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001031 em28xx_resolution_set(dev);
1032
1033 return 0;
1034}
1035
Hans Verkuil78b526a2008-05-28 12:16:41 -03001036static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001037 struct v4l2_format *f)
1038{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001039 struct em28xx *dev = video_drvdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001040
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001041 if (dev->streaming_users > 0)
1042 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001043
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001044 vidioc_try_fmt_vid_cap(file, priv, f);
1045
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001046 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001047 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001048}
1049
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001050static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1051{
1052 struct em28xx_fh *fh = priv;
1053 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001054
1055 *norm = dev->norm;
1056
1057 return 0;
1058}
1059
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001060static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1061{
1062 struct em28xx_fh *fh = priv;
1063 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001064
1065 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1066
1067 return 0;
1068}
1069
Hans Verkuil314527a2013-03-15 06:10:40 -03001070static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001071{
1072 struct em28xx_fh *fh = priv;
1073 struct em28xx *dev = fh->dev;
1074 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001075
Hans Verkuil314527a2013-03-15 06:10:40 -03001076 if (norm == dev->norm)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001077 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001079 if (dev->streaming_users > 0)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001080 return -EBUSY;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001081
Hans Verkuil314527a2013-03-15 06:10:40 -03001082 dev->norm = norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001083
1084 /* Adjusts width/height, if needed */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001085 f.fmt.pix.width = 720;
Hans Verkuil314527a2013-03-15 06:10:40 -03001086 f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001087 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001088
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001089 /* set new image size */
1090 dev->width = f.fmt.pix.width;
1091 dev->height = f.fmt.pix.height;
Frank Schaefer6b09a212013-02-10 16:05:12 -03001092 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093
1094 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001095 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001097 return 0;
1098}
1099
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001100static int vidioc_g_parm(struct file *file, void *priv,
1101 struct v4l2_streamparm *p)
1102{
1103 struct em28xx_fh *fh = priv;
1104 struct em28xx *dev = fh->dev;
1105 int rc = 0;
1106
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001107 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001108 if (dev->board.is_webcam)
1109 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1110 video, g_parm, p);
1111 else
1112 v4l2_video_std_frame_period(dev->norm,
1113 &p->parm.capture.timeperframe);
1114
1115 return rc;
1116}
1117
1118static int vidioc_s_parm(struct file *file, void *priv,
1119 struct v4l2_streamparm *p)
1120{
1121 struct em28xx_fh *fh = priv;
1122 struct em28xx *dev = fh->dev;
1123
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001124 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001125 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1126}
1127
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001128static const char *iname[] = {
1129 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1130 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1131 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1132 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1133 [EM28XX_VMUX_SVIDEO] = "S-Video",
1134 [EM28XX_VMUX_TELEVISION] = "Television",
1135 [EM28XX_VMUX_CABLE] = "Cable TV",
1136 [EM28XX_VMUX_DVB] = "DVB",
1137 [EM28XX_VMUX_DEBUG] = "for debug only",
1138};
1139
1140static int vidioc_enum_input(struct file *file, void *priv,
1141 struct v4l2_input *i)
1142{
1143 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev;
1145 unsigned int n;
1146
1147 n = i->index;
1148 if (n >= MAX_EM28XX_INPUT)
1149 return -EINVAL;
1150 if (0 == INPUT(n)->type)
1151 return -EINVAL;
1152
1153 i->index = n;
1154 i->type = V4L2_INPUT_TYPE_CAMERA;
1155
1156 strcpy(i->name, iname[INPUT(n)->type]);
1157
1158 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1159 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1160 i->type = V4L2_INPUT_TYPE_TUNER;
1161
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001162 i->std = dev->vdev->tvnorms;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001163 /* webcams do not have the STD API */
1164 if (dev->board.is_webcam)
1165 i->capabilities = 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001166
1167 return 0;
1168}
1169
1170static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1171{
1172 struct em28xx_fh *fh = priv;
1173 struct em28xx *dev = fh->dev;
1174
1175 *i = dev->ctl_input;
1176
1177 return 0;
1178}
1179
1180static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1181{
1182 struct em28xx_fh *fh = priv;
1183 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184
1185 if (i >= MAX_EM28XX_INPUT)
1186 return -EINVAL;
1187 if (0 == INPUT(i)->type)
1188 return -EINVAL;
1189
Ezequiel García96371fc2012-03-23 18:09:34 -03001190 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001191 return 0;
1192}
1193
1194static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1195{
1196 struct em28xx_fh *fh = priv;
1197 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001199 switch (a->index) {
1200 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001201 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001202 break;
1203 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001205 break;
1206 case EM28XX_AMUX_VIDEO2:
1207 strcpy(a->name, "Television alt");
1208 break;
1209 case EM28XX_AMUX_PHONE:
1210 strcpy(a->name, "Phone");
1211 break;
1212 case EM28XX_AMUX_MIC:
1213 strcpy(a->name, "Mic");
1214 break;
1215 case EM28XX_AMUX_CD:
1216 strcpy(a->name, "CD");
1217 break;
1218 case EM28XX_AMUX_AUX:
1219 strcpy(a->name, "Aux");
1220 break;
1221 case EM28XX_AMUX_PCM_OUT:
1222 strcpy(a->name, "PCM");
1223 break;
1224 default:
1225 return -EINVAL;
1226 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001227
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001228 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001229 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230
1231 return 0;
1232}
1233
Hans Verkuil0e8025b2012-09-04 11:59:31 -03001234static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001235{
1236 struct em28xx_fh *fh = priv;
1237 struct em28xx *dev = fh->dev;
1238
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001239 if (a->index >= MAX_EM28XX_INPUT)
1240 return -EINVAL;
1241 if (0 == INPUT(a->index)->type)
1242 return -EINVAL;
1243
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001244 dev->ctl_ainput = INPUT(a->index)->amux;
1245 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001246
1247 if (!dev->ctl_aoutput)
1248 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001249
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001250 return 0;
1251}
1252
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253static int vidioc_g_tuner(struct file *file, void *priv,
1254 struct v4l2_tuner *t)
1255{
1256 struct em28xx_fh *fh = priv;
1257 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001258
1259 if (0 != t->index)
1260 return -EINVAL;
1261
1262 strcpy(t->name, "Tuner");
1263
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001264 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001265 return 0;
1266}
1267
1268static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001269 const struct v4l2_tuner *t)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001270{
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001273
1274 if (0 != t->index)
1275 return -EINVAL;
1276
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001277 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001278 return 0;
1279}
1280
1281static int vidioc_g_frequency(struct file *file, void *priv,
1282 struct v4l2_frequency *f)
1283{
1284 struct em28xx_fh *fh = priv;
1285 struct em28xx *dev = fh->dev;
1286
Hans Verkuil20deebf2012-09-06 10:07:25 -03001287 if (0 != f->tuner)
1288 return -EINVAL;
1289
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291 return 0;
1292}
1293
1294static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001295 const struct v4l2_frequency *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296{
Hans Verkuilb530a442013-03-19 04:09:26 -03001297 struct v4l2_frequency new_freq = *f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001300
1301 if (0 != f->tuner)
1302 return -EINVAL;
1303
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001304 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -03001305 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1306 dev->ctl_freq = new_freq.frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001308 return 0;
1309}
1310
Hans Verkuilcd634f12013-03-27 08:04:23 -03001311#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -03001312static int vidioc_g_chip_info(struct file *file, void *priv,
1313 struct v4l2_dbg_chip_info *chip)
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001314{
1315 struct em28xx_fh *fh = priv;
1316 struct em28xx *dev = fh->dev;
1317
1318 if (chip->match.addr > 1)
1319 return -EINVAL;
1320 if (chip->match.addr == 1)
1321 strlcpy(chip->name, "ac97", sizeof(chip->name));
1322 else
1323 strlcpy(chip->name, dev->v4l2_dev.name, sizeof(chip->name));
1324 return 0;
1325}
1326
Frank Schaefer35deba32013-02-07 13:39:19 -03001327static int em28xx_reg_len(int reg)
1328{
1329 switch (reg) {
1330 case EM28XX_R40_AC97LSB:
1331 case EM28XX_R30_HSCALELOW:
1332 case EM28XX_R32_VSCALELOW:
1333 return 2;
1334 default:
1335 return 1;
1336 }
1337}
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001338
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001339static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001340 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001341{
1342 struct em28xx_fh *fh = priv;
1343 struct em28xx *dev = fh->dev;
1344 int ret;
1345
Hans Verkuilabca2052013-05-29 06:59:35 -03001346 if (reg->match.addr > 1)
1347 return -EINVAL;
1348 if (reg->match.addr) {
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001349 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001350 if (ret < 0)
1351 return ret;
1352
1353 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001354 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001355 return 0;
1356 }
1357
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001358 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001359 reg->size = em28xx_reg_len(reg->reg);
1360 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001361 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001362
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001363 if (ret < 0)
1364 return ret;
1365
1366 reg->val = ret;
1367 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001368 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001369 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1370 reg->reg, (char *)&val, 2);
1371 if (ret < 0)
1372 return ret;
1373
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001374 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001375 }
1376
1377 return 0;
1378}
1379
1380static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001381 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001382{
1383 struct em28xx_fh *fh = priv;
1384 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001385 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001386
Hans Verkuilabca2052013-05-29 06:59:35 -03001387 if (reg->match.addr > 1)
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001388 return -EINVAL;
Hans Verkuilabca2052013-05-29 06:59:35 -03001389 if (reg->match.addr)
1390 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001391
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001392 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001393 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001394
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001395 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001396 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001397}
1398#endif
1399
1400
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401static int vidioc_querycap(struct file *file, void *priv,
1402 struct v4l2_capability *cap)
1403{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001404 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001405 struct em28xx_fh *fh = priv;
1406 struct em28xx *dev = fh->dev;
1407
1408 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1409 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001410 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001412 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1413 cap->device_caps = V4L2_CAP_READWRITE |
1414 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1415 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1416 cap->device_caps = V4L2_CAP_RADIO;
1417 else
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001418 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001419
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001420 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001421 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001422
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001423 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001424 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001425
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001426 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1427 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1428 if (dev->vbi_dev)
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001429 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001430 if (dev->radio_dev)
1431 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432 return 0;
1433}
1434
Hans Verkuil78b526a2008-05-28 12:16:41 -03001435static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001436 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001437{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001438 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 return -EINVAL;
1440
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001441 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1442 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443
1444 return 0;
1445}
1446
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001447static int vidioc_enum_framesizes(struct file *file, void *priv,
1448 struct v4l2_frmsizeenum *fsize)
1449{
1450 struct em28xx_fh *fh = priv;
1451 struct em28xx *dev = fh->dev;
1452 struct em28xx_fmt *fmt;
1453 unsigned int maxw = norm_maxw(dev);
1454 unsigned int maxh = norm_maxh(dev);
1455
1456 fmt = format_by_fourcc(fsize->pixel_format);
1457 if (!fmt) {
1458 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1459 fsize->pixel_format);
1460 return -EINVAL;
1461 }
1462
1463 if (dev->board.is_em2800) {
1464 if (fsize->index > 1)
1465 return -EINVAL;
1466 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1467 fsize->discrete.width = maxw / (1 + fsize->index);
1468 fsize->discrete.height = maxh / (1 + fsize->index);
1469 return 0;
1470 }
1471
1472 if (fsize->index != 0)
1473 return -EINVAL;
1474
1475 /* Report a continuous range */
1476 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
Frank Schaefer6c3598e2013-02-10 16:05:14 -03001477 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
1478 &fsize->stepwise.min_width, &fsize->stepwise.min_height);
1479 if (fsize->stepwise.min_width < 48)
1480 fsize->stepwise.min_width = 48;
1481 if (fsize->stepwise.min_height < 38)
1482 fsize->stepwise.min_height = 38;
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001483 fsize->stepwise.max_width = maxw;
1484 fsize->stepwise.max_height = maxh;
1485 fsize->stepwise.step_width = 1;
1486 fsize->stepwise.step_height = 1;
1487 return 0;
1488}
1489
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001490/* RAW VBI ioctls */
1491
1492static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1493 struct v4l2_format *format)
1494{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001495 struct em28xx_fh *fh = priv;
1496 struct em28xx *dev = fh->dev;
1497
1498 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001499 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1500 format->fmt.vbi.offset = 0;
1501 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001502 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1503 format->fmt.vbi.count[0] = dev->vbi_height;
1504 format->fmt.vbi.count[1] = dev->vbi_height;
Hans Verkuil2a221d32012-09-07 08:51:32 -03001505 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001506
1507 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001508 if (dev->norm & V4L2_STD_525_60) {
1509 /* NTSC */
1510 format->fmt.vbi.start[0] = 10;
1511 format->fmt.vbi.start[1] = 273;
1512 } else if (dev->norm & V4L2_STD_625_50) {
1513 /* PAL */
1514 format->fmt.vbi.start[0] = 6;
1515 format->fmt.vbi.start[1] = 318;
1516 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001517
1518 return 0;
1519}
1520
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001521/* ----------------------------------------------------------- */
1522/* RADIO ESPECIFIC IOCTLS */
1523/* ----------------------------------------------------------- */
1524
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525static int radio_g_tuner(struct file *file, void *priv,
1526 struct v4l2_tuner *t)
1527{
1528 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1529
1530 if (unlikely(t->index > 0))
1531 return -EINVAL;
1532
1533 strcpy(t->name, "Radio");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001534
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001535 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001536
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001537 return 0;
1538}
1539
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001540static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001541 const struct v4l2_tuner *t)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001542{
1543 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1544
1545 if (0 != t->index)
1546 return -EINVAL;
1547
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001548 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001549
1550 return 0;
1551}
1552
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001553/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001554 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001555 * inits the device and starts isoc transfer
1556 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001557static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001558{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001559 struct video_device *vdev = video_devdata(filp);
1560 struct em28xx *dev = video_drvdata(filp);
1561 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001562 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001563
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001564 switch (vdev->vfl_type) {
1565 case VFL_TYPE_GRABBER:
1566 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1567 break;
1568 case VFL_TYPE_VBI:
1569 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1570 break;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001571 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001572
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001573 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1574 video_device_node_name(vdev), v4l2_type_names[fh_type],
1575 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001576
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001577
Hans Verkuil876cb142012-06-23 08:12:47 -03001578 if (mutex_lock_interruptible(&dev->lock))
1579 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001580 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001581 if (!fh) {
1582 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001583 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001584 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001585 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001586 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001587 fh->dev = dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001588 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001589 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001591 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001592 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001593 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001595 /* Needed, since GPIO might have disabled power of
1596 some i2c device
1597 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001598 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001599
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001600 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001601
1602 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001603 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001604 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001605 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001606
1607 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608
Hans Verkuil876cb142012-06-23 08:12:47 -03001609 mutex_unlock(&dev->lock);
Hans Verkuil69a61642012-09-07 05:52:40 -03001610 v4l2_fh_add(&fh->fh);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001611
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001612 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613}
1614
1615/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001616 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001617 * unregisters the v4l2,i2c and usb devices
1618 * called when the device gets disconected or at module unload
1619*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001620void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001621{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001623 /*FIXME: I2C IR should be disconnected */
1624
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001625 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001626 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001627 video_unregister_device(dev->radio_dev);
1628 else
1629 video_device_release(dev->radio_dev);
1630 dev->radio_dev = NULL;
1631 }
1632 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001633 em28xx_info("V4L2 device %s deregistered\n",
1634 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001635 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001636 video_unregister_device(dev->vbi_dev);
1637 else
1638 video_device_release(dev->vbi_dev);
1639 dev->vbi_dev = NULL;
1640 }
1641 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001642 em28xx_info("V4L2 device %s deregistered\n",
1643 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001644 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001645 video_unregister_device(dev->vdev);
1646 else
1647 video_device_release(dev->vdev);
1648 dev->vdev = NULL;
1649 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001650}
1651
1652/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001653 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001654 * stops streaming and deallocates all resources allocated by the v4l2
1655 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001656 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001657static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001659 struct em28xx_fh *fh = filp->private_data;
1660 struct em28xx *dev = fh->dev;
1661 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001662
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001663 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664
Hans Verkuil876cb142012-06-23 08:12:47 -03001665 mutex_lock(&dev->lock);
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001666 vb2_fop_release(filp);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001667
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001668 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001669 /* the device is already disconnect,
1670 free the remaining resources */
Frank Schaefer2665c292012-12-27 19:02:43 -03001671 if (dev->disconnected) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001672 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001673 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001674 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001675 kfree(dev);
1676 return 0;
1677 }
1678
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001679 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001680 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001681
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001682 /* do this before setting alternate! */
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001683 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001684
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001685 /* set alternate 0 */
1686 dev->alt = 0;
1687 em28xx_videodbg("setting alternate 0\n");
1688 errCode = usb_set_interface(dev->udev, 0, 0);
1689 if (errCode < 0) {
1690 em28xx_errdev("cannot change alternate number to "
1691 "0 (error=%i)\n", errCode);
1692 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001694
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001696 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001697 return 0;
1698}
1699
Hans Verkuilbec43662008-12-30 06:58:20 -03001700static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001701 .owner = THIS_MODULE,
1702 .open = em28xx_v4l2_open,
1703 .release = em28xx_v4l2_close,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001704 .read = vb2_fop_read,
1705 .poll = vb2_fop_poll,
1706 .mmap = vb2_fop_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001707 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708};
1709
Hans Verkuila3998102008-07-21 02:57:38 -03001710static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001711 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001712 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1713 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1714 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1715 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001716 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil2a221d32012-09-07 08:51:32 -03001717 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Frank Schaeferaab34612013-02-07 13:39:16 -03001718 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001719 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001720 .vidioc_g_audio = vidioc_g_audio,
1721 .vidioc_s_audio = vidioc_s_audio,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001722
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001723 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1724 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1725 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1726 .vidioc_querybuf = vb2_ioctl_querybuf,
1727 .vidioc_qbuf = vb2_ioctl_qbuf,
1728 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1729
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001730 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001731 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001732 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001733 .vidioc_g_parm = vidioc_g_parm,
1734 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001735 .vidioc_enum_input = vidioc_enum_input,
1736 .vidioc_g_input = vidioc_g_input,
1737 .vidioc_s_input = vidioc_s_input,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001738 .vidioc_streamon = vb2_ioctl_streamon,
1739 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001740 .vidioc_g_tuner = vidioc_g_tuner,
1741 .vidioc_s_tuner = vidioc_s_tuner,
1742 .vidioc_g_frequency = vidioc_g_frequency,
1743 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001744 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1745 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001746#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -03001747 .vidioc_g_chip_info = vidioc_g_chip_info,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001748 .vidioc_g_register = vidioc_g_register,
1749 .vidioc_s_register = vidioc_s_register,
1750#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001751};
1752
1753static const struct video_device em28xx_video_template = {
1754 .fops = &em28xx_v4l_fops,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001755 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -03001756 .ioctl_ops = &video_ioctl_ops,
1757
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001758 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001759};
1760
Hans Verkuilbec43662008-12-30 06:58:20 -03001761static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001762 .owner = THIS_MODULE,
1763 .open = em28xx_v4l2_open,
1764 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03001765 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001766};
1767
1768static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001769 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001770 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001771 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001772 .vidioc_g_frequency = vidioc_g_frequency,
1773 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001774 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1775 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001776#ifdef CONFIG_VIDEO_ADV_DEBUG
Michal Marek430c73f2013-04-16 08:06:30 -03001777 .vidioc_g_chip_info = vidioc_g_chip_info,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001778 .vidioc_g_register = vidioc_g_register,
1779 .vidioc_s_register = vidioc_s_register,
1780#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001781};
1782
Hans Verkuila3998102008-07-21 02:57:38 -03001783static struct video_device em28xx_radio_template = {
1784 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001785 .fops = &radio_fops,
1786 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001787};
1788
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001789/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001791
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001792
Adrian Bunk532fe652008-01-28 22:10:48 -03001793static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001794 const struct video_device *template,
1795 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001796{
1797 struct video_device *vfd;
1798
1799 vfd = video_device_alloc();
1800 if (NULL == vfd)
1801 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001802
1803 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001804 vfd->v4l2_dev = &dev->v4l2_dev;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001805 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001806 vfd->lock = &dev->lock;
Hans Verkuil69a61642012-09-07 05:52:40 -03001807 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuild8c95c02012-09-07 07:31:54 -03001808 if (dev->board.is_webcam)
1809 vfd->tvnorms = 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001810
1811 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1812 dev->name, type_name);
1813
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001814 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001815 return vfd;
1816}
1817
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001818int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001819{
Hans Verkuil081b9452012-09-07 05:43:59 -03001820 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001821 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03001822 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001823
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03001824 printk(KERN_INFO "%s: v4l2 driver version %s\n",
1825 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001826
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001827 /* set default norm */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001828 dev->norm = V4L2_STD_PAL;
Hans Verkuild5906dd2010-09-26 07:45:15 -03001829 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001830 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001831
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001832 /* Analog specific initialization */
1833 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03001834
1835 maxw = norm_maxw(dev);
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001836 /* MaxPacketSize for em2800 is too small to capture at full resolution
1837 * use half of maxw as the scaler can only scale to 50% */
1838 if (dev->board.is_em2800)
1839 maxw /= 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001840
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001841 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03001842 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001843
Ezequiel García96371fc2012-03-23 18:09:34 -03001844 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001845
1846 /* Audio defaults */
1847 dev->mute = 1;
1848 dev->volume = 0x1f;
1849
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001850/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001851 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1852 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1853 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001854
1855 em28xx_set_outfmt(dev);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001856 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001857
Frank Schaefer8f8b113a2013-02-15 14:38:32 -03001858 /* Add image controls */
1859 /* NOTE: at this point, the subdevices are already registered, so bridge
1860 * controls are only added/enabled when no subdevice provides them */
1861 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_CONTRAST))
1862 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1863 V4L2_CID_CONTRAST,
1864 0, 0x1f, 1, CONTRAST_DEFAULT);
1865 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BRIGHTNESS))
1866 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1867 V4L2_CID_BRIGHTNESS,
1868 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
1869 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SATURATION))
1870 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1871 V4L2_CID_SATURATION,
1872 0, 0x1f, 1, SATURATION_DEFAULT);
1873 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BLUE_BALANCE))
1874 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1875 V4L2_CID_BLUE_BALANCE,
1876 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
1877 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RED_BALANCE))
1878 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1879 V4L2_CID_RED_BALANCE,
1880 -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
1881 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SHARPNESS))
1882 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1883 V4L2_CID_SHARPNESS,
1884 0, 0x0f, 1, SHARPNESS_DEFAULT);
1885
1886 /* Reset image controls */
1887 em28xx_colorlevels_set_default(dev);
1888 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1889 if (dev->ctrl_handler.error)
1890 return dev->ctrl_handler.error;
1891
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001892 /* allocate and fill video video_device struct */
1893 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1894 if (!dev->vdev) {
1895 em28xx_errdev("cannot allocate video_device.\n");
1896 return -ENODEV;
1897 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001898 dev->vdev->queue = &dev->vb_vidq;
1899 dev->vdev->queue->lock = &dev->vb_queue_lock;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001900
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001901 /* disable inapplicable ioctls */
1902 if (dev->board.is_webcam) {
1903 v4l2_disable_ioctl(dev->vdev, VIDIOC_QUERYSTD);
1904 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_STD);
1905 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_STD);
Frank Schaefer3bc85cc2013-02-07 13:39:12 -03001906 } else {
1907 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001908 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03001909 if (dev->tuner_type == TUNER_ABSENT) {
1910 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_TUNER);
1911 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_TUNER);
1912 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_FREQUENCY);
1913 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_FREQUENCY);
1914 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001915 if (!dev->audio_mode.has_audio) {
1916 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_AUDIO);
1917 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_AUDIO);
1918 }
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001919
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001920 /* register v4l2 video video_device */
1921 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1922 video_nr[dev->devno]);
1923 if (ret) {
1924 em28xx_errdev("unable to register video device (error=%i).\n",
1925 ret);
1926 return ret;
1927 }
1928
1929 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001930 if (em28xx_vbi_supported(dev) == 1) {
1931 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1932 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001933
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001934 dev->vbi_dev->queue = &dev->vb_vbiq;
1935 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1936
Frank Schaefer66df67b2013-02-07 13:39:10 -03001937 /* disable inapplicable ioctls */
Frank Schaefer83c8bcc2013-02-07 13:39:13 -03001938 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer66df67b2013-02-07 13:39:10 -03001939 if (dev->tuner_type == TUNER_ABSENT) {
1940 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_TUNER);
1941 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_TUNER);
1942 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_FREQUENCY);
1943 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_FREQUENCY);
1944 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001945 if (!dev->audio_mode.has_audio) {
1946 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_AUDIO);
1947 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_AUDIO);
1948 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03001949
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001950 /* register v4l2 vbi video_device */
1951 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1952 vbi_nr[dev->devno]);
1953 if (ret < 0) {
1954 em28xx_errdev("unable to register vbi device\n");
1955 return ret;
1956 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001957 }
1958
1959 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001960 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1961 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001962 if (!dev->radio_dev) {
1963 em28xx_errdev("cannot allocate video_device.\n");
1964 return -ENODEV;
1965 }
1966 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1967 radio_nr[dev->devno]);
1968 if (ret < 0) {
1969 em28xx_errdev("can't register radio device\n");
1970 return ret;
1971 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001972 em28xx_info("Registered radio device as %s\n",
1973 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001974 }
1975
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001976 em28xx_info("V4L2 video device registered as %s\n",
1977 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001978
1979 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001980 em28xx_info("V4L2 VBI device registered as %s\n",
1981 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001982
1983 return 0;
1984}