blob: ef1959bbd23f7958253e37c225ef2a915370f9d7 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
Frank Schaefer0fa4a402012-11-08 14:11:45 -03009 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080010
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020011 Some parts based on SN9C10x PC Camera Controllers GPL driver made
12 by Luca Risolia <luca.risolia@studio.unibo.it>
13
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/module.h>
32#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020033#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Hans Verkuil50fdf402012-09-07 06:10:12 -030043#include <media/v4l2-event.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030044#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030045#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030046#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
49 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030050 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080052
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080053#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030054
Frank Schaefer43a20d72013-02-11 14:07:55 -030055#define EM28XX_VERSION "0.2.0"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080057#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080058 if (video_debug) \
59 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030060 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080061
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030063module_param(isoc_debug, int, 0644);
64MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030065
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030066#define em28xx_isocdbg(fmt, arg...) \
67do {\
68 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030069 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030070 dev->name, __func__ , ##arg); \
71 } \
72 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030073
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080074MODULE_AUTHOR(DRIVER_AUTHOR);
75MODULE_DESCRIPTION(DRIVER_DESC);
76MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030077MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080078
Frank Schaefere507e0e2013-03-26 13:38:38 -030079
80#define EM25XX_FRMDATAHDR_BYTE1 0x02
81#define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
82#define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
83#define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
84#define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
85 EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
86 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
87
88
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -030089static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
90static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
91static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030092
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020093module_param_array(video_nr, int, NULL, 0444);
94module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030095module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030096MODULE_PARM_DESC(video_nr, "video device numbers");
97MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
98MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080099
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300100static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300101module_param(video_debug, int, 0644);
102MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800103
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300104/* supported video standards */
105static struct em28xx_fmt format[] = {
106 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300107 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300108 .fourcc = V4L2_PIX_FMT_YUYV,
109 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -0300110 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300111 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300112 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300113 .fourcc = V4L2_PIX_FMT_RGB565,
114 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300115 .reg = EM28XX_OUTFMT_RGB_16_656,
116 }, {
117 .name = "8 bpp Bayer BGBG..GRGR",
118 .fourcc = V4L2_PIX_FMT_SBGGR8,
119 .depth = 8,
120 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
121 }, {
122 .name = "8 bpp Bayer GRGR..BGBG",
123 .fourcc = V4L2_PIX_FMT_SGRBG8,
124 .depth = 8,
125 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
126 }, {
127 .name = "8 bpp Bayer GBGB..RGRG",
128 .fourcc = V4L2_PIX_FMT_SGBRG8,
129 .depth = 8,
130 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
131 }, {
132 .name = "12 bpp YUV411",
133 .fourcc = V4L2_PIX_FMT_YUV411P,
134 .depth = 12,
135 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300136 },
137};
138
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300139/* ------------------------------------------------------------------
140 DMA and thread functions
141 ------------------------------------------------------------------*/
142
143/*
Frank Schaefer948a49a2012-12-08 11:31:25 -0300144 * Finish the current buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300145 */
Frank Schaefer948a49a2012-12-08 11:31:25 -0300146static inline void finish_buffer(struct em28xx *dev,
147 struct em28xx_buffer *buf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300148{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300149 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
150
151 buf->vb.v4l2_buf.sequence = dev->field_count++;
152 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
153 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
154
155 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300156}
157
158/*
Frank Schaefer36016a32012-12-08 11:31:32 -0300159 * Copy picture data from USB buffer to videobuf buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 */
161static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300163 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300164 unsigned long len)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300165{
166 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300167 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300168 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300170 if (buf->pos + len > buf->length)
171 len = buf->length - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300172
Frank Schaefer36016a32012-12-08 11:31:32 -0300173 startread = usb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300174 remain = len;
175
Frank Schaeferc02ec712012-11-08 14:11:33 -0300176 if (dev->progressive || buf->top_field)
Frank Schaefer36016a32012-12-08 11:31:32 -0300177 fieldstart = buf->vb_buf;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300178 else /* interlaced mode, even nr. of lines */
Frank Schaefer36016a32012-12-08 11:31:32 -0300179 fieldstart = buf->vb_buf + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300180
Frank Schaefer87325332012-12-08 11:31:27 -0300181 linesdone = buf->pos / bytesperline;
182 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300183
184 if (dev->progressive)
185 offset = linesdone * bytesperline + currlinedone;
186 else
187 offset = linesdone * bytesperline * 2 + currlinedone;
188
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300189 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300190 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300191 lencopy = lencopy > remain ? remain : lencopy;
192
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300193 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300194 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Frank Schaefer36016a32012-12-08 11:31:32 -0300195 ((char *)startwrite + lencopy) -
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300196 ((char *)buf->vb_buf + buf->length));
197 remain = (char *)buf->vb_buf + buf->length -
Frank Schaefer36016a32012-12-08 11:31:32 -0300198 (char *)startwrite;
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300199 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300200 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300201 if (lencopy <= 0)
202 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300203 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
205 remain -= lencopy;
206
207 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300208 if (dev->progressive)
209 startwrite += lencopy;
210 else
211 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300213 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300214 lencopy = remain;
215 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300217
Frank Schaefer36016a32012-12-08 11:31:32 -0300218 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300219 buf->length) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300220 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
221 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300222 ((char *)startwrite + lencopy) -
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300223 ((char *)buf->vb_buf + buf->length));
224 lencopy = remain = (char *)buf->vb_buf + buf->length -
225 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300226 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227 if (lencopy <= 0)
228 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300229
230 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300231
232 remain -= lencopy;
233 }
234
Frank Schaefer87325332012-12-08 11:31:27 -0300235 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300236}
237
Frank Schaefer36016a32012-12-08 11:31:32 -0300238/*
239 * Copy VBI data from USB buffer to videobuf buffer
240 */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300241static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300242 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300243 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300244 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300245{
Frank Schaefer36016a32012-12-08 11:31:32 -0300246 unsigned int offset;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300247
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300248 if (buf->pos + len > buf->length)
249 len = buf->length - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300250
Frank Schaefer87325332012-12-08 11:31:27 -0300251 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300252 /* Make sure the bottom field populates the second half of the frame */
Frank Schaefer36016a32012-12-08 11:31:32 -0300253 if (buf->top_field == 0)
254 offset += dev->vbi_width * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300255
Frank Schaefer36016a32012-12-08 11:31:32 -0300256 memcpy(buf->vb_buf + offset, usb_buf, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300257 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300258}
259
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300260static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300261 int packet, int status)
262{
263 char *errmsg = "Unknown";
264
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300265 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266 case -ENOENT:
267 errmsg = "unlinked synchronuously";
268 break;
269 case -ECONNRESET:
270 errmsg = "unlinked asynchronuously";
271 break;
272 case -ENOSR:
273 errmsg = "Buffer error (overrun)";
274 break;
275 case -EPIPE:
276 errmsg = "Stalled (device not responding)";
277 break;
278 case -EOVERFLOW:
279 errmsg = "Babble (bad cable?)";
280 break;
281 case -EPROTO:
282 errmsg = "Bit-stuff error (bad cable?)";
283 break;
284 case -EILSEQ:
285 errmsg = "CRC/Timeout (could be anything)";
286 break;
287 case -ETIME:
288 errmsg = "Device does not respond";
289 break;
290 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300291 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300292 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
293 } else {
294 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
295 packet, status, errmsg);
296 }
297}
298
299/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300300 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300302static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
303 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300304{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300305 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300306
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300307 if (list_empty(&dma_q->active)) {
308 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300309 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300310 }
311
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300312 /* Get the next buffer */
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300313 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300314 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300315 list_del(&buf->list);
Frank Schaefer87325332012-12-08 11:31:27 -0300316 buf->pos = 0;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300317 buf->vb_buf = buf->mem;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300318
Frank Schaefer24a6d842012-12-08 11:31:24 -0300319 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300320}
321
Frank Schaefere04c00d2012-12-08 11:31:30 -0300322/*
323 * Finish the current buffer if completed and prepare for the next field
324 */
325static struct em28xx_buffer *
326finish_field_prepare_next(struct em28xx *dev,
327 struct em28xx_buffer *buf,
328 struct em28xx_dmaqueue *dma_q)
329{
330 if (dev->progressive || dev->top_field) { /* Brand new frame */
331 if (buf != NULL)
332 finish_buffer(dev, buf);
333 buf = get_next_buf(dev, dma_q);
334 }
335 if (buf != NULL) {
336 buf->top_field = dev->top_field;
337 buf->pos = 0;
338 }
339
340 return buf;
341}
342
Frank Schaefer227b7c92012-12-08 11:31:31 -0300343/*
344 * Process data packet according to the em2710/em2750/em28xx frame data format
345 */
346static inline void process_frame_data_em28xx(struct em28xx *dev,
347 unsigned char *data_pkt,
348 unsigned int data_len)
349{
350 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
351 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
352 struct em28xx_dmaqueue *dma_q = &dev->vidq;
353 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
354
355 /* capture type 0 = vbi start
356 capture type 1 = vbi in progress
357 capture type 2 = video start
358 capture type 3 = video in progress */
359 if (data_len >= 4) {
360 /* NOTE: Headers are always 4 bytes and
361 * never split across packets */
362 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
363 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
364 /* Continuation */
365 data_pkt += 4;
366 data_len -= 4;
367 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
368 /* Field start (VBI mode) */
369 dev->capture_type = 0;
370 dev->vbi_read = 0;
371 em28xx_isocdbg("VBI START HEADER !!!\n");
372 dev->top_field = !(data_pkt[2] & 1);
373 data_pkt += 4;
374 data_len -= 4;
375 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
376 /* Field start (VBI disabled) */
377 dev->capture_type = 2;
378 em28xx_isocdbg("VIDEO START HEADER !!!\n");
379 dev->top_field = !(data_pkt[2] & 1);
380 data_pkt += 4;
381 data_len -= 4;
382 }
383 }
384 /* NOTE: With bulk transfers, intermediate data packets
385 * have no continuation header */
386
387 if (dev->capture_type == 0) {
388 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
389 dev->usb_ctl.vbi_buf = vbi_buf;
390 dev->capture_type = 1;
391 }
392
393 if (dev->capture_type == 1) {
394 int vbi_size = dev->vbi_width * dev->vbi_height;
395 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
396 (vbi_size - dev->vbi_read) : data_len;
397
398 /* Copy VBI data */
399 if (vbi_buf != NULL)
400 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
401 dev->vbi_read += vbi_data_len;
402
403 if (vbi_data_len < data_len) {
404 /* Continue with copying video data */
405 dev->capture_type = 2;
406 data_pkt += vbi_data_len;
407 data_len -= vbi_data_len;
408 }
409 }
410
411 if (dev->capture_type == 2) {
412 buf = finish_field_prepare_next(dev, buf, dma_q);
413 dev->usb_ctl.vid_buf = buf;
414 dev->capture_type = 3;
415 }
416
417 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
418 em28xx_copy_video(dev, buf, data_pkt, data_len);
419}
420
Frank Schaefere507e0e2013-03-26 13:38:38 -0300421/*
422 * Process data packet according to the em25xx/em276x/7x/8x frame data format
423 */
424static inline void process_frame_data_em25xx(struct em28xx *dev,
425 unsigned char *data_pkt,
426 unsigned int data_len)
427{
428 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
429 struct em28xx_dmaqueue *dmaq = &dev->vidq;
430 bool frame_end = 0;
431
432 /* Check for header */
433 /* NOTE: at least with bulk transfers, only the first packet
434 * has a header and has always set the FRAME_END bit */
435 if (data_len >= 2) { /* em25xx header is only 2 bytes long */
436 if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
437 ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
438 dev->top_field = !(data_pkt[1] &
439 EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
440 frame_end = data_pkt[1] &
441 EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
442 data_pkt += 2;
443 data_len -= 2;
444 }
445
446 /* Finish field and prepare next (BULK only) */
447 if (dev->analog_xfer_bulk && frame_end) {
448 buf = finish_field_prepare_next(dev, buf, dmaq);
449 dev->usb_ctl.vid_buf = buf;
450 }
451 /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
452 * we COULD already prepare a buffer here to avoid skipping the
453 * first frame.
454 */
455 }
456
457 /* Copy data */
458 if (buf != NULL && data_len > 0)
459 em28xx_copy_video(dev, buf, data_pkt, data_len);
460
461 /* Finish frame (ISOC only) => avoids lag of 1 frame */
462 if (!dev->analog_xfer_bulk && frame_end) {
463 buf = finish_field_prepare_next(dev, buf, dmaq);
464 dev->usb_ctl.vid_buf = buf;
465 }
466
467 /* NOTE: Tested with USB bulk transfers only !
468 * The wording in the datasheet suggests that isoc might work different.
469 * The current code assumes that with isoc transfers each packet has a
470 * header like with the other em28xx devices.
471 */
472 /* NOTE: Support for interlaced mode is pure theory. It has not been
473 * tested and it is unknown if these devices actually support it. */
474 /* NOTE: No VBI support yet (these chips likely do not support VBI). */
475}
476
Frank Schaefer960da932012-11-25 06:37:37 -0300477/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300478static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300480 int xfer_bulk, num_packets, i;
481 unsigned char *usb_data_pkt;
482 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300483
484 if (!dev)
485 return 0;
486
Frank Schaefer2665c292012-12-27 19:02:43 -0300487 if (dev->disconnected)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300488 return 0;
489
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300490 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300491 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300492
Frank Schaefer4601cc32012-11-08 14:11:46 -0300493 xfer_bulk = usb_pipebulk(urb->pipe);
494
Frank Schaefer4601cc32012-11-08 14:11:46 -0300495 if (xfer_bulk) /* bulk */
496 num_packets = 1;
497 else /* isoc */
498 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300499
Frank Schaefer4601cc32012-11-08 14:11:46 -0300500 for (i = 0; i < num_packets; i++) {
501 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300502 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300503
Frank Schaefer227b7c92012-12-08 11:31:31 -0300504 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300505 } else { /* isoc */
506 if (urb->iso_frame_desc[i].status < 0) {
507 print_err_status(dev, i,
508 urb->iso_frame_desc[i].status);
509 if (urb->iso_frame_desc[i].status != -EPROTO)
510 continue;
511 }
512
Frank Schaefer227b7c92012-12-08 11:31:31 -0300513 usb_data_len = urb->iso_frame_desc[i].actual_length;
514 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300515 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300516 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300517 }
518
Frank Schaefer227b7c92012-12-08 11:31:31 -0300519 usb_data_pkt = urb->transfer_buffer +
520 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300521 }
522
Frank Schaefer227b7c92012-12-08 11:31:31 -0300523 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300524 /* NOTE: happens very often with isoc transfers */
525 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300526 continue;
527 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300528
Frank Schaefere507e0e2013-03-26 13:38:38 -0300529 if (dev->is_em25xx)
530 process_frame_data_em25xx(dev,
531 usb_data_pkt, usb_data_len);
532 else
533 process_frame_data_em28xx(dev,
534 usb_data_pkt, usb_data_len);
535
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300536 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300537 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300538}
539
540
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300541static int get_ressource(enum v4l2_buf_type f_type)
542{
543 switch (f_type) {
544 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
545 return EM28XX_RESOURCE_VIDEO;
546 case V4L2_BUF_TYPE_VBI_CAPTURE:
547 return EM28XX_RESOURCE_VBI;
548 default:
549 BUG();
550 return 0;
551 }
552}
553
554/* Usage lock check functions */
555static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
556{
557 int res_type = get_ressource(f_type);
558
559 /* is it free? */
560 if (dev->resources & res_type) {
561 /* no, someone else uses it */
562 return -EBUSY;
563 }
564
565 /* it's free, grab it */
566 dev->resources |= res_type;
567 em28xx_videodbg("res: get %d\n", res_type);
568 return 0;
569}
570
571static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
572{
573 int res_type = get_ressource(f_type);
574
575 dev->resources &= ~res_type;
576 em28xx_videodbg("res: put %d\n", res_type);
577}
578
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300579/* ------------------------------------------------------------------
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300580 Videobuf2 operations
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300581 ------------------------------------------------------------------*/
582
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300583static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
584 unsigned int *nbuffers, unsigned int *nplanes,
585 unsigned int sizes[], void *alloc_ctxs[])
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300586{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300587 struct em28xx *dev = vb2_get_drv_priv(vq);
588 unsigned long size;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300589
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300590 if (fmt)
591 size = fmt->fmt.pix.sizeimage;
592 else
593 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300594
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300595 if (size == 0)
596 return -EINVAL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300597
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300598 if (0 == *nbuffers)
599 *nbuffers = 32;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300600
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300601 *nplanes = 1;
602 sizes[0] = size;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300603
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300604 return 0;
605}
606
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300607static int
608buffer_prepare(struct vb2_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300609{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300610 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
611 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
612 unsigned long size;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300613
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300614 em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300615
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300616 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300617
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300618 if (vb2_plane_size(vb, 0) < size) {
619 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
620 __func__, vb2_plane_size(vb, 0), size);
621 return -EINVAL;
622 }
623 vb2_set_plane_payload(&buf->vb, 0, size);
624
625 return 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300626}
627
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300628int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300629{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300630 struct em28xx *dev = vb2_get_drv_priv(vq);
631 struct v4l2_frequency f;
632 int rc = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300633
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300634 em28xx_videodbg("%s\n", __func__);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300635
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300636 /* Make sure streaming is not already in progress for this type
637 of filehandle (e.g. video, vbi) */
638 rc = res_get(dev, vq->type);
639 if (rc)
640 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300641
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300642 if (dev->streaming_users++ == 0) {
643 /* First active streaming user, so allocate all the URBs */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300644
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300645 /* Allocate the USB bandwidth */
646 em28xx_set_alternate(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300647
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300648 /* Needed, since GPIO might have disabled power of
649 some i2c device
650 */
651 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300652
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300653 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300654 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
655 dev->analog_xfer_bulk,
656 EM28XX_NUM_BUFS,
657 dev->max_pkt_size,
658 dev->packet_multiplier,
659 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300660 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661 goto fail;
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -0300662
663 /*
664 * djh: it's not clear whether this code is still needed. I'm
665 * leaving it in here for now entirely out of concern for
666 * backward compatibility (the old code did it)
667 */
668
669 /* Ask tuner to go to analog or radio mode */
670 memset(&f, 0, sizeof(f));
671 f.frequency = dev->ctl_freq;
672 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
673 f.type = V4L2_TUNER_RADIO;
674 else
675 f.type = V4L2_TUNER_ANALOG_TV;
676 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300677 }
678
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679fail:
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);
Frank Schaeferb8374132013-02-10 16:05:13 -0300999 scale_to_size(dev, hscale, hscale, &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;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 return 0;
1014}
1015
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001016static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1017 unsigned width, unsigned height)
1018{
1019 struct em28xx_fmt *fmt;
1020
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001021 fmt = format_by_fourcc(fourcc);
1022 if (!fmt)
1023 return -EINVAL;
1024
1025 dev->format = fmt;
1026 dev->width = width;
1027 dev->height = height;
1028
1029 /* set new image size */
Frank Schaefer6b09a212013-02-10 16:05:12 -03001030 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001031
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001032 em28xx_resolution_set(dev);
1033
1034 return 0;
1035}
1036
Hans Verkuil78b526a2008-05-28 12:16:41 -03001037static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038 struct v4l2_format *f)
1039{
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001040 struct em28xx *dev = video_drvdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001041
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001042 if (dev->streaming_users > 0)
1043 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001045 vidioc_try_fmt_vid_cap(file, priv, f);
1046
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001047 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001048 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001049}
1050
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001051static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1052{
1053 struct em28xx_fh *fh = priv;
1054 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001055
1056 *norm = dev->norm;
1057
1058 return 0;
1059}
1060
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001061static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1062{
1063 struct em28xx_fh *fh = priv;
1064 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001065
1066 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1067
1068 return 0;
1069}
1070
Hans Verkuil314527a2013-03-15 06:10:40 -03001071static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001072{
1073 struct em28xx_fh *fh = priv;
1074 struct em28xx *dev = fh->dev;
1075 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001076
Hans Verkuil314527a2013-03-15 06:10:40 -03001077 if (norm == dev->norm)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001078 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001079
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001080 if (dev->streaming_users > 0)
Hans Verkuild8c95c02012-09-07 07:31:54 -03001081 return -EBUSY;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001082
Hans Verkuil314527a2013-03-15 06:10:40 -03001083 dev->norm = norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001084
1085 /* Adjusts width/height, if needed */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001086 f.fmt.pix.width = 720;
Hans Verkuil314527a2013-03-15 06:10:40 -03001087 f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001088 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001089
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090 /* set new image size */
1091 dev->width = f.fmt.pix.width;
1092 dev->height = f.fmt.pix.height;
Frank Schaefer6b09a212013-02-10 16:05:12 -03001093 size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001094
1095 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001096 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001097
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001098 return 0;
1099}
1100
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001101static int vidioc_g_parm(struct file *file, void *priv,
1102 struct v4l2_streamparm *p)
1103{
1104 struct em28xx_fh *fh = priv;
1105 struct em28xx *dev = fh->dev;
1106 int rc = 0;
1107
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001108 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001109 if (dev->board.is_webcam)
1110 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1111 video, g_parm, p);
1112 else
1113 v4l2_video_std_frame_period(dev->norm,
1114 &p->parm.capture.timeperframe);
1115
1116 return rc;
1117}
1118
1119static int vidioc_s_parm(struct file *file, void *priv,
1120 struct v4l2_streamparm *p)
1121{
1122 struct em28xx_fh *fh = priv;
1123 struct em28xx *dev = fh->dev;
1124
Hans Verkuil86ff7f12012-09-07 06:16:03 -03001125 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001126 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1127}
1128
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129static const char *iname[] = {
1130 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1131 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1132 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1133 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1134 [EM28XX_VMUX_SVIDEO] = "S-Video",
1135 [EM28XX_VMUX_TELEVISION] = "Television",
1136 [EM28XX_VMUX_CABLE] = "Cable TV",
1137 [EM28XX_VMUX_DVB] = "DVB",
1138 [EM28XX_VMUX_DEBUG] = "for debug only",
1139};
1140
1141static int vidioc_enum_input(struct file *file, void *priv,
1142 struct v4l2_input *i)
1143{
1144 struct em28xx_fh *fh = priv;
1145 struct em28xx *dev = fh->dev;
1146 unsigned int n;
1147
1148 n = i->index;
1149 if (n >= MAX_EM28XX_INPUT)
1150 return -EINVAL;
1151 if (0 == INPUT(n)->type)
1152 return -EINVAL;
1153
1154 i->index = n;
1155 i->type = V4L2_INPUT_TYPE_CAMERA;
1156
1157 strcpy(i->name, iname[INPUT(n)->type]);
1158
1159 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1160 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1161 i->type = V4L2_INPUT_TYPE_TUNER;
1162
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001163 i->std = dev->vdev->tvnorms;
Hans Verkuild8c95c02012-09-07 07:31:54 -03001164 /* webcams do not have the STD API */
1165 if (dev->board.is_webcam)
1166 i->capabilities = 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001167
1168 return 0;
1169}
1170
1171static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1172{
1173 struct em28xx_fh *fh = priv;
1174 struct em28xx *dev = fh->dev;
1175
1176 *i = dev->ctl_input;
1177
1178 return 0;
1179}
1180
1181static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1182{
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001185
1186 if (i >= MAX_EM28XX_INPUT)
1187 return -EINVAL;
1188 if (0 == INPUT(i)->type)
1189 return -EINVAL;
1190
Ezequiel García96371fc2012-03-23 18:09:34 -03001191 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001192 return 0;
1193}
1194
1195static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1196{
1197 struct em28xx_fh *fh = priv;
1198 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001200 switch (a->index) {
1201 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001202 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001203 break;
1204 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001205 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001206 break;
1207 case EM28XX_AMUX_VIDEO2:
1208 strcpy(a->name, "Television alt");
1209 break;
1210 case EM28XX_AMUX_PHONE:
1211 strcpy(a->name, "Phone");
1212 break;
1213 case EM28XX_AMUX_MIC:
1214 strcpy(a->name, "Mic");
1215 break;
1216 case EM28XX_AMUX_CD:
1217 strcpy(a->name, "CD");
1218 break;
1219 case EM28XX_AMUX_AUX:
1220 strcpy(a->name, "Aux");
1221 break;
1222 case EM28XX_AMUX_PCM_OUT:
1223 strcpy(a->name, "PCM");
1224 break;
1225 default:
1226 return -EINVAL;
1227 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001228
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001229 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231
1232 return 0;
1233}
1234
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001235static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001236{
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1239
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001240 if (a->index >= MAX_EM28XX_INPUT)
1241 return -EINVAL;
1242 if (0 == INPUT(a->index)->type)
1243 return -EINVAL;
1244
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001245 dev->ctl_ainput = INPUT(a->index)->amux;
1246 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001247
1248 if (!dev->ctl_aoutput)
1249 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001250
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001251 return 0;
1252}
1253
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001254static int vidioc_g_tuner(struct file *file, void *priv,
1255 struct v4l2_tuner *t)
1256{
1257 struct em28xx_fh *fh = priv;
1258 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001259
1260 if (0 != t->index)
1261 return -EINVAL;
1262
1263 strcpy(t->name, "Tuner");
1264
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001265 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001266 return 0;
1267}
1268
1269static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001270 const struct v4l2_tuner *t)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001271{
1272 struct em28xx_fh *fh = priv;
1273 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001274
1275 if (0 != t->index)
1276 return -EINVAL;
1277
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001278 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001279 return 0;
1280}
1281
1282static int vidioc_g_frequency(struct file *file, void *priv,
1283 struct v4l2_frequency *f)
1284{
1285 struct em28xx_fh *fh = priv;
1286 struct em28xx *dev = fh->dev;
1287
Hans Verkuil20deebf2012-09-06 10:07:25 -03001288 if (0 != f->tuner)
1289 return -EINVAL;
1290
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001292 return 0;
1293}
1294
1295static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001296 const struct v4l2_frequency *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297{
Hans Verkuilb530a442013-03-19 04:09:26 -03001298 struct v4l2_frequency new_freq = *f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001299 struct em28xx_fh *fh = priv;
1300 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001301
1302 if (0 != f->tuner)
1303 return -EINVAL;
1304
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001305 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuilb530a442013-03-19 04:09:26 -03001306 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1307 dev->ctl_freq = new_freq.frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001308
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309 return 0;
1310}
1311
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001312static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001313 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001314{
1315 struct em28xx_fh *fh = priv;
1316 struct em28xx *dev = fh->dev;
1317
1318 chip->ident = V4L2_IDENT_NONE;
1319 chip->revision = 0;
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001320 if (chip->match.type == V4L2_CHIP_MATCH_BRIDGE) {
1321 if (chip->match.addr > 1)
1322 return -EINVAL;
Hans Verkuil319a55f2012-09-06 09:53:08 -03001323 return 0;
1324 }
1325 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1326 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1327 return -EINVAL;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001328
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001329 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001330
1331 return 0;
1332}
1333
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001334static int vidioc_g_chip_name(struct file *file, void *priv,
1335 struct v4l2_dbg_chip_name *chip)
1336{
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
1339
1340 if (chip->match.addr > 1)
1341 return -EINVAL;
1342 if (chip->match.addr == 1)
1343 strlcpy(chip->name, "ac97", sizeof(chip->name));
1344 else
1345 strlcpy(chip->name, dev->v4l2_dev.name, sizeof(chip->name));
1346 return 0;
1347}
1348
Frank Schaefer35deba32013-02-07 13:39:19 -03001349#ifdef CONFIG_VIDEO_ADV_DEBUG
1350static int em28xx_reg_len(int reg)
1351{
1352 switch (reg) {
1353 case EM28XX_R40_AC97LSB:
1354 case EM28XX_R30_HSCALELOW:
1355 case EM28XX_R32_VSCALELOW:
1356 return 2;
1357 default:
1358 return 1;
1359 }
1360}
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001361
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001362static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001363 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001364{
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1367 int ret;
1368
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001369 switch (reg->match.type) {
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001370 case V4L2_CHIP_MATCH_BRIDGE:
1371 if (reg->match.addr > 1)
1372 return -EINVAL;
1373 if (!reg->match.addr)
1374 break;
1375 /* fall-through */
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001376 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001377 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001378 if (ret < 0)
1379 return ret;
1380
1381 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001382 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001383 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001384 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001385 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001386 return 0;
1387 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001388 /* TODO: is this correct? */
1389 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1390 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001391 default:
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001392 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001393 }
1394
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001395 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001396 reg->size = em28xx_reg_len(reg->reg);
1397 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001398 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001399
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001400 if (ret < 0)
1401 return ret;
1402
1403 reg->val = ret;
1404 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001405 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001406 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1407 reg->reg, (char *)&val, 2);
1408 if (ret < 0)
1409 return ret;
1410
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001411 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001412 }
1413
1414 return 0;
1415}
1416
1417static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001418 const struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001419{
1420 struct em28xx_fh *fh = priv;
1421 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001422 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001423
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001424 switch (reg->match.type) {
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001425 case V4L2_CHIP_MATCH_BRIDGE:
1426 if (reg->match.addr > 1)
1427 return -EINVAL;
1428 if (!reg->match.addr)
1429 break;
1430 /* fall-through */
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001431 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001432 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001433 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001434 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001435 return 0;
1436 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001437 /* TODO: is this correct? */
1438 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1439 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001440 default:
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001441 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001442 }
1443
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001444 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001445 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001446
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001447 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001448 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001449}
1450#endif
1451
1452
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001453static int vidioc_querycap(struct file *file, void *priv,
1454 struct v4l2_capability *cap)
1455{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001456 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
1459
1460 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1461 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001462 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001463
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001464 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1465 cap->device_caps = V4L2_CAP_READWRITE |
1466 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1467 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1468 cap->device_caps = V4L2_CAP_RADIO;
1469 else
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001470 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001471
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001472 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001473 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001474
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001475 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001476 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001477
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001478 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1479 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1480 if (dev->vbi_dev)
Hans Verkuil1d179ee2012-09-07 08:45:10 -03001481 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001482 if (dev->radio_dev)
1483 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001484 return 0;
1485}
1486
Hans Verkuil78b526a2008-05-28 12:16:41 -03001487static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001488 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001489{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001490 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001491 return -EINVAL;
1492
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001493 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1494 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001495
1496 return 0;
1497}
1498
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001499static int vidioc_enum_framesizes(struct file *file, void *priv,
1500 struct v4l2_frmsizeenum *fsize)
1501{
1502 struct em28xx_fh *fh = priv;
1503 struct em28xx *dev = fh->dev;
1504 struct em28xx_fmt *fmt;
1505 unsigned int maxw = norm_maxw(dev);
1506 unsigned int maxh = norm_maxh(dev);
1507
1508 fmt = format_by_fourcc(fsize->pixel_format);
1509 if (!fmt) {
1510 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1511 fsize->pixel_format);
1512 return -EINVAL;
1513 }
1514
1515 if (dev->board.is_em2800) {
1516 if (fsize->index > 1)
1517 return -EINVAL;
1518 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1519 fsize->discrete.width = maxw / (1 + fsize->index);
1520 fsize->discrete.height = maxh / (1 + fsize->index);
1521 return 0;
1522 }
1523
1524 if (fsize->index != 0)
1525 return -EINVAL;
1526
1527 /* Report a continuous range */
1528 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
Frank Schaefer6c3598e2013-02-10 16:05:14 -03001529 scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
1530 &fsize->stepwise.min_width, &fsize->stepwise.min_height);
1531 if (fsize->stepwise.min_width < 48)
1532 fsize->stepwise.min_width = 48;
1533 if (fsize->stepwise.min_height < 38)
1534 fsize->stepwise.min_height = 38;
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001535 fsize->stepwise.max_width = maxw;
1536 fsize->stepwise.max_height = maxh;
1537 fsize->stepwise.step_width = 1;
1538 fsize->stepwise.step_height = 1;
1539 return 0;
1540}
1541
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001542/* RAW VBI ioctls */
1543
1544static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1545 struct v4l2_format *format)
1546{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001547 struct em28xx_fh *fh = priv;
1548 struct em28xx *dev = fh->dev;
1549
1550 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001551 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1552 format->fmt.vbi.offset = 0;
1553 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001554 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1555 format->fmt.vbi.count[0] = dev->vbi_height;
1556 format->fmt.vbi.count[1] = dev->vbi_height;
Hans Verkuil2a221d32012-09-07 08:51:32 -03001557 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001558
1559 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001560 if (dev->norm & V4L2_STD_525_60) {
1561 /* NTSC */
1562 format->fmt.vbi.start[0] = 10;
1563 format->fmt.vbi.start[1] = 273;
1564 } else if (dev->norm & V4L2_STD_625_50) {
1565 /* PAL */
1566 format->fmt.vbi.start[0] = 6;
1567 format->fmt.vbi.start[1] = 318;
1568 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001569
1570 return 0;
1571}
1572
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001573/* ----------------------------------------------------------- */
1574/* RADIO ESPECIFIC IOCTLS */
1575/* ----------------------------------------------------------- */
1576
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001577static int radio_g_tuner(struct file *file, void *priv,
1578 struct v4l2_tuner *t)
1579{
1580 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1581
1582 if (unlikely(t->index > 0))
1583 return -EINVAL;
1584
1585 strcpy(t->name, "Radio");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001586
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001587 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001588
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001589 return 0;
1590}
1591
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001592static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001593 const struct v4l2_tuner *t)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001594{
1595 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1596
1597 if (0 != t->index)
1598 return -EINVAL;
1599
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001600 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001601
1602 return 0;
1603}
1604
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001605/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001606 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001607 * inits the device and starts isoc transfer
1608 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001609static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001611 struct video_device *vdev = video_devdata(filp);
1612 struct em28xx *dev = video_drvdata(filp);
1613 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001614 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001615
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001616 switch (vdev->vfl_type) {
1617 case VFL_TYPE_GRABBER:
1618 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1619 break;
1620 case VFL_TYPE_VBI:
1621 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1622 break;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001623 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001624
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001625 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1626 video_device_node_name(vdev), v4l2_type_names[fh_type],
1627 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001628
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629
Hans Verkuil876cb142012-06-23 08:12:47 -03001630 if (mutex_lock_interruptible(&dev->lock))
1631 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001632 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001633 if (!fh) {
1634 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001635 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001636 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001637 }
Hans Verkuil69a61642012-09-07 05:52:40 -03001638 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001639 fh->dev = dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001640 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001641 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001643 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001644 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001645 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001647 /* Needed, since GPIO might have disabled power of
1648 some i2c device
1649 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001650 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001651
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001652 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001653
1654 if (vdev->vfl_type == VFL_TYPE_RADIO) {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001655 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001656 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001657 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658
1659 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660
Hans Verkuil876cb142012-06-23 08:12:47 -03001661 mutex_unlock(&dev->lock);
Hans Verkuil69a61642012-09-07 05:52:40 -03001662 v4l2_fh_add(&fh->fh);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001663
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001664 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001665}
1666
1667/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001668 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 * unregisters the v4l2,i2c and usb devices
1670 * called when the device gets disconected or at module unload
1671*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001672void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001675 /*FIXME: I2C IR should be disconnected */
1676
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001677 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001678 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001679 video_unregister_device(dev->radio_dev);
1680 else
1681 video_device_release(dev->radio_dev);
1682 dev->radio_dev = NULL;
1683 }
1684 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001685 em28xx_info("V4L2 device %s deregistered\n",
1686 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001687 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001688 video_unregister_device(dev->vbi_dev);
1689 else
1690 video_device_release(dev->vbi_dev);
1691 dev->vbi_dev = NULL;
1692 }
1693 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001694 em28xx_info("V4L2 device %s deregistered\n",
1695 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001696 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001697 video_unregister_device(dev->vdev);
1698 else
1699 video_device_release(dev->vdev);
1700 dev->vdev = NULL;
1701 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702}
1703
1704/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001705 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001706 * stops streaming and deallocates all resources allocated by the v4l2
1707 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001709static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 struct em28xx_fh *fh = filp->private_data;
1712 struct em28xx *dev = fh->dev;
1713 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001715 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001716
Hans Verkuil876cb142012-06-23 08:12:47 -03001717 mutex_lock(&dev->lock);
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001718 vb2_fop_release(filp);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001720 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001721 /* the device is already disconnect,
1722 free the remaining resources */
Frank Schaefer2665c292012-12-27 19:02:43 -03001723 if (dev->disconnected) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001724 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001725 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001726 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001727 kfree(dev);
1728 return 0;
1729 }
1730
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001731 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001732 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001733
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001734 /* do this before setting alternate! */
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001735 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001736
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001737 /* set alternate 0 */
1738 dev->alt = 0;
1739 em28xx_videodbg("setting alternate 0\n");
1740 errCode = usb_set_interface(dev->udev, 0, 0);
1741 if (errCode < 0) {
1742 em28xx_errdev("cannot change alternate number to "
1743 "0 (error=%i)\n", errCode);
1744 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001746
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001748 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001749 return 0;
1750}
1751
Hans Verkuilbec43662008-12-30 06:58:20 -03001752static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001753 .owner = THIS_MODULE,
1754 .open = em28xx_v4l2_open,
1755 .release = em28xx_v4l2_close,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001756 .read = vb2_fop_read,
1757 .poll = vb2_fop_poll,
1758 .mmap = vb2_fop_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001759 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760};
1761
Hans Verkuila3998102008-07-21 02:57:38 -03001762static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001763 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001764 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1765 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1766 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1767 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001768 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Hans Verkuil2a221d32012-09-07 08:51:32 -03001769 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Frank Schaeferaab34612013-02-07 13:39:16 -03001770 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001771 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001772 .vidioc_g_audio = vidioc_g_audio,
1773 .vidioc_s_audio = vidioc_s_audio,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001774
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001775 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1776 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1777 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1778 .vidioc_querybuf = vb2_ioctl_querybuf,
1779 .vidioc_qbuf = vb2_ioctl_qbuf,
1780 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1781
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001782 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001783 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001785 .vidioc_g_parm = vidioc_g_parm,
1786 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001787 .vidioc_enum_input = vidioc_enum_input,
1788 .vidioc_g_input = vidioc_g_input,
1789 .vidioc_s_input = vidioc_s_input,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001790 .vidioc_streamon = vb2_ioctl_streamon,
1791 .vidioc_streamoff = vb2_ioctl_streamoff,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 .vidioc_g_tuner = vidioc_g_tuner,
1793 .vidioc_s_tuner = vidioc_s_tuner,
1794 .vidioc_g_frequency = vidioc_g_frequency,
1795 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001796 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1797 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Frank Schaefer35deba32013-02-07 13:39:19 -03001798 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001799 .vidioc_g_chip_name = vidioc_g_chip_name,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001800#ifdef CONFIG_VIDEO_ADV_DEBUG
1801 .vidioc_g_register = vidioc_g_register,
1802 .vidioc_s_register = vidioc_s_register,
1803#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001804};
1805
1806static const struct video_device em28xx_video_template = {
1807 .fops = &em28xx_v4l_fops,
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001808 .release = video_device_release_empty,
Hans Verkuila3998102008-07-21 02:57:38 -03001809 .ioctl_ops = &video_ioctl_ops,
1810
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001811 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001812};
1813
Hans Verkuilbec43662008-12-30 06:58:20 -03001814static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001815 .owner = THIS_MODULE,
1816 .open = em28xx_v4l2_open,
1817 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03001818 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001819};
1820
1821static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001822 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001823 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001824 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001825 .vidioc_g_frequency = vidioc_g_frequency,
1826 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil50fdf402012-09-07 06:10:12 -03001827 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1828 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Frank Schaeferfff459e32013-02-07 13:39:20 -03001829 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Hans Verkuil3b2d17b2013-03-18 12:21:53 -03001830 .vidioc_g_chip_name = vidioc_g_chip_name,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001831#ifdef CONFIG_VIDEO_ADV_DEBUG
1832 .vidioc_g_register = vidioc_g_register,
1833 .vidioc_s_register = vidioc_s_register,
1834#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001835};
1836
Hans Verkuila3998102008-07-21 02:57:38 -03001837static struct video_device em28xx_radio_template = {
1838 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001839 .fops = &radio_fops,
1840 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001841};
1842
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001843/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001844
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001845
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001846
Adrian Bunk532fe652008-01-28 22:10:48 -03001847static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001848 const struct video_device *template,
1849 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001850{
1851 struct video_device *vfd;
1852
1853 vfd = video_device_alloc();
1854 if (NULL == vfd)
1855 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001856
1857 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001858 vfd->v4l2_dev = &dev->v4l2_dev;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001859 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001860 vfd->lock = &dev->lock;
Hans Verkuil69a61642012-09-07 05:52:40 -03001861 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuild8c95c02012-09-07 07:31:54 -03001862 if (dev->board.is_webcam)
1863 vfd->tvnorms = 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001864
1865 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1866 dev->name, type_name);
1867
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001868 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001869 return vfd;
1870}
1871
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001872int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001873{
Hans Verkuil081b9452012-09-07 05:43:59 -03001874 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001875 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03001876 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001877
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03001878 printk(KERN_INFO "%s: v4l2 driver version %s\n",
1879 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001880
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001881 /* set default norm */
Hans Verkuild8c95c02012-09-07 07:31:54 -03001882 dev->norm = V4L2_STD_PAL;
Hans Verkuild5906dd2010-09-26 07:45:15 -03001883 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001884 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001885
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001886 /* Analog specific initialization */
1887 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03001888
1889 maxw = norm_maxw(dev);
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001890 /* MaxPacketSize for em2800 is too small to capture at full resolution
1891 * use half of maxw as the scaler can only scale to 50% */
1892 if (dev->board.is_em2800)
1893 maxw /= 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001894
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001895 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03001896 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001897
Ezequiel García96371fc2012-03-23 18:09:34 -03001898 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001899
1900 /* Audio defaults */
1901 dev->mute = 1;
1902 dev->volume = 0x1f;
1903
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001904/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001905 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1906 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1907 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001908
1909 em28xx_set_outfmt(dev);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001910 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001911
Frank Schaefer8f8b113a2013-02-15 14:38:32 -03001912 /* Add image controls */
1913 /* NOTE: at this point, the subdevices are already registered, so bridge
1914 * controls are only added/enabled when no subdevice provides them */
1915 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_CONTRAST))
1916 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1917 V4L2_CID_CONTRAST,
1918 0, 0x1f, 1, CONTRAST_DEFAULT);
1919 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BRIGHTNESS))
1920 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1921 V4L2_CID_BRIGHTNESS,
1922 -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
1923 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SATURATION))
1924 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1925 V4L2_CID_SATURATION,
1926 0, 0x1f, 1, SATURATION_DEFAULT);
1927 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BLUE_BALANCE))
1928 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1929 V4L2_CID_BLUE_BALANCE,
1930 -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
1931 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RED_BALANCE))
1932 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1933 V4L2_CID_RED_BALANCE,
1934 -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
1935 if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SHARPNESS))
1936 v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
1937 V4L2_CID_SHARPNESS,
1938 0, 0x0f, 1, SHARPNESS_DEFAULT);
1939
1940 /* Reset image controls */
1941 em28xx_colorlevels_set_default(dev);
1942 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
1943 if (dev->ctrl_handler.error)
1944 return dev->ctrl_handler.error;
1945
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001946 /* allocate and fill video video_device struct */
1947 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1948 if (!dev->vdev) {
1949 em28xx_errdev("cannot allocate video_device.\n");
1950 return -ENODEV;
1951 }
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001952 dev->vdev->queue = &dev->vb_vidq;
1953 dev->vdev->queue->lock = &dev->vb_queue_lock;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001954
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001955 /* disable inapplicable ioctls */
1956 if (dev->board.is_webcam) {
1957 v4l2_disable_ioctl(dev->vdev, VIDIOC_QUERYSTD);
1958 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_STD);
1959 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_STD);
Frank Schaefer3bc85cc2013-02-07 13:39:12 -03001960 } else {
1961 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001962 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03001963 if (dev->tuner_type == TUNER_ABSENT) {
1964 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_TUNER);
1965 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_TUNER);
1966 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_FREQUENCY);
1967 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_FREQUENCY);
1968 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001969 if (!dev->audio_mode.has_audio) {
1970 v4l2_disable_ioctl(dev->vdev, VIDIOC_G_AUDIO);
1971 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_AUDIO);
1972 }
Frank Schaefer6e46dab2013-02-07 13:39:09 -03001973
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001974 /* register v4l2 video video_device */
1975 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1976 video_nr[dev->devno]);
1977 if (ret) {
1978 em28xx_errdev("unable to register video device (error=%i).\n",
1979 ret);
1980 return ret;
1981 }
1982
1983 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03001984 if (em28xx_vbi_supported(dev) == 1) {
1985 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1986 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001987
Devin Heitmuellerd3829fa2013-01-04 16:16:24 -03001988 dev->vbi_dev->queue = &dev->vb_vbiq;
1989 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1990
Frank Schaefer66df67b2013-02-07 13:39:10 -03001991 /* disable inapplicable ioctls */
Frank Schaefer83c8bcc2013-02-07 13:39:13 -03001992 v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
Frank Schaefer66df67b2013-02-07 13:39:10 -03001993 if (dev->tuner_type == TUNER_ABSENT) {
1994 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_TUNER);
1995 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_TUNER);
1996 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_FREQUENCY);
1997 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_FREQUENCY);
1998 }
Frank Schaeferc2dcef82013-02-07 13:39:11 -03001999 if (!dev->audio_mode.has_audio) {
2000 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_AUDIO);
2001 v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_AUDIO);
2002 }
Frank Schaefer66df67b2013-02-07 13:39:10 -03002003
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002004 /* register v4l2 vbi video_device */
2005 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2006 vbi_nr[dev->devno]);
2007 if (ret < 0) {
2008 em28xx_errdev("unable to register vbi device\n");
2009 return ret;
2010 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002011 }
2012
2013 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002014 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2015 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002016 if (!dev->radio_dev) {
2017 em28xx_errdev("cannot allocate video_device.\n");
2018 return -ENODEV;
2019 }
2020 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2021 radio_nr[dev->devno]);
2022 if (ret < 0) {
2023 em28xx_errdev("can't register radio device\n");
2024 return ret;
2025 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002026 em28xx_info("Registered radio device as %s\n",
2027 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002028 }
2029
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002030 em28xx_info("V4L2 video device registered as %s\n",
2031 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002032
2033 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002034 em28xx_info("V4L2 VBI device registered as %s\n",
2035 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002036
2037 return 0;
2038}