blob: 06cf1979250447a89c8a6b0100cd112671b98056 [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>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030053#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
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");
75
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020076static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030077static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020080module_param_array(video_nr, int, NULL, 0444);
81module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083MODULE_PARM_DESC(video_nr, "video device numbers");
84MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
85MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080086
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030087static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030088module_param(video_debug, int, 0644);
89MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080090
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030091/* supported video standards */
92static struct em28xx_fmt format[] = {
93 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030094 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030095 .fourcc = V4L2_PIX_FMT_YUYV,
96 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030097 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030098 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030099 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300102 .reg = EM28XX_OUTFMT_RGB_16_656,
103 }, {
104 .name = "8 bpp Bayer BGBG..GRGR",
105 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .depth = 8,
107 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 }, {
109 .name = "8 bpp Bayer GRGR..BGBG",
110 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .depth = 8,
112 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 }, {
114 .name = "8 bpp Bayer GBGB..RGRG",
115 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .depth = 8,
117 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 }, {
119 .name = "12 bpp YUV411",
120 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .depth = 12,
122 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300123 },
124};
125
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800126/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200127/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300128static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800129 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200130 .id = V4L2_CID_AUDIO_VOLUME,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Volume",
133 .minimum = 0x0,
134 .maximum = 0x1f,
135 .step = 0x1,
136 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300137 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300138 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200139 .id = V4L2_CID_AUDIO_MUTE,
140 .type = V4L2_CTRL_TYPE_BOOLEAN,
141 .name = "Mute",
142 .minimum = 0,
143 .maximum = 1,
144 .step = 1,
145 .default_value = 1,
146 .flags = 0,
147 }
148};
149
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300150/* ------------------------------------------------------------------
151 DMA and thread functions
152 ------------------------------------------------------------------*/
153
154/*
155 * Announces that a buffer were filled and request the next
156 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158 struct em28xx_dmaqueue *dma_q,
159 struct em28xx_buffer *buf)
160{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 /* Advice that buffer was filled */
162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
163 buf->vb.state = VIDEOBUF_DONE;
164 buf->vb.field_count++;
165 do_gettimeofday(&buf->vb.ts);
166
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300167 dev->isoc_ctl.vid_buf = NULL;
168
169 list_del(&buf->vb.queue);
170 wake_up(&buf->vb.done);
171}
172
173static inline void vbi_buffer_filled(struct em28xx *dev,
174 struct em28xx_dmaqueue *dma_q,
175 struct em28xx_buffer *buf)
176{
177 /* Advice that buffer was filled */
178 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179
180 buf->vb.state = VIDEOBUF_DONE;
181 buf->vb.field_count++;
182 do_gettimeofday(&buf->vb.ts);
183
184 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300185
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 list_del(&buf->vb.queue);
187 wake_up(&buf->vb.done);
188}
189
190/*
191 * Identify the buffer header type and properly handles
192 */
193static void em28xx_copy_video(struct em28xx *dev,
194 struct em28xx_dmaqueue *dma_q,
195 struct em28xx_buffer *buf,
196 unsigned char *p,
197 unsigned char *outp, unsigned long len)
198{
199 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300200 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202
203 if (dma_q->pos + len > buf->vb.size)
204 len = buf->vb.size - dma_q->pos;
205
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206 startread = p;
207 remain = len;
208
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300209 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300211 else {
212 /* Interlaces two half frames */
213 if (buf->top_field)
214 fieldstart = outp;
215 else
216 fieldstart = outp + bytesperline;
217 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300218
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300219 linesdone = dma_q->pos / bytesperline;
220 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300221
222 if (dev->progressive)
223 offset = linesdone * bytesperline + currlinedone;
224 else
225 offset = linesdone * bytesperline * 2 + currlinedone;
226
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300227 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300228 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229 lencopy = lencopy > remain ? remain : lencopy;
230
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300231 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300232 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300233 ((char *)startwrite + lencopy) -
234 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300235 remain = (char *)outp + buf->vb.size - (char *)startwrite;
236 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300238 if (lencopy <= 0)
239 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300240 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300241
242 remain -= lencopy;
243
244 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300280 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300286 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292 if (buf == NULL) {
293 return;
294 }
295 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300296 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300297 return;
298 }
299 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303
304 if (dma_q->pos + len > buf->vb.size)
305 len = buf->vb.size - dma_q->pos;
306
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300307 startread = p;
308
309 startwrite = outp + dma_q->pos;
310 offset = dma_q->pos;
311
312 /* Make sure the bottom field populates the second half of the frame */
313 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300314 startwrite += bytesperline * dev->vbi_height;
315 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300316 }
317
318 memcpy(startwrite, startread, len);
319 dma_q->pos += len;
320}
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 int packet, int status)
324{
325 char *errmsg = "Unknown";
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 case -ENOENT:
329 errmsg = "unlinked synchronuously";
330 break;
331 case -ECONNRESET:
332 errmsg = "unlinked asynchronuously";
333 break;
334 case -ENOSR:
335 errmsg = "Buffer error (overrun)";
336 break;
337 case -EPIPE:
338 errmsg = "Stalled (device not responding)";
339 break;
340 case -EOVERFLOW:
341 errmsg = "Babble (bad cable?)";
342 break;
343 case -EPROTO:
344 errmsg = "Bit-stuff error (bad cable?)";
345 break;
346 case -EILSEQ:
347 errmsg = "CRC/Timeout (could be anything)";
348 break;
349 case -ETIME:
350 errmsg = "Device does not respond";
351 break;
352 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300354 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
355 } else {
356 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
357 packet, status, errmsg);
358 }
359}
360
361/*
362 * video-buf generic routine to get the next available buffer
363 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300364static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 struct em28xx_buffer **buf)
366{
367 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300368 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300370 if (list_empty(&dma_q->active)) {
371 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300372 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300373 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300374 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300375 }
376
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300377 /* Get the next buffer */
378 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
379
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 /* Cleans up buffer - Usefull for testing for frame/URB loss */
381 outp = videobuf_to_vmalloc(&(*buf)->vb);
382 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300383
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384 dev->isoc_ctl.vid_buf = *buf;
385
386 return;
387}
388
389/*
390 * video-buf generic routine to get the next available VBI buffer
391 */
392static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
393 struct em28xx_buffer **buf)
394{
395 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
396 char *outp;
397
398 if (list_empty(&dma_q->active)) {
399 em28xx_isocdbg("No active queue to serve\n");
400 dev->isoc_ctl.vbi_buf = NULL;
401 *buf = NULL;
402 return;
403 }
404
405 /* Get the next buffer */
406 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
407 /* Cleans up buffer - Usefull for testing for frame/URB loss */
408 outp = videobuf_to_vmalloc(&(*buf)->vb);
409 memset(outp, 0x00, (*buf)->vb.size);
410
411 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414}
415
416/*
417 * Controls the isoc copy of each urb packet
418 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300419static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300420{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300421 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300422 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300423 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300425 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426
427 if (!dev)
428 return 0;
429
430 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
431 return 0;
432
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300433 if (urb->status < 0) {
434 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300435 if (urb->status == -ENOENT)
436 return 0;
437 }
438
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300439 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300440 if (buf != NULL)
441 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300442
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443 for (i = 0; i < urb->number_of_packets; i++) {
444 int status = urb->iso_frame_desc[i].status;
445
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300446 if (status < 0) {
447 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300448 if (urb->iso_frame_desc[i].status != -EPROTO)
449 continue;
450 }
451
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300452 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300453
454 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300455 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 continue;
457 }
458 if (urb->iso_frame_desc[i].actual_length >
459 dev->max_pkt_size) {
460 em28xx_isocdbg("packet bigger than packet size");
461 continue;
462 }
463
464 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465
466 /* FIXME: incomplete buffer checks where removed to make
467 logic simpler. Impacts of those changes should be evaluated
468 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300469 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
470 em28xx_isocdbg("VBI HEADER!!!\n");
471 /* FIXME: Should add vbi copy */
472 continue;
473 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300474 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300475 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300476 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300477
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300478 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300479 if (buf != NULL)
480 buffer_filled(dev, dma_q, buf);
481 get_next_buf(dma_q, &buf);
482 if (buf == NULL)
483 outp = NULL;
484 else
485 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300486 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300487
488 if (buf != NULL) {
489 if (p[2] & 1)
490 buf->top_field = 0;
491 else
492 buf->top_field = 1;
493 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300494
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300497 if (buf != NULL) {
498 if (p[0] != 0x88 && p[0] != 0x22) {
499 em28xx_isocdbg("frame is not complete\n");
500 len += 4;
501 } else {
502 p += 4;
503 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300504 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300505 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300506 }
507 return rc;
508}
509
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300510/* Version of isoc handler that takes into account a mixture of video and
511 VBI data */
512static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
513{
514 struct em28xx_buffer *buf, *vbi_buf;
515 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300516 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300517 unsigned char *outp = NULL;
518 unsigned char *vbioutp = NULL;
519 int i, len = 0, rc = 1;
520 unsigned char *p;
521 int vbi_size;
522
523 if (!dev)
524 return 0;
525
526 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
527 return 0;
528
529 if (urb->status < 0) {
530 print_err_status(dev, -1, urb->status);
531 if (urb->status == -ENOENT)
532 return 0;
533 }
534
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300535 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300536 if (buf != NULL)
537 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300538
539 vbi_buf = dev->isoc_ctl.vbi_buf;
540 if (vbi_buf != NULL)
541 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
542
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300543 for (i = 0; i < urb->number_of_packets; i++) {
544 int status = urb->iso_frame_desc[i].status;
545
546 if (status < 0) {
547 print_err_status(dev, i, status);
548 if (urb->iso_frame_desc[i].status != -EPROTO)
549 continue;
550 }
551
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300552 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300553 if (urb->iso_frame_desc[i].actual_length <= 0) {
554 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
555 continue;
556 }
557 if (urb->iso_frame_desc[i].actual_length >
558 dev->max_pkt_size) {
559 em28xx_isocdbg("packet bigger than packet size");
560 continue;
561 }
562
563 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
564
565 /* capture type 0 = vbi start
566 capture type 1 = video start
567 capture type 2 = video in progress */
568 if (p[0] == 0x33 && p[1] == 0x95) {
569 dev->capture_type = 0;
570 dev->vbi_read = 0;
571 em28xx_isocdbg("VBI START HEADER!!!\n");
572 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300573 p += 4;
574 len -= 4;
575 } else if (p[0] == 0x88 && p[1] == 0x88 &&
576 p[2] == 0x88 && p[3] == 0x88) {
577 /* continuation */
578 p += 4;
579 len -= 4;
580 } else if (p[0] == 0x22 && p[1] == 0x5a) {
581 /* start video */
582 p += 4;
583 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300584 }
585
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300586 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300587
588 if (dev->capture_type == 0) {
589 if (dev->vbi_read >= vbi_size) {
590 /* We've already read all the VBI data, so
591 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300592 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300593 } else if ((dev->vbi_read + len) < vbi_size) {
594 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300595 if (dev->vbi_read == 0 &&
596 (!(dev->cur_field & 1))) {
597 /* Brand new frame */
598 if (vbi_buf != NULL)
599 vbi_buffer_filled(dev,
600 vbi_dma_q,
601 vbi_buf);
602 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
603 if (vbi_buf == NULL)
604 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300605 else
606 vbioutp = videobuf_to_vmalloc(
607 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300608 }
609
610 if (dev->vbi_read == 0) {
611 vbi_dma_q->pos = 0;
612 if (vbi_buf != NULL) {
613 if (dev->cur_field & 1)
614 vbi_buf->top_field = 0;
615 else
616 vbi_buf->top_field = 1;
617 }
618 }
619
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300620 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300621 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
622 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300623 } else {
624 /* Some of this frame is VBI data and some is
625 video data */
626 int vbi_data_len = vbi_size - dev->vbi_read;
627 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300628 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
629 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300630 dev->capture_type = 1;
631 p += vbi_data_len;
632 len -= vbi_data_len;
633 }
634 }
635
636 if (dev->capture_type == 1) {
637 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300638 if (dev->progressive || !(dev->cur_field & 1)) {
639 if (buf != NULL)
640 buffer_filled(dev, dma_q, buf);
641 get_next_buf(dma_q, &buf);
642 if (buf == NULL)
643 outp = NULL;
644 else
645 outp = videobuf_to_vmalloc(&buf->vb);
646 }
647 if (buf != NULL) {
648 if (dev->cur_field & 1)
649 buf->top_field = 0;
650 else
651 buf->top_field = 1;
652 }
653
654 dma_q->pos = 0;
655 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300656
657 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300658 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300659 p[2] == 0x88 && p[3] == 0x88) {
660 p += 4;
661 len -= 4;
662 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300663 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300664 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
665 p[2], len, (p[2] & 1) ?
666 "odd" : "even");
667 p += 4;
668 len -= 4;
669 }
670
671 if (len > 0)
672 em28xx_copy_video(dev, dma_q, buf, p, outp,
673 len);
674 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300675 }
676 return rc;
677}
678
679
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681 Videobuf operations
682 ------------------------------------------------------------------*/
683
684static int
685buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
686{
687 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300688 struct em28xx *dev = fh->dev;
689 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300690
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300691 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
692 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300693
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300694 if (0 == *count)
695 *count = EM28XX_DEF_BUF;
696
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300697 if (*count < EM28XX_MIN_BUF)
698 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300699
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300700 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300701 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300702 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300703 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300704
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300705 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300706
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300707 return 0;
708}
709
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300710/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300711static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
712{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300713 struct em28xx_fh *fh = vq->priv_data;
714 struct em28xx *dev = fh->dev;
715 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300716 if (in_interrupt())
717 BUG();
718
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300719 /* We used to wait for the buffer to finish here, but this didn't work
720 because, as we were keeping the state as VIDEOBUF_QUEUED,
721 videobuf_queue_cancel marked it as finished for us.
722 (Also, it could wedge forever if the hardware was misconfigured.)
723
724 This should be safe; by the time we get here, the buffer isn't
725 queued anymore. If we ever start marking the buffers as
726 VIDEOBUF_ACTIVE, it won't be, though.
727 */
728 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300729 if (dev->isoc_ctl.vid_buf == buf)
730 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300731 spin_unlock_irqrestore(&dev->slock, flags);
732
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300733 videobuf_vmalloc_free(&buf->vb);
734 buf->vb.state = VIDEOBUF_NEEDS_INIT;
735}
736
737static int
738buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
739 enum v4l2_field field)
740{
741 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300742 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300743 struct em28xx *dev = fh->dev;
744 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300745
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300746 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
747 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300748
749 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
750 return -EINVAL;
751
Brandon Philips05612972008-04-13 14:57:01 -0300752 buf->vb.width = dev->width;
753 buf->vb.height = dev->height;
754 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300755
756 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300757 rc = videobuf_iolock(vq, &buf->vb, NULL);
758 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300760 }
761
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300762 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300763 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300764
765 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300766 if (em28xx_vbi_supported(dev) == 1)
767 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
768 EM28XX_NUM_BUFS,
769 dev->max_pkt_size,
770 em28xx_isoc_copy_vbi);
771 else
772 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
773 EM28XX_NUM_BUFS,
774 dev->max_pkt_size,
775 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300776 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300777 goto fail;
778 }
779
780 buf->vb.state = VIDEOBUF_PREPARED;
781 return 0;
782
783fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300784 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300785 return rc;
786}
787
788static void
789buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
790{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300791 struct em28xx_buffer *buf = container_of(vb,
792 struct em28xx_buffer,
793 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300794 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300795 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300796 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300798 buf->vb.state = VIDEOBUF_QUEUED;
799 list_add_tail(&buf->vb.queue, &vidq->active);
800
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300801}
802
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300803static void buffer_release(struct videobuf_queue *vq,
804 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300805{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300806 struct em28xx_buffer *buf = container_of(vb,
807 struct em28xx_buffer,
808 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300809 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300810 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300811
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300812 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300813
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300814 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300815}
816
817static struct videobuf_queue_ops em28xx_video_qops = {
818 .buf_setup = buffer_setup,
819 .buf_prepare = buffer_prepare,
820 .buf_queue = buffer_queue,
821 .buf_release = buffer_release,
822};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800823
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300824/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800825
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800826static void video_mux(struct em28xx *dev, int index)
827{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800828 dev->ctl_input = index;
829 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300830 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800831
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300832 if (!dev->ctl_aoutput)
833 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
834
Hans Verkuil5325b422009-04-02 11:26:22 -0300835 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
836 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800837
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300838 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300839 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300840 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
841 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300842 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300843 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300844 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
845 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800846 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300847
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300848 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300849 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
850 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300851 }
852
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300853 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800854}
855
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300856/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300857static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300858{
859 struct em28xx *dev = fh->dev;
860
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300861 if (fh->resources & bit)
862 /* have it already allocated */
863 return 1;
864
865 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300866 if (dev->resources & bit) {
867 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300868 return 0;
869 }
870 /* it's free, grab it */
871 fh->resources |= bit;
872 dev->resources |= bit;
873 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300874 return 1;
875}
876
877static int res_check(struct em28xx_fh *fh, unsigned int bit)
878{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300879 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300880}
881
882static int res_locked(struct em28xx *dev, unsigned int bit)
883{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300884 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300885}
886
887static void res_free(struct em28xx_fh *fh, unsigned int bits)
888{
889 struct em28xx *dev = fh->dev;
890
891 BUG_ON((fh->resources & bits) != bits);
892
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300893 fh->resources &= ~bits;
894 dev->resources &= ~bits;
895 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300896}
897
898static int get_ressource(struct em28xx_fh *fh)
899{
900 switch (fh->type) {
901 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
902 return EM28XX_RESOURCE_VIDEO;
903 case V4L2_BUF_TYPE_VBI_CAPTURE:
904 return EM28XX_RESOURCE_VBI;
905 default:
906 BUG();
907 return 0;
908 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300909}
910
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800911/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300912 * ac97_queryctrl()
913 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300914 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300915static int ac97_queryctrl(struct v4l2_queryctrl *qc)
916{
917 int i;
918
919 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
920 if (qc->id && qc->id == ac97_qctrl[i].id) {
921 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
922 return 0;
923 }
924 }
925
926 /* Control is not ac97 related */
927 return 1;
928}
929
930/*
931 * ac97_get_ctrl()
932 * return the current values for ac97 mute and volume
933 */
934static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935{
936 switch (ctrl->id) {
937 case V4L2_CID_AUDIO_MUTE:
938 ctrl->value = dev->mute;
939 return 0;
940 case V4L2_CID_AUDIO_VOLUME:
941 ctrl->value = dev->volume;
942 return 0;
943 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300944 /* Control is not ac97 related */
945 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300946 }
947}
948
949/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300950 * ac97_set_ctrl()
951 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300952 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300953static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300954{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300955 int i;
956
957 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
958 if (ctrl->id == ac97_qctrl[i].id)
959 goto handle;
960
961 /* Announce that hasn't handle it */
962 return 1;
963
964handle:
965 if (ctrl->value < ac97_qctrl[i].minimum ||
966 ctrl->value > ac97_qctrl[i].maximum)
967 return -ERANGE;
968
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300969 switch (ctrl->id) {
970 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300971 dev->mute = ctrl->value;
972 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300973 case V4L2_CID_AUDIO_VOLUME:
974 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300975 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300976 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300977
978 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979}
980
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300981static int check_dev(struct em28xx *dev)
982{
983 if (dev->state & DEV_DISCONNECTED) {
984 em28xx_errdev("v4l2 ioctl: device not present\n");
985 return -ENODEV;
986 }
987
988 if (dev->state & DEV_MISCONFIGURED) {
989 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
990 "close and open it again\n");
991 return -EIO;
992 }
993 return 0;
994}
995
996static void get_scale(struct em28xx *dev,
997 unsigned int width, unsigned int height,
998 unsigned int *hscale, unsigned int *vscale)
999{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001000 unsigned int maxw = norm_maxw(dev);
1001 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001002
1003 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1004 if (*hscale >= 0x4000)
1005 *hscale = 0x3fff;
1006
1007 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1008 if (*vscale >= 0x4000)
1009 *vscale = 0x3fff;
1010}
1011
1012/* ------------------------------------------------------------------
1013 IOCTL vidioc handling
1014 ------------------------------------------------------------------*/
1015
Hans Verkuil78b526a2008-05-28 12:16:41 -03001016static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001017 struct v4l2_format *f)
1018{
1019 struct em28xx_fh *fh = priv;
1020 struct em28xx *dev = fh->dev;
1021
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001022 f->fmt.pix.width = dev->width;
1023 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001024 f->fmt.pix.pixelformat = dev->format->fourcc;
1025 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001026 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001027 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1028
1029 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001030 if (dev->progressive)
1031 f->fmt.pix.field = V4L2_FIELD_NONE;
1032 else
1033 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001034 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035 return 0;
1036}
1037
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001038static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1039{
1040 unsigned int i;
1041
1042 for (i = 0; i < ARRAY_SIZE(format); i++)
1043 if (format[i].fourcc == fourcc)
1044 return &format[i];
1045
1046 return NULL;
1047}
1048
Hans Verkuil78b526a2008-05-28 12:16:41 -03001049static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001050 struct v4l2_format *f)
1051{
1052 struct em28xx_fh *fh = priv;
1053 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001054 unsigned int width = f->fmt.pix.width;
1055 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001056 unsigned int maxw = norm_maxw(dev);
1057 unsigned int maxh = norm_maxh(dev);
1058 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001059 struct em28xx_fmt *fmt;
1060
1061 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1062 if (!fmt) {
1063 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1064 f->fmt.pix.pixelformat);
1065 return -EINVAL;
1066 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001067
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001068 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001069 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001070 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1071 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001072 } else {
1073 /* width must even because of the YUYV format
1074 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001075 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1076 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077 }
1078
1079 get_scale(dev, width, height, &hscale, &vscale);
1080
1081 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1082 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1083
1084 f->fmt.pix.width = width;
1085 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001086 f->fmt.pix.pixelformat = fmt->fourcc;
1087 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1088 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001089 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001090 if (dev->progressive)
1091 f->fmt.pix.field = V4L2_FIELD_NONE;
1092 else
1093 f->fmt.pix.field = dev->interlaced ?
1094 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 return 0;
1097}
1098
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001099static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1100 unsigned width, unsigned height)
1101{
1102 struct em28xx_fmt *fmt;
1103
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001104 fmt = format_by_fourcc(fourcc);
1105 if (!fmt)
1106 return -EINVAL;
1107
1108 dev->format = fmt;
1109 dev->width = width;
1110 dev->height = height;
1111
1112 /* set new image size */
1113 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1114
1115 em28xx_set_alternate(dev);
1116 em28xx_resolution_set(dev);
1117
1118 return 0;
1119}
1120
Hans Verkuil78b526a2008-05-28 12:16:41 -03001121static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001122 struct v4l2_format *f)
1123{
1124 struct em28xx_fh *fh = priv;
1125 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001126 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001127
1128 rc = check_dev(dev);
1129 if (rc < 0)
1130 return rc;
1131
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001132 vidioc_try_fmt_vid_cap(file, priv, f);
1133
Brandon Philips05612972008-04-13 14:57:01 -03001134 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1135 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001136 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001137 }
1138
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001139 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001140 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141}
1142
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001143static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1144{
1145 struct em28xx_fh *fh = priv;
1146 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001147 int rc;
1148
1149 rc = check_dev(dev);
1150 if (rc < 0)
1151 return rc;
1152
1153 *norm = dev->norm;
1154
1155 return 0;
1156}
1157
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001158static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001159{
1160 struct em28xx_fh *fh = priv;
1161 struct em28xx *dev = fh->dev;
1162 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001163 int rc;
1164
1165 rc = check_dev(dev);
1166 if (rc < 0)
1167 return rc;
1168
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001169 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001170
1171 /* Adjusts width/height, if needed */
1172 f.fmt.pix.width = dev->width;
1173 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001174 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001175
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001176 /* set new image size */
1177 dev->width = f.fmt.pix.width;
1178 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001179 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1180
1181 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001182 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001183
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184 return 0;
1185}
1186
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001187static int vidioc_g_parm(struct file *file, void *priv,
1188 struct v4l2_streamparm *p)
1189{
1190 struct em28xx_fh *fh = priv;
1191 struct em28xx *dev = fh->dev;
1192 int rc = 0;
1193
1194 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1195 return -EINVAL;
1196
1197 if (dev->board.is_webcam)
1198 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1199 video, g_parm, p);
1200 else
1201 v4l2_video_std_frame_period(dev->norm,
1202 &p->parm.capture.timeperframe);
1203
1204 return rc;
1205}
1206
1207static int vidioc_s_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
1213 if (!dev->board.is_webcam)
1214 return -EINVAL;
1215
1216 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL;
1218
1219 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1220}
1221
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001222static const char *iname[] = {
1223 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1224 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1225 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1226 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1227 [EM28XX_VMUX_SVIDEO] = "S-Video",
1228 [EM28XX_VMUX_TELEVISION] = "Television",
1229 [EM28XX_VMUX_CABLE] = "Cable TV",
1230 [EM28XX_VMUX_DVB] = "DVB",
1231 [EM28XX_VMUX_DEBUG] = "for debug only",
1232};
1233
1234static int vidioc_enum_input(struct file *file, void *priv,
1235 struct v4l2_input *i)
1236{
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1239 unsigned int n;
1240
1241 n = i->index;
1242 if (n >= MAX_EM28XX_INPUT)
1243 return -EINVAL;
1244 if (0 == INPUT(n)->type)
1245 return -EINVAL;
1246
1247 i->index = n;
1248 i->type = V4L2_INPUT_TYPE_CAMERA;
1249
1250 strcpy(i->name, iname[INPUT(n)->type]);
1251
1252 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1253 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1254 i->type = V4L2_INPUT_TYPE_TUNER;
1255
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001256 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001257
1258 return 0;
1259}
1260
1261static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1262{
1263 struct em28xx_fh *fh = priv;
1264 struct em28xx *dev = fh->dev;
1265
1266 *i = dev->ctl_input;
1267
1268 return 0;
1269}
1270
1271static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1272{
1273 struct em28xx_fh *fh = priv;
1274 struct em28xx *dev = fh->dev;
1275 int rc;
1276
1277 rc = check_dev(dev);
1278 if (rc < 0)
1279 return rc;
1280
1281 if (i >= MAX_EM28XX_INPUT)
1282 return -EINVAL;
1283 if (0 == INPUT(i)->type)
1284 return -EINVAL;
1285
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001286 dev->ctl_input = i;
1287
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001288 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001289 return 0;
1290}
1291
1292static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1293{
1294 struct em28xx_fh *fh = priv;
1295 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001297 if (!dev->audio_mode.has_audio)
1298 return -EINVAL;
1299
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001300 switch (a->index) {
1301 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001302 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001303 break;
1304 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001305 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001306 break;
1307 case EM28XX_AMUX_VIDEO2:
1308 strcpy(a->name, "Television alt");
1309 break;
1310 case EM28XX_AMUX_PHONE:
1311 strcpy(a->name, "Phone");
1312 break;
1313 case EM28XX_AMUX_MIC:
1314 strcpy(a->name, "Mic");
1315 break;
1316 case EM28XX_AMUX_CD:
1317 strcpy(a->name, "CD");
1318 break;
1319 case EM28XX_AMUX_AUX:
1320 strcpy(a->name, "Aux");
1321 break;
1322 case EM28XX_AMUX_PCM_OUT:
1323 strcpy(a->name, "PCM");
1324 break;
1325 default:
1326 return -EINVAL;
1327 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001328
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001329 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331
1332 return 0;
1333}
1334
1335static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1336{
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
1339
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001340
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001341 if (!dev->audio_mode.has_audio)
1342 return -EINVAL;
1343
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001344 if (a->index >= MAX_EM28XX_INPUT)
1345 return -EINVAL;
1346 if (0 == INPUT(a->index)->type)
1347 return -EINVAL;
1348
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001349 dev->ctl_ainput = INPUT(a->index)->amux;
1350 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001351
1352 if (!dev->ctl_aoutput)
1353 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001354
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355 return 0;
1356}
1357
1358static int vidioc_queryctrl(struct file *file, void *priv,
1359 struct v4l2_queryctrl *qc)
1360{
1361 struct em28xx_fh *fh = priv;
1362 struct em28xx *dev = fh->dev;
1363 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001364 int rc;
1365
1366 rc = check_dev(dev);
1367 if (rc < 0)
1368 return rc;
1369
1370 memset(qc, 0, sizeof(*qc));
1371
1372 qc->id = id;
1373
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001374 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001375 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1376 rc = ac97_queryctrl(qc);
1377 if (!rc)
1378 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001379 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001380
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001381 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001382 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383
1384 if (qc->type)
1385 return 0;
1386 else
1387 return -EINVAL;
1388}
1389
1390static int vidioc_g_ctrl(struct file *file, void *priv,
1391 struct v4l2_control *ctrl)
1392{
1393 struct em28xx_fh *fh = priv;
1394 struct em28xx *dev = fh->dev;
1395 int rc;
1396
1397 rc = check_dev(dev);
1398 if (rc < 0)
1399 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001400 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001401
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001402
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001403 /* Set an AC97 control */
1404 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1405 rc = ac97_get_ctrl(dev, ctrl);
1406 else
1407 rc = 1;
1408
1409 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1410 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001411 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001412 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001413 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001414
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415 return rc;
1416}
1417
1418static int vidioc_s_ctrl(struct file *file, void *priv,
1419 struct v4l2_control *ctrl)
1420{
1421 struct em28xx_fh *fh = priv;
1422 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001423 int rc;
1424
1425 rc = check_dev(dev);
1426 if (rc < 0)
1427 return rc;
1428
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001429 /* Set an AC97 control */
1430 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1431 rc = ac97_set_ctrl(dev, ctrl);
1432 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001433 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001434
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001435 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001437 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001438
1439 /*
1440 * In the case of non-AC97 volume controls, we still need
1441 * to do some setups at em28xx, in order to mute/unmute
1442 * and to adjust audio volume. However, the value ranges
1443 * should be checked by the corresponding V4L subdriver.
1444 */
1445 switch (ctrl->id) {
1446 case V4L2_CID_AUDIO_MUTE:
1447 dev->mute = ctrl->value;
1448 rc = em28xx_audio_analog_set(dev);
1449 break;
1450 case V4L2_CID_AUDIO_VOLUME:
1451 dev->volume = ctrl->value;
1452 rc = em28xx_audio_analog_set(dev);
1453 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001454 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001455 return rc;
1456}
1457
1458static int vidioc_g_tuner(struct file *file, void *priv,
1459 struct v4l2_tuner *t)
1460{
1461 struct em28xx_fh *fh = priv;
1462 struct em28xx *dev = fh->dev;
1463 int rc;
1464
1465 rc = check_dev(dev);
1466 if (rc < 0)
1467 return rc;
1468
1469 if (0 != t->index)
1470 return -EINVAL;
1471
1472 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001473 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001474
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001475 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001476 return 0;
1477}
1478
1479static int vidioc_s_tuner(struct file *file, void *priv,
1480 struct v4l2_tuner *t)
1481{
1482 struct em28xx_fh *fh = priv;
1483 struct em28xx *dev = fh->dev;
1484 int rc;
1485
1486 rc = check_dev(dev);
1487 if (rc < 0)
1488 return rc;
1489
1490 if (0 != t->index)
1491 return -EINVAL;
1492
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001493 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001494 return 0;
1495}
1496
1497static int vidioc_g_frequency(struct file *file, void *priv,
1498 struct v4l2_frequency *f)
1499{
1500 struct em28xx_fh *fh = priv;
1501 struct em28xx *dev = fh->dev;
1502
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001503 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001504 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001505 return 0;
1506}
1507
1508static int vidioc_s_frequency(struct file *file, void *priv,
1509 struct v4l2_frequency *f)
1510{
1511 struct em28xx_fh *fh = priv;
1512 struct em28xx *dev = fh->dev;
1513 int rc;
1514
1515 rc = check_dev(dev);
1516 if (rc < 0)
1517 return rc;
1518
1519 if (0 != f->tuner)
1520 return -EINVAL;
1521
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001522 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1523 return -EINVAL;
1524 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001525 return -EINVAL;
1526
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001527 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001528 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001529
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001530 return 0;
1531}
1532
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001533#ifdef CONFIG_VIDEO_ADV_DEBUG
1534static int em28xx_reg_len(int reg)
1535{
1536 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001537 case EM28XX_R40_AC97LSB:
1538 case EM28XX_R30_HSCALELOW:
1539 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001540 return 2;
1541 default:
1542 return 1;
1543 }
1544}
1545
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001546static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001547 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001548{
1549 struct em28xx_fh *fh = priv;
1550 struct em28xx *dev = fh->dev;
1551
1552 chip->ident = V4L2_IDENT_NONE;
1553 chip->revision = 0;
1554
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001555 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001556
1557 return 0;
1558}
1559
1560
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001561static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001562 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001563{
1564 struct em28xx_fh *fh = priv;
1565 struct em28xx *dev = fh->dev;
1566 int ret;
1567
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001568 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001569 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001570 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001571 if (ret < 0)
1572 return ret;
1573
1574 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001575 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001576 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001577 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001578 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001579 return 0;
1580 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001581 /* TODO: is this correct? */
1582 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1583 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001584 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001585 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001586 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001587 }
1588
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001589 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001590 reg->size = em28xx_reg_len(reg->reg);
1591 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001592 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001593
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001594 if (ret < 0)
1595 return ret;
1596
1597 reg->val = ret;
1598 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001599 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001600 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1601 reg->reg, (char *)&val, 2);
1602 if (ret < 0)
1603 return ret;
1604
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001605 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001606 }
1607
1608 return 0;
1609}
1610
1611static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001612 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001613{
1614 struct em28xx_fh *fh = priv;
1615 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001616 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001617 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001618
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001619 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001620 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001621 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001622 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001623 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001624 return 0;
1625 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001626 /* TODO: is this correct? */
1627 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1628 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001629 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001630 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001631 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001632 }
1633
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001634 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001635 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001636
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001637 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001638 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001639}
1640#endif
1641
1642
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001643static int vidioc_cropcap(struct file *file, void *priv,
1644 struct v4l2_cropcap *cc)
1645{
1646 struct em28xx_fh *fh = priv;
1647 struct em28xx *dev = fh->dev;
1648
1649 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1650 return -EINVAL;
1651
1652 cc->bounds.left = 0;
1653 cc->bounds.top = 0;
1654 cc->bounds.width = dev->width;
1655 cc->bounds.height = dev->height;
1656 cc->defrect = cc->bounds;
1657 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1658 cc->pixelaspect.denominator = 59;
1659
1660 return 0;
1661}
1662
1663static int vidioc_streamon(struct file *file, void *priv,
1664 enum v4l2_buf_type type)
1665{
1666 struct em28xx_fh *fh = priv;
1667 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001668 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001669
1670 rc = check_dev(dev);
1671 if (rc < 0)
1672 return rc;
1673
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001674 if (unlikely(type != fh->type))
1675 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001676
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001677 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1678 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001679
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001680 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001681 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001682
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001683 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1684 rc = videobuf_streamon(&fh->vb_vidq);
1685 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1686 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001687
1688 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001689}
1690
1691static int vidioc_streamoff(struct file *file, void *priv,
1692 enum v4l2_buf_type type)
1693{
1694 struct em28xx_fh *fh = priv;
1695 struct em28xx *dev = fh->dev;
1696 int rc;
1697
1698 rc = check_dev(dev);
1699 if (rc < 0)
1700 return rc;
1701
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001702 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1703 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001704 return -EINVAL;
1705 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001706 return -EINVAL;
1707
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001708 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1709 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001710
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001711 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001712 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001713 res_free(fh, EM28XX_RESOURCE_VIDEO);
1714 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001715 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001716 res_free(fh, EM28XX_RESOURCE_VBI);
1717 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001718
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001719 return 0;
1720}
1721
1722static int vidioc_querycap(struct file *file, void *priv,
1723 struct v4l2_capability *cap)
1724{
1725 struct em28xx_fh *fh = priv;
1726 struct em28xx *dev = fh->dev;
1727
1728 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1729 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001730 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001731
1732 cap->version = EM28XX_VERSION_CODE;
1733
1734 cap->capabilities =
1735 V4L2_CAP_SLICED_VBI_CAPTURE |
1736 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001737 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1738
Devin Heitmueller04146142009-09-11 00:08:44 -03001739 if (dev->vbi_dev)
1740 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1741
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001742 if (dev->audio_mode.has_audio)
1743 cap->capabilities |= V4L2_CAP_AUDIO;
1744
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001745 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001746 cap->capabilities |= V4L2_CAP_TUNER;
1747
1748 return 0;
1749}
1750
Hans Verkuil78b526a2008-05-28 12:16:41 -03001751static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001752 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001753{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001754 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001755 return -EINVAL;
1756
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001757 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1758 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001759
1760 return 0;
1761}
1762
1763/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001764static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001765 struct v4l2_format *f)
1766{
1767 struct em28xx_fh *fh = priv;
1768 struct em28xx *dev = fh->dev;
1769 int rc;
1770
1771 rc = check_dev(dev);
1772 if (rc < 0)
1773 return rc;
1774
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001776 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001777
1778 if (f->fmt.sliced.service_set == 0)
1779 rc = -EINVAL;
1780
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001781 return rc;
1782}
1783
Hans Verkuil78b526a2008-05-28 12:16:41 -03001784static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785 struct v4l2_format *f)
1786{
1787 struct em28xx_fh *fh = priv;
1788 struct em28xx *dev = fh->dev;
1789 int rc;
1790
1791 rc = check_dev(dev);
1792 if (rc < 0)
1793 return rc;
1794
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001795 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001796
1797 if (f->fmt.sliced.service_set == 0)
1798 return -EINVAL;
1799
1800 return 0;
1801}
1802
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001803/* RAW VBI ioctls */
1804
1805static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1806 struct v4l2_format *format)
1807{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001808 struct em28xx_fh *fh = priv;
1809 struct em28xx *dev = fh->dev;
1810
1811 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001812 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1813 format->fmt.vbi.offset = 0;
1814 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001815 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1816 format->fmt.vbi.count[0] = dev->vbi_height;
1817 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001818
1819 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001820 if (dev->norm & V4L2_STD_525_60) {
1821 /* NTSC */
1822 format->fmt.vbi.start[0] = 10;
1823 format->fmt.vbi.start[1] = 273;
1824 } else if (dev->norm & V4L2_STD_625_50) {
1825 /* PAL */
1826 format->fmt.vbi.start[0] = 6;
1827 format->fmt.vbi.start[1] = 318;
1828 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001829
1830 return 0;
1831}
1832
1833static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1834 struct v4l2_format *format)
1835{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001836 struct em28xx_fh *fh = priv;
1837 struct em28xx *dev = fh->dev;
1838
1839 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001840 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1841 format->fmt.vbi.offset = 0;
1842 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001843 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1844 format->fmt.vbi.count[0] = dev->vbi_height;
1845 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001846
1847 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001848 if (dev->norm & V4L2_STD_525_60) {
1849 /* NTSC */
1850 format->fmt.vbi.start[0] = 10;
1851 format->fmt.vbi.start[1] = 273;
1852 } else if (dev->norm & V4L2_STD_625_50) {
1853 /* PAL */
1854 format->fmt.vbi.start[0] = 6;
1855 format->fmt.vbi.start[1] = 318;
1856 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001857
1858 return 0;
1859}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860
1861static int vidioc_reqbufs(struct file *file, void *priv,
1862 struct v4l2_requestbuffers *rb)
1863{
1864 struct em28xx_fh *fh = priv;
1865 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001866 int rc;
1867
1868 rc = check_dev(dev);
1869 if (rc < 0)
1870 return rc;
1871
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001872 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1873 return videobuf_reqbufs(&fh->vb_vidq, rb);
1874 else
1875 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001876}
1877
1878static int vidioc_querybuf(struct file *file, void *priv,
1879 struct v4l2_buffer *b)
1880{
1881 struct em28xx_fh *fh = priv;
1882 struct em28xx *dev = fh->dev;
1883 int rc;
1884
1885 rc = check_dev(dev);
1886 if (rc < 0)
1887 return rc;
1888
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001889 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1890 return videobuf_querybuf(&fh->vb_vidq, b);
1891 else {
1892 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1893 the videobuf framework, but we probably shouldn't be
1894 returning a buffer larger than that which was asked for.
1895 At a minimum, it causes a crash in zvbi since it does
1896 a memcpy based on the source buffer length */
1897 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001898 b->length = dev->vbi_width * dev->vbi_height * 2;
1899
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001900 return result;
1901 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001902}
1903
1904static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1905{
1906 struct em28xx_fh *fh = priv;
1907 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001908 int rc;
1909
1910 rc = check_dev(dev);
1911 if (rc < 0)
1912 return rc;
1913
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001914 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1915 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001916 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001917 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001918}
1919
1920static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1921{
1922 struct em28xx_fh *fh = priv;
1923 struct em28xx *dev = fh->dev;
1924 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001925
1926 rc = check_dev(dev);
1927 if (rc < 0)
1928 return rc;
1929
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001930 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1931 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1932 O_NONBLOCK);
1933 else
1934 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1935 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001936}
1937
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001938#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001939static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001940{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001941 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001942
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001943 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1944 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1945 else
1946 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001947}
1948#endif
1949
1950
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001951/* ----------------------------------------------------------- */
1952/* RADIO ESPECIFIC IOCTLS */
1953/* ----------------------------------------------------------- */
1954
1955static int radio_querycap(struct file *file, void *priv,
1956 struct v4l2_capability *cap)
1957{
1958 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1959
1960 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1961 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001962 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001963
1964 cap->version = EM28XX_VERSION_CODE;
1965 cap->capabilities = V4L2_CAP_TUNER;
1966 return 0;
1967}
1968
1969static int radio_g_tuner(struct file *file, void *priv,
1970 struct v4l2_tuner *t)
1971{
1972 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1973
1974 if (unlikely(t->index > 0))
1975 return -EINVAL;
1976
1977 strcpy(t->name, "Radio");
1978 t->type = V4L2_TUNER_RADIO;
1979
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001980 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001981
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001982 return 0;
1983}
1984
1985static int radio_enum_input(struct file *file, void *priv,
1986 struct v4l2_input *i)
1987{
1988 if (i->index != 0)
1989 return -EINVAL;
1990 strcpy(i->name, "Radio");
1991 i->type = V4L2_INPUT_TYPE_TUNER;
1992
1993 return 0;
1994}
1995
1996static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1997{
1998 if (unlikely(a->index))
1999 return -EINVAL;
2000
2001 strcpy(a->name, "Radio");
2002 return 0;
2003}
2004
2005static int radio_s_tuner(struct file *file, void *priv,
2006 struct v4l2_tuner *t)
2007{
2008 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2009
2010 if (0 != t->index)
2011 return -EINVAL;
2012
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002013 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002014
2015 return 0;
2016}
2017
2018static int radio_s_audio(struct file *file, void *fh,
2019 struct v4l2_audio *a)
2020{
2021 return 0;
2022}
2023
2024static int radio_s_input(struct file *file, void *fh, unsigned int i)
2025{
2026 return 0;
2027}
2028
2029static int radio_queryctrl(struct file *file, void *priv,
2030 struct v4l2_queryctrl *qc)
2031{
2032 int i;
2033
2034 if (qc->id < V4L2_CID_BASE ||
2035 qc->id >= V4L2_CID_LASTP1)
2036 return -EINVAL;
2037
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002038 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2039 if (qc->id && qc->id == ac97_qctrl[i].id) {
2040 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002041 return 0;
2042 }
2043 }
2044
2045 return -EINVAL;
2046}
2047
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002048/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002049 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002050 * inits the device and starts isoc transfer
2051 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002052static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002053{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002054 int errCode = 0, radio = 0;
2055 struct video_device *vdev = video_devdata(filp);
2056 struct em28xx *dev = video_drvdata(filp);
2057 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002058 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002059 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002060
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002061 switch (vdev->vfl_type) {
2062 case VFL_TYPE_GRABBER:
2063 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2064 break;
2065 case VFL_TYPE_VBI:
2066 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2067 break;
2068 case VFL_TYPE_RADIO:
2069 radio = 1;
2070 break;
2071 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002072
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002073 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2074 video_device_node_name(vdev), v4l2_type_names[fh_type],
2075 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002076
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002077
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002078 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002079 if (!fh) {
2080 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2081 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002082 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002083 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002084 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002085 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002086 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002088 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002089 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002090 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002091 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002092
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002093 /* Needed, since GPIO might have disabled power of
2094 some i2c device
2095 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002096 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002097
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002098 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002099 if (fh->radio) {
2100 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002101 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002102 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002103
2104 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002106 if (dev->progressive)
2107 field = V4L2_FIELD_NONE;
2108 else
2109 field = V4L2_FIELD_INTERLACED;
2110
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002111 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2112 NULL, &dev->slock,
2113 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002114 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002115
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002116 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2117 NULL, &dev->slock,
2118 V4L2_BUF_TYPE_VBI_CAPTURE,
2119 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002120 sizeof(struct em28xx_buffer), fh, &dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002121
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002122 return errCode;
2123}
2124
2125/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002126 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127 * unregisters the v4l2,i2c and usb devices
2128 * called when the device gets disconected or at module unload
2129*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002130void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002131{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002132
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002133 /*FIXME: I2C IR should be disconnected */
2134
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002135 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002136 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002137 video_unregister_device(dev->radio_dev);
2138 else
2139 video_device_release(dev->radio_dev);
2140 dev->radio_dev = NULL;
2141 }
2142 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002143 em28xx_info("V4L2 device %s deregistered\n",
2144 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002145 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002146 video_unregister_device(dev->vbi_dev);
2147 else
2148 video_device_release(dev->vbi_dev);
2149 dev->vbi_dev = NULL;
2150 }
2151 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002152 em28xx_info("V4L2 device %s deregistered\n",
2153 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002154 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002155 video_unregister_device(dev->vdev);
2156 else
2157 video_device_release(dev->vdev);
2158 dev->vdev = NULL;
2159 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002160}
2161
2162/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002163 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002164 * stops streaming and deallocates all resources allocated by the v4l2
2165 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002166 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002167static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002168{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002169 struct em28xx_fh *fh = filp->private_data;
2170 struct em28xx *dev = fh->dev;
2171 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002173 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002175 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002176 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002177 res_free(fh, EM28XX_RESOURCE_VIDEO);
2178 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002179
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002180 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2181 videobuf_stop(&fh->vb_vbiq);
2182 res_free(fh, EM28XX_RESOURCE_VBI);
2183 }
2184
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002185 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002186 /* the device is already disconnect,
2187 free the remaining resources */
2188 if (dev->state & DEV_DISCONNECTED) {
2189 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002190 kfree(dev);
2191 return 0;
2192 }
2193
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002194 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002195 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002196
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002197 /* do this before setting alternate! */
2198 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002199 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002200
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002201 /* set alternate 0 */
2202 dev->alt = 0;
2203 em28xx_videodbg("setting alternate 0\n");
2204 errCode = usb_set_interface(dev->udev, 0, 0);
2205 if (errCode < 0) {
2206 em28xx_errdev("cannot change alternate number to "
2207 "0 (error=%i)\n", errCode);
2208 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002210
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002211 videobuf_mmap_free(&fh->vb_vidq);
2212 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002213 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214 dev->users--;
2215 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002216 return 0;
2217}
2218
2219/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002220 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002221 * will allocate buffers when called for the first time
2222 */
2223static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002224em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002225 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002226{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002227 struct em28xx_fh *fh = filp->private_data;
2228 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002229 int rc;
2230
2231 rc = check_dev(dev);
2232 if (rc < 0)
2233 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002234
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002235 /* FIXME: read() is not prepared to allow changing the video
2236 resolution while streaming. Seems a bug at em28xx_set_fmt
2237 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002238
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002239 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002240 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2241 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002242
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002243 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2244 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002245 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002246
2247
2248 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002249 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2250 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002251
2252 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2253 filp->f_flags & O_NONBLOCK);
2254 }
2255
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002256 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002257}
2258
2259/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002260 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002261 * will allocate buffers when called for the first time
2262 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002263static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002265 struct em28xx_fh *fh = filp->private_data;
2266 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002267 int rc;
2268
2269 rc = check_dev(dev);
2270 if (rc < 0)
2271 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002272
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002273 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2274 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2275 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002276 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002277 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2278 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2279 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002280 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002281 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002282 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002283 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002284}
2285
2286/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002289static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002290{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002291 struct em28xx_fh *fh = filp->private_data;
2292 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002293 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002294
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002295 rc = check_dev(dev);
2296 if (rc < 0)
2297 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002298
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002299 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2300 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2301 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2302 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002304 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2305 (unsigned long)vma->vm_start,
2306 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2307 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002308
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002309 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310}
2311
Hans Verkuilbec43662008-12-30 06:58:20 -03002312static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002313 .owner = THIS_MODULE,
2314 .open = em28xx_v4l2_open,
2315 .release = em28xx_v4l2_close,
2316 .read = em28xx_v4l2_read,
2317 .poll = em28xx_v4l2_poll,
2318 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002319 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002320};
2321
Hans Verkuila3998102008-07-21 02:57:38 -03002322static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002323 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002324 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2325 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2326 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2327 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002328 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2329 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002330 .vidioc_g_audio = vidioc_g_audio,
2331 .vidioc_s_audio = vidioc_s_audio,
2332 .vidioc_cropcap = vidioc_cropcap,
2333
Hans Verkuil78b526a2008-05-28 12:16:41 -03002334 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2335 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2336 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002337
2338 .vidioc_reqbufs = vidioc_reqbufs,
2339 .vidioc_querybuf = vidioc_querybuf,
2340 .vidioc_qbuf = vidioc_qbuf,
2341 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002342 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002343 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002344 .vidioc_g_parm = vidioc_g_parm,
2345 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002346 .vidioc_enum_input = vidioc_enum_input,
2347 .vidioc_g_input = vidioc_g_input,
2348 .vidioc_s_input = vidioc_s_input,
2349 .vidioc_queryctrl = vidioc_queryctrl,
2350 .vidioc_g_ctrl = vidioc_g_ctrl,
2351 .vidioc_s_ctrl = vidioc_s_ctrl,
2352 .vidioc_streamon = vidioc_streamon,
2353 .vidioc_streamoff = vidioc_streamoff,
2354 .vidioc_g_tuner = vidioc_g_tuner,
2355 .vidioc_s_tuner = vidioc_s_tuner,
2356 .vidioc_g_frequency = vidioc_g_frequency,
2357 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002358#ifdef CONFIG_VIDEO_ADV_DEBUG
2359 .vidioc_g_register = vidioc_g_register,
2360 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002361 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002362#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002363#ifdef CONFIG_VIDEO_V4L1_COMPAT
2364 .vidiocgmbuf = vidiocgmbuf,
2365#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002366};
2367
2368static const struct video_device em28xx_video_template = {
2369 .fops = &em28xx_v4l_fops,
2370 .release = video_device_release,
2371 .ioctl_ops = &video_ioctl_ops,
2372
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002373 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002374 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002375};
2376
Hans Verkuilbec43662008-12-30 06:58:20 -03002377static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002378 .owner = THIS_MODULE,
2379 .open = em28xx_v4l2_open,
2380 .release = em28xx_v4l2_close,
2381 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002382};
2383
2384static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002385 .vidioc_querycap = radio_querycap,
2386 .vidioc_g_tuner = radio_g_tuner,
2387 .vidioc_enum_input = radio_enum_input,
2388 .vidioc_g_audio = radio_g_audio,
2389 .vidioc_s_tuner = radio_s_tuner,
2390 .vidioc_s_audio = radio_s_audio,
2391 .vidioc_s_input = radio_s_input,
2392 .vidioc_queryctrl = radio_queryctrl,
2393 .vidioc_g_ctrl = vidioc_g_ctrl,
2394 .vidioc_s_ctrl = vidioc_s_ctrl,
2395 .vidioc_g_frequency = vidioc_g_frequency,
2396 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002397#ifdef CONFIG_VIDEO_ADV_DEBUG
2398 .vidioc_g_register = vidioc_g_register,
2399 .vidioc_s_register = vidioc_s_register,
2400#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002401};
2402
Hans Verkuila3998102008-07-21 02:57:38 -03002403static struct video_device em28xx_radio_template = {
2404 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002405 .fops = &radio_fops,
2406 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002407};
2408
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002409/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002410
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002411
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002412
Adrian Bunk532fe652008-01-28 22:10:48 -03002413static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002414 const struct video_device *template,
2415 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002416{
2417 struct video_device *vfd;
2418
2419 vfd = video_device_alloc();
2420 if (NULL == vfd)
2421 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002422
2423 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002424 vfd->v4l2_dev = &dev->v4l2_dev;
2425 vfd->release = video_device_release;
2426 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002427 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002428
2429 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2430 dev->name, type_name);
2431
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002432 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002433 return vfd;
2434}
2435
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002436int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002437{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002438 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002439 int ret;
2440
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002441 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2442 dev->name,
2443 (EM28XX_VERSION_CODE >> 16) & 0xff,
2444 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2445
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002446 /* set default norm */
2447 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002448 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002449 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002450 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002451
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002452 /* Analog specific initialization */
2453 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002454 em28xx_set_video_format(dev, format[0].fourcc,
2455 norm_maxw(dev), norm_maxh(dev));
2456
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002457 video_mux(dev, dev->ctl_input);
2458
2459 /* Audio defaults */
2460 dev->mute = 1;
2461 dev->volume = 0x1f;
2462
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002463/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002464 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2465 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2466 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002467
2468 em28xx_set_outfmt(dev);
2469 em28xx_colorlevels_set_default(dev);
2470 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002471
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002472 /* allocate and fill video video_device struct */
2473 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2474 if (!dev->vdev) {
2475 em28xx_errdev("cannot allocate video_device.\n");
2476 return -ENODEV;
2477 }
2478
2479 /* register v4l2 video video_device */
2480 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2481 video_nr[dev->devno]);
2482 if (ret) {
2483 em28xx_errdev("unable to register video device (error=%i).\n",
2484 ret);
2485 return ret;
2486 }
2487
2488 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002489 if (em28xx_vbi_supported(dev) == 1) {
2490 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2491 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002492
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002493 /* register v4l2 vbi video_device */
2494 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2495 vbi_nr[dev->devno]);
2496 if (ret < 0) {
2497 em28xx_errdev("unable to register vbi device\n");
2498 return ret;
2499 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002500 }
2501
2502 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002503 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2504 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002505 if (!dev->radio_dev) {
2506 em28xx_errdev("cannot allocate video_device.\n");
2507 return -ENODEV;
2508 }
2509 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2510 radio_nr[dev->devno]);
2511 if (ret < 0) {
2512 em28xx_errdev("can't register radio device\n");
2513 return ret;
2514 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002515 em28xx_info("Registered radio device as %s\n",
2516 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002517 }
2518
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002519 em28xx_info("V4L2 video device registered as %s\n",
2520 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002521
2522 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002523 em28xx_info("V4L2 VBI device registered as %s\n",
2524 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002525
2526 return 0;
2527}