blob: dd19c9ff76e0f9a159c6630320814c89e9e83d5a [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 Thorntone0fadfd342008-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
Frank Schaefer032f1dd2013-10-10 15:31:53 -0300641 if (dev->streaming_users == 0) {
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300642 /* 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)
Frank Schaefer032f1dd2013-10-10 15:31:53 -0300660 return rc;
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
Frank Schaefer032f1dd2013-10-10 15:31:53 -0300678 dev->streaming_users++;
679
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680 return rc;
681}
682
Mauro Carvalho Chehab3a799c22013-01-05 01:27:21 -0200683static int em28xx_stop_streaming(struct vb2_queue *vq)
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300684{
685 struct em28xx *dev = vb2_get_drv_priv(vq);
686 struct em28xx_dmaqueue *vidq = &dev->vidq;
687 unsigned long flags = 0;
688
689 em28xx_videodbg("%s\n", __func__);
690
691 res_free(dev, vq->type);
692
693 if (dev->streaming_users-- == 1) {
694 /* Last active user, so shutdown all the URBS */
695 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
696 }
697
698 spin_lock_irqsave(&dev->slock, flags);
699 while (!list_empty(&vidq->active)) {
700 struct em28xx_buffer *buf;
701 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
702 list_del(&buf->list);
703 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
704 }
705 dev->usb_ctl.vid_buf = NULL;
706 spin_unlock_irqrestore(&dev->slock, flags);
707
708 return 0;
709}
710
711int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
712{
713 struct em28xx *dev = vb2_get_drv_priv(vq);
714 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
715 unsigned long flags = 0;
716
717 em28xx_videodbg("%s\n", __func__);
718
719 res_free(dev, vq->type);
720
721 if (dev->streaming_users-- == 1) {
722 /* Last active user, so shutdown all the URBS */
723 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
724 }
725
726 spin_lock_irqsave(&dev->slock, flags);
727 while (!list_empty(&vbiq->active)) {
728 struct em28xx_buffer *buf;
729 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
730 list_del(&buf->list);
731 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
732 }
733 dev->usb_ctl.vbi_buf = NULL;
734 spin_unlock_irqrestore(&dev->slock, flags);
735
736 return 0;
737}
738
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300739static void
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300740buffer_queue(struct vb2_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300742 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
743 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
744 struct em28xx_dmaqueue *vidq = &dev->vidq;
745 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300747 em28xx_videodbg("%s\n", __func__);
748 buf->mem = vb2_plane_vaddr(vb, 0);
749 buf->length = vb2_plane_size(vb, 0);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300750
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300751 spin_lock_irqsave(&dev->slock, flags);
752 list_add_tail(&buf->list, &vidq->active);
753 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300754}
755
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300756static struct vb2_ops em28xx_video_qops = {
757 .queue_setup = queue_setup,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300758 .buf_prepare = buffer_prepare,
759 .buf_queue = buffer_queue,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300760 .start_streaming = em28xx_start_analog_streaming,
761 .stop_streaming = em28xx_stop_streaming,
762 .wait_prepare = vb2_ops_wait_prepare,
763 .wait_finish = vb2_ops_wait_finish,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300764};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800765
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300766int em28xx_vb2_setup(struct em28xx *dev)
767{
768 int rc;
769 struct vb2_queue *q;
770
771 /* Setup Videobuf2 for Video capture */
772 q = &dev->vb_vidq;
773 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabef85cd92013-01-06 00:34:22 -0200774 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Frank Schaefer195281d2013-03-09 06:53:01 -0300775 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300776 q->drv_priv = dev;
777 q->buf_struct_size = sizeof(struct em28xx_buffer);
778 q->ops = &em28xx_video_qops;
779 q->mem_ops = &vb2_vmalloc_memops;
780
781 rc = vb2_queue_init(q);
782 if (rc < 0)
783 return rc;
784
785 /* Setup Videobuf2 for VBI capture */
786 q = &dev->vb_vbiq;
787 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
788 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
Frank Schaefer195281d2013-03-09 06:53:01 -0300789 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300790 q->drv_priv = dev;
791 q->buf_struct_size = sizeof(struct em28xx_buffer);
792 q->ops = &em28xx_vbi_qops;
793 q->mem_ops = &vb2_vmalloc_memops;
794
795 rc = vb2_queue_init(q);
796 if (rc < 0)
797 return rc;
798
799 return 0;
800}
801
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300802/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800803
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800804static void video_mux(struct em28xx *dev, int index)
805{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800806 dev->ctl_input = index;
807 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300808 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800809
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300810 if (!dev->ctl_aoutput)
811 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
812
Hans Verkuil5325b422009-04-02 11:26:22 -0300813 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
814 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800815
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300816 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300817 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300818 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
819 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300820 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300821 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300822 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
823 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800824 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300825
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300826 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300827 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
828 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300829 }
830
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300831 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800832}
833
Hans Verkuil081b9452012-09-07 05:43:59 -0300834void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300835{
Hans Verkuil081b9452012-09-07 05:43:59 -0300836 struct em28xx *dev = priv;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300837
Hans Verkuil081b9452012-09-07 05:43:59 -0300838 /*
839 * In the case of non-AC97 volume controls, we still need
840 * to do some setups at em28xx, in order to mute/unmute
841 * and to adjust audio volume. However, the value ranges
842 * should be checked by the corresponding V4L subdriver.
843 */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300844 switch (ctrl->id) {
845 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil081b9452012-09-07 05:43:59 -0300846 dev->mute = ctrl->val;
847 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300848 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300849 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil081b9452012-09-07 05:43:59 -0300850 dev->volume = ctrl->val;
851 em28xx_audio_analog_set(dev);
852 break;
853 }
854}
855
856static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
857{
858 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300859 int ret = -EINVAL;
Hans Verkuil081b9452012-09-07 05:43:59 -0300860
861 switch (ctrl->id) {
862 case V4L2_CID_AUDIO_MUTE:
863 dev->mute = ctrl->val;
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300864 ret = em28xx_audio_analog_set(dev);
Hans Verkuil081b9452012-09-07 05:43:59 -0300865 break;
866 case V4L2_CID_AUDIO_VOLUME:
867 dev->volume = ctrl->val;
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300868 ret = em28xx_audio_analog_set(dev);
869 break;
870 case V4L2_CID_CONTRAST:
871 ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
872 break;
873 case V4L2_CID_BRIGHTNESS:
874 ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
875 break;
876 case V4L2_CID_SATURATION:
877 ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
878 break;
879 case V4L2_CID_BLUE_BALANCE:
880 ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
881 break;
882 case V4L2_CID_RED_BALANCE:
883 ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
884 break;
885 case V4L2_CID_SHARPNESS:
886 ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300887 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300888 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300889
Frank Schaefer8f8b113a2013-02-15 14:38:32 -0300890 return (ret < 0) ? ret : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891}
892
Hans Verkuil081b9452012-09-07 05:43:59 -0300893const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
894 .s_ctrl = em28xx_s_ctrl,
895};
896
Frank Schaefer6b09a212013-02-10 16:05:12 -0300897static void size_to_scale(struct em28xx *dev,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300898 unsigned int width, unsigned int height,
899 unsigned int *hscale, unsigned int *vscale)
900{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300901 unsigned int maxw = norm_maxw(dev);
902 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300903
904 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
Frank Schaefer81685322013-02-10 16:05:11 -0300905 if (*hscale > EM28XX_HVSCALE_MAX)
906 *hscale = EM28XX_HVSCALE_MAX;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300907
908 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
Frank Schaefer81685322013-02-10 16:05:11 -0300909 if (*vscale > EM28XX_HVSCALE_MAX)
910 *vscale = EM28XX_HVSCALE_MAX;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300911}
912
Frank Schaeferb8374132013-02-10 16:05:13 -0300913static void scale_to_size(struct em28xx *dev,
914 unsigned int hscale, unsigned int vscale,
915 unsigned int *width, unsigned int *height)
916{
917 unsigned int maxw = norm_maxw(dev);
918 unsigned int maxh = norm_maxh(dev);
919
920 *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
921 *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
922}
923
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300924/* ------------------------------------------------------------------
925 IOCTL vidioc handling
926 ------------------------------------------------------------------*/
927
Hans Verkuil78b526a2008-05-28 12:16:41 -0300928static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300929 struct v4l2_format *f)
930{
931 struct em28xx_fh *fh = priv;
932 struct em28xx *dev = fh->dev;
933
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934 f->fmt.pix.width = dev->width;
935 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300936 f->fmt.pix.pixelformat = dev->format->fourcc;
937 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300938 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
940
941 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300942 if (dev->progressive)
943 f->fmt.pix.field = V4L2_FIELD_NONE;
944 else
945 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300946 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947 return 0;
948}
949
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300950static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
951{
952 unsigned int i;
953
954 for (i = 0; i < ARRAY_SIZE(format); i++)
955 if (format[i].fourcc == fourcc)
956 return &format[i];
957
958 return NULL;
959}
960
Hans Verkuil78b526a2008-05-28 12:16:41 -0300961static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 struct v4l2_format *f)
963{
964 struct em28xx_fh *fh = priv;
965 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300966 unsigned int width = f->fmt.pix.width;
967 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968 unsigned int maxw = norm_maxw(dev);
969 unsigned int maxh = norm_maxh(dev);
970 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300971 struct em28xx_fmt *fmt;
972
973 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
974 if (!fmt) {
975 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
976 f->fmt.pix.pixelformat);
977 return -EINVAL;
978 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300980 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300981 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300982 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
983 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300984 /*
985 * MaxPacketSize for em2800 is too small to capture at full
986 * resolution use half of maxw as the scaler can only scale
987 * to 50%
988 */
Sascha Sommer1020d132012-01-08 16:54:28 -0300989 if (width == maxw && height == maxh)
990 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300991 } else {
992 /* width must even because of the YUYV format
993 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300994 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
995 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300996 }
997
Frank Schaefer6b09a212013-02-10 16:05:12 -0300998 size_to_scale(dev, width, height, &hscale, &vscale);
Hans Verkuil46f85972013-03-30 05:31:42 -0300999 scale_to_size(dev, hscale, vscale, &width, &height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001000
1001 f->fmt.pix.width = width;
1002 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001003 f->fmt.pix.pixelformat = fmt->fourcc;
Hans Verkuile6066db2013-02-06 08:14:47 -03001004 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001005 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001006 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001007 if (dev->progressive)
1008 f->fmt.pix.field = V4L2_FIELD_NONE;
1009 else
1010 f->fmt.pix.field = dev->interlaced ?
1011 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Alban Browaeys51dd4d72013-07-16 19:06:46 -03001012 f->fmt.pix.priv = 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001014 return 0;
1015}
1016
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001017static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1018 unsigned width, unsigned height)
1019{
1020 struct em28xx_fmt *fmt;
1021
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001022 fmt = format_by_fourcc(fourcc);
1023 if (!fmt)
1024 return -EINVAL;
1025
1026 dev->format = fmt;
1027 dev->width = width;
1028 dev->height = height;
1029
1030 /* set new image size */
Frank Schaefer6b09a212013-02-10 16:05:12 -03001031 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001032
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001033 em28xx_resolution_set(dev);
1034
1035 return 0;
1036}
1037
Hans Verkuil78b526a2008-05-28 12:16:41 -03001038static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001039 struct v4l2_format *f)
1040{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001041 struct em28xx *dev = video_drvdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001042
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001043 if (dev->streaming_users > 0)
1044 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001045
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001046 vidioc_try_fmt_vid_cap(file, priv, f);
1047
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001048 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001049 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001050}
1051
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001052static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1053{
1054 struct em28xx_fh *fh = priv;
1055 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001056
1057 *norm = dev->norm;
1058
1059 return 0;
1060}
1061
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001062static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1063{
1064 struct em28xx_fh *fh = priv;
1065 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001066
1067 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1068
1069 return 0;
1070}
1071
Hans Verkuil314527a2013-03-15 06:10:40 -03001072static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001073{
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1076 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077
Hans Verkuil314527a2013-03-15 06:10:40 -03001078 if (norm == dev->norm)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001079 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001080
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001081 if (dev->streaming_users > 0)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001082 return -EBUSY;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001083
Hans Verkuil314527a2013-03-15 06:10:40 -03001084 dev->norm = norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001085
1086 /* Adjusts width/height, if needed */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001087 f.fmt.pix.width = 720;
Hans Verkuil314527a2013-03-15 06:10:40 -03001088 f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001089 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001091 /* set new image size */
1092 dev->width = f.fmt.pix.width;
1093 dev->height = f.fmt.pix.height;
Frank Schaefer6b09a212013-02-10 16:05:12 -03001094 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095
1096 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001097 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001098
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001099 return 0;
1100}
1101
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001102static int vidioc_g_parm(struct file *file, void *priv,
1103 struct v4l2_streamparm *p)
1104{
1105 struct em28xx_fh *fh = priv;
1106 struct em28xx *dev = fh->dev;
1107 int rc = 0;
1108
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001109 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001110 if (dev->board.is_webcam)
1111 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1112 video, g_parm, p);
1113 else
1114 v4l2_video_std_frame_period(dev->norm,
1115 &p->parm.capture.timeperframe);
1116
1117 return rc;
1118}
1119
1120static int vidioc_s_parm(struct file *file, void *priv,
1121 struct v4l2_streamparm *p)
1122{
1123 struct em28xx_fh *fh = priv;
1124 struct em28xx *dev = fh->dev;
1125
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001126 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001127 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1128}
1129
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001130static const char *iname[] = {
1131 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1132 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1133 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1134 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1135 [EM28XX_VMUX_SVIDEO] = "S-Video",
1136 [EM28XX_VMUX_TELEVISION] = "Television",
1137 [EM28XX_VMUX_CABLE] = "Cable TV",
1138 [EM28XX_VMUX_DVB] = "DVB",
1139 [EM28XX_VMUX_DEBUG] = "for debug only",
1140};
1141
1142static int vidioc_enum_input(struct file *file, void *priv,
1143 struct v4l2_input *i)
1144{
1145 struct em28xx_fh *fh = priv;
1146 struct em28xx *dev = fh->dev;
1147 unsigned int n;
1148
1149 n = i->index;
1150 if (n >= MAX_EM28XX_INPUT)
1151 return -EINVAL;
1152 if (0 == INPUT(n)->type)
1153 return -EINVAL;
1154
1155 i->index = n;
1156 i->type = V4L2_INPUT_TYPE_CAMERA;
1157
1158 strcpy(i->name, iname[INPUT(n)->type]);
1159
1160 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1161 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1162 i->type = V4L2_INPUT_TYPE_TUNER;
1163
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001164 i->std = dev->vdev->tvnorms;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001165 /* webcams do not have the STD API */
1166 if (dev->board.is_webcam)
1167 i->capabilities = 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001168
1169 return 0;
1170}
1171
1172static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1173{
1174 struct em28xx_fh *fh = priv;
1175 struct em28xx *dev = fh->dev;
1176
1177 *i = dev->ctl_input;
1178
1179 return 0;
1180}
1181
1182static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1183{
1184 struct em28xx_fh *fh = priv;
1185 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001186
1187 if (i >= MAX_EM28XX_INPUT)
1188 return -EINVAL;
1189 if (0 == INPUT(i)->type)
1190 return -EINVAL;
1191
Ezequiel García96371fc2012-03-23 18:09:34 -03001192 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001193 return 0;
1194}
1195
1196static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1197{
1198 struct em28xx_fh *fh = priv;
1199 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001200
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001201 switch (a->index) {
1202 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001204 break;
1205 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001207 break;
1208 case EM28XX_AMUX_VIDEO2:
1209 strcpy(a->name, "Television alt");
1210 break;
1211 case EM28XX_AMUX_PHONE:
1212 strcpy(a->name, "Phone");
1213 break;
1214 case EM28XX_AMUX_MIC:
1215 strcpy(a->name, "Mic");
1216 break;
1217 case EM28XX_AMUX_CD:
1218 strcpy(a->name, "CD");
1219 break;
1220 case EM28XX_AMUX_AUX:
1221 strcpy(a->name, "Aux");
1222 break;
1223 case EM28XX_AMUX_PCM_OUT:
1224 strcpy(a->name, "PCM");
1225 break;
1226 default:
1227 return -EINVAL;
1228 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001229
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001230 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001232
1233 return 0;
1234}
1235
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001236static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001237{
1238 struct em28xx_fh *fh = priv;
1239 struct em28xx *dev = fh->dev;
1240
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001241 if (a->index >= MAX_EM28XX_INPUT)
1242 return -EINVAL;
1243 if (0 == INPUT(a->index)->type)
1244 return -EINVAL;
1245
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001246 dev->ctl_ainput = INPUT(a->index)->amux;
1247 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001248
1249 if (!dev->ctl_aoutput)
1250 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001251
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001252 return 0;
1253}
1254
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001255static int vidioc_g_tuner(struct file *file, void *priv,
1256 struct v4l2_tuner *t)
1257{
1258 struct em28xx_fh *fh = priv;
1259 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001260
1261 if (0 != t->index)
1262 return -EINVAL;
1263
1264 strcpy(t->name, "Tuner");
1265
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001266 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001267 return 0;
1268}
1269
1270static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001271 const struct v4l2_tuner *t)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001272{
1273 struct em28xx_fh *fh = priv;
1274 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001275
1276 if (0 != t->index)
1277 return -EINVAL;
1278
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001279 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001280 return 0;
1281}
1282
1283static int vidioc_g_frequency(struct file *file, void *priv,
1284 struct v4l2_frequency *f)
1285{
1286 struct em28xx_fh *fh = priv;
1287 struct em28xx *dev = fh->dev;
1288
Hans Verkuil20deebf2012-09-06 10:07:25 -03001289 if (0 != f->tuner)
1290 return -EINVAL;
1291
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001292 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001293 return 0;
1294}
1295
1296static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001297 const struct v4l2_frequency *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001298{
Hans Verkuilb530a442013-03-19 04:09:26 -03001299 struct v4l2_frequency new_freq = *f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001300 struct em28xx_fh *fh = priv;
1301 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001302
1303 if (0 != f->tuner)
1304 return -EINVAL;
1305
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001306 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -03001307 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1308 dev->ctl_freq = new_freq.frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001310 return 0;
1311}
1312
Hans Verkuilcd634f12013-03-27 08:04:23 -03001313#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -03001314static int vidioc_g_chip_info(struct file *file, void *priv,
1315 struct v4l2_dbg_chip_info *chip)
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001316{
1317 struct em28xx_fh *fh = priv;
1318 struct em28xx *dev = fh->dev;
1319
1320 if (chip->match.addr > 1)
1321 return -EINVAL;
1322 if (chip->match.addr == 1)
1323 strlcpy(chip->name, "ac97", sizeof(chip->name));
1324 else
1325 strlcpy(chip->name, dev->v4l2_dev.name, sizeof(chip->name));
1326 return 0;
1327}
1328
Frank Schaefer35deba32013-02-07 13:39:19 -03001329static int em28xx_reg_len(int reg)
1330{
1331 switch (reg) {
1332 case EM28XX_R40_AC97LSB:
1333 case EM28XX_R30_HSCALELOW:
1334 case EM28XX_R32_VSCALELOW:
1335 return 2;
1336 default:
1337 return 1;
1338 }
1339}
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001340
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001341static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001342 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001343{
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
1346 int ret;
1347
Hans Verkuilabca2052013-05-29 06:59:35 -03001348 if (reg->match.addr > 1)
1349 return -EINVAL;
1350 if (reg->match.addr) {
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001351 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001352 if (ret < 0)
1353 return ret;
1354
1355 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001356 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001357 return 0;
1358 }
1359
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001360 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001361 reg->size = em28xx_reg_len(reg->reg);
1362 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001363 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001364
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001365 if (ret < 0)
1366 return ret;
1367
1368 reg->val = ret;
1369 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001370 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001371 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1372 reg->reg, (char *)&val, 2);
1373 if (ret < 0)
1374 return ret;
1375
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001376 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001377 }
1378
1379 return 0;
1380}
1381
1382static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001383 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001384{
1385 struct em28xx_fh *fh = priv;
1386 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001387 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001388
Hans Verkuilabca2052013-05-29 06:59:35 -03001389 if (reg->match.addr > 1)
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001390 return -EINVAL;
Hans Verkuilabca2052013-05-29 06:59:35 -03001391 if (reg->match.addr)
1392 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001393
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001394 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001395 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001396
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001397 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001398 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001399}
1400#endif
1401
1402
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001403static int vidioc_querycap(struct file *file, void *priv,
1404 struct v4l2_capability *cap)
1405{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001406 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407 struct em28xx_fh *fh = priv;
1408 struct em28xx *dev = fh->dev;
1409
1410 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1411 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001412 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001413
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001414 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1415 cap->device_caps = V4L2_CAP_READWRITE |
1416 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1417 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1418 cap->device_caps = V4L2_CAP_RADIO;
1419 else
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001420 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001421
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001422 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001423 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001424
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001425 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001426 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001427
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001428 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1429 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1430 if (dev->vbi_dev)
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001431 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001432 if (dev->radio_dev)
1433 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001434 return 0;
1435}
1436
Hans Verkuil78b526a2008-05-28 12:16:41 -03001437static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001438 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001440 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001441 return -EINVAL;
1442
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001443 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1444 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001445
1446 return 0;
1447}
1448
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001449static int vidioc_enum_framesizes(struct file *file, void *priv,
1450 struct v4l2_frmsizeenum *fsize)
1451{
1452 struct em28xx_fh *fh = priv;
1453 struct em28xx *dev = fh->dev;
1454 struct em28xx_fmt *fmt;
1455 unsigned int maxw = norm_maxw(dev);
1456 unsigned int maxh = norm_maxh(dev);
1457
1458 fmt = format_by_fourcc(fsize->pixel_format);
1459 if (!fmt) {
1460 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1461 fsize->pixel_format);
1462 return -EINVAL;
1463 }
1464
1465 if (dev->board.is_em2800) {
1466 if (fsize->index > 1)
1467 return -EINVAL;
1468 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1469 fsize->discrete.width = maxw / (1 + fsize->index);
1470 fsize->discrete.height = maxh / (1 + fsize->index);
1471 return 0;
1472 }
1473
1474 if (fsize->index != 0)
1475 return -EINVAL;
1476
1477 /* Report a continuous range */
1478 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
Frank Schaefer6c3598e2013-02-10 16:05:14 -03001479 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
1480 &fsize->stepwise.min_width, &fsize->stepwise.min_height);
1481 if (fsize->stepwise.min_width < 48)
1482 fsize->stepwise.min_width = 48;
1483 if (fsize->stepwise.min_height < 38)
1484 fsize->stepwise.min_height = 38;
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001485 fsize->stepwise.max_width = maxw;
1486 fsize->stepwise.max_height = maxh;
1487 fsize->stepwise.step_width = 1;
1488 fsize->stepwise.step_height = 1;
1489 return 0;
1490}
1491
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001492/* RAW VBI ioctls */
1493
1494static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1495 struct v4l2_format *format)
1496{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001497 struct em28xx_fh *fh = priv;
1498 struct em28xx *dev = fh->dev;
1499
1500 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001501 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1502 format->fmt.vbi.offset = 0;
1503 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001504 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1505 format->fmt.vbi.count[0] = dev->vbi_height;
1506 format->fmt.vbi.count[1] = dev->vbi_height;
Hans Verkuil2a221d32012-09-07 08:51:32 -03001507 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001508
1509 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001510 if (dev->norm & V4L2_STD_525_60) {
1511 /* NTSC */
1512 format->fmt.vbi.start[0] = 10;
1513 format->fmt.vbi.start[1] = 273;
1514 } else if (dev->norm & V4L2_STD_625_50) {
1515 /* PAL */
1516 format->fmt.vbi.start[0] = 6;
1517 format->fmt.vbi.start[1] = 318;
1518 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001519
1520 return 0;
1521}
1522
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001523/* ----------------------------------------------------------- */
1524/* RADIO ESPECIFIC IOCTLS */
1525/* ----------------------------------------------------------- */
1526
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001527static int radio_g_tuner(struct file *file, void *priv,
1528 struct v4l2_tuner *t)
1529{
1530 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1531
1532 if (unlikely(t->index > 0))
1533 return -EINVAL;
1534
1535 strcpy(t->name, "Radio");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001536
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001537 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001538
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001539 return 0;
1540}
1541
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001542static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001543 const struct v4l2_tuner *t)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001544{
1545 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1546
1547 if (0 != t->index)
1548 return -EINVAL;
1549
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001550 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001551
1552 return 0;
1553}
1554
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001556 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557 * inits the device and starts isoc transfer
1558 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001559static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001560{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001561 struct video_device *vdev = video_devdata(filp);
1562 struct em28xx *dev = video_drvdata(filp);
1563 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001564 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001565
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001566 switch (vdev->vfl_type) {
1567 case VFL_TYPE_GRABBER:
1568 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1569 break;
1570 case VFL_TYPE_VBI:
1571 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1572 break;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001573 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001574
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001575 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1576 video_device_node_name(vdev), v4l2_type_names[fh_type],
1577 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001578
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001579
Hans Verkuil876cb142012-06-23 08:12:47 -03001580 if (mutex_lock_interruptible(&dev->lock))
1581 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001582 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001583 if (!fh) {
1584 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001585 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001586 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001587 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001588 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001589 fh->dev = dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001590 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001591 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001592
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001593 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001594 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001595 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001596
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001597 /* Needed, since GPIO might have disabled power of
1598 some i2c device
1599 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001600 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001601
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001602 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001603
1604 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001605 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001606 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001607 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608
1609 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610
Hans Verkuil876cb142012-06-23 08:12:47 -03001611 mutex_unlock(&dev->lock);
Hans Verkuil69a61642012-09-07 05:52:40 -03001612 v4l2_fh_add(&fh->fh);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001613
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001614 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001615}
1616
1617/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001618 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001619 * unregisters the v4l2,i2c and usb devices
1620 * called when the device gets disconected or at module unload
1621*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001622void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001624
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 /*FIXME: I2C IR should be disconnected */
1626
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001627 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001628 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001629 video_unregister_device(dev->radio_dev);
1630 else
1631 video_device_release(dev->radio_dev);
1632 dev->radio_dev = NULL;
1633 }
1634 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001635 em28xx_info("V4L2 device %s deregistered\n",
1636 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001637 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001638 video_unregister_device(dev->vbi_dev);
1639 else
1640 video_device_release(dev->vbi_dev);
1641 dev->vbi_dev = NULL;
1642 }
1643 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001644 em28xx_info("V4L2 device %s deregistered\n",
1645 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001646 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001647 video_unregister_device(dev->vdev);
1648 else
1649 video_device_release(dev->vdev);
1650 dev->vdev = NULL;
1651 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652}
1653
1654/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001655 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001656 * stops streaming and deallocates all resources allocated by the v4l2
1657 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001659static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001661 struct em28xx_fh *fh = filp->private_data;
1662 struct em28xx *dev = fh->dev;
1663 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001665 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001666
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001667 vb2_fop_release(filp);
Ricardo Ribalda98c24dcd2013-11-06 05:39:35 -03001668 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001669
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001670 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001671 /* the device is already disconnect,
1672 free the remaining resources */
Frank Schaefer2665c292012-12-27 19:02:43 -03001673 if (dev->disconnected) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001674 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001675 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001676 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001677 kfree(dev);
1678 return 0;
1679 }
1680
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001681 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001682 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001683
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001684 /* do this before setting alternate! */
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001685 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001686
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001687 /* set alternate 0 */
1688 dev->alt = 0;
1689 em28xx_videodbg("setting alternate 0\n");
1690 errCode = usb_set_interface(dev->udev, 0, 0);
1691 if (errCode < 0) {
1692 em28xx_errdev("cannot change alternate number to "
1693 "0 (error=%i)\n", errCode);
1694 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001696
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001697 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001698 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001699 return 0;
1700}
1701
Hans Verkuilbec43662008-12-30 06:58:20 -03001702static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001703 .owner = THIS_MODULE,
1704 .open = em28xx_v4l2_open,
1705 .release = em28xx_v4l2_close,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001706 .read = vb2_fop_read,
1707 .poll = vb2_fop_poll,
1708 .mmap = vb2_fop_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001709 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710};
1711
Hans Verkuila3998102008-07-21 02:57:38 -03001712static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001713 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001714 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1715 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1716 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1717 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001718 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil2a221d32012-09-07 08:51:32 -03001719 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Frank Schaeferaab34612013-02-07 13:39:16 -03001720 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001721 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001722 .vidioc_g_audio = vidioc_g_audio,
1723 .vidioc_s_audio = vidioc_s_audio,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001724
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001725 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1726 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1727 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1728 .vidioc_querybuf = vb2_ioctl_querybuf,
1729 .vidioc_qbuf = vb2_ioctl_qbuf,
1730 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1731
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001732 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001733 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001734 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001735 .vidioc_g_parm = vidioc_g_parm,
1736 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001737 .vidioc_enum_input = vidioc_enum_input,
1738 .vidioc_g_input = vidioc_g_input,
1739 .vidioc_s_input = vidioc_s_input,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001740 .vidioc_streamon = vb2_ioctl_streamon,
1741 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001742 .vidioc_g_tuner = vidioc_g_tuner,
1743 .vidioc_s_tuner = vidioc_s_tuner,
1744 .vidioc_g_frequency = vidioc_g_frequency,
1745 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001746 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1747 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001748#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -03001749 .vidioc_g_chip_info = vidioc_g_chip_info,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001750 .vidioc_g_register = vidioc_g_register,
1751 .vidioc_s_register = vidioc_s_register,
1752#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001753};
1754
1755static const struct video_device em28xx_video_template = {
1756 .fops = &em28xx_v4l_fops,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001757 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -03001758 .ioctl_ops = &video_ioctl_ops,
1759
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001760 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001761};
1762
Hans Verkuilbec43662008-12-30 06:58:20 -03001763static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001764 .owner = THIS_MODULE,
1765 .open = em28xx_v4l2_open,
1766 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03001767 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001768};
1769
1770static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001771 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001772 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001773 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001774 .vidioc_g_frequency = vidioc_g_frequency,
1775 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001776 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1777 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001778#ifdef CONFIG_VIDEO_ADV_DEBUG
Michal Marek430c73f2013-04-16 08:06:30 -03001779 .vidioc_g_chip_info = vidioc_g_chip_info,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001780 .vidioc_g_register = vidioc_g_register,
1781 .vidioc_s_register = vidioc_s_register,
1782#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001783};
1784
Hans Verkuila3998102008-07-21 02:57:38 -03001785static struct video_device em28xx_radio_template = {
1786 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001787 .fops = &radio_fops,
1788 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001789};
1790
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001791/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001792
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001793
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001794
Adrian Bunk532fe652008-01-28 22:10:48 -03001795static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001796 const struct video_device *template,
1797 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001798{
1799 struct video_device *vfd;
1800
1801 vfd = video_device_alloc();
1802 if (NULL == vfd)
1803 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001804
1805 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001806 vfd->v4l2_dev = &dev->v4l2_dev;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001807 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001808 vfd->lock = &dev->lock;
Hans Verkuil69a61642012-09-07 05:52:40 -03001809 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuild8c95c02012-09-07 07:31:54 -03001810 if (dev->board.is_webcam)
1811 vfd->tvnorms = 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001812
1813 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1814 dev->name, type_name);
1815
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001816 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001817 return vfd;
1818}
1819
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001820int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001821{
Hans Verkuil081b9452012-09-07 05:43:59 -03001822 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001823 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03001824 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001825
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03001826 printk(KERN_INFO "%s: v4l2 driver version %s\n",
1827 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001828
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001829 /* set default norm */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001830 dev->norm = V4L2_STD_PAL;
Hans Verkuild5906dd2010-09-26 07:45:15 -03001831 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001832 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001833
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001834 /* Analog specific initialization */
1835 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03001836
1837 maxw = norm_maxw(dev);
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001838 /* MaxPacketSize for em2800 is too small to capture at full resolution
1839 * use half of maxw as the scaler can only scale to 50% */
1840 if (dev->board.is_em2800)
1841 maxw /= 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001842
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001843 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03001844 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001845
Ezequiel García96371fc2012-03-23 18:09:34 -03001846 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001847
1848 /* Audio defaults */
1849 dev->mute = 1;
1850 dev->volume = 0x1f;
1851
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001852/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001853 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1854 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1855 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001856
1857 em28xx_set_outfmt(dev);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001858 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001859
Frank Schaefer8f8b113a2013-02-15 14:38:32 -03001860 /* Add image controls */
1861 /* NOTE: at this point, the subdevices are already registered, so bridge
1862 * controls are only added/enabled when no subdevice provides them */
1863 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_CONTRAST))
1864 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1865 V4L2_CID_CONTRAST,
1866 0, 0x1f, 1, CONTRAST_DEFAULT);
1867 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BRIGHTNESS))
1868 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1869 V4L2_CID_BRIGHTNESS,
1870 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
1871 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SATURATION))
1872 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1873 V4L2_CID_SATURATION,
1874 0, 0x1f, 1, SATURATION_DEFAULT);
1875 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BLUE_BALANCE))
1876 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1877 V4L2_CID_BLUE_BALANCE,
1878 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
1879 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RED_BALANCE))
1880 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1881 V4L2_CID_RED_BALANCE,
1882 -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
1883 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SHARPNESS))
1884 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1885 V4L2_CID_SHARPNESS,
1886 0, 0x0f, 1, SHARPNESS_DEFAULT);
1887
1888 /* Reset image controls */
1889 em28xx_colorlevels_set_default(dev);
1890 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1891 if (dev->ctrl_handler.error)
1892 return dev->ctrl_handler.error;
1893
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001894 /* allocate and fill video video_device struct */
1895 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1896 if (!dev->vdev) {
1897 em28xx_errdev("cannot allocate video_device.\n");
1898 return -ENODEV;
1899 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001900 dev->vdev->queue = &dev->vb_vidq;
1901 dev->vdev->queue->lock = &dev->vb_queue_lock;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001902
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001903 /* disable inapplicable ioctls */
1904 if (dev->board.is_webcam) {
1905 v4l2_disable_ioctl(dev->vdev, VIDIOC_QUERYSTD);
1906 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_STD);
1907 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_STD);
Frank Schaefer3bc85cc2013-02-07 13:39:12 -03001908 } else {
1909 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001910 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03001911 if (dev->tuner_type == TUNER_ABSENT) {
1912 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_TUNER);
1913 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_TUNER);
1914 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_FREQUENCY);
1915 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_FREQUENCY);
1916 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001917 if (!dev->audio_mode.has_audio) {
1918 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_AUDIO);
1919 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_AUDIO);
1920 }
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001921
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001922 /* register v4l2 video video_device */
1923 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1924 video_nr[dev->devno]);
1925 if (ret) {
1926 em28xx_errdev("unable to register video device (error=%i).\n",
1927 ret);
1928 return ret;
1929 }
1930
1931 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001932 if (em28xx_vbi_supported(dev) == 1) {
1933 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1934 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001935
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001936 dev->vbi_dev->queue = &dev->vb_vbiq;
1937 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1938
Frank Schaefer66df67b2013-02-07 13:39:10 -03001939 /* disable inapplicable ioctls */
Frank Schaefer83c8bcc2013-02-07 13:39:13 -03001940 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer66df67b2013-02-07 13:39:10 -03001941 if (dev->tuner_type == TUNER_ABSENT) {
1942 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_TUNER);
1943 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_TUNER);
1944 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_FREQUENCY);
1945 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_FREQUENCY);
1946 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001947 if (!dev->audio_mode.has_audio) {
1948 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_AUDIO);
1949 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_AUDIO);
1950 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03001951
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001952 /* register v4l2 vbi video_device */
1953 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1954 vbi_nr[dev->devno]);
1955 if (ret < 0) {
1956 em28xx_errdev("unable to register vbi device\n");
1957 return ret;
1958 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001959 }
1960
1961 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001962 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1963 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001964 if (!dev->radio_dev) {
1965 em28xx_errdev("cannot allocate video_device.\n");
1966 return -ENODEV;
1967 }
1968 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1969 radio_nr[dev->devno]);
1970 if (ret < 0) {
1971 em28xx_errdev("can't register radio device\n");
1972 return ret;
1973 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001974 em28xx_info("Registered radio device as %s\n",
1975 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001976 }
1977
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001978 em28xx_info("V4L2 video device registered as %s\n",
1979 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001980
1981 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001982 em28xx_info("V4L2 VBI device registered as %s\n",
1983 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001984
1985 return 0;
1986}