blob: ebbf775fcaba03dbf50bf28b3249078aa44684e3 [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>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030053
54#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080057 if (video_debug) \
58 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080060
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030062module_param(isoc_debug, int, 0644);
63MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030064
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030065#define em28xx_isocdbg(fmt, arg...) \
66do {\
67 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030068 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030069 dev->name, __func__ , ##arg); \
70 } \
71 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073MODULE_AUTHOR(DRIVER_AUTHOR);
74MODULE_DESCRIPTION(DRIVER_DESC);
75MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030076MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080077
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085MODULE_PARM_DESC(video_nr, "video device numbers");
86MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
87MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080088
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030089static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030090module_param(video_debug, int, 0644);
91MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080092
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030093/* supported video standards */
94static struct em28xx_fmt format[] = {
95 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030096 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030099 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300102 .fourcc = V4L2_PIX_FMT_RGB565,
103 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300104 .reg = EM28XX_OUTFMT_RGB_16_656,
105 }, {
106 .name = "8 bpp Bayer BGBG..GRGR",
107 .fourcc = V4L2_PIX_FMT_SBGGR8,
108 .depth = 8,
109 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
110 }, {
111 .name = "8 bpp Bayer GRGR..BGBG",
112 .fourcc = V4L2_PIX_FMT_SGRBG8,
113 .depth = 8,
114 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
115 }, {
116 .name = "8 bpp Bayer GBGB..RGRG",
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
120 }, {
121 .name = "12 bpp YUV411",
122 .fourcc = V4L2_PIX_FMT_YUV411P,
123 .depth = 12,
124 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300125 },
126};
127
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300128/* ------------------------------------------------------------------
129 DMA and thread functions
130 ------------------------------------------------------------------*/
131
132/*
Frank Schaefer948a49a2012-12-08 11:31:25 -0300133 * Finish the current buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300134 */
Frank Schaefer948a49a2012-12-08 11:31:25 -0300135static inline void finish_buffer(struct em28xx *dev,
136 struct em28xx_buffer *buf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300137{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300138 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
139 buf->vb.state = VIDEOBUF_DONE;
140 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300141 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
144}
145
146/*
Frank Schaefer36016a32012-12-08 11:31:32 -0300147 * Copy picture data from USB buffer to videobuf buffer
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300148 */
149static void em28xx_copy_video(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300150 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300151 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300152 unsigned long len)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300153{
154 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300155 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300156 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157
Frank Schaefer87325332012-12-08 11:31:27 -0300158 if (buf->pos + len > buf->vb.size)
159 len = buf->vb.size - buf->pos;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160
Frank Schaefer36016a32012-12-08 11:31:32 -0300161 startread = usb_buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 remain = len;
163
Frank Schaeferc02ec712012-11-08 14:11:33 -0300164 if (dev->progressive || buf->top_field)
Frank Schaefer36016a32012-12-08 11:31:32 -0300165 fieldstart = buf->vb_buf;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300166 else /* interlaced mode, even nr. of lines */
Frank Schaefer36016a32012-12-08 11:31:32 -0300167 fieldstart = buf->vb_buf + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
Frank Schaefer87325332012-12-08 11:31:27 -0300169 linesdone = buf->pos / bytesperline;
170 currlinedone = buf->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300171
172 if (dev->progressive)
173 offset = linesdone * bytesperline + currlinedone;
174 else
175 offset = linesdone * bytesperline * 2 + currlinedone;
176
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300177 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300178 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300179 lencopy = lencopy > remain ? remain : lencopy;
180
Frank Schaefer36016a32012-12-08 11:31:32 -0300181 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300182 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Frank Schaefer36016a32012-12-08 11:31:32 -0300183 ((char *)startwrite + lencopy) -
184 ((char *)buf->vb_buf + buf->vb.size));
185 remain = (char *)buf->vb_buf + buf->vb.size -
186 (char *)startwrite;
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300187 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300188 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300189 if (lencopy <= 0)
190 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300191 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300192
193 remain -= lencopy;
194
195 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300196 if (dev->progressive)
197 startwrite += lencopy;
198 else
199 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 lencopy = remain;
203 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300204 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205
Frank Schaefer36016a32012-12-08 11:31:32 -0300206 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300207 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300208 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
209 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300210 ((char *)startwrite + lencopy) -
Frank Schaefer36016a32012-12-08 11:31:32 -0300211 ((char *)buf->vb_buf + buf->vb.size));
212 lencopy = remain = (char *)buf->vb_buf + buf->vb.size -
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300214 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300215 if (lencopy <= 0)
216 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300217
218 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300219
220 remain -= lencopy;
221 }
222
Frank Schaefer87325332012-12-08 11:31:27 -0300223 buf->pos += len;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224}
225
Frank Schaefer36016a32012-12-08 11:31:32 -0300226/*
227 * Copy VBI data from USB buffer to videobuf buffer
228 */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300229static void em28xx_copy_vbi(struct em28xx *dev,
Frank Schaefer87325332012-12-08 11:31:27 -0300230 struct em28xx_buffer *buf,
Frank Schaefer36016a32012-12-08 11:31:32 -0300231 unsigned char *usb_buf,
Frank Schaefer4078d622012-12-08 11:31:29 -0300232 unsigned long len)
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300233{
Frank Schaefer36016a32012-12-08 11:31:32 -0300234 unsigned int offset;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300235
Frank Schaefer87325332012-12-08 11:31:27 -0300236 if (buf->pos + len > buf->vb.size)
237 len = buf->vb.size - buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300238
Frank Schaefer87325332012-12-08 11:31:27 -0300239 offset = buf->pos;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300240 /* Make sure the bottom field populates the second half of the frame */
Frank Schaefer36016a32012-12-08 11:31:32 -0300241 if (buf->top_field == 0)
242 offset += dev->vbi_width * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300243
Frank Schaefer36016a32012-12-08 11:31:32 -0300244 memcpy(buf->vb_buf + offset, usb_buf, len);
Frank Schaefer87325332012-12-08 11:31:27 -0300245 buf->pos += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300246}
247
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300248static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300249 int packet, int status)
250{
251 char *errmsg = "Unknown";
252
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300253 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300254 case -ENOENT:
255 errmsg = "unlinked synchronuously";
256 break;
257 case -ECONNRESET:
258 errmsg = "unlinked asynchronuously";
259 break;
260 case -ENOSR:
261 errmsg = "Buffer error (overrun)";
262 break;
263 case -EPIPE:
264 errmsg = "Stalled (device not responding)";
265 break;
266 case -EOVERFLOW:
267 errmsg = "Babble (bad cable?)";
268 break;
269 case -EPROTO:
270 errmsg = "Bit-stuff error (bad cable?)";
271 break;
272 case -EILSEQ:
273 errmsg = "CRC/Timeout (could be anything)";
274 break;
275 case -ETIME:
276 errmsg = "Device does not respond";
277 break;
278 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300279 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300280 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
281 } else {
282 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
283 packet, status, errmsg);
284 }
285}
286
287/*
Frank Schaefer24a6d842012-12-08 11:31:24 -0300288 * get the next available buffer from dma queue
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300289 */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300290static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
291 struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300292{
Frank Schaefer24a6d842012-12-08 11:31:24 -0300293 struct em28xx_buffer *buf;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300294 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300296 if (list_empty(&dma_q->active)) {
297 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer24a6d842012-12-08 11:31:24 -0300298 return NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300299 }
300
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300301 /* Get the next buffer */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300302 buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300303 /* Cleans up buffer - Useful for testing for frame/URB loss */
Frank Schaefer24a6d842012-12-08 11:31:24 -0300304 outp = videobuf_to_vmalloc(&buf->vb);
305 memset(outp, 0, buf->vb.size);
Frank Schaefer87325332012-12-08 11:31:27 -0300306 buf->pos = 0;
Frank Schaefer4078d622012-12-08 11:31:29 -0300307 buf->vb_buf = outp;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300308
Frank Schaefer24a6d842012-12-08 11:31:24 -0300309 return buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300310}
311
Frank Schaefere04c00d2012-12-08 11:31:30 -0300312/*
313 * Finish the current buffer if completed and prepare for the next field
314 */
315static struct em28xx_buffer *
316finish_field_prepare_next(struct em28xx *dev,
317 struct em28xx_buffer *buf,
318 struct em28xx_dmaqueue *dma_q)
319{
320 if (dev->progressive || dev->top_field) { /* Brand new frame */
321 if (buf != NULL)
322 finish_buffer(dev, buf);
323 buf = get_next_buf(dev, dma_q);
324 }
325 if (buf != NULL) {
326 buf->top_field = dev->top_field;
327 buf->pos = 0;
328 }
329
330 return buf;
331}
332
Frank Schaefer227b7c92012-12-08 11:31:31 -0300333/*
334 * Process data packet according to the em2710/em2750/em28xx frame data format
335 */
336static inline void process_frame_data_em28xx(struct em28xx *dev,
337 unsigned char *data_pkt,
338 unsigned int data_len)
339{
340 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
341 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
342 struct em28xx_dmaqueue *dma_q = &dev->vidq;
343 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
344
345 /* capture type 0 = vbi start
346 capture type 1 = vbi in progress
347 capture type 2 = video start
348 capture type 3 = video in progress */
349 if (data_len >= 4) {
350 /* NOTE: Headers are always 4 bytes and
351 * never split across packets */
352 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
353 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
354 /* Continuation */
355 data_pkt += 4;
356 data_len -= 4;
357 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
358 /* Field start (VBI mode) */
359 dev->capture_type = 0;
360 dev->vbi_read = 0;
361 em28xx_isocdbg("VBI START HEADER !!!\n");
362 dev->top_field = !(data_pkt[2] & 1);
363 data_pkt += 4;
364 data_len -= 4;
365 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
366 /* Field start (VBI disabled) */
367 dev->capture_type = 2;
368 em28xx_isocdbg("VIDEO START HEADER !!!\n");
369 dev->top_field = !(data_pkt[2] & 1);
370 data_pkt += 4;
371 data_len -= 4;
372 }
373 }
374 /* NOTE: With bulk transfers, intermediate data packets
375 * have no continuation header */
376
377 if (dev->capture_type == 0) {
378 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
379 dev->usb_ctl.vbi_buf = vbi_buf;
380 dev->capture_type = 1;
381 }
382
383 if (dev->capture_type == 1) {
384 int vbi_size = dev->vbi_width * dev->vbi_height;
385 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
386 (vbi_size - dev->vbi_read) : data_len;
387
388 /* Copy VBI data */
389 if (vbi_buf != NULL)
390 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
391 dev->vbi_read += vbi_data_len;
392
393 if (vbi_data_len < data_len) {
394 /* Continue with copying video data */
395 dev->capture_type = 2;
396 data_pkt += vbi_data_len;
397 data_len -= vbi_data_len;
398 }
399 }
400
401 if (dev->capture_type == 2) {
402 buf = finish_field_prepare_next(dev, buf, dma_q);
403 dev->usb_ctl.vid_buf = buf;
404 dev->capture_type = 3;
405 }
406
407 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
408 em28xx_copy_video(dev, buf, data_pkt, data_len);
409}
410
Frank Schaefer960da932012-11-25 06:37:37 -0300411/* Processes and copies the URB data content (video and VBI data) */
Frank Schaefer0fa4a402012-11-08 14:11:45 -0300412static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300413{
Frank Schaefer227b7c92012-12-08 11:31:31 -0300414 int xfer_bulk, num_packets, i;
415 unsigned char *usb_data_pkt;
416 unsigned int usb_data_len;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300417
418 if (!dev)
419 return 0;
420
421 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
422 return 0;
423
Frank Schaefer1653cb0c2012-11-08 14:11:44 -0300424 if (urb->status < 0)
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300425 print_err_status(dev, -1, urb->status);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300426
Frank Schaefer4601cc32012-11-08 14:11:46 -0300427 xfer_bulk = usb_pipebulk(urb->pipe);
428
Frank Schaefer4601cc32012-11-08 14:11:46 -0300429 if (xfer_bulk) /* bulk */
430 num_packets = 1;
431 else /* isoc */
432 num_packets = urb->number_of_packets;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300433
Frank Schaefer4601cc32012-11-08 14:11:46 -0300434 for (i = 0; i < num_packets; i++) {
435 if (xfer_bulk) { /* bulk */
Frank Schaefer227b7c92012-12-08 11:31:31 -0300436 usb_data_len = urb->actual_length;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300437
Frank Schaefer227b7c92012-12-08 11:31:31 -0300438 usb_data_pkt = urb->transfer_buffer;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300439 } else { /* isoc */
440 if (urb->iso_frame_desc[i].status < 0) {
441 print_err_status(dev, i,
442 urb->iso_frame_desc[i].status);
443 if (urb->iso_frame_desc[i].status != -EPROTO)
444 continue;
445 }
446
Frank Schaefer227b7c92012-12-08 11:31:31 -0300447 usb_data_len = urb->iso_frame_desc[i].actual_length;
448 if (usb_data_len > dev->max_pkt_size) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300449 em28xx_isocdbg("packet bigger than packet size");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300450 continue;
Frank Schaefer4601cc32012-11-08 14:11:46 -0300451 }
452
Frank Schaefer227b7c92012-12-08 11:31:31 -0300453 usb_data_pkt = urb->transfer_buffer +
454 urb->iso_frame_desc[i].offset;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300455 }
456
Frank Schaefer227b7c92012-12-08 11:31:31 -0300457 if (usb_data_len == 0) {
Frank Schaefer4601cc32012-11-08 14:11:46 -0300458 /* NOTE: happens very often with isoc transfers */
459 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300460 continue;
461 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300462
Frank Schaefer227b7c92012-12-08 11:31:31 -0300463 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300464 }
Frank Schaefer227b7c92012-12-08 11:31:31 -0300465 return 1;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300466}
467
468
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300469/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300470 Videobuf operations
471 ------------------------------------------------------------------*/
472
473static int
474buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
475{
476 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300477 struct em28xx *dev = fh->dev;
478 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300480 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
481 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300482
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483 if (0 == *count)
484 *count = EM28XX_DEF_BUF;
485
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300486 if (*count < EM28XX_MIN_BUF)
487 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300489 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300490 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300491 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300492 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300493
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300494 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300495
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496 return 0;
497}
498
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300499/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
501{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300502 struct em28xx_fh *fh = vq->priv_data;
503 struct em28xx *dev = fh->dev;
504 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300505 if (in_interrupt())
506 BUG();
507
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300508 /* We used to wait for the buffer to finish here, but this didn't work
509 because, as we were keeping the state as VIDEOBUF_QUEUED,
510 videobuf_queue_cancel marked it as finished for us.
511 (Also, it could wedge forever if the hardware was misconfigured.)
512
513 This should be safe; by the time we get here, the buffer isn't
514 queued anymore. If we ever start marking the buffers as
515 VIDEOBUF_ACTIVE, it won't be, though.
516 */
517 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300518 if (dev->usb_ctl.vid_buf == buf)
519 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300520 spin_unlock_irqrestore(&dev->slock, flags);
521
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300522 videobuf_vmalloc_free(&buf->vb);
523 buf->vb.state = VIDEOBUF_NEEDS_INIT;
524}
525
526static int
527buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
528 enum v4l2_field field)
529{
530 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300531 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300532 struct em28xx *dev = fh->dev;
533 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300534
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300535 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
536 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300537
538 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
539 return -EINVAL;
540
Brandon Philips05612972008-04-13 14:57:01 -0300541 buf->vb.width = dev->width;
542 buf->vb.height = dev->height;
543 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300544
545 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300546 rc = videobuf_iolock(vq, &buf->vb, NULL);
547 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300548 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300549 }
550
Frank Schaefer74209dc2012-11-08 14:11:37 -0300551 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300552 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300553
554 if (urb_init) {
Frank Schaefer0455eeb2012-11-25 06:37:34 -0300555 dev->capture_type = -1;
Frank Schaefer960da932012-11-25 06:37:37 -0300556 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
557 dev->analog_xfer_bulk,
558 EM28XX_NUM_BUFS,
559 dev->max_pkt_size,
560 dev->packet_multiplier,
561 em28xx_urb_data_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300562 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300563 goto fail;
564 }
565
566 buf->vb.state = VIDEOBUF_PREPARED;
567 return 0;
568
569fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300570 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300571 return rc;
572}
573
574static void
575buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
576{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300577 struct em28xx_buffer *buf = container_of(vb,
578 struct em28xx_buffer,
579 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300580 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300581 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300582 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300583
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300584 buf->vb.state = VIDEOBUF_QUEUED;
585 list_add_tail(&buf->vb.queue, &vidq->active);
586
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300587}
588
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300589static void buffer_release(struct videobuf_queue *vq,
590 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300591{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300592 struct em28xx_buffer *buf = container_of(vb,
593 struct em28xx_buffer,
594 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300596 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300597
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300598 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300599
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300600 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300601}
602
603static struct videobuf_queue_ops em28xx_video_qops = {
604 .buf_setup = buffer_setup,
605 .buf_prepare = buffer_prepare,
606 .buf_queue = buffer_queue,
607 .buf_release = buffer_release,
608};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800609
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300610/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800611
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800612static void video_mux(struct em28xx *dev, int index)
613{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800614 dev->ctl_input = index;
615 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300616 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800617
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300618 if (!dev->ctl_aoutput)
619 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
620
Hans Verkuil5325b422009-04-02 11:26:22 -0300621 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
622 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800623
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300624 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300625 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300626 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
627 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300628 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300629 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300630 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
631 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800632 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300633
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300634 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300635 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
636 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300637 }
638
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300639 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800640}
641
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300642/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300643static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300644{
645 struct em28xx *dev = fh->dev;
646
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300647 if (fh->resources & bit)
648 /* have it already allocated */
649 return 1;
650
651 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300652 if (dev->resources & bit) {
653 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300654 return 0;
655 }
656 /* it's free, grab it */
657 fh->resources |= bit;
658 dev->resources |= bit;
659 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300660 return 1;
661}
662
663static int res_check(struct em28xx_fh *fh, unsigned int bit)
664{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300665 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300666}
667
668static int res_locked(struct em28xx *dev, unsigned int bit)
669{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300670 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300671}
672
673static void res_free(struct em28xx_fh *fh, unsigned int bits)
674{
675 struct em28xx *dev = fh->dev;
676
677 BUG_ON((fh->resources & bits) != bits);
678
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300679 fh->resources &= ~bits;
680 dev->resources &= ~bits;
681 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300682}
683
684static int get_ressource(struct em28xx_fh *fh)
685{
686 switch (fh->type) {
687 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
688 return EM28XX_RESOURCE_VIDEO;
689 case V4L2_BUF_TYPE_VBI_CAPTURE:
690 return EM28XX_RESOURCE_VBI;
691 default:
692 BUG();
693 return 0;
694 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300695}
696
Hans Verkuil081b9452012-09-07 05:43:59 -0300697void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300698{
Hans Verkuil081b9452012-09-07 05:43:59 -0300699 struct em28xx *dev = priv;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300700
Hans Verkuil081b9452012-09-07 05:43:59 -0300701 /*
702 * In the case of non-AC97 volume controls, we still need
703 * to do some setups at em28xx, in order to mute/unmute
704 * and to adjust audio volume. However, the value ranges
705 * should be checked by the corresponding V4L subdriver.
706 */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300707 switch (ctrl->id) {
708 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil081b9452012-09-07 05:43:59 -0300709 dev->mute = ctrl->val;
710 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300711 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300712 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil081b9452012-09-07 05:43:59 -0300713 dev->volume = ctrl->val;
714 em28xx_audio_analog_set(dev);
715 break;
716 }
717}
718
719static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
720{
721 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
722
723 switch (ctrl->id) {
724 case V4L2_CID_AUDIO_MUTE:
725 dev->mute = ctrl->val;
726 break;
727 case V4L2_CID_AUDIO_VOLUME:
728 dev->volume = ctrl->val;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300729 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300730 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300731
732 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300733}
734
Hans Verkuil081b9452012-09-07 05:43:59 -0300735const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
736 .s_ctrl = em28xx_s_ctrl,
737};
738
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300739static int check_dev(struct em28xx *dev)
740{
741 if (dev->state & DEV_DISCONNECTED) {
742 em28xx_errdev("v4l2 ioctl: device not present\n");
743 return -ENODEV;
744 }
745
746 if (dev->state & DEV_MISCONFIGURED) {
747 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
748 "close and open it again\n");
749 return -EIO;
750 }
751 return 0;
752}
753
754static void get_scale(struct em28xx *dev,
755 unsigned int width, unsigned int height,
756 unsigned int *hscale, unsigned int *vscale)
757{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300758 unsigned int maxw = norm_maxw(dev);
759 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300760
761 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
762 if (*hscale >= 0x4000)
763 *hscale = 0x3fff;
764
765 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
766 if (*vscale >= 0x4000)
767 *vscale = 0x3fff;
768}
769
770/* ------------------------------------------------------------------
771 IOCTL vidioc handling
772 ------------------------------------------------------------------*/
773
Hans Verkuil78b526a2008-05-28 12:16:41 -0300774static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300775 struct v4l2_format *f)
776{
777 struct em28xx_fh *fh = priv;
778 struct em28xx *dev = fh->dev;
779
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300780 f->fmt.pix.width = dev->width;
781 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300782 f->fmt.pix.pixelformat = dev->format->fourcc;
783 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300784 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300785 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
786
787 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300788 if (dev->progressive)
789 f->fmt.pix.field = V4L2_FIELD_NONE;
790 else
791 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300792 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793 return 0;
794}
795
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300796static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
797{
798 unsigned int i;
799
800 for (i = 0; i < ARRAY_SIZE(format); i++)
801 if (format[i].fourcc == fourcc)
802 return &format[i];
803
804 return NULL;
805}
806
Hans Verkuil78b526a2008-05-28 12:16:41 -0300807static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 struct v4l2_format *f)
809{
810 struct em28xx_fh *fh = priv;
811 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300812 unsigned int width = f->fmt.pix.width;
813 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814 unsigned int maxw = norm_maxw(dev);
815 unsigned int maxh = norm_maxh(dev);
816 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300817 struct em28xx_fmt *fmt;
818
819 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
820 if (!fmt) {
821 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
822 f->fmt.pix.pixelformat);
823 return -EINVAL;
824 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300826 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300827 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300828 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
829 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -0300830 /* MaxPacketSize for em2800 is too small to capture at full resolution
831 * use half of maxw as the scaler can only scale to 50% */
832 if (width == maxw && height == maxh)
833 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300834 } else {
835 /* width must even because of the YUYV format
836 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300837 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
838 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300839 }
840
841 get_scale(dev, width, height, &hscale, &vscale);
842
843 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
844 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
845
846 f->fmt.pix.width = width;
847 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300848 f->fmt.pix.pixelformat = fmt->fourcc;
849 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
850 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300851 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300852 if (dev->progressive)
853 f->fmt.pix.field = V4L2_FIELD_NONE;
854 else
855 f->fmt.pix.field = dev->interlaced ?
856 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300857
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300858 return 0;
859}
860
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300861static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
862 unsigned width, unsigned height)
863{
864 struct em28xx_fmt *fmt;
865
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300866 fmt = format_by_fourcc(fourcc);
867 if (!fmt)
868 return -EINVAL;
869
870 dev->format = fmt;
871 dev->width = width;
872 dev->height = height;
873
874 /* set new image size */
875 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
876
877 em28xx_set_alternate(dev);
878 em28xx_resolution_set(dev);
879
880 return 0;
881}
882
Hans Verkuil78b526a2008-05-28 12:16:41 -0300883static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300884 struct v4l2_format *f)
885{
886 struct em28xx_fh *fh = priv;
887 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300888 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300889
890 rc = check_dev(dev);
891 if (rc < 0)
892 return rc;
893
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300894 vidioc_try_fmt_vid_cap(file, priv, f);
895
Brandon Philips05612972008-04-13 14:57:01 -0300896 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
897 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300898 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -0300899 }
900
Hans Verkuil0499a5a2010-09-26 07:34:45 -0300901 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300902 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300903}
904
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300905static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
906{
907 struct em28xx_fh *fh = priv;
908 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -0300909 int rc;
910
911 rc = check_dev(dev);
912 if (rc < 0)
913 return rc;
914
915 *norm = dev->norm;
916
917 return 0;
918}
919
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -0300920static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
921{
922 struct em28xx_fh *fh = priv;
923 struct em28xx *dev = fh->dev;
924 int rc;
925
926 rc = check_dev(dev);
927 if (rc < 0)
928 return rc;
929
930 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
931
932 return 0;
933}
934
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300935static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936{
937 struct em28xx_fh *fh = priv;
938 struct em28xx *dev = fh->dev;
939 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300940 int rc;
941
942 rc = check_dev(dev);
943 if (rc < 0)
944 return rc;
945
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300946 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947
948 /* Adjusts width/height, if needed */
949 f.fmt.pix.width = dev->width;
950 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300951 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300952
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300953 /* set new image size */
954 dev->width = f.fmt.pix.width;
955 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
957
958 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300959 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300961 return 0;
962}
963
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -0300964static int vidioc_g_parm(struct file *file, void *priv,
965 struct v4l2_streamparm *p)
966{
967 struct em28xx_fh *fh = priv;
968 struct em28xx *dev = fh->dev;
969 int rc = 0;
970
971 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
972 return -EINVAL;
973
974 if (dev->board.is_webcam)
975 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
976 video, g_parm, p);
977 else
978 v4l2_video_std_frame_period(dev->norm,
979 &p->parm.capture.timeperframe);
980
981 return rc;
982}
983
984static int vidioc_s_parm(struct file *file, void *priv,
985 struct v4l2_streamparm *p)
986{
987 struct em28xx_fh *fh = priv;
988 struct em28xx *dev = fh->dev;
989
990 if (!dev->board.is_webcam)
991 return -EINVAL;
992
993 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
994 return -EINVAL;
995
996 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
997}
998
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300999static const char *iname[] = {
1000 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1001 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1002 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1003 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1004 [EM28XX_VMUX_SVIDEO] = "S-Video",
1005 [EM28XX_VMUX_TELEVISION] = "Television",
1006 [EM28XX_VMUX_CABLE] = "Cable TV",
1007 [EM28XX_VMUX_DVB] = "DVB",
1008 [EM28XX_VMUX_DEBUG] = "for debug only",
1009};
1010
1011static int vidioc_enum_input(struct file *file, void *priv,
1012 struct v4l2_input *i)
1013{
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
1016 unsigned int n;
1017
1018 n = i->index;
1019 if (n >= MAX_EM28XX_INPUT)
1020 return -EINVAL;
1021 if (0 == INPUT(n)->type)
1022 return -EINVAL;
1023
1024 i->index = n;
1025 i->type = V4L2_INPUT_TYPE_CAMERA;
1026
1027 strcpy(i->name, iname[INPUT(n)->type]);
1028
1029 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1030 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1031 i->type = V4L2_INPUT_TYPE_TUNER;
1032
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001033 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001034
1035 return 0;
1036}
1037
1038static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1039{
1040 struct em28xx_fh *fh = priv;
1041 struct em28xx *dev = fh->dev;
1042
1043 *i = dev->ctl_input;
1044
1045 return 0;
1046}
1047
1048static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1049{
1050 struct em28xx_fh *fh = priv;
1051 struct em28xx *dev = fh->dev;
1052 int rc;
1053
1054 rc = check_dev(dev);
1055 if (rc < 0)
1056 return rc;
1057
1058 if (i >= MAX_EM28XX_INPUT)
1059 return -EINVAL;
1060 if (0 == INPUT(i)->type)
1061 return -EINVAL;
1062
Ezequiel García96371fc2012-03-23 18:09:34 -03001063 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064 return 0;
1065}
1066
1067static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1068{
1069 struct em28xx_fh *fh = priv;
1070 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001071
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001072 if (!dev->audio_mode.has_audio)
1073 return -EINVAL;
1074
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001075 switch (a->index) {
1076 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001078 break;
1079 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001080 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001081 break;
1082 case EM28XX_AMUX_VIDEO2:
1083 strcpy(a->name, "Television alt");
1084 break;
1085 case EM28XX_AMUX_PHONE:
1086 strcpy(a->name, "Phone");
1087 break;
1088 case EM28XX_AMUX_MIC:
1089 strcpy(a->name, "Mic");
1090 break;
1091 case EM28XX_AMUX_CD:
1092 strcpy(a->name, "CD");
1093 break;
1094 case EM28XX_AMUX_AUX:
1095 strcpy(a->name, "Aux");
1096 break;
1097 case EM28XX_AMUX_PCM_OUT:
1098 strcpy(a->name, "PCM");
1099 break;
1100 default:
1101 return -EINVAL;
1102 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001103
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001104 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001105 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001106
1107 return 0;
1108}
1109
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001110static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001111{
1112 struct em28xx_fh *fh = priv;
1113 struct em28xx *dev = fh->dev;
1114
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001115
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001116 if (!dev->audio_mode.has_audio)
1117 return -EINVAL;
1118
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001119 if (a->index >= MAX_EM28XX_INPUT)
1120 return -EINVAL;
1121 if (0 == INPUT(a->index)->type)
1122 return -EINVAL;
1123
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001124 dev->ctl_ainput = INPUT(a->index)->amux;
1125 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001126
1127 if (!dev->ctl_aoutput)
1128 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001129
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001130 return 0;
1131}
1132
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001133static int vidioc_g_tuner(struct file *file, void *priv,
1134 struct v4l2_tuner *t)
1135{
1136 struct em28xx_fh *fh = priv;
1137 struct em28xx *dev = fh->dev;
1138 int rc;
1139
1140 rc = check_dev(dev);
1141 if (rc < 0)
1142 return rc;
1143
1144 if (0 != t->index)
1145 return -EINVAL;
1146
1147 strcpy(t->name, "Tuner");
1148
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001149 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001150 return 0;
1151}
1152
1153static int vidioc_s_tuner(struct file *file, void *priv,
1154 struct v4l2_tuner *t)
1155{
1156 struct em28xx_fh *fh = priv;
1157 struct em28xx *dev = fh->dev;
1158 int rc;
1159
1160 rc = check_dev(dev);
1161 if (rc < 0)
1162 return rc;
1163
1164 if (0 != t->index)
1165 return -EINVAL;
1166
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001167 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001168 return 0;
1169}
1170
1171static int vidioc_g_frequency(struct file *file, void *priv,
1172 struct v4l2_frequency *f)
1173{
1174 struct em28xx_fh *fh = priv;
1175 struct em28xx *dev = fh->dev;
1176
Hans Verkuil20deebf2012-09-06 10:07:25 -03001177 if (0 != f->tuner)
1178 return -EINVAL;
1179
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001180 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181 return 0;
1182}
1183
1184static int vidioc_s_frequency(struct file *file, void *priv,
1185 struct v4l2_frequency *f)
1186{
1187 struct em28xx_fh *fh = priv;
1188 struct em28xx *dev = fh->dev;
1189 int rc;
1190
1191 rc = check_dev(dev);
1192 if (rc < 0)
1193 return rc;
1194
1195 if (0 != f->tuner)
1196 return -EINVAL;
1197
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001198 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Hans Verkuil20deebf2012-09-06 10:07:25 -03001199 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1200 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001201
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001202 return 0;
1203}
1204
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001205#ifdef CONFIG_VIDEO_ADV_DEBUG
1206static int em28xx_reg_len(int reg)
1207{
1208 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001209 case EM28XX_R40_AC97LSB:
1210 case EM28XX_R30_HSCALELOW:
1211 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001212 return 2;
1213 default:
1214 return 1;
1215 }
1216}
1217
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001218static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001219 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001220{
1221 struct em28xx_fh *fh = priv;
1222 struct em28xx *dev = fh->dev;
1223
1224 chip->ident = V4L2_IDENT_NONE;
1225 chip->revision = 0;
Hans Verkuil319a55f2012-09-06 09:53:08 -03001226 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1227 if (v4l2_chip_match_host(&chip->match))
1228 chip->ident = V4L2_IDENT_NONE;
1229 return 0;
1230 }
1231 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1232 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1233 return -EINVAL;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001234
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001235 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001236
1237 return 0;
1238}
1239
1240
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001241static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001242 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001243{
1244 struct em28xx_fh *fh = priv;
1245 struct em28xx *dev = fh->dev;
1246 int ret;
1247
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001248 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001249 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001250 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001251 if (ret < 0)
1252 return ret;
1253
1254 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001255 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001256 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001257 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001258 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001259 return 0;
1260 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001261 /* TODO: is this correct? */
1262 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1263 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001264 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001265 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001266 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001267 }
1268
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001269 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001270 reg->size = em28xx_reg_len(reg->reg);
1271 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001272 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001273
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001274 if (ret < 0)
1275 return ret;
1276
1277 reg->val = ret;
1278 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001279 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001280 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1281 reg->reg, (char *)&val, 2);
1282 if (ret < 0)
1283 return ret;
1284
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001285 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001286 }
1287
1288 return 0;
1289}
1290
1291static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001292 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001293{
1294 struct em28xx_fh *fh = priv;
1295 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001296 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001297
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001298 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001299 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001300 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001301 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001302 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001303 return 0;
1304 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001305 /* TODO: is this correct? */
1306 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1307 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001308 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001309 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001310 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001311 }
1312
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001313 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001314 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001315
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001316 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001317 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001318}
1319#endif
1320
1321
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001322static int vidioc_cropcap(struct file *file, void *priv,
1323 struct v4l2_cropcap *cc)
1324{
1325 struct em28xx_fh *fh = priv;
1326 struct em28xx *dev = fh->dev;
1327
1328 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1329 return -EINVAL;
1330
1331 cc->bounds.left = 0;
1332 cc->bounds.top = 0;
1333 cc->bounds.width = dev->width;
1334 cc->bounds.height = dev->height;
1335 cc->defrect = cc->bounds;
1336 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1337 cc->pixelaspect.denominator = 59;
1338
1339 return 0;
1340}
1341
1342static int vidioc_streamon(struct file *file, void *priv,
1343 enum v4l2_buf_type type)
1344{
1345 struct em28xx_fh *fh = priv;
1346 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001347 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348
1349 rc = check_dev(dev);
1350 if (rc < 0)
1351 return rc;
1352
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001353 if (unlikely(type != fh->type))
1354 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001356 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1357 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001358
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001359 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001360 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001361
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001362 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1363 rc = videobuf_streamon(&fh->vb_vidq);
1364 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1365 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001366
1367 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368}
1369
1370static int vidioc_streamoff(struct file *file, void *priv,
1371 enum v4l2_buf_type type)
1372{
1373 struct em28xx_fh *fh = priv;
1374 struct em28xx *dev = fh->dev;
1375 int rc;
1376
1377 rc = check_dev(dev);
1378 if (rc < 0)
1379 return rc;
1380
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001381 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1382 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001383 return -EINVAL;
1384 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001385 return -EINVAL;
1386
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001387 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1388 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001389
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001390 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001391 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1392 videobuf_streamoff(&fh->vb_vidq);
1393 res_free(fh, EM28XX_RESOURCE_VIDEO);
1394 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001395 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001396 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1397 videobuf_streamoff(&fh->vb_vbiq);
1398 res_free(fh, EM28XX_RESOURCE_VBI);
1399 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001400 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001402 return 0;
1403}
1404
1405static int vidioc_querycap(struct file *file, void *priv,
1406 struct v4l2_capability *cap)
1407{
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001408 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001409 struct em28xx_fh *fh = priv;
1410 struct em28xx *dev = fh->dev;
1411
1412 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1413 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001414 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001416 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1417 cap->device_caps = V4L2_CAP_READWRITE |
1418 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1419 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1420 cap->device_caps = V4L2_CAP_RADIO;
1421 else
1422 cap->device_caps = V4L2_CAP_READWRITE |
1423 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
Devin Heitmueller04146142009-09-11 00:08:44 -03001424
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001425 if (dev->audio_mode.has_audio)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001426 cap->device_caps |= V4L2_CAP_AUDIO;
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001427
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001428 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001429 cap->device_caps |= V4L2_CAP_TUNER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001430
Hans Verkuila9d79fe2012-09-06 07:31:04 -03001431 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1432 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1433 if (dev->vbi_dev)
1434 cap->capabilities |=
1435 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
1436 if (dev->radio_dev)
1437 cap->capabilities |= V4L2_CAP_RADIO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001438 return 0;
1439}
1440
Hans Verkuil78b526a2008-05-28 12:16:41 -03001441static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001442 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001444 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001445 return -EINVAL;
1446
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001447 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1448 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001449
1450 return 0;
1451}
1452
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001453static int vidioc_enum_framesizes(struct file *file, void *priv,
1454 struct v4l2_frmsizeenum *fsize)
1455{
1456 struct em28xx_fh *fh = priv;
1457 struct em28xx *dev = fh->dev;
1458 struct em28xx_fmt *fmt;
1459 unsigned int maxw = norm_maxw(dev);
1460 unsigned int maxh = norm_maxh(dev);
1461
1462 fmt = format_by_fourcc(fsize->pixel_format);
1463 if (!fmt) {
1464 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1465 fsize->pixel_format);
1466 return -EINVAL;
1467 }
1468
1469 if (dev->board.is_em2800) {
1470 if (fsize->index > 1)
1471 return -EINVAL;
1472 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1473 fsize->discrete.width = maxw / (1 + fsize->index);
1474 fsize->discrete.height = maxh / (1 + fsize->index);
1475 return 0;
1476 }
1477
1478 if (fsize->index != 0)
1479 return -EINVAL;
1480
1481 /* Report a continuous range */
1482 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1483 fsize->stepwise.min_width = 48;
1484 fsize->stepwise.min_height = 32;
1485 fsize->stepwise.max_width = maxw;
1486 fsize->stepwise.max_height = maxh;
1487 fsize->stepwise.step_width = 1;
1488 fsize->stepwise.step_height = 1;
1489 return 0;
1490}
1491
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001492/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001493static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001494 struct v4l2_format *f)
1495{
1496 struct em28xx_fh *fh = priv;
1497 struct em28xx *dev = fh->dev;
1498 int rc;
1499
1500 rc = check_dev(dev);
1501 if (rc < 0)
1502 return rc;
1503
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001504 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001505 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001506
1507 if (f->fmt.sliced.service_set == 0)
1508 rc = -EINVAL;
1509
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001510 return rc;
1511}
1512
Hans Verkuil78b526a2008-05-28 12:16:41 -03001513static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001514 struct v4l2_format *f)
1515{
1516 struct em28xx_fh *fh = priv;
1517 struct em28xx *dev = fh->dev;
1518 int rc;
1519
1520 rc = check_dev(dev);
1521 if (rc < 0)
1522 return rc;
1523
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001524 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001525
1526 if (f->fmt.sliced.service_set == 0)
1527 return -EINVAL;
1528
1529 return 0;
1530}
1531
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001532/* RAW VBI ioctls */
1533
1534static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1535 struct v4l2_format *format)
1536{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001537 struct em28xx_fh *fh = priv;
1538 struct em28xx *dev = fh->dev;
1539
1540 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001541 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1542 format->fmt.vbi.offset = 0;
1543 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001544 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1545 format->fmt.vbi.count[0] = dev->vbi_height;
1546 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001547
1548 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001549 if (dev->norm & V4L2_STD_525_60) {
1550 /* NTSC */
1551 format->fmt.vbi.start[0] = 10;
1552 format->fmt.vbi.start[1] = 273;
1553 } else if (dev->norm & V4L2_STD_625_50) {
1554 /* PAL */
1555 format->fmt.vbi.start[0] = 6;
1556 format->fmt.vbi.start[1] = 318;
1557 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001558
1559 return 0;
1560}
1561
1562static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1563 struct v4l2_format *format)
1564{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001565 struct em28xx_fh *fh = priv;
1566 struct em28xx *dev = fh->dev;
1567
1568 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001569 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1570 format->fmt.vbi.offset = 0;
1571 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001572 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1573 format->fmt.vbi.count[0] = dev->vbi_height;
1574 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001575
1576 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001577 if (dev->norm & V4L2_STD_525_60) {
1578 /* NTSC */
1579 format->fmt.vbi.start[0] = 10;
1580 format->fmt.vbi.start[1] = 273;
1581 } else if (dev->norm & V4L2_STD_625_50) {
1582 /* PAL */
1583 format->fmt.vbi.start[0] = 6;
1584 format->fmt.vbi.start[1] = 318;
1585 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001586
1587 return 0;
1588}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001589
1590static int vidioc_reqbufs(struct file *file, void *priv,
1591 struct v4l2_requestbuffers *rb)
1592{
1593 struct em28xx_fh *fh = priv;
1594 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595 int rc;
1596
1597 rc = check_dev(dev);
1598 if (rc < 0)
1599 return rc;
1600
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001601 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1602 return videobuf_reqbufs(&fh->vb_vidq, rb);
1603 else
1604 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001605}
1606
1607static int vidioc_querybuf(struct file *file, void *priv,
1608 struct v4l2_buffer *b)
1609{
1610 struct em28xx_fh *fh = priv;
1611 struct em28xx *dev = fh->dev;
1612 int rc;
1613
1614 rc = check_dev(dev);
1615 if (rc < 0)
1616 return rc;
1617
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001618 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1619 return videobuf_querybuf(&fh->vb_vidq, b);
1620 else {
1621 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1622 the videobuf framework, but we probably shouldn't be
1623 returning a buffer larger than that which was asked for.
1624 At a minimum, it causes a crash in zvbi since it does
1625 a memcpy based on the source buffer length */
1626 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001627 b->length = dev->vbi_width * dev->vbi_height * 2;
1628
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001629 return result;
1630 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001631}
1632
1633static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1634{
1635 struct em28xx_fh *fh = priv;
1636 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001637 int rc;
1638
1639 rc = check_dev(dev);
1640 if (rc < 0)
1641 return rc;
1642
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001643 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1644 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001645 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001646 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001647}
1648
1649static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1650{
1651 struct em28xx_fh *fh = priv;
1652 struct em28xx *dev = fh->dev;
1653 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001654
1655 rc = check_dev(dev);
1656 if (rc < 0)
1657 return rc;
1658
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001659 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1660 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1661 O_NONBLOCK);
1662 else
1663 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1664 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001665}
1666
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001667/* ----------------------------------------------------------- */
1668/* RADIO ESPECIFIC IOCTLS */
1669/* ----------------------------------------------------------- */
1670
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001671static int radio_g_tuner(struct file *file, void *priv,
1672 struct v4l2_tuner *t)
1673{
1674 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1675
1676 if (unlikely(t->index > 0))
1677 return -EINVAL;
1678
1679 strcpy(t->name, "Radio");
1680 t->type = V4L2_TUNER_RADIO;
1681
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001682 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001683
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001684 return 0;
1685}
1686
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001687static int radio_s_tuner(struct file *file, void *priv,
1688 struct v4l2_tuner *t)
1689{
1690 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1691
1692 if (0 != t->index)
1693 return -EINVAL;
1694
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001695 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001696
1697 return 0;
1698}
1699
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001700/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001701 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702 * inits the device and starts isoc transfer
1703 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001704static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001705{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001706 int errCode = 0, radio = 0;
1707 struct video_device *vdev = video_devdata(filp);
1708 struct em28xx *dev = video_drvdata(filp);
1709 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001710 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001711 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001712
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02001713 switch (vdev->vfl_type) {
1714 case VFL_TYPE_GRABBER:
1715 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1716 break;
1717 case VFL_TYPE_VBI:
1718 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1719 break;
1720 case VFL_TYPE_RADIO:
1721 radio = 1;
1722 break;
1723 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001724
Laurent Pinchart50462eb2009-12-10 11:47:13 -02001725 em28xx_videodbg("open dev=%s type=%s users=%d\n",
1726 video_device_node_name(vdev), v4l2_type_names[fh_type],
1727 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001729
Hans Verkuil876cb142012-06-23 08:12:47 -03001730 if (mutex_lock_interruptible(&dev->lock))
1731 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001732 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001733 if (!fh) {
1734 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03001735 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001736 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001738 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001739 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001740 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001741 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001743 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001744 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001745 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001746 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001748 /* Needed, since GPIO might have disabled power of
1749 some i2c device
1750 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001751 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001752
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001753 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001754 if (fh->radio) {
1755 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001756 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001757 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001758
1759 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001761 if (dev->progressive)
1762 field = V4L2_FIELD_NONE;
1763 else
1764 field = V4L2_FIELD_INTERLACED;
1765
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001766 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1767 NULL, &dev->slock,
1768 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001769 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001770
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001771 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
1772 NULL, &dev->slock,
1773 V4L2_BUF_TYPE_VBI_CAPTURE,
1774 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001775 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03001776 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001777
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001778 return errCode;
1779}
1780
1781/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001782 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001783 * unregisters the v4l2,i2c and usb devices
1784 * called when the device gets disconected or at module unload
1785*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001786void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001787{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001788
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001789 /*FIXME: I2C IR should be disconnected */
1790
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001791 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001792 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001793 video_unregister_device(dev->radio_dev);
1794 else
1795 video_device_release(dev->radio_dev);
1796 dev->radio_dev = NULL;
1797 }
1798 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001799 em28xx_info("V4L2 device %s deregistered\n",
1800 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001801 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001802 video_unregister_device(dev->vbi_dev);
1803 else
1804 video_device_release(dev->vbi_dev);
1805 dev->vbi_dev = NULL;
1806 }
1807 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001808 em28xx_info("V4L2 device %s deregistered\n",
1809 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001810 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001811 video_unregister_device(dev->vdev);
1812 else
1813 video_device_release(dev->vdev);
1814 dev->vdev = NULL;
1815 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001816}
1817
1818/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001819 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001820 * stops streaming and deallocates all resources allocated by the v4l2
1821 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001823static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001824{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001825 struct em28xx_fh *fh = filp->private_data;
1826 struct em28xx *dev = fh->dev;
1827 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001829 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001830
Hans Verkuil876cb142012-06-23 08:12:47 -03001831 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001832 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001833 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001834 res_free(fh, EM28XX_RESOURCE_VIDEO);
1835 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001836
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001837 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1838 videobuf_stop(&fh->vb_vbiq);
1839 res_free(fh, EM28XX_RESOURCE_VBI);
1840 }
1841
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001842 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001843 /* the device is already disconnect,
1844 free the remaining resources */
1845 if (dev->state & DEV_DISCONNECTED) {
1846 em28xx_release_resources(dev);
Frank Schaefer0cf544a2012-11-08 14:11:49 -03001847 kfree(dev->alt_max_pkt_size_isoc);
Dan Carpentere36c92f2012-08-14 02:58:15 -03001848 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001849 kfree(dev);
Ezequiel Garcíadedb8cb2012-05-05 16:13:22 -03001850 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001851 return 0;
1852 }
1853
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001854 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001855 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001856
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001857 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03001858 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001859 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001860
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001861 /* set alternate 0 */
1862 dev->alt = 0;
1863 em28xx_videodbg("setting alternate 0\n");
1864 errCode = usb_set_interface(dev->udev, 0, 0);
1865 if (errCode < 0) {
1866 em28xx_errdev("cannot change alternate number to "
1867 "0 (error=%i)\n", errCode);
1868 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001869 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001870
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001871 videobuf_mmap_free(&fh->vb_vidq);
1872 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001873 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001874 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03001875 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001876 return 0;
1877}
1878
1879/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001880 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001881 * will allocate buffers when called for the first time
1882 */
1883static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001884em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001885 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001887 struct em28xx_fh *fh = filp->private_data;
1888 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001889 int rc;
1890
1891 rc = check_dev(dev);
1892 if (rc < 0)
1893 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894
Hans Verkuil876cb142012-06-23 08:12:47 -03001895 if (mutex_lock_interruptible(&dev->lock))
1896 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001897 /* FIXME: read() is not prepared to allow changing the video
1898 resolution while streaming. Seems a bug at em28xx_set_fmt
1899 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001900
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001901 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001902 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03001903 rc = -EBUSY;
1904 else
1905 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001906 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03001907 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001908 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03001909 rc = -EBUSY;
1910 else
1911 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001912 filp->f_flags & O_NONBLOCK);
1913 }
Hans Verkuil876cb142012-06-23 08:12:47 -03001914 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001915
Hans Verkuil876cb142012-06-23 08:12:47 -03001916 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001917}
1918
1919/*
Hans Verkuil876cb142012-06-23 08:12:47 -03001920 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001921 * will allocate buffers when called for the first time
1922 */
Hans Verkuil876cb142012-06-23 08:12:47 -03001923static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001924{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001925 struct em28xx_fh *fh = filp->private_data;
1926 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001927 int rc;
1928
1929 rc = check_dev(dev);
1930 if (rc < 0)
1931 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001932
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001933 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1934 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
1935 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001936 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001937 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1938 if (!res_get(fh, EM28XX_RESOURCE_VBI))
1939 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001940 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001941 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001942 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001943 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001944}
1945
Hans Verkuil876cb142012-06-23 08:12:47 -03001946static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
1947{
1948 struct em28xx_fh *fh = filp->private_data;
1949 struct em28xx *dev = fh->dev;
1950 unsigned int res;
1951
1952 mutex_lock(&dev->lock);
1953 res = em28xx_poll(filp, wait);
1954 mutex_unlock(&dev->lock);
1955 return res;
1956}
1957
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001958/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001959 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001960 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001961static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001962{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001963 struct em28xx_fh *fh = filp->private_data;
1964 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001965 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001966
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001967 rc = check_dev(dev);
1968 if (rc < 0)
1969 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001970
Hans Verkuil876cb142012-06-23 08:12:47 -03001971 if (mutex_lock_interruptible(&dev->lock))
1972 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03001973 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1974 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1975 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1976 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03001977 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001978
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001979 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1980 (unsigned long)vma->vm_start,
1981 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1982 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001983
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001984 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001985}
1986
Hans Verkuilbec43662008-12-30 06:58:20 -03001987static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001988 .owner = THIS_MODULE,
1989 .open = em28xx_v4l2_open,
1990 .release = em28xx_v4l2_close,
1991 .read = em28xx_v4l2_read,
1992 .poll = em28xx_v4l2_poll,
1993 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001994 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001995};
1996
Hans Verkuila3998102008-07-21 02:57:38 -03001997static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001998 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001999 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2000 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2001 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2002 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002003 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2004 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002005 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002006 .vidioc_g_audio = vidioc_g_audio,
2007 .vidioc_s_audio = vidioc_s_audio,
2008 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002009 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2010 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2011 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012
2013 .vidioc_reqbufs = vidioc_reqbufs,
2014 .vidioc_querybuf = vidioc_querybuf,
2015 .vidioc_qbuf = vidioc_qbuf,
2016 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002017 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002018 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002019 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002020 .vidioc_g_parm = vidioc_g_parm,
2021 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002022 .vidioc_enum_input = vidioc_enum_input,
2023 .vidioc_g_input = vidioc_g_input,
2024 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002025 .vidioc_streamon = vidioc_streamon,
2026 .vidioc_streamoff = vidioc_streamoff,
2027 .vidioc_g_tuner = vidioc_g_tuner,
2028 .vidioc_s_tuner = vidioc_s_tuner,
2029 .vidioc_g_frequency = vidioc_g_frequency,
2030 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002031#ifdef CONFIG_VIDEO_ADV_DEBUG
2032 .vidioc_g_register = vidioc_g_register,
2033 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002034 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002035#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002036};
2037
2038static const struct video_device em28xx_video_template = {
2039 .fops = &em28xx_v4l_fops,
2040 .release = video_device_release,
2041 .ioctl_ops = &video_ioctl_ops,
2042
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002043 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002044 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002045};
2046
Hans Verkuilbec43662008-12-30 06:58:20 -03002047static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002048 .owner = THIS_MODULE,
2049 .open = em28xx_v4l2_open,
2050 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002051 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002052};
2053
2054static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuila9d79fe2012-09-06 07:31:04 -03002055 .vidioc_querycap = vidioc_querycap,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002056 .vidioc_g_tuner = radio_g_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002057 .vidioc_s_tuner = radio_s_tuner,
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002058 .vidioc_g_frequency = vidioc_g_frequency,
2059 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002060#ifdef CONFIG_VIDEO_ADV_DEBUG
2061 .vidioc_g_register = vidioc_g_register,
2062 .vidioc_s_register = vidioc_s_register,
2063#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002064};
2065
Hans Verkuila3998102008-07-21 02:57:38 -03002066static struct video_device em28xx_radio_template = {
2067 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002068 .fops = &radio_fops,
2069 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002070};
2071
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002072/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002074
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002075
Adrian Bunk532fe652008-01-28 22:10:48 -03002076static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002077 const struct video_device *template,
2078 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002079{
2080 struct video_device *vfd;
2081
2082 vfd = video_device_alloc();
2083 if (NULL == vfd)
2084 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002085
2086 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002087 vfd->v4l2_dev = &dev->v4l2_dev;
2088 vfd->release = video_device_release;
2089 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002090 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002091
2092 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2093 dev->name, type_name);
2094
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002095 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002096 return vfd;
2097}
2098
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002099int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002100{
Hans Verkuil081b9452012-09-07 05:43:59 -03002101 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002102 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002103 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002104
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002105 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2106 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002107
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002108 /* set default norm */
2109 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002110 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002111 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002112
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002113 /* Analog specific initialization */
2114 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002115
2116 maxw = norm_maxw(dev);
2117 /* MaxPacketSize for em2800 is too small to capture at full resolution
2118 * use half of maxw as the scaler can only scale to 50% */
2119 if (dev->board.is_em2800)
2120 maxw /= 2;
2121
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002122 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002123 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002124
Ezequiel García96371fc2012-03-23 18:09:34 -03002125 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002126
2127 /* Audio defaults */
2128 dev->mute = 1;
2129 dev->volume = 0x1f;
2130
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002131/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002132 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2133 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2134 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002135
2136 em28xx_set_outfmt(dev);
2137 em28xx_colorlevels_set_default(dev);
2138 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002139
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002140 /* allocate and fill video video_device struct */
2141 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2142 if (!dev->vdev) {
2143 em28xx_errdev("cannot allocate video_device.\n");
2144 return -ENODEV;
2145 }
2146
2147 /* register v4l2 video video_device */
2148 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2149 video_nr[dev->devno]);
2150 if (ret) {
2151 em28xx_errdev("unable to register video device (error=%i).\n",
2152 ret);
2153 return ret;
2154 }
2155
2156 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002157 if (em28xx_vbi_supported(dev) == 1) {
2158 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2159 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002160
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002161 /* register v4l2 vbi video_device */
2162 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2163 vbi_nr[dev->devno]);
2164 if (ret < 0) {
2165 em28xx_errdev("unable to register vbi device\n");
2166 return ret;
2167 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002168 }
2169
2170 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002171 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2172 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002173 if (!dev->radio_dev) {
2174 em28xx_errdev("cannot allocate video_device.\n");
2175 return -ENODEV;
2176 }
2177 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2178 radio_nr[dev->devno]);
2179 if (ret < 0) {
2180 em28xx_errdev("can't register radio device\n");
2181 return ret;
2182 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002183 em28xx_info("Registered radio device as %s\n",
2184 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002185 }
2186
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002187 em28xx_info("V4L2 video device registered as %s\n",
2188 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002189
2190 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002191 em28xx_info("V4L2 VBI device registered as %s\n",
2192 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002193
2194 return 0;
2195}