blob: d61d4dcca626f46ad3e71a54bd01ebbb98d55398 [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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030052
53#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030075MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080076
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084MODULE_PARM_DESC(video_nr, "video device numbers");
85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080087
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030088static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030089module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080091
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030092/* supported video standards */
93static struct em28xx_fmt format[] = {
94 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030095 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030096 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030098 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300100 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300101 .fourcc = V4L2_PIX_FMT_RGB565,
102 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300103 .reg = EM28XX_OUTFMT_RGB_16_656,
104 }, {
105 .name = "8 bpp Bayer BGBG..GRGR",
106 .fourcc = V4L2_PIX_FMT_SBGGR8,
107 .depth = 8,
108 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
109 }, {
110 .name = "8 bpp Bayer GRGR..BGBG",
111 .fourcc = V4L2_PIX_FMT_SGRBG8,
112 .depth = 8,
113 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
114 }, {
115 .name = "8 bpp Bayer GBGB..RGRG",
116 .fourcc = V4L2_PIX_FMT_SGBRG8,
117 .depth = 8,
118 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
119 }, {
120 .name = "12 bpp YUV411",
121 .fourcc = V4L2_PIX_FMT_YUV411P,
122 .depth = 12,
123 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300124 },
125};
126
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800127/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200128/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300129static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800130 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300138 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300139 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300151/* ------------------------------------------------------------------
152 DMA and thread functions
153 ------------------------------------------------------------------*/
154
155/*
156 * Announces that a buffer were filled and request the next
157 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 struct em28xx_dmaqueue *dma_q,
160 struct em28xx_buffer *buf)
161{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 /* Advice that buffer was filled */
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300166 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300167
Frank Schaefer74209dc2012-11-08 14:11:37 -0300168 dev->usb_ctl.vid_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300169
170 list_del(&buf->vb.queue);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300183 v4l2_get_timestamp(&buf->vb.ts);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300184
Frank Schaefer74209dc2012-11-08 14:11:37 -0300185 dev->usb_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300186
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189}
190
191/*
192 * Identify the buffer header type and properly handles
193 */
194static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf,
197 unsigned char *p,
198 unsigned char *outp, unsigned long len)
199{
200 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300201 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
204 if (dma_q->pos + len > buf->vb.size)
205 len = buf->vb.size - dma_q->pos;
206
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 startread = p;
208 remain = len;
209
Frank Schaeferc02ec712012-11-08 14:11:33 -0300210 if (dev->progressive || buf->top_field)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211 fieldstart = outp;
Frank Schaeferc02ec712012-11-08 14:11:33 -0300212 else /* interlaced mode, even nr. of lines */
213 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300214
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300215 linesdone = dma_q->pos / bytesperline;
216 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300217
218 if (dev->progressive)
219 offset = linesdone * bytesperline + currlinedone;
220 else
221 offset = linesdone * bytesperline * 2 + currlinedone;
222
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300224 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 lencopy = lencopy > remain ? remain : lencopy;
226
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300228 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229 ((char *)startwrite + lencopy) -
230 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300231 remain = (char *)outp + buf->vb.size - (char *)startwrite;
232 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300233 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300234 if (lencopy <= 0)
235 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300236 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300237
238 remain -= lencopy;
239
240 while (remain > 0) {
Frank Schaeferc02ec712012-11-08 14:11:33 -0300241 if (dev->progressive)
242 startwrite += lencopy;
243 else
244 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300245 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300246 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247 lencopy = remain;
248 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300249 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300250
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300251 if ((char *)startwrite + lencopy > (char *)outp +
252 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300253 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
254 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300255 ((char *)startwrite + lencopy) -
256 ((char *)outp + buf->vb.size));
257 lencopy = remain = (char *)outp + buf->vb.size -
258 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300259 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300260 if (lencopy <= 0)
261 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300262
263 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300264
265 remain -= lencopy;
266 }
267
268 dma_q->pos += len;
269}
270
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300271static void em28xx_copy_vbi(struct em28xx *dev,
272 struct em28xx_dmaqueue *dma_q,
273 struct em28xx_buffer *buf,
274 unsigned char *p,
275 unsigned char *outp, unsigned long len)
276{
277 void *startwrite, *startread;
278 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300279 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300280
281 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300282 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300283 return;
284 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300285 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300286
287 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300288 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300289 return;
290 }
291 if (buf == NULL) {
292 return;
293 }
294 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300295 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300296 return;
297 }
298 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300299 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300300 return;
301 }
302
303 if (dma_q->pos + len > buf->vb.size)
304 len = buf->vb.size - dma_q->pos;
305
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300306 startread = p;
307
308 startwrite = outp + dma_q->pos;
309 offset = dma_q->pos;
310
311 /* Make sure the bottom field populates the second half of the frame */
312 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300313 startwrite += bytesperline * dev->vbi_height;
314 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300315 }
316
317 memcpy(startwrite, startread, len);
318 dma_q->pos += len;
319}
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 int packet, int status)
323{
324 char *errmsg = "Unknown";
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327 case -ENOENT:
328 errmsg = "unlinked synchronuously";
329 break;
330 case -ECONNRESET:
331 errmsg = "unlinked asynchronuously";
332 break;
333 case -ENOSR:
334 errmsg = "Buffer error (overrun)";
335 break;
336 case -EPIPE:
337 errmsg = "Stalled (device not responding)";
338 break;
339 case -EOVERFLOW:
340 errmsg = "Babble (bad cable?)";
341 break;
342 case -EPROTO:
343 errmsg = "Bit-stuff error (bad cable?)";
344 break;
345 case -EILSEQ:
346 errmsg = "CRC/Timeout (could be anything)";
347 break;
348 case -ETIME:
349 errmsg = "Device does not respond";
350 break;
351 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300352 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300353 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
354 } else {
355 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 packet, status, errmsg);
357 }
358}
359
360/*
361 * video-buf generic routine to get the next available buffer
362 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300363static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364 struct em28xx_buffer **buf)
365{
366 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300367 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300369 if (list_empty(&dma_q->active)) {
370 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300371 dev->usb_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300372 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300373 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300374 }
375
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300376 /* Get the next buffer */
377 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
378
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300379 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 outp = videobuf_to_vmalloc(&(*buf)->vb);
381 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300382
Frank Schaefer74209dc2012-11-08 14:11:37 -0300383 dev->usb_ctl.vid_buf = *buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384
385 return;
386}
387
388/*
389 * video-buf generic routine to get the next available VBI buffer
390 */
391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 struct em28xx_buffer **buf)
393{
394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 char *outp;
396
397 if (list_empty(&dma_q->active)) {
398 em28xx_isocdbg("No active queue to serve\n");
Frank Schaefer74209dc2012-11-08 14:11:37 -0300399 dev->usb_ctl.vbi_buf = NULL;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300400 *buf = NULL;
401 return;
402 }
403
404 /* Get the next buffer */
405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300407 outp = videobuf_to_vmalloc(&(*buf)->vb);
408 memset(outp, 0x00, (*buf)->vb.size);
409
Frank Schaefer74209dc2012-11-08 14:11:37 -0300410 dev->usb_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300411
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300412 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300413}
414
415/*
416 * Controls the isoc copy of each urb packet
417 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300418static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300419{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300420 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300421 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300422 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300423 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300424 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425
426 if (!dev)
427 return 0;
428
429 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
430 return 0;
431
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 if (urb->status < 0) {
433 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300434 if (urb->status == -ENOENT)
435 return 0;
436 }
437
Frank Schaefer74209dc2012-11-08 14:11:37 -0300438 buf = dev->usb_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300439 if (buf != NULL)
440 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300441
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300442 for (i = 0; i < urb->number_of_packets; i++) {
443 int status = urb->iso_frame_desc[i].status;
444
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300445 if (status < 0) {
446 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447 if (urb->iso_frame_desc[i].status != -EPROTO)
448 continue;
449 }
450
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300451 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452
453 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 continue;
456 }
457 if (urb->iso_frame_desc[i].actual_length >
458 dev->max_pkt_size) {
459 em28xx_isocdbg("packet bigger than packet size");
460 continue;
461 }
462
463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464
465 /* FIXME: incomplete buffer checks where removed to make
466 logic simpler. Impacts of those changes should be evaluated
467 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300468 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
469 em28xx_isocdbg("VBI HEADER!!!\n");
470 /* FIXME: Should add vbi copy */
471 continue;
472 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300473 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300474 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300475 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300476
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300477 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300478 if (buf != NULL)
479 buffer_filled(dev, dma_q, buf);
480 get_next_buf(dma_q, &buf);
481 if (buf == NULL)
482 outp = NULL;
483 else
484 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300485 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300486
487 if (buf != NULL) {
488 if (p[2] & 1)
489 buf->top_field = 0;
490 else
491 buf->top_field = 1;
492 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300493
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300496 if (buf != NULL) {
497 if (p[0] != 0x88 && p[0] != 0x22) {
498 em28xx_isocdbg("frame is not complete\n");
499 len += 4;
500 } else {
501 p += 4;
502 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300503 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300504 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300505 }
506 return rc;
507}
508
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300509/* Version of isoc handler that takes into account a mixture of video and
510 VBI data */
511static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
512{
513 struct em28xx_buffer *buf, *vbi_buf;
514 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300515 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300516 unsigned char *outp = NULL;
517 unsigned char *vbioutp = NULL;
518 int i, len = 0, rc = 1;
519 unsigned char *p;
520 int vbi_size;
521
522 if (!dev)
523 return 0;
524
525 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
526 return 0;
527
528 if (urb->status < 0) {
529 print_err_status(dev, -1, urb->status);
530 if (urb->status == -ENOENT)
531 return 0;
532 }
533
Frank Schaefer74209dc2012-11-08 14:11:37 -0300534 buf = dev->usb_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300535 if (buf != NULL)
536 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300537
Frank Schaefer74209dc2012-11-08 14:11:37 -0300538 vbi_buf = dev->usb_ctl.vbi_buf;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300539 if (vbi_buf != NULL)
540 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
541
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300542 for (i = 0; i < urb->number_of_packets; i++) {
543 int status = urb->iso_frame_desc[i].status;
544
545 if (status < 0) {
546 print_err_status(dev, i, status);
547 if (urb->iso_frame_desc[i].status != -EPROTO)
548 continue;
549 }
550
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300551 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300552 if (urb->iso_frame_desc[i].actual_length <= 0) {
553 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
554 continue;
555 }
556 if (urb->iso_frame_desc[i].actual_length >
557 dev->max_pkt_size) {
558 em28xx_isocdbg("packet bigger than packet size");
559 continue;
560 }
561
562 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
563
564 /* capture type 0 = vbi start
565 capture type 1 = video start
566 capture type 2 = video in progress */
567 if (p[0] == 0x33 && p[1] == 0x95) {
568 dev->capture_type = 0;
569 dev->vbi_read = 0;
570 em28xx_isocdbg("VBI START HEADER!!!\n");
571 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300572 p += 4;
573 len -= 4;
574 } else if (p[0] == 0x88 && p[1] == 0x88 &&
575 p[2] == 0x88 && p[3] == 0x88) {
576 /* continuation */
577 p += 4;
578 len -= 4;
579 } else if (p[0] == 0x22 && p[1] == 0x5a) {
580 /* start video */
581 p += 4;
582 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300583 }
584
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300585 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300586
587 if (dev->capture_type == 0) {
588 if (dev->vbi_read >= vbi_size) {
589 /* We've already read all the VBI data, so
590 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300591 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300592 } else if ((dev->vbi_read + len) < vbi_size) {
593 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300594 if (dev->vbi_read == 0 &&
595 (!(dev->cur_field & 1))) {
596 /* Brand new frame */
597 if (vbi_buf != NULL)
598 vbi_buffer_filled(dev,
599 vbi_dma_q,
600 vbi_buf);
601 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
602 if (vbi_buf == NULL)
603 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300604 else
605 vbioutp = videobuf_to_vmalloc(
606 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300607 }
608
609 if (dev->vbi_read == 0) {
610 vbi_dma_q->pos = 0;
611 if (vbi_buf != NULL) {
612 if (dev->cur_field & 1)
613 vbi_buf->top_field = 0;
614 else
615 vbi_buf->top_field = 1;
616 }
617 }
618
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300619 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300620 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
621 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300622 } else {
623 /* Some of this frame is VBI data and some is
624 video data */
625 int vbi_data_len = vbi_size - dev->vbi_read;
626 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300627 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300629 dev->capture_type = 1;
630 p += vbi_data_len;
631 len -= vbi_data_len;
632 }
633 }
634
635 if (dev->capture_type == 1) {
636 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300637 if (dev->progressive || !(dev->cur_field & 1)) {
638 if (buf != NULL)
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
641 if (buf == NULL)
642 outp = NULL;
643 else
644 outp = videobuf_to_vmalloc(&buf->vb);
645 }
646 if (buf != NULL) {
647 if (dev->cur_field & 1)
648 buf->top_field = 0;
649 else
650 buf->top_field = 1;
651 }
652
653 dma_q->pos = 0;
654 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300655
656 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300657 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300658 p[2] == 0x88 && p[3] == 0x88) {
659 p += 4;
660 len -= 4;
661 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300662 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300663 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
664 p[2], len, (p[2] & 1) ?
665 "odd" : "even");
666 p += 4;
667 len -= 4;
668 }
669
670 if (len > 0)
671 em28xx_copy_video(dev, dma_q, buf, p, outp,
672 len);
673 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300674 }
675 return rc;
676}
677
678
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680 Videobuf operations
681 ------------------------------------------------------------------*/
682
683static int
684buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
685{
686 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300687 struct em28xx *dev = fh->dev;
688 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300690 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
691 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300692
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300693 if (0 == *count)
694 *count = EM28XX_DEF_BUF;
695
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300696 if (*count < EM28XX_MIN_BUF)
697 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300699 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300700 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300701 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300702 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300703
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300704 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300705
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300706 return 0;
707}
708
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300709/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300710static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
711{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300712 struct em28xx_fh *fh = vq->priv_data;
713 struct em28xx *dev = fh->dev;
714 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 if (in_interrupt())
716 BUG();
717
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300718 /* We used to wait for the buffer to finish here, but this didn't work
719 because, as we were keeping the state as VIDEOBUF_QUEUED,
720 videobuf_queue_cancel marked it as finished for us.
721 (Also, it could wedge forever if the hardware was misconfigured.)
722
723 This should be safe; by the time we get here, the buffer isn't
724 queued anymore. If we ever start marking the buffers as
725 VIDEOBUF_ACTIVE, it won't be, though.
726 */
727 spin_lock_irqsave(&dev->slock, flags);
Frank Schaefer74209dc2012-11-08 14:11:37 -0300728 if (dev->usb_ctl.vid_buf == buf)
729 dev->usb_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300730 spin_unlock_irqrestore(&dev->slock, flags);
731
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300732 videobuf_vmalloc_free(&buf->vb);
733 buf->vb.state = VIDEOBUF_NEEDS_INIT;
734}
735
736static int
737buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
738 enum v4l2_field field)
739{
740 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300741 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300742 struct em28xx *dev = fh->dev;
743 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300745 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
746 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300747
748 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
749 return -EINVAL;
750
Brandon Philips05612972008-04-13 14:57:01 -0300751 buf->vb.width = dev->width;
752 buf->vb.height = dev->height;
753 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300754
755 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300756 rc = videobuf_iolock(vq, &buf->vb, NULL);
757 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300758 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759 }
760
Frank Schaefer74209dc2012-11-08 14:11:37 -0300761 if (!dev->usb_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300762 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763
764 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300765 if (em28xx_vbi_supported(dev) == 1)
Gianluca Gennari86d38d12012-02-13 13:59:22 -0300766 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
Frank Schaefer515688a2012-11-08 14:11:35 -0300767 EM28XX_NUM_ISOC_PACKETS,
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300768 EM28XX_NUM_BUFS,
769 dev->max_pkt_size,
770 em28xx_isoc_copy_vbi);
771 else
Gianluca Gennari86d38d12012-02-13 13:59:22 -0300772 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
Frank Schaefer515688a2012-11-08 14:11:35 -0300773 EM28XX_NUM_ISOC_PACKETS,
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300774 EM28XX_NUM_BUFS,
775 dev->max_pkt_size,
776 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300777 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300778 goto fail;
779 }
780
781 buf->vb.state = VIDEOBUF_PREPARED;
782 return 0;
783
784fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300785 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300786 return rc;
787}
788
789static void
790buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
791{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300792 struct em28xx_buffer *buf = container_of(vb,
793 struct em28xx_buffer,
794 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300796 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300798
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300799 buf->vb.state = VIDEOBUF_QUEUED;
800 list_add_tail(&buf->vb.queue, &vidq->active);
801
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300802}
803
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300804static void buffer_release(struct videobuf_queue *vq,
805 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300807 struct em28xx_buffer *buf = container_of(vb,
808 struct em28xx_buffer,
809 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300811 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300812
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300813 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300814
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300815 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300816}
817
818static struct videobuf_queue_ops em28xx_video_qops = {
819 .buf_setup = buffer_setup,
820 .buf_prepare = buffer_prepare,
821 .buf_queue = buffer_queue,
822 .buf_release = buffer_release,
823};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800824
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300825/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800826
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800827static void video_mux(struct em28xx *dev, int index)
828{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800829 dev->ctl_input = index;
830 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300831 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800832
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300833 if (!dev->ctl_aoutput)
834 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
835
Hans Verkuil5325b422009-04-02 11:26:22 -0300836 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
837 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800838
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300839 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300840 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300841 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
842 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300843 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300844 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300845 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
846 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800847 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300848
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300849 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300850 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
851 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300852 }
853
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300854 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800855}
856
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300857/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300858static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300859{
860 struct em28xx *dev = fh->dev;
861
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300862 if (fh->resources & bit)
863 /* have it already allocated */
864 return 1;
865
866 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300867 if (dev->resources & bit) {
868 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869 return 0;
870 }
871 /* it's free, grab it */
872 fh->resources |= bit;
873 dev->resources |= bit;
874 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300875 return 1;
876}
877
878static int res_check(struct em28xx_fh *fh, unsigned int bit)
879{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300880 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300881}
882
883static int res_locked(struct em28xx *dev, unsigned int bit)
884{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300885 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300886}
887
888static void res_free(struct em28xx_fh *fh, unsigned int bits)
889{
890 struct em28xx *dev = fh->dev;
891
892 BUG_ON((fh->resources & bits) != bits);
893
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300894 fh->resources &= ~bits;
895 dev->resources &= ~bits;
896 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300897}
898
899static int get_ressource(struct em28xx_fh *fh)
900{
901 switch (fh->type) {
902 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
903 return EM28XX_RESOURCE_VIDEO;
904 case V4L2_BUF_TYPE_VBI_CAPTURE:
905 return EM28XX_RESOURCE_VBI;
906 default:
907 BUG();
908 return 0;
909 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300910}
911
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800912/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300913 * ac97_queryctrl()
914 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300915 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300916static int ac97_queryctrl(struct v4l2_queryctrl *qc)
917{
918 int i;
919
920 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
921 if (qc->id && qc->id == ac97_qctrl[i].id) {
922 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
923 return 0;
924 }
925 }
926
927 /* Control is not ac97 related */
928 return 1;
929}
930
931/*
932 * ac97_get_ctrl()
933 * return the current values for ac97 mute and volume
934 */
935static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936{
937 switch (ctrl->id) {
938 case V4L2_CID_AUDIO_MUTE:
939 ctrl->value = dev->mute;
940 return 0;
941 case V4L2_CID_AUDIO_VOLUME:
942 ctrl->value = dev->volume;
943 return 0;
944 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300945 /* Control is not ac97 related */
946 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947 }
948}
949
950/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300951 * ac97_set_ctrl()
952 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300953 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300954static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300956 int i;
957
958 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
959 if (ctrl->id == ac97_qctrl[i].id)
960 goto handle;
961
962 /* Announce that hasn't handle it */
963 return 1;
964
965handle:
966 if (ctrl->value < ac97_qctrl[i].minimum ||
967 ctrl->value > ac97_qctrl[i].maximum)
968 return -ERANGE;
969
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300970 switch (ctrl->id) {
971 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300972 dev->mute = ctrl->value;
973 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974 case V4L2_CID_AUDIO_VOLUME:
975 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300976 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300977 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300978
979 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980}
981
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300982static int check_dev(struct em28xx *dev)
983{
984 if (dev->state & DEV_DISCONNECTED) {
985 em28xx_errdev("v4l2 ioctl: device not present\n");
986 return -ENODEV;
987 }
988
989 if (dev->state & DEV_MISCONFIGURED) {
990 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
991 "close and open it again\n");
992 return -EIO;
993 }
994 return 0;
995}
996
997static void get_scale(struct em28xx *dev,
998 unsigned int width, unsigned int height,
999 unsigned int *hscale, unsigned int *vscale)
1000{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001001 unsigned int maxw = norm_maxw(dev);
1002 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001003
1004 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1005 if (*hscale >= 0x4000)
1006 *hscale = 0x3fff;
1007
1008 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1009 if (*vscale >= 0x4000)
1010 *vscale = 0x3fff;
1011}
1012
1013/* ------------------------------------------------------------------
1014 IOCTL vidioc handling
1015 ------------------------------------------------------------------*/
1016
Hans Verkuil78b526a2008-05-28 12:16:41 -03001017static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001018 struct v4l2_format *f)
1019{
1020 struct em28xx_fh *fh = priv;
1021 struct em28xx *dev = fh->dev;
1022
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 f->fmt.pix.width = dev->width;
1024 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001025 f->fmt.pix.pixelformat = dev->format->fourcc;
1026 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001027 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1029
1030 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001031 if (dev->progressive)
1032 f->fmt.pix.field = V4L2_FIELD_NONE;
1033 else
1034 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001036 return 0;
1037}
1038
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001039static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1040{
1041 unsigned int i;
1042
1043 for (i = 0; i < ARRAY_SIZE(format); i++)
1044 if (format[i].fourcc == fourcc)
1045 return &format[i];
1046
1047 return NULL;
1048}
1049
Hans Verkuil78b526a2008-05-28 12:16:41 -03001050static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001051 struct v4l2_format *f)
1052{
1053 struct em28xx_fh *fh = priv;
1054 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001055 unsigned int width = f->fmt.pix.width;
1056 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001057 unsigned int maxw = norm_maxw(dev);
1058 unsigned int maxh = norm_maxh(dev);
1059 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001060 struct em28xx_fmt *fmt;
1061
1062 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1063 if (!fmt) {
1064 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1065 f->fmt.pix.pixelformat);
1066 return -EINVAL;
1067 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001069 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001070 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001071 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1072 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001073 /* MaxPacketSize for em2800 is too small to capture at full resolution
1074 * use half of maxw as the scaler can only scale to 50% */
1075 if (width == maxw && height == maxh)
1076 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001077 } else {
1078 /* width must even because of the YUYV format
1079 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001080 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1081 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082 }
1083
1084 get_scale(dev, width, height, &hscale, &vscale);
1085
1086 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1087 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1088
1089 f->fmt.pix.width = width;
1090 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001091 f->fmt.pix.pixelformat = fmt->fourcc;
1092 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1093 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001094 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001095 if (dev->progressive)
1096 f->fmt.pix.field = V4L2_FIELD_NONE;
1097 else
1098 f->fmt.pix.field = dev->interlaced ?
1099 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001100
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001101 return 0;
1102}
1103
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001104static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1105 unsigned width, unsigned height)
1106{
1107 struct em28xx_fmt *fmt;
1108
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001109 fmt = format_by_fourcc(fourcc);
1110 if (!fmt)
1111 return -EINVAL;
1112
1113 dev->format = fmt;
1114 dev->width = width;
1115 dev->height = height;
1116
1117 /* set new image size */
1118 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1119
1120 em28xx_set_alternate(dev);
1121 em28xx_resolution_set(dev);
1122
1123 return 0;
1124}
1125
Hans Verkuil78b526a2008-05-28 12:16:41 -03001126static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001127 struct v4l2_format *f)
1128{
1129 struct em28xx_fh *fh = priv;
1130 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001131 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001132
1133 rc = check_dev(dev);
1134 if (rc < 0)
1135 return rc;
1136
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001137 vidioc_try_fmt_vid_cap(file, priv, f);
1138
Brandon Philips05612972008-04-13 14:57:01 -03001139 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1140 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001141 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001142 }
1143
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001144 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001145 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001146}
1147
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001148static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1149{
1150 struct em28xx_fh *fh = priv;
1151 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001152 int rc;
1153
1154 rc = check_dev(dev);
1155 if (rc < 0)
1156 return rc;
1157
1158 *norm = dev->norm;
1159
1160 return 0;
1161}
1162
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001163static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1164{
1165 struct em28xx_fh *fh = priv;
1166 struct em28xx *dev = fh->dev;
1167 int rc;
1168
1169 rc = check_dev(dev);
1170 if (rc < 0)
1171 return rc;
1172
1173 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1174
1175 return 0;
1176}
1177
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001178static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001179{
1180 struct em28xx_fh *fh = priv;
1181 struct em28xx *dev = fh->dev;
1182 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001183 int rc;
1184
1185 rc = check_dev(dev);
1186 if (rc < 0)
1187 return rc;
1188
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001189 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001190
1191 /* Adjusts width/height, if needed */
1192 f.fmt.pix.width = dev->width;
1193 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001194 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001195
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001196 /* set new image size */
1197 dev->width = f.fmt.pix.width;
1198 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1200
1201 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001202 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001204 return 0;
1205}
1206
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001207static int vidioc_g_parm(struct file *file, void *priv,
1208 struct v4l2_streamparm *p)
1209{
1210 struct em28xx_fh *fh = priv;
1211 struct em28xx *dev = fh->dev;
1212 int rc = 0;
1213
1214 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1215 return -EINVAL;
1216
1217 if (dev->board.is_webcam)
1218 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1219 video, g_parm, p);
1220 else
1221 v4l2_video_std_frame_period(dev->norm,
1222 &p->parm.capture.timeperframe);
1223
1224 return rc;
1225}
1226
1227static int vidioc_s_parm(struct file *file, void *priv,
1228 struct v4l2_streamparm *p)
1229{
1230 struct em28xx_fh *fh = priv;
1231 struct em28xx *dev = fh->dev;
1232
1233 if (!dev->board.is_webcam)
1234 return -EINVAL;
1235
1236 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1237 return -EINVAL;
1238
1239 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1240}
1241
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001242static const char *iname[] = {
1243 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1244 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1245 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1246 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1247 [EM28XX_VMUX_SVIDEO] = "S-Video",
1248 [EM28XX_VMUX_TELEVISION] = "Television",
1249 [EM28XX_VMUX_CABLE] = "Cable TV",
1250 [EM28XX_VMUX_DVB] = "DVB",
1251 [EM28XX_VMUX_DEBUG] = "for debug only",
1252};
1253
1254static int vidioc_enum_input(struct file *file, void *priv,
1255 struct v4l2_input *i)
1256{
1257 struct em28xx_fh *fh = priv;
1258 struct em28xx *dev = fh->dev;
1259 unsigned int n;
1260
1261 n = i->index;
1262 if (n >= MAX_EM28XX_INPUT)
1263 return -EINVAL;
1264 if (0 == INPUT(n)->type)
1265 return -EINVAL;
1266
1267 i->index = n;
1268 i->type = V4L2_INPUT_TYPE_CAMERA;
1269
1270 strcpy(i->name, iname[INPUT(n)->type]);
1271
1272 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1273 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1274 i->type = V4L2_INPUT_TYPE_TUNER;
1275
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001276 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001277
1278 return 0;
1279}
1280
1281static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1282{
1283 struct em28xx_fh *fh = priv;
1284 struct em28xx *dev = fh->dev;
1285
1286 *i = dev->ctl_input;
1287
1288 return 0;
1289}
1290
1291static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1292{
1293 struct em28xx_fh *fh = priv;
1294 struct em28xx *dev = fh->dev;
1295 int rc;
1296
1297 rc = check_dev(dev);
1298 if (rc < 0)
1299 return rc;
1300
1301 if (i >= MAX_EM28XX_INPUT)
1302 return -EINVAL;
1303 if (0 == INPUT(i)->type)
1304 return -EINVAL;
1305
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03001306 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307 return 0;
1308}
1309
1310static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1311{
1312 struct em28xx_fh *fh = priv;
1313 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001314
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001315 if (!dev->audio_mode.has_audio)
1316 return -EINVAL;
1317
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001318 switch (a->index) {
1319 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001320 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001321 break;
1322 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001323 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001324 break;
1325 case EM28XX_AMUX_VIDEO2:
1326 strcpy(a->name, "Television alt");
1327 break;
1328 case EM28XX_AMUX_PHONE:
1329 strcpy(a->name, "Phone");
1330 break;
1331 case EM28XX_AMUX_MIC:
1332 strcpy(a->name, "Mic");
1333 break;
1334 case EM28XX_AMUX_CD:
1335 strcpy(a->name, "CD");
1336 break;
1337 case EM28XX_AMUX_AUX:
1338 strcpy(a->name, "Aux");
1339 break;
1340 case EM28XX_AMUX_PCM_OUT:
1341 strcpy(a->name, "PCM");
1342 break;
1343 default:
1344 return -EINVAL;
1345 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001346
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001347 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001349
1350 return 0;
1351}
1352
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001353static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354{
1355 struct em28xx_fh *fh = priv;
1356 struct em28xx *dev = fh->dev;
1357
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001358
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001359 if (!dev->audio_mode.has_audio)
1360 return -EINVAL;
1361
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001362 if (a->index >= MAX_EM28XX_INPUT)
1363 return -EINVAL;
1364 if (0 == INPUT(a->index)->type)
1365 return -EINVAL;
1366
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001367 dev->ctl_ainput = INPUT(a->index)->amux;
1368 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001369
1370 if (!dev->ctl_aoutput)
1371 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001372
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001373 return 0;
1374}
1375
1376static int vidioc_queryctrl(struct file *file, void *priv,
1377 struct v4l2_queryctrl *qc)
1378{
1379 struct em28xx_fh *fh = priv;
1380 struct em28xx *dev = fh->dev;
1381 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 int rc;
1383
1384 rc = check_dev(dev);
1385 if (rc < 0)
1386 return rc;
1387
1388 memset(qc, 0, sizeof(*qc));
1389
1390 qc->id = id;
1391
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001392 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001393 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1394 rc = ac97_queryctrl(qc);
1395 if (!rc)
1396 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001397 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001398
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001399 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001400 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401
1402 if (qc->type)
1403 return 0;
1404 else
1405 return -EINVAL;
1406}
1407
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001408/*
1409 * FIXME: This is an indirect way to check if a control exists at a
1410 * subdev. Instead of that hack, maybe the better would be to change all
1411 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1412 */
1413static int check_subdev_ctrl(struct em28xx *dev, int id)
1414{
1415 struct v4l2_queryctrl qc;
1416
1417 memset(&qc, 0, sizeof(qc));
1418 qc.id = id;
1419
1420 /* enumerate V4L2 device controls */
1421 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1422
1423 if (qc.type)
1424 return 0;
1425 else
1426 return -EINVAL;
1427}
1428
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001429static int vidioc_g_ctrl(struct file *file, void *priv,
1430 struct v4l2_control *ctrl)
1431{
1432 struct em28xx_fh *fh = priv;
1433 struct em28xx *dev = fh->dev;
1434 int rc;
1435
1436 rc = check_dev(dev);
1437 if (rc < 0)
1438 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001439 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001440
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001441 /* Set an AC97 control */
1442 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1443 rc = ac97_get_ctrl(dev, ctrl);
1444 else
1445 rc = 1;
1446
1447 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1448 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001449 if (check_subdev_ctrl(dev, ctrl->id))
1450 return -EINVAL;
1451
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001452 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001453 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001454 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001455
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001456 return rc;
1457}
1458
1459static int vidioc_s_ctrl(struct file *file, void *priv,
1460 struct v4l2_control *ctrl)
1461{
1462 struct em28xx_fh *fh = priv;
1463 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464 int rc;
1465
1466 rc = check_dev(dev);
1467 if (rc < 0)
1468 return rc;
1469
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001470 /* Set an AC97 control */
1471 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1472 rc = ac97_set_ctrl(dev, ctrl);
1473 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001474 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001475
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001476 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001477 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001478 rc = check_subdev_ctrl(dev, ctrl->id);
1479 if (!rc)
1480 v4l2_device_call_all(&dev->v4l2_dev, 0,
1481 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001482 /*
1483 * In the case of non-AC97 volume controls, we still need
1484 * to do some setups at em28xx, in order to mute/unmute
1485 * and to adjust audio volume. However, the value ranges
1486 * should be checked by the corresponding V4L subdriver.
1487 */
1488 switch (ctrl->id) {
1489 case V4L2_CID_AUDIO_MUTE:
1490 dev->mute = ctrl->value;
1491 rc = em28xx_audio_analog_set(dev);
1492 break;
1493 case V4L2_CID_AUDIO_VOLUME:
1494 dev->volume = ctrl->value;
1495 rc = em28xx_audio_analog_set(dev);
1496 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001497 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001498 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001499}
1500
1501static int vidioc_g_tuner(struct file *file, void *priv,
1502 struct v4l2_tuner *t)
1503{
1504 struct em28xx_fh *fh = priv;
1505 struct em28xx *dev = fh->dev;
1506 int rc;
1507
1508 rc = check_dev(dev);
1509 if (rc < 0)
1510 return rc;
1511
1512 if (0 != t->index)
1513 return -EINVAL;
1514
1515 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001516 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001517
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001518 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001519 return 0;
1520}
1521
1522static int vidioc_s_tuner(struct file *file, void *priv,
1523 struct v4l2_tuner *t)
1524{
1525 struct em28xx_fh *fh = priv;
1526 struct em28xx *dev = fh->dev;
1527 int rc;
1528
1529 rc = check_dev(dev);
1530 if (rc < 0)
1531 return rc;
1532
1533 if (0 != t->index)
1534 return -EINVAL;
1535
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001536 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001537 return 0;
1538}
1539
1540static int vidioc_g_frequency(struct file *file, void *priv,
1541 struct v4l2_frequency *f)
1542{
1543 struct em28xx_fh *fh = priv;
1544 struct em28xx *dev = fh->dev;
1545
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001546 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001547 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001548 return 0;
1549}
1550
1551static int vidioc_s_frequency(struct file *file, void *priv,
1552 struct v4l2_frequency *f)
1553{
1554 struct em28xx_fh *fh = priv;
1555 struct em28xx *dev = fh->dev;
1556 int rc;
1557
1558 rc = check_dev(dev);
1559 if (rc < 0)
1560 return rc;
1561
1562 if (0 != f->tuner)
1563 return -EINVAL;
1564
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001565 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1566 return -EINVAL;
1567 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001568 return -EINVAL;
1569
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001570 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001571 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001572
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001573 return 0;
1574}
1575
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001576#ifdef CONFIG_VIDEO_ADV_DEBUG
1577static int em28xx_reg_len(int reg)
1578{
1579 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001580 case EM28XX_R40_AC97LSB:
1581 case EM28XX_R30_HSCALELOW:
1582 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001583 return 2;
1584 default:
1585 return 1;
1586 }
1587}
1588
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001589static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001590 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001591{
1592 struct em28xx_fh *fh = priv;
1593 struct em28xx *dev = fh->dev;
1594
1595 chip->ident = V4L2_IDENT_NONE;
1596 chip->revision = 0;
1597
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001598 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001599
1600 return 0;
1601}
1602
1603
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001604static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001605 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001606{
1607 struct em28xx_fh *fh = priv;
1608 struct em28xx *dev = fh->dev;
1609 int ret;
1610
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001611 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001612 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001613 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001614 if (ret < 0)
1615 return ret;
1616
1617 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001618 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001619 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001620 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001621 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001622 return 0;
1623 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001624 /* TODO: is this correct? */
1625 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1626 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001627 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001628 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001629 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001630 }
1631
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001632 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001633 reg->size = em28xx_reg_len(reg->reg);
1634 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001635 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001636
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001637 if (ret < 0)
1638 return ret;
1639
1640 reg->val = ret;
1641 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001642 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1644 reg->reg, (char *)&val, 2);
1645 if (ret < 0)
1646 return ret;
1647
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001648 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001649 }
1650
1651 return 0;
1652}
1653
1654static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001655 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001656{
1657 struct em28xx_fh *fh = priv;
1658 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001659 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001660
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001661 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001662 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001663 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001664 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001665 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001666 return 0;
1667 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001668 /* TODO: is this correct? */
1669 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1670 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001671 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001672 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001673 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001674 }
1675
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001676 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001677 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001678
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001679 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001680 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001681}
1682#endif
1683
1684
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001685static int vidioc_cropcap(struct file *file, void *priv,
1686 struct v4l2_cropcap *cc)
1687{
1688 struct em28xx_fh *fh = priv;
1689 struct em28xx *dev = fh->dev;
1690
1691 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1692 return -EINVAL;
1693
1694 cc->bounds.left = 0;
1695 cc->bounds.top = 0;
1696 cc->bounds.width = dev->width;
1697 cc->bounds.height = dev->height;
1698 cc->defrect = cc->bounds;
1699 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1700 cc->pixelaspect.denominator = 59;
1701
1702 return 0;
1703}
1704
1705static int vidioc_streamon(struct file *file, void *priv,
1706 enum v4l2_buf_type type)
1707{
1708 struct em28xx_fh *fh = priv;
1709 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001710 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001711
1712 rc = check_dev(dev);
1713 if (rc < 0)
1714 return rc;
1715
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001716 if (unlikely(type != fh->type))
1717 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001718
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1720 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001721
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001722 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001723 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001724
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001725 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1726 rc = videobuf_streamon(&fh->vb_vidq);
1727 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1728 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001729
1730 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001731}
1732
1733static int vidioc_streamoff(struct file *file, void *priv,
1734 enum v4l2_buf_type type)
1735{
1736 struct em28xx_fh *fh = priv;
1737 struct em28xx *dev = fh->dev;
1738 int rc;
1739
1740 rc = check_dev(dev);
1741 if (rc < 0)
1742 return rc;
1743
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001744 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1745 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001746 return -EINVAL;
1747 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001748 return -EINVAL;
1749
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001750 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1751 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001752
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001753 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001754 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1755 videobuf_streamoff(&fh->vb_vidq);
1756 res_free(fh, EM28XX_RESOURCE_VIDEO);
1757 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001758 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001759 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1760 videobuf_streamoff(&fh->vb_vbiq);
1761 res_free(fh, EM28XX_RESOURCE_VBI);
1762 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001763 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001764
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001765 return 0;
1766}
1767
1768static int vidioc_querycap(struct file *file, void *priv,
1769 struct v4l2_capability *cap)
1770{
1771 struct em28xx_fh *fh = priv;
1772 struct em28xx *dev = fh->dev;
1773
1774 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1775 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001776 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001777
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001778 cap->capabilities =
1779 V4L2_CAP_SLICED_VBI_CAPTURE |
1780 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001781 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1782
Devin Heitmueller04146142009-09-11 00:08:44 -03001783 if (dev->vbi_dev)
1784 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1785
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001786 if (dev->audio_mode.has_audio)
1787 cap->capabilities |= V4L2_CAP_AUDIO;
1788
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001789 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001790 cap->capabilities |= V4L2_CAP_TUNER;
1791
1792 return 0;
1793}
1794
Hans Verkuil78b526a2008-05-28 12:16:41 -03001795static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001796 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001797{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001798 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001799 return -EINVAL;
1800
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001801 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1802 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001803
1804 return 0;
1805}
1806
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02001807static int vidioc_enum_framesizes(struct file *file, void *priv,
1808 struct v4l2_frmsizeenum *fsize)
1809{
1810 struct em28xx_fh *fh = priv;
1811 struct em28xx *dev = fh->dev;
1812 struct em28xx_fmt *fmt;
1813 unsigned int maxw = norm_maxw(dev);
1814 unsigned int maxh = norm_maxh(dev);
1815
1816 fmt = format_by_fourcc(fsize->pixel_format);
1817 if (!fmt) {
1818 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1819 fsize->pixel_format);
1820 return -EINVAL;
1821 }
1822
1823 if (dev->board.is_em2800) {
1824 if (fsize->index > 1)
1825 return -EINVAL;
1826 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1827 fsize->discrete.width = maxw / (1 + fsize->index);
1828 fsize->discrete.height = maxh / (1 + fsize->index);
1829 return 0;
1830 }
1831
1832 if (fsize->index != 0)
1833 return -EINVAL;
1834
1835 /* Report a continuous range */
1836 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1837 fsize->stepwise.min_width = 48;
1838 fsize->stepwise.min_height = 32;
1839 fsize->stepwise.max_width = maxw;
1840 fsize->stepwise.max_height = maxh;
1841 fsize->stepwise.step_width = 1;
1842 fsize->stepwise.step_height = 1;
1843 return 0;
1844}
1845
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001846/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001847static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001848 struct v4l2_format *f)
1849{
1850 struct em28xx_fh *fh = priv;
1851 struct em28xx *dev = fh->dev;
1852 int rc;
1853
1854 rc = check_dev(dev);
1855 if (rc < 0)
1856 return rc;
1857
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001858 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001859 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860
1861 if (f->fmt.sliced.service_set == 0)
1862 rc = -EINVAL;
1863
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001864 return rc;
1865}
1866
Hans Verkuil78b526a2008-05-28 12:16:41 -03001867static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001868 struct v4l2_format *f)
1869{
1870 struct em28xx_fh *fh = priv;
1871 struct em28xx *dev = fh->dev;
1872 int rc;
1873
1874 rc = check_dev(dev);
1875 if (rc < 0)
1876 return rc;
1877
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001878 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001879
1880 if (f->fmt.sliced.service_set == 0)
1881 return -EINVAL;
1882
1883 return 0;
1884}
1885
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001886/* RAW VBI ioctls */
1887
1888static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1889 struct v4l2_format *format)
1890{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001891 struct em28xx_fh *fh = priv;
1892 struct em28xx *dev = fh->dev;
1893
1894 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001895 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1896 format->fmt.vbi.offset = 0;
1897 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001898 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1899 format->fmt.vbi.count[0] = dev->vbi_height;
1900 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001901
1902 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001903 if (dev->norm & V4L2_STD_525_60) {
1904 /* NTSC */
1905 format->fmt.vbi.start[0] = 10;
1906 format->fmt.vbi.start[1] = 273;
1907 } else if (dev->norm & V4L2_STD_625_50) {
1908 /* PAL */
1909 format->fmt.vbi.start[0] = 6;
1910 format->fmt.vbi.start[1] = 318;
1911 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001912
1913 return 0;
1914}
1915
1916static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1917 struct v4l2_format *format)
1918{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001919 struct em28xx_fh *fh = priv;
1920 struct em28xx *dev = fh->dev;
1921
1922 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001923 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1924 format->fmt.vbi.offset = 0;
1925 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001926 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1927 format->fmt.vbi.count[0] = dev->vbi_height;
1928 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001929
1930 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001931 if (dev->norm & V4L2_STD_525_60) {
1932 /* NTSC */
1933 format->fmt.vbi.start[0] = 10;
1934 format->fmt.vbi.start[1] = 273;
1935 } else if (dev->norm & V4L2_STD_625_50) {
1936 /* PAL */
1937 format->fmt.vbi.start[0] = 6;
1938 format->fmt.vbi.start[1] = 318;
1939 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001940
1941 return 0;
1942}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001943
1944static int vidioc_reqbufs(struct file *file, void *priv,
1945 struct v4l2_requestbuffers *rb)
1946{
1947 struct em28xx_fh *fh = priv;
1948 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001949 int rc;
1950
1951 rc = check_dev(dev);
1952 if (rc < 0)
1953 return rc;
1954
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001955 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1956 return videobuf_reqbufs(&fh->vb_vidq, rb);
1957 else
1958 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001959}
1960
1961static int vidioc_querybuf(struct file *file, void *priv,
1962 struct v4l2_buffer *b)
1963{
1964 struct em28xx_fh *fh = priv;
1965 struct em28xx *dev = fh->dev;
1966 int rc;
1967
1968 rc = check_dev(dev);
1969 if (rc < 0)
1970 return rc;
1971
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001972 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1973 return videobuf_querybuf(&fh->vb_vidq, b);
1974 else {
1975 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1976 the videobuf framework, but we probably shouldn't be
1977 returning a buffer larger than that which was asked for.
1978 At a minimum, it causes a crash in zvbi since it does
1979 a memcpy based on the source buffer length */
1980 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001981 b->length = dev->vbi_width * dev->vbi_height * 2;
1982
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001983 return result;
1984 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001985}
1986
1987static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1988{
1989 struct em28xx_fh *fh = priv;
1990 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001991 int rc;
1992
1993 rc = check_dev(dev);
1994 if (rc < 0)
1995 return rc;
1996
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001997 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1998 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001999 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002000 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002001}
2002
2003static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2004{
2005 struct em28xx_fh *fh = priv;
2006 struct em28xx *dev = fh->dev;
2007 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002008
2009 rc = check_dev(dev);
2010 if (rc < 0)
2011 return rc;
2012
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002013 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2014 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2015 O_NONBLOCK);
2016 else
2017 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2018 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002019}
2020
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002021/* ----------------------------------------------------------- */
2022/* RADIO ESPECIFIC IOCTLS */
2023/* ----------------------------------------------------------- */
2024
2025static int radio_querycap(struct file *file, void *priv,
2026 struct v4l2_capability *cap)
2027{
2028 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2029
2030 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2031 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002032 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002033
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002034 cap->capabilities = V4L2_CAP_TUNER;
2035 return 0;
2036}
2037
2038static int radio_g_tuner(struct file *file, void *priv,
2039 struct v4l2_tuner *t)
2040{
2041 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2042
2043 if (unlikely(t->index > 0))
2044 return -EINVAL;
2045
2046 strcpy(t->name, "Radio");
2047 t->type = V4L2_TUNER_RADIO;
2048
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002049 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002050
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002051 return 0;
2052}
2053
2054static int radio_enum_input(struct file *file, void *priv,
2055 struct v4l2_input *i)
2056{
2057 if (i->index != 0)
2058 return -EINVAL;
2059 strcpy(i->name, "Radio");
2060 i->type = V4L2_INPUT_TYPE_TUNER;
2061
2062 return 0;
2063}
2064
2065static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2066{
2067 if (unlikely(a->index))
2068 return -EINVAL;
2069
2070 strcpy(a->name, "Radio");
2071 return 0;
2072}
2073
2074static int radio_s_tuner(struct file *file, void *priv,
2075 struct v4l2_tuner *t)
2076{
2077 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2078
2079 if (0 != t->index)
2080 return -EINVAL;
2081
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002082 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002083
2084 return 0;
2085}
2086
2087static int radio_s_audio(struct file *file, void *fh,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03002088 const struct v4l2_audio *a)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002089{
2090 return 0;
2091}
2092
2093static int radio_s_input(struct file *file, void *fh, unsigned int i)
2094{
2095 return 0;
2096}
2097
2098static int radio_queryctrl(struct file *file, void *priv,
2099 struct v4l2_queryctrl *qc)
2100{
2101 int i;
2102
2103 if (qc->id < V4L2_CID_BASE ||
2104 qc->id >= V4L2_CID_LASTP1)
2105 return -EINVAL;
2106
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002107 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2108 if (qc->id && qc->id == ac97_qctrl[i].id) {
2109 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002110 return 0;
2111 }
2112 }
2113
2114 return -EINVAL;
2115}
2116
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002117/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002118 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002119 * inits the device and starts isoc transfer
2120 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002121static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002122{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002123 int errCode = 0, radio = 0;
2124 struct video_device *vdev = video_devdata(filp);
2125 struct em28xx *dev = video_drvdata(filp);
2126 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002127 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002128 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002129
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002130 switch (vdev->vfl_type) {
2131 case VFL_TYPE_GRABBER:
2132 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2133 break;
2134 case VFL_TYPE_VBI:
2135 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2136 break;
2137 case VFL_TYPE_RADIO:
2138 radio = 1;
2139 break;
2140 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002141
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002142 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2143 video_device_node_name(vdev), v4l2_type_names[fh_type],
2144 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146
Hans Verkuil876cb142012-06-23 08:12:47 -03002147 if (mutex_lock_interruptible(&dev->lock))
2148 return -ERESTARTSYS;
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002149 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002150 if (!fh) {
2151 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Hans Verkuil876cb142012-06-23 08:12:47 -03002152 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002153 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002155 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002156 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002157 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002158 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002159
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002160 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002161 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002162 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002163 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002164
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002165 /* Needed, since GPIO might have disabled power of
2166 some i2c device
2167 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002168 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002169
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002170 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002171 if (fh->radio) {
2172 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002173 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002174 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002175
2176 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002177
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002178 if (dev->progressive)
2179 field = V4L2_FIELD_NONE;
2180 else
2181 field = V4L2_FIELD_INTERLACED;
2182
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002183 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2184 NULL, &dev->slock,
2185 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002186 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002187
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002188 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2189 NULL, &dev->slock,
2190 V4L2_BUF_TYPE_VBI_CAPTURE,
2191 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002192 sizeof(struct em28xx_buffer), fh, &dev->lock);
Hans Verkuil876cb142012-06-23 08:12:47 -03002193 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002194
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195 return errCode;
2196}
2197
2198/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002199 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002200 * unregisters the v4l2,i2c and usb devices
2201 * called when the device gets disconected or at module unload
2202*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002203void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002204{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002206 /*FIXME: I2C IR should be disconnected */
2207
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002208 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002209 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002210 video_unregister_device(dev->radio_dev);
2211 else
2212 video_device_release(dev->radio_dev);
2213 dev->radio_dev = NULL;
2214 }
2215 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002216 em28xx_info("V4L2 device %s deregistered\n",
2217 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002218 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002219 video_unregister_device(dev->vbi_dev);
2220 else
2221 video_device_release(dev->vbi_dev);
2222 dev->vbi_dev = NULL;
2223 }
2224 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002225 em28xx_info("V4L2 device %s deregistered\n",
2226 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002227 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002228 video_unregister_device(dev->vdev);
2229 else
2230 video_device_release(dev->vdev);
2231 dev->vdev = NULL;
2232 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233}
2234
2235/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002236 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002237 * stops streaming and deallocates all resources allocated by the v4l2
2238 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002240static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002242 struct em28xx_fh *fh = filp->private_data;
2243 struct em28xx *dev = fh->dev;
2244 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002245
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002246 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247
Hans Verkuil876cb142012-06-23 08:12:47 -03002248 mutex_lock(&dev->lock);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002249 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002250 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002251 res_free(fh, EM28XX_RESOURCE_VIDEO);
2252 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002253
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002254 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2255 videobuf_stop(&fh->vb_vbiq);
2256 res_free(fh, EM28XX_RESOURCE_VBI);
2257 }
2258
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002259 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002260 /* the device is already disconnect,
2261 free the remaining resources */
2262 if (dev->state & DEV_DISCONNECTED) {
2263 em28xx_release_resources(dev);
Chris Rankin16e3d2f2011-09-04 15:38:14 -03002264 kfree(dev->alt_max_pkt_size);
Dan Carpentere36c92f2012-08-14 02:58:15 -03002265 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002266 kfree(dev);
Ezequiel GarcĂ­adedb8cb2012-05-05 16:13:22 -03002267 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002268 return 0;
2269 }
2270
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002271 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002272 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002273
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002274 /* do this before setting alternate! */
Frank Schaeferafb177e2012-11-08 14:11:40 -03002275 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002276 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002277
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002278 /* set alternate 0 */
2279 dev->alt = 0;
2280 em28xx_videodbg("setting alternate 0\n");
2281 errCode = usb_set_interface(dev->udev, 0, 0);
2282 if (errCode < 0) {
2283 em28xx_errdev("cannot change alternate number to "
2284 "0 (error=%i)\n", errCode);
2285 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002286 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002287
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002288 videobuf_mmap_free(&fh->vb_vidq);
2289 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002290 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002291 dev->users--;
Hans Verkuil876cb142012-06-23 08:12:47 -03002292 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 return 0;
2294}
2295
2296/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002297 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002298 * will allocate buffers when called for the first time
2299 */
2300static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002301em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002302 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002304 struct em28xx_fh *fh = filp->private_data;
2305 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002306 int rc;
2307
2308 rc = check_dev(dev);
2309 if (rc < 0)
2310 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311
Hans Verkuil876cb142012-06-23 08:12:47 -03002312 if (mutex_lock_interruptible(&dev->lock))
2313 return -ERESTARTSYS;
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002314 /* FIXME: read() is not prepared to allow changing the video
2315 resolution while streaming. Seems a bug at em28xx_set_fmt
2316 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002317
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002318 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002319 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
Hans Verkuil876cb142012-06-23 08:12:47 -03002320 rc = -EBUSY;
2321 else
2322 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002323 filp->f_flags & O_NONBLOCK);
Hans Verkuil876cb142012-06-23 08:12:47 -03002324 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002325 if (!res_get(fh, EM28XX_RESOURCE_VBI))
Hans Verkuil876cb142012-06-23 08:12:47 -03002326 rc = -EBUSY;
2327 else
2328 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002329 filp->f_flags & O_NONBLOCK);
2330 }
Hans Verkuil876cb142012-06-23 08:12:47 -03002331 mutex_unlock(&dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002332
Hans Verkuil876cb142012-06-23 08:12:47 -03002333 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002334}
2335
2336/*
Hans Verkuil876cb142012-06-23 08:12:47 -03002337 * em28xx_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002338 * will allocate buffers when called for the first time
2339 */
Hans Verkuil876cb142012-06-23 08:12:47 -03002340static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002341{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002342 struct em28xx_fh *fh = filp->private_data;
2343 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002344 int rc;
2345
2346 rc = check_dev(dev);
2347 if (rc < 0)
2348 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002349
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002350 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2351 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2352 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002353 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002354 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2355 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2356 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002357 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002358 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002359 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002360 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002361}
2362
Hans Verkuil876cb142012-06-23 08:12:47 -03002363static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2364{
2365 struct em28xx_fh *fh = filp->private_data;
2366 struct em28xx *dev = fh->dev;
2367 unsigned int res;
2368
2369 mutex_lock(&dev->lock);
2370 res = em28xx_poll(filp, wait);
2371 mutex_unlock(&dev->lock);
2372 return res;
2373}
2374
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002375/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002376 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002377 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002378static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002379{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002380 struct em28xx_fh *fh = filp->private_data;
2381 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002382 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002383
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002384 rc = check_dev(dev);
2385 if (rc < 0)
2386 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002387
Hans Verkuil876cb142012-06-23 08:12:47 -03002388 if (mutex_lock_interruptible(&dev->lock))
2389 return -ERESTARTSYS;
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002390 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2391 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2392 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2393 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
Hans Verkuil876cb142012-06-23 08:12:47 -03002394 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002395
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002396 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2397 (unsigned long)vma->vm_start,
2398 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2399 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002400
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002401 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402}
2403
Hans Verkuilbec43662008-12-30 06:58:20 -03002404static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002405 .owner = THIS_MODULE,
2406 .open = em28xx_v4l2_open,
2407 .release = em28xx_v4l2_close,
2408 .read = em28xx_v4l2_read,
2409 .poll = em28xx_v4l2_poll,
2410 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002411 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002412};
2413
Hans Verkuila3998102008-07-21 02:57:38 -03002414static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002415 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002416 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2417 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2418 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2419 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002420 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2421 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c50682011-10-16 13:52:43 -02002422 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002423 .vidioc_g_audio = vidioc_g_audio,
2424 .vidioc_s_audio = vidioc_s_audio,
2425 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002426 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2427 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2428 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002429
2430 .vidioc_reqbufs = vidioc_reqbufs,
2431 .vidioc_querybuf = vidioc_querybuf,
2432 .vidioc_qbuf = vidioc_qbuf,
2433 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002434 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002435 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002436 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002437 .vidioc_g_parm = vidioc_g_parm,
2438 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002439 .vidioc_enum_input = vidioc_enum_input,
2440 .vidioc_g_input = vidioc_g_input,
2441 .vidioc_s_input = vidioc_s_input,
2442 .vidioc_queryctrl = vidioc_queryctrl,
2443 .vidioc_g_ctrl = vidioc_g_ctrl,
2444 .vidioc_s_ctrl = vidioc_s_ctrl,
2445 .vidioc_streamon = vidioc_streamon,
2446 .vidioc_streamoff = vidioc_streamoff,
2447 .vidioc_g_tuner = vidioc_g_tuner,
2448 .vidioc_s_tuner = vidioc_s_tuner,
2449 .vidioc_g_frequency = vidioc_g_frequency,
2450 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002451#ifdef CONFIG_VIDEO_ADV_DEBUG
2452 .vidioc_g_register = vidioc_g_register,
2453 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002454 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002455#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002456};
2457
2458static const struct video_device em28xx_video_template = {
2459 .fops = &em28xx_v4l_fops,
2460 .release = video_device_release,
2461 .ioctl_ops = &video_ioctl_ops,
2462
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002463 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002464 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002465};
2466
Hans Verkuilbec43662008-12-30 06:58:20 -03002467static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002468 .owner = THIS_MODULE,
2469 .open = em28xx_v4l2_open,
2470 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002471 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002472};
2473
2474static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002475 .vidioc_querycap = radio_querycap,
2476 .vidioc_g_tuner = radio_g_tuner,
2477 .vidioc_enum_input = radio_enum_input,
2478 .vidioc_g_audio = radio_g_audio,
2479 .vidioc_s_tuner = radio_s_tuner,
2480 .vidioc_s_audio = radio_s_audio,
2481 .vidioc_s_input = radio_s_input,
2482 .vidioc_queryctrl = radio_queryctrl,
2483 .vidioc_g_ctrl = vidioc_g_ctrl,
2484 .vidioc_s_ctrl = vidioc_s_ctrl,
2485 .vidioc_g_frequency = vidioc_g_frequency,
2486 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002487#ifdef CONFIG_VIDEO_ADV_DEBUG
2488 .vidioc_g_register = vidioc_g_register,
2489 .vidioc_s_register = vidioc_s_register,
2490#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002491};
2492
Hans Verkuila3998102008-07-21 02:57:38 -03002493static struct video_device em28xx_radio_template = {
2494 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002495 .fops = &radio_fops,
2496 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002497};
2498
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002499/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002500
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002501
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002502
Adrian Bunk532fe652008-01-28 22:10:48 -03002503static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002504 const struct video_device *template,
2505 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002506{
2507 struct video_device *vfd;
2508
2509 vfd = video_device_alloc();
2510 if (NULL == vfd)
2511 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002512
2513 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002514 vfd->v4l2_dev = &dev->v4l2_dev;
2515 vfd->release = video_device_release;
2516 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002517 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002518
2519 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2520 dev->name, type_name);
2521
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002522 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002523 return vfd;
2524}
2525
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002526int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002527{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002528 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002529 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002530 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002531
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002532 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2533 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002534
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002535 /* set default norm */
2536 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002537 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002538 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002539
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002540 /* Analog specific initialization */
2541 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002542
2543 maxw = norm_maxw(dev);
2544 /* MaxPacketSize for em2800 is too small to capture at full resolution
2545 * use half of maxw as the scaler can only scale to 50% */
2546 if (dev->board.is_em2800)
2547 maxw /= 2;
2548
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002549 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002550 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002551
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03002552 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002553
2554 /* Audio defaults */
2555 dev->mute = 1;
2556 dev->volume = 0x1f;
2557
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002558/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002559 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2560 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2561 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002562
2563 em28xx_set_outfmt(dev);
2564 em28xx_colorlevels_set_default(dev);
2565 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002566
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002567 /* allocate and fill video video_device struct */
2568 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2569 if (!dev->vdev) {
2570 em28xx_errdev("cannot allocate video_device.\n");
2571 return -ENODEV;
2572 }
2573
2574 /* register v4l2 video video_device */
2575 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2576 video_nr[dev->devno]);
2577 if (ret) {
2578 em28xx_errdev("unable to register video device (error=%i).\n",
2579 ret);
2580 return ret;
2581 }
2582
2583 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002584 if (em28xx_vbi_supported(dev) == 1) {
2585 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2586 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002587
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002588 /* register v4l2 vbi video_device */
2589 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2590 vbi_nr[dev->devno]);
2591 if (ret < 0) {
2592 em28xx_errdev("unable to register vbi device\n");
2593 return ret;
2594 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002595 }
2596
2597 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002598 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2599 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002600 if (!dev->radio_dev) {
2601 em28xx_errdev("cannot allocate video_device.\n");
2602 return -ENODEV;
2603 }
2604 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2605 radio_nr[dev->devno]);
2606 if (ret < 0) {
2607 em28xx_errdev("can't register radio device\n");
2608 return ret;
2609 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002610 em28xx_info("Registered radio device as %s\n",
2611 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002612 }
2613
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002614 em28xx_info("V4L2 video device registered as %s\n",
2615 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002616
2617 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002618 em28xx_info("V4L2 VBI device registered as %s\n",
2619 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002620
2621 return 0;
2622}