blob: d176dc0394e2f9c276c4e333f695c7c91f39a563 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030052
53#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030075MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080076
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084MODULE_PARM_DESC(video_nr, "video device numbers");
85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080087
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030088static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030089module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080091
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030092/* supported video standards */
93static struct em28xx_fmt format[] = {
94 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030095 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030096 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030098 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300100 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300101 .fourcc = V4L2_PIX_FMT_RGB565,
102 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300103 .reg = EM28XX_OUTFMT_RGB_16_656,
104 }, {
105 .name = "8 bpp Bayer BGBG..GRGR",
106 .fourcc = V4L2_PIX_FMT_SBGGR8,
107 .depth = 8,
108 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
109 }, {
110 .name = "8 bpp Bayer GRGR..BGBG",
111 .fourcc = V4L2_PIX_FMT_SGRBG8,
112 .depth = 8,
113 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
114 }, {
115 .name = "8 bpp Bayer GBGB..RGRG",
116 .fourcc = V4L2_PIX_FMT_SGBRG8,
117 .depth = 8,
118 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
119 }, {
120 .name = "12 bpp YUV411",
121 .fourcc = V4L2_PIX_FMT_YUV411P,
122 .depth = 12,
123 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300124 },
125};
126
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800127/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200128/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300129static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800130 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300138 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300139 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300151/* ------------------------------------------------------------------
152 DMA and thread functions
153 ------------------------------------------------------------------*/
154
155/*
156 * Announces that a buffer were filled and request the next
157 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 struct em28xx_dmaqueue *dma_q,
160 struct em28xx_buffer *buf)
161{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 /* Advice that buffer was filled */
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
166 do_gettimeofday(&buf->vb.ts);
167
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300168 dev->isoc_ctl.vid_buf = NULL;
169
170 list_del(&buf->vb.queue);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
183 do_gettimeofday(&buf->vb.ts);
184
185 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300186
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189}
190
191/*
192 * Identify the buffer header type and properly handles
193 */
194static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf,
197 unsigned char *p,
198 unsigned char *outp, unsigned long len)
199{
200 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300201 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
204 if (dma_q->pos + len > buf->vb.size)
205 len = buf->vb.size - dma_q->pos;
206
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 startread = p;
208 remain = len;
209
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300210 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300212 else {
213 /* Interlaces two half frames */
214 if (buf->top_field)
215 fieldstart = outp;
216 else
217 fieldstart = outp + bytesperline;
218 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300219
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300220 linesdone = dma_q->pos / bytesperline;
221 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300222
223 if (dev->progressive)
224 offset = linesdone * bytesperline + currlinedone;
225 else
226 offset = linesdone * bytesperline * 2 + currlinedone;
227
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300228 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300229 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 lencopy = lencopy > remain ? remain : lencopy;
231
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300232 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300236 remain = (char *)outp + buf->vb.size - (char *)startwrite;
237 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300238 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300239 if (lencopy <= 0)
240 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300241 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300242
243 remain -= lencopy;
244
245 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300246 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300248 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300249 lencopy = remain;
250 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300251 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300252
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300253 if ((char *)startwrite + lencopy > (char *)outp +
254 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300255 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300257 ((char *)startwrite + lencopy) -
258 ((char *)outp + buf->vb.size));
259 lencopy = remain = (char *)outp + buf->vb.size -
260 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300261 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300262 if (lencopy <= 0)
263 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300264
265 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266
267 remain -= lencopy;
268 }
269
270 dma_q->pos += len;
271}
272
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300273static void em28xx_copy_vbi(struct em28xx *dev,
274 struct em28xx_dmaqueue *dma_q,
275 struct em28xx_buffer *buf,
276 unsigned char *p,
277 unsigned char *outp, unsigned long len)
278{
279 void *startwrite, *startread;
280 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300281 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300282
283 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300284 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300285 return;
286 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300287 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300288
289 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300290 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300291 return;
292 }
293 if (buf == NULL) {
294 return;
295 }
296 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300297 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300298 return;
299 }
300 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300301 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300302 return;
303 }
304
305 if (dma_q->pos + len > buf->vb.size)
306 len = buf->vb.size - dma_q->pos;
307
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300308 startread = p;
309
310 startwrite = outp + dma_q->pos;
311 offset = dma_q->pos;
312
313 /* Make sure the bottom field populates the second half of the frame */
314 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300315 startwrite += bytesperline * dev->vbi_height;
316 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300317 }
318
319 memcpy(startwrite, startread, len);
320 dma_q->pos += len;
321}
322
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300323static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300324 int packet, int status)
325{
326 char *errmsg = "Unknown";
327
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300328 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 case -ENOENT:
330 errmsg = "unlinked synchronuously";
331 break;
332 case -ECONNRESET:
333 errmsg = "unlinked asynchronuously";
334 break;
335 case -ENOSR:
336 errmsg = "Buffer error (overrun)";
337 break;
338 case -EPIPE:
339 errmsg = "Stalled (device not responding)";
340 break;
341 case -EOVERFLOW:
342 errmsg = "Babble (bad cable?)";
343 break;
344 case -EPROTO:
345 errmsg = "Bit-stuff error (bad cable?)";
346 break;
347 case -EILSEQ:
348 errmsg = "CRC/Timeout (could be anything)";
349 break;
350 case -ETIME:
351 errmsg = "Device does not respond";
352 break;
353 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300354 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300355 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
356 } else {
357 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
358 packet, status, errmsg);
359 }
360}
361
362/*
363 * video-buf generic routine to get the next available buffer
364 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300365static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366 struct em28xx_buffer **buf)
367{
368 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300369 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300371 if (list_empty(&dma_q->active)) {
372 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300373 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300374 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300375 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300376 }
377
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300378 /* Get the next buffer */
379 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
380
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300381 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300382 outp = videobuf_to_vmalloc(&(*buf)->vb);
383 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300384
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385 dev->isoc_ctl.vid_buf = *buf;
386
387 return;
388}
389
390/*
391 * video-buf generic routine to get the next available VBI buffer
392 */
393static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
394 struct em28xx_buffer **buf)
395{
396 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
397 char *outp;
398
399 if (list_empty(&dma_q->active)) {
400 em28xx_isocdbg("No active queue to serve\n");
401 dev->isoc_ctl.vbi_buf = NULL;
402 *buf = NULL;
403 return;
404 }
405
406 /* Get the next buffer */
407 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300408 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300409 outp = videobuf_to_vmalloc(&(*buf)->vb);
410 memset(outp, 0x00, (*buf)->vb.size);
411
412 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300415}
416
417/*
418 * Controls the isoc copy of each urb packet
419 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300420static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300421{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300422 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300423 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300424 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300426 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300427
428 if (!dev)
429 return 0;
430
431 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
432 return 0;
433
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300434 if (urb->status < 0) {
435 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 if (urb->status == -ENOENT)
437 return 0;
438 }
439
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300440 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300441 if (buf != NULL)
442 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444 for (i = 0; i < urb->number_of_packets; i++) {
445 int status = urb->iso_frame_desc[i].status;
446
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300447 if (status < 0) {
448 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300449 if (urb->iso_frame_desc[i].status != -EPROTO)
450 continue;
451 }
452
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300453 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454
455 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300456 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 continue;
458 }
459 if (urb->iso_frame_desc[i].actual_length >
460 dev->max_pkt_size) {
461 em28xx_isocdbg("packet bigger than packet size");
462 continue;
463 }
464
465 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466
467 /* FIXME: incomplete buffer checks where removed to make
468 logic simpler. Impacts of those changes should be evaluated
469 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300470 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
471 em28xx_isocdbg("VBI HEADER!!!\n");
472 /* FIXME: Should add vbi copy */
473 continue;
474 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300475 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300476 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300477 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300478
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300479 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300480 if (buf != NULL)
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
483 if (buf == NULL)
484 outp = NULL;
485 else
486 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300487 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300488
489 if (buf != NULL) {
490 if (p[2] & 1)
491 buf->top_field = 0;
492 else
493 buf->top_field = 1;
494 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300495
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300498 if (buf != NULL) {
499 if (p[0] != 0x88 && p[0] != 0x22) {
500 em28xx_isocdbg("frame is not complete\n");
501 len += 4;
502 } else {
503 p += 4;
504 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300505 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300506 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 }
508 return rc;
509}
510
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300511/* Version of isoc handler that takes into account a mixture of video and
512 VBI data */
513static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
514{
515 struct em28xx_buffer *buf, *vbi_buf;
516 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300517 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300518 unsigned char *outp = NULL;
519 unsigned char *vbioutp = NULL;
520 int i, len = 0, rc = 1;
521 unsigned char *p;
522 int vbi_size;
523
524 if (!dev)
525 return 0;
526
527 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
528 return 0;
529
530 if (urb->status < 0) {
531 print_err_status(dev, -1, urb->status);
532 if (urb->status == -ENOENT)
533 return 0;
534 }
535
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300536 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300537 if (buf != NULL)
538 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300539
540 vbi_buf = dev->isoc_ctl.vbi_buf;
541 if (vbi_buf != NULL)
542 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
543
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300544 for (i = 0; i < urb->number_of_packets; i++) {
545 int status = urb->iso_frame_desc[i].status;
546
547 if (status < 0) {
548 print_err_status(dev, i, status);
549 if (urb->iso_frame_desc[i].status != -EPROTO)
550 continue;
551 }
552
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300553 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300554 if (urb->iso_frame_desc[i].actual_length <= 0) {
555 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
556 continue;
557 }
558 if (urb->iso_frame_desc[i].actual_length >
559 dev->max_pkt_size) {
560 em28xx_isocdbg("packet bigger than packet size");
561 continue;
562 }
563
564 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
565
566 /* capture type 0 = vbi start
567 capture type 1 = video start
568 capture type 2 = video in progress */
569 if (p[0] == 0x33 && p[1] == 0x95) {
570 dev->capture_type = 0;
571 dev->vbi_read = 0;
572 em28xx_isocdbg("VBI START HEADER!!!\n");
573 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300574 p += 4;
575 len -= 4;
576 } else if (p[0] == 0x88 && p[1] == 0x88 &&
577 p[2] == 0x88 && p[3] == 0x88) {
578 /* continuation */
579 p += 4;
580 len -= 4;
581 } else if (p[0] == 0x22 && p[1] == 0x5a) {
582 /* start video */
583 p += 4;
584 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300585 }
586
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300587 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300588
589 if (dev->capture_type == 0) {
590 if (dev->vbi_read >= vbi_size) {
591 /* We've already read all the VBI data, so
592 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300593 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300594 } else if ((dev->vbi_read + len) < vbi_size) {
595 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300596 if (dev->vbi_read == 0 &&
597 (!(dev->cur_field & 1))) {
598 /* Brand new frame */
599 if (vbi_buf != NULL)
600 vbi_buffer_filled(dev,
601 vbi_dma_q,
602 vbi_buf);
603 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
604 if (vbi_buf == NULL)
605 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300606 else
607 vbioutp = videobuf_to_vmalloc(
608 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300609 }
610
611 if (dev->vbi_read == 0) {
612 vbi_dma_q->pos = 0;
613 if (vbi_buf != NULL) {
614 if (dev->cur_field & 1)
615 vbi_buf->top_field = 0;
616 else
617 vbi_buf->top_field = 1;
618 }
619 }
620
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300621 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300622 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
623 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300624 } else {
625 /* Some of this frame is VBI data and some is
626 video data */
627 int vbi_data_len = vbi_size - dev->vbi_read;
628 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300631 dev->capture_type = 1;
632 p += vbi_data_len;
633 len -= vbi_data_len;
634 }
635 }
636
637 if (dev->capture_type == 1) {
638 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300639 if (dev->progressive || !(dev->cur_field & 1)) {
640 if (buf != NULL)
641 buffer_filled(dev, dma_q, buf);
642 get_next_buf(dma_q, &buf);
643 if (buf == NULL)
644 outp = NULL;
645 else
646 outp = videobuf_to_vmalloc(&buf->vb);
647 }
648 if (buf != NULL) {
649 if (dev->cur_field & 1)
650 buf->top_field = 0;
651 else
652 buf->top_field = 1;
653 }
654
655 dma_q->pos = 0;
656 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300657
658 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300659 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300660 p[2] == 0x88 && p[3] == 0x88) {
661 p += 4;
662 len -= 4;
663 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300664 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300665 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
666 p[2], len, (p[2] & 1) ?
667 "odd" : "even");
668 p += 4;
669 len -= 4;
670 }
671
672 if (len > 0)
673 em28xx_copy_video(dev, dma_q, buf, p, outp,
674 len);
675 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300676 }
677 return rc;
678}
679
680
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300682 Videobuf operations
683 ------------------------------------------------------------------*/
684
685static int
686buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
687{
688 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300689 struct em28xx *dev = fh->dev;
690 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300691
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300692 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
693 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300694
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300695 if (0 == *count)
696 *count = EM28XX_DEF_BUF;
697
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300698 if (*count < EM28XX_MIN_BUF)
699 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300700
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300701 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300702 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300703 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300704 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300705
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300706 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300707
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300708 return 0;
709}
710
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300711/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300712static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
713{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300714 struct em28xx_fh *fh = vq->priv_data;
715 struct em28xx *dev = fh->dev;
716 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300717 if (in_interrupt())
718 BUG();
719
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300720 /* We used to wait for the buffer to finish here, but this didn't work
721 because, as we were keeping the state as VIDEOBUF_QUEUED,
722 videobuf_queue_cancel marked it as finished for us.
723 (Also, it could wedge forever if the hardware was misconfigured.)
724
725 This should be safe; by the time we get here, the buffer isn't
726 queued anymore. If we ever start marking the buffers as
727 VIDEOBUF_ACTIVE, it won't be, though.
728 */
729 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300730 if (dev->isoc_ctl.vid_buf == buf)
731 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300732 spin_unlock_irqrestore(&dev->slock, flags);
733
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300734 videobuf_vmalloc_free(&buf->vb);
735 buf->vb.state = VIDEOBUF_NEEDS_INIT;
736}
737
738static int
739buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
740 enum v4l2_field field)
741{
742 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300743 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744 struct em28xx *dev = fh->dev;
745 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300747 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
748 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300749
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
751 return -EINVAL;
752
Brandon Philips05612972008-04-13 14:57:01 -0300753 buf->vb.width = dev->width;
754 buf->vb.height = dev->height;
755 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756
757 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300758 rc = videobuf_iolock(vq, &buf->vb, NULL);
759 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300760 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300761 }
762
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300764 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300765
766 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300767 if (em28xx_vbi_supported(dev) == 1)
768 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
769 EM28XX_NUM_BUFS,
770 dev->max_pkt_size,
771 em28xx_isoc_copy_vbi);
772 else
773 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
774 EM28XX_NUM_BUFS,
775 dev->max_pkt_size,
776 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300777 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300778 goto fail;
779 }
780
781 buf->vb.state = VIDEOBUF_PREPARED;
782 return 0;
783
784fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300785 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300786 return rc;
787}
788
789static void
790buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
791{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300792 struct em28xx_buffer *buf = container_of(vb,
793 struct em28xx_buffer,
794 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300796 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300798
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300799 buf->vb.state = VIDEOBUF_QUEUED;
800 list_add_tail(&buf->vb.queue, &vidq->active);
801
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300802}
803
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300804static void buffer_release(struct videobuf_queue *vq,
805 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300806{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300807 struct em28xx_buffer *buf = container_of(vb,
808 struct em28xx_buffer,
809 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300811 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300812
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300813 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300814
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300815 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300816}
817
818static struct videobuf_queue_ops em28xx_video_qops = {
819 .buf_setup = buffer_setup,
820 .buf_prepare = buffer_prepare,
821 .buf_queue = buffer_queue,
822 .buf_release = buffer_release,
823};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800824
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300825/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800826
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800827static void video_mux(struct em28xx *dev, int index)
828{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800829 dev->ctl_input = index;
830 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300831 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800832
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300833 if (!dev->ctl_aoutput)
834 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
835
Hans Verkuil5325b422009-04-02 11:26:22 -0300836 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
837 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800838
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300839 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300840 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300841 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
842 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300843 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300844 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300845 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
846 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800847 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300848
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300849 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300850 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
851 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300852 }
853
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300854 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800855}
856
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300857/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300858static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300859{
860 struct em28xx *dev = fh->dev;
861
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300862 if (fh->resources & bit)
863 /* have it already allocated */
864 return 1;
865
866 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300867 if (dev->resources & bit) {
868 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869 return 0;
870 }
871 /* it's free, grab it */
872 fh->resources |= bit;
873 dev->resources |= bit;
874 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300875 return 1;
876}
877
878static int res_check(struct em28xx_fh *fh, unsigned int bit)
879{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300880 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300881}
882
883static int res_locked(struct em28xx *dev, unsigned int bit)
884{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300885 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300886}
887
888static void res_free(struct em28xx_fh *fh, unsigned int bits)
889{
890 struct em28xx *dev = fh->dev;
891
892 BUG_ON((fh->resources & bits) != bits);
893
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300894 fh->resources &= ~bits;
895 dev->resources &= ~bits;
896 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300897}
898
899static int get_ressource(struct em28xx_fh *fh)
900{
901 switch (fh->type) {
902 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
903 return EM28XX_RESOURCE_VIDEO;
904 case V4L2_BUF_TYPE_VBI_CAPTURE:
905 return EM28XX_RESOURCE_VBI;
906 default:
907 BUG();
908 return 0;
909 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300910}
911
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800912/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300913 * ac97_queryctrl()
914 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300915 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300916static int ac97_queryctrl(struct v4l2_queryctrl *qc)
917{
918 int i;
919
920 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
921 if (qc->id && qc->id == ac97_qctrl[i].id) {
922 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
923 return 0;
924 }
925 }
926
927 /* Control is not ac97 related */
928 return 1;
929}
930
931/*
932 * ac97_get_ctrl()
933 * return the current values for ac97 mute and volume
934 */
935static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936{
937 switch (ctrl->id) {
938 case V4L2_CID_AUDIO_MUTE:
939 ctrl->value = dev->mute;
940 return 0;
941 case V4L2_CID_AUDIO_VOLUME:
942 ctrl->value = dev->volume;
943 return 0;
944 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300945 /* Control is not ac97 related */
946 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300947 }
948}
949
950/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300951 * ac97_set_ctrl()
952 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300953 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300954static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300956 int i;
957
958 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
959 if (ctrl->id == ac97_qctrl[i].id)
960 goto handle;
961
962 /* Announce that hasn't handle it */
963 return 1;
964
965handle:
966 if (ctrl->value < ac97_qctrl[i].minimum ||
967 ctrl->value > ac97_qctrl[i].maximum)
968 return -ERANGE;
969
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300970 switch (ctrl->id) {
971 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300972 dev->mute = ctrl->value;
973 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300974 case V4L2_CID_AUDIO_VOLUME:
975 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300976 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300977 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300978
979 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980}
981
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300982static int check_dev(struct em28xx *dev)
983{
984 if (dev->state & DEV_DISCONNECTED) {
985 em28xx_errdev("v4l2 ioctl: device not present\n");
986 return -ENODEV;
987 }
988
989 if (dev->state & DEV_MISCONFIGURED) {
990 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
991 "close and open it again\n");
992 return -EIO;
993 }
994 return 0;
995}
996
997static void get_scale(struct em28xx *dev,
998 unsigned int width, unsigned int height,
999 unsigned int *hscale, unsigned int *vscale)
1000{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001001 unsigned int maxw = norm_maxw(dev);
1002 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001003
1004 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1005 if (*hscale >= 0x4000)
1006 *hscale = 0x3fff;
1007
1008 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1009 if (*vscale >= 0x4000)
1010 *vscale = 0x3fff;
1011}
1012
1013/* ------------------------------------------------------------------
1014 IOCTL vidioc handling
1015 ------------------------------------------------------------------*/
1016
Hans Verkuil78b526a2008-05-28 12:16:41 -03001017static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001018 struct v4l2_format *f)
1019{
1020 struct em28xx_fh *fh = priv;
1021 struct em28xx *dev = fh->dev;
1022
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 f->fmt.pix.width = dev->width;
1024 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001025 f->fmt.pix.pixelformat = dev->format->fourcc;
1026 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001027 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1029
1030 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001031 if (dev->progressive)
1032 f->fmt.pix.field = V4L2_FIELD_NONE;
1033 else
1034 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001035 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001036 return 0;
1037}
1038
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001039static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1040{
1041 unsigned int i;
1042
1043 for (i = 0; i < ARRAY_SIZE(format); i++)
1044 if (format[i].fourcc == fourcc)
1045 return &format[i];
1046
1047 return NULL;
1048}
1049
Hans Verkuil78b526a2008-05-28 12:16:41 -03001050static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001051 struct v4l2_format *f)
1052{
1053 struct em28xx_fh *fh = priv;
1054 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001055 unsigned int width = f->fmt.pix.width;
1056 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001057 unsigned int maxw = norm_maxw(dev);
1058 unsigned int maxh = norm_maxh(dev);
1059 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001060 struct em28xx_fmt *fmt;
1061
1062 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1063 if (!fmt) {
1064 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1065 f->fmt.pix.pixelformat);
1066 return -EINVAL;
1067 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001069 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001070 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001071 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1072 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001073 } else {
1074 /* width must even because of the YUYV format
1075 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001076 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1077 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 }
1079
1080 get_scale(dev, width, height, &hscale, &vscale);
1081
1082 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1083 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1084
1085 f->fmt.pix.width = width;
1086 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001087 f->fmt.pix.pixelformat = fmt->fourcc;
1088 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1089 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001091 if (dev->progressive)
1092 f->fmt.pix.field = V4L2_FIELD_NONE;
1093 else
1094 f->fmt.pix.field = dev->interlaced ?
1095 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001097 return 0;
1098}
1099
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001100static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1101 unsigned width, unsigned height)
1102{
1103 struct em28xx_fmt *fmt;
1104
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001105 fmt = format_by_fourcc(fourcc);
1106 if (!fmt)
1107 return -EINVAL;
1108
1109 dev->format = fmt;
1110 dev->width = width;
1111 dev->height = height;
1112
1113 /* set new image size */
1114 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1115
1116 em28xx_set_alternate(dev);
1117 em28xx_resolution_set(dev);
1118
1119 return 0;
1120}
1121
Hans Verkuil78b526a2008-05-28 12:16:41 -03001122static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001123 struct v4l2_format *f)
1124{
1125 struct em28xx_fh *fh = priv;
1126 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001127 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001128
1129 rc = check_dev(dev);
1130 if (rc < 0)
1131 return rc;
1132
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001133 vidioc_try_fmt_vid_cap(file, priv, f);
1134
Brandon Philips05612972008-04-13 14:57:01 -03001135 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1136 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001137 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001138 }
1139
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001140 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001141 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001142}
1143
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001144static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1145{
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 *norm = dev->norm;
1155
1156 return 0;
1157}
1158
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001159static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160{
1161 struct em28xx_fh *fh = priv;
1162 struct em28xx *dev = fh->dev;
1163 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001164 int rc;
1165
1166 rc = check_dev(dev);
1167 if (rc < 0)
1168 return rc;
1169
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001170 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001171
1172 /* Adjusts width/height, if needed */
1173 f.fmt.pix.width = dev->width;
1174 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001175 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001176
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001177 /* set new image size */
1178 dev->width = f.fmt.pix.width;
1179 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001180 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1181
1182 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001183 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001185 return 0;
1186}
1187
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001188static int vidioc_g_parm(struct file *file, void *priv,
1189 struct v4l2_streamparm *p)
1190{
1191 struct em28xx_fh *fh = priv;
1192 struct em28xx *dev = fh->dev;
1193 int rc = 0;
1194
1195 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1196 return -EINVAL;
1197
1198 if (dev->board.is_webcam)
1199 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1200 video, g_parm, p);
1201 else
1202 v4l2_video_std_frame_period(dev->norm,
1203 &p->parm.capture.timeperframe);
1204
1205 return rc;
1206}
1207
1208static int vidioc_s_parm(struct file *file, void *priv,
1209 struct v4l2_streamparm *p)
1210{
1211 struct em28xx_fh *fh = priv;
1212 struct em28xx *dev = fh->dev;
1213
1214 if (!dev->board.is_webcam)
1215 return -EINVAL;
1216
1217 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1218 return -EINVAL;
1219
1220 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1221}
1222
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001223static const char *iname[] = {
1224 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1225 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1226 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1227 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1228 [EM28XX_VMUX_SVIDEO] = "S-Video",
1229 [EM28XX_VMUX_TELEVISION] = "Television",
1230 [EM28XX_VMUX_CABLE] = "Cable TV",
1231 [EM28XX_VMUX_DVB] = "DVB",
1232 [EM28XX_VMUX_DEBUG] = "for debug only",
1233};
1234
1235static int vidioc_enum_input(struct file *file, void *priv,
1236 struct v4l2_input *i)
1237{
1238 struct em28xx_fh *fh = priv;
1239 struct em28xx *dev = fh->dev;
1240 unsigned int n;
1241
1242 n = i->index;
1243 if (n >= MAX_EM28XX_INPUT)
1244 return -EINVAL;
1245 if (0 == INPUT(n)->type)
1246 return -EINVAL;
1247
1248 i->index = n;
1249 i->type = V4L2_INPUT_TYPE_CAMERA;
1250
1251 strcpy(i->name, iname[INPUT(n)->type]);
1252
1253 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1254 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1255 i->type = V4L2_INPUT_TYPE_TUNER;
1256
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001257 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001258
1259 return 0;
1260}
1261
1262static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1263{
1264 struct em28xx_fh *fh = priv;
1265 struct em28xx *dev = fh->dev;
1266
1267 *i = dev->ctl_input;
1268
1269 return 0;
1270}
1271
1272static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1273{
1274 struct em28xx_fh *fh = priv;
1275 struct em28xx *dev = fh->dev;
1276 int rc;
1277
1278 rc = check_dev(dev);
1279 if (rc < 0)
1280 return rc;
1281
1282 if (i >= MAX_EM28XX_INPUT)
1283 return -EINVAL;
1284 if (0 == INPUT(i)->type)
1285 return -EINVAL;
1286
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001287 dev->ctl_input = i;
1288
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001289 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 return 0;
1291}
1292
1293static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1294{
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001298 if (!dev->audio_mode.has_audio)
1299 return -EINVAL;
1300
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001301 switch (a->index) {
1302 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001303 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001304 break;
1305 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001307 break;
1308 case EM28XX_AMUX_VIDEO2:
1309 strcpy(a->name, "Television alt");
1310 break;
1311 case EM28XX_AMUX_PHONE:
1312 strcpy(a->name, "Phone");
1313 break;
1314 case EM28XX_AMUX_MIC:
1315 strcpy(a->name, "Mic");
1316 break;
1317 case EM28XX_AMUX_CD:
1318 strcpy(a->name, "CD");
1319 break;
1320 case EM28XX_AMUX_AUX:
1321 strcpy(a->name, "Aux");
1322 break;
1323 case EM28XX_AMUX_PCM_OUT:
1324 strcpy(a->name, "PCM");
1325 break;
1326 default:
1327 return -EINVAL;
1328 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001329
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001330 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332
1333 return 0;
1334}
1335
1336static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1337{
1338 struct em28xx_fh *fh = priv;
1339 struct em28xx *dev = fh->dev;
1340
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001341
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001342 if (!dev->audio_mode.has_audio)
1343 return -EINVAL;
1344
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001345 if (a->index >= MAX_EM28XX_INPUT)
1346 return -EINVAL;
1347 if (0 == INPUT(a->index)->type)
1348 return -EINVAL;
1349
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001350 dev->ctl_ainput = INPUT(a->index)->amux;
1351 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001352
1353 if (!dev->ctl_aoutput)
1354 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001355
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 return 0;
1357}
1358
1359static int vidioc_queryctrl(struct file *file, void *priv,
1360 struct v4l2_queryctrl *qc)
1361{
1362 struct em28xx_fh *fh = priv;
1363 struct em28xx *dev = fh->dev;
1364 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001365 int rc;
1366
1367 rc = check_dev(dev);
1368 if (rc < 0)
1369 return rc;
1370
1371 memset(qc, 0, sizeof(*qc));
1372
1373 qc->id = id;
1374
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001375 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001376 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1377 rc = ac97_queryctrl(qc);
1378 if (!rc)
1379 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001380 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001381
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001382 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001383 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384
1385 if (qc->type)
1386 return 0;
1387 else
1388 return -EINVAL;
1389}
1390
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001391/*
1392 * FIXME: This is an indirect way to check if a control exists at a
1393 * subdev. Instead of that hack, maybe the better would be to change all
1394 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1395 */
1396static int check_subdev_ctrl(struct em28xx *dev, int id)
1397{
1398 struct v4l2_queryctrl qc;
1399
1400 memset(&qc, 0, sizeof(qc));
1401 qc.id = id;
1402
1403 /* enumerate V4L2 device controls */
1404 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1405
1406 if (qc.type)
1407 return 0;
1408 else
1409 return -EINVAL;
1410}
1411
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001412static int vidioc_g_ctrl(struct file *file, void *priv,
1413 struct v4l2_control *ctrl)
1414{
1415 struct em28xx_fh *fh = priv;
1416 struct em28xx *dev = fh->dev;
1417 int rc;
1418
1419 rc = check_dev(dev);
1420 if (rc < 0)
1421 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001422 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001423
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001424 /* Set an AC97 control */
1425 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1426 rc = ac97_get_ctrl(dev, ctrl);
1427 else
1428 rc = 1;
1429
1430 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1431 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001432 if (check_subdev_ctrl(dev, ctrl->id))
1433 return -EINVAL;
1434
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001435 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001436 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001437 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001438
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 return rc;
1440}
1441
1442static int vidioc_s_ctrl(struct file *file, void *priv,
1443 struct v4l2_control *ctrl)
1444{
1445 struct em28xx_fh *fh = priv;
1446 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001447 int rc;
1448
1449 rc = check_dev(dev);
1450 if (rc < 0)
1451 return rc;
1452
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001453 /* Set an AC97 control */
1454 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1455 rc = ac97_set_ctrl(dev, ctrl);
1456 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001457 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001458
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001459 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001460 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001461 rc = check_subdev_ctrl(dev, ctrl->id);
1462 if (!rc)
1463 v4l2_device_call_all(&dev->v4l2_dev, 0,
1464 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001465 /*
1466 * In the case of non-AC97 volume controls, we still need
1467 * to do some setups at em28xx, in order to mute/unmute
1468 * and to adjust audio volume. However, the value ranges
1469 * should be checked by the corresponding V4L subdriver.
1470 */
1471 switch (ctrl->id) {
1472 case V4L2_CID_AUDIO_MUTE:
1473 dev->mute = ctrl->value;
1474 rc = em28xx_audio_analog_set(dev);
1475 break;
1476 case V4L2_CID_AUDIO_VOLUME:
1477 dev->volume = ctrl->value;
1478 rc = em28xx_audio_analog_set(dev);
1479 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001480 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001481 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482}
1483
1484static int vidioc_g_tuner(struct file *file, void *priv,
1485 struct v4l2_tuner *t)
1486{
1487 struct em28xx_fh *fh = priv;
1488 struct em28xx *dev = fh->dev;
1489 int rc;
1490
1491 rc = check_dev(dev);
1492 if (rc < 0)
1493 return rc;
1494
1495 if (0 != t->index)
1496 return -EINVAL;
1497
1498 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001499 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001500
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001501 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001502 return 0;
1503}
1504
1505static int vidioc_s_tuner(struct file *file, void *priv,
1506 struct v4l2_tuner *t)
1507{
1508 struct em28xx_fh *fh = priv;
1509 struct em28xx *dev = fh->dev;
1510 int rc;
1511
1512 rc = check_dev(dev);
1513 if (rc < 0)
1514 return rc;
1515
1516 if (0 != t->index)
1517 return -EINVAL;
1518
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001519 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001520 return 0;
1521}
1522
1523static int vidioc_g_frequency(struct file *file, void *priv,
1524 struct v4l2_frequency *f)
1525{
1526 struct em28xx_fh *fh = priv;
1527 struct em28xx *dev = fh->dev;
1528
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001529 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001530 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001531 return 0;
1532}
1533
1534static int vidioc_s_frequency(struct file *file, void *priv,
1535 struct v4l2_frequency *f)
1536{
1537 struct em28xx_fh *fh = priv;
1538 struct em28xx *dev = fh->dev;
1539 int rc;
1540
1541 rc = check_dev(dev);
1542 if (rc < 0)
1543 return rc;
1544
1545 if (0 != f->tuner)
1546 return -EINVAL;
1547
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001548 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1549 return -EINVAL;
1550 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001551 return -EINVAL;
1552
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001553 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001554 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001556 return 0;
1557}
1558
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001559#ifdef CONFIG_VIDEO_ADV_DEBUG
1560static int em28xx_reg_len(int reg)
1561{
1562 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001563 case EM28XX_R40_AC97LSB:
1564 case EM28XX_R30_HSCALELOW:
1565 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001566 return 2;
1567 default:
1568 return 1;
1569 }
1570}
1571
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001572static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001573 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001574{
1575 struct em28xx_fh *fh = priv;
1576 struct em28xx *dev = fh->dev;
1577
1578 chip->ident = V4L2_IDENT_NONE;
1579 chip->revision = 0;
1580
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001581 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001582
1583 return 0;
1584}
1585
1586
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001587static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001588 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001589{
1590 struct em28xx_fh *fh = priv;
1591 struct em28xx *dev = fh->dev;
1592 int ret;
1593
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001594 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001595 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001596 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001597 if (ret < 0)
1598 return ret;
1599
1600 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001601 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001602 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001603 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001604 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001605 return 0;
1606 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001607 /* TODO: is this correct? */
1608 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1609 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001610 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001611 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001612 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001613 }
1614
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001615 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001616 reg->size = em28xx_reg_len(reg->reg);
1617 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001618 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001619
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001620 if (ret < 0)
1621 return ret;
1622
1623 reg->val = ret;
1624 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001625 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001626 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1627 reg->reg, (char *)&val, 2);
1628 if (ret < 0)
1629 return ret;
1630
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001631 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001632 }
1633
1634 return 0;
1635}
1636
1637static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001638 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001639{
1640 struct em28xx_fh *fh = priv;
1641 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001642 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001644 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001645 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001646 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001647 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001648 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001649 return 0;
1650 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001651 /* TODO: is this correct? */
1652 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1653 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001654 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001655 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001656 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001657 }
1658
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001659 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001660 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001661
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001662 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001663 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001664}
1665#endif
1666
1667
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001668static int vidioc_cropcap(struct file *file, void *priv,
1669 struct v4l2_cropcap *cc)
1670{
1671 struct em28xx_fh *fh = priv;
1672 struct em28xx *dev = fh->dev;
1673
1674 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1675 return -EINVAL;
1676
1677 cc->bounds.left = 0;
1678 cc->bounds.top = 0;
1679 cc->bounds.width = dev->width;
1680 cc->bounds.height = dev->height;
1681 cc->defrect = cc->bounds;
1682 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1683 cc->pixelaspect.denominator = 59;
1684
1685 return 0;
1686}
1687
1688static int vidioc_streamon(struct file *file, void *priv,
1689 enum v4l2_buf_type type)
1690{
1691 struct em28xx_fh *fh = priv;
1692 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001693 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001694
1695 rc = check_dev(dev);
1696 if (rc < 0)
1697 return rc;
1698
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001699 if (unlikely(type != fh->type))
1700 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001701
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001702 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1703 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001704
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001705 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001706 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001707
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001708 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1709 rc = videobuf_streamon(&fh->vb_vidq);
1710 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1711 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001712
1713 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001714}
1715
1716static int vidioc_streamoff(struct file *file, void *priv,
1717 enum v4l2_buf_type type)
1718{
1719 struct em28xx_fh *fh = priv;
1720 struct em28xx *dev = fh->dev;
1721 int rc;
1722
1723 rc = check_dev(dev);
1724 if (rc < 0)
1725 return rc;
1726
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001727 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1728 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001729 return -EINVAL;
1730 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001731 return -EINVAL;
1732
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001733 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1734 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001735
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001736 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001737 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1738 videobuf_streamoff(&fh->vb_vidq);
1739 res_free(fh, EM28XX_RESOURCE_VIDEO);
1740 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001741 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001742 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1743 videobuf_streamoff(&fh->vb_vbiq);
1744 res_free(fh, EM28XX_RESOURCE_VBI);
1745 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001746 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001747
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001748 return 0;
1749}
1750
1751static int vidioc_querycap(struct file *file, void *priv,
1752 struct v4l2_capability *cap)
1753{
1754 struct em28xx_fh *fh = priv;
1755 struct em28xx *dev = fh->dev;
1756
1757 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1758 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001759 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001760
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001761 cap->capabilities =
1762 V4L2_CAP_SLICED_VBI_CAPTURE |
1763 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001764 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1765
Devin Heitmueller04146142009-09-11 00:08:44 -03001766 if (dev->vbi_dev)
1767 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1768
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001769 if (dev->audio_mode.has_audio)
1770 cap->capabilities |= V4L2_CAP_AUDIO;
1771
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001772 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773 cap->capabilities |= V4L2_CAP_TUNER;
1774
1775 return 0;
1776}
1777
Hans Verkuil78b526a2008-05-28 12:16:41 -03001778static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001779 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001780{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001781 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001782 return -EINVAL;
1783
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001784 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1785 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001786
1787 return 0;
1788}
1789
1790/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001791static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 struct v4l2_format *f)
1793{
1794 struct em28xx_fh *fh = priv;
1795 struct em28xx *dev = fh->dev;
1796 int rc;
1797
1798 rc = check_dev(dev);
1799 if (rc < 0)
1800 return rc;
1801
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001802 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001803 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804
1805 if (f->fmt.sliced.service_set == 0)
1806 rc = -EINVAL;
1807
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001808 return rc;
1809}
1810
Hans Verkuil78b526a2008-05-28 12:16:41 -03001811static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001812 struct v4l2_format *f)
1813{
1814 struct em28xx_fh *fh = priv;
1815 struct em28xx *dev = fh->dev;
1816 int rc;
1817
1818 rc = check_dev(dev);
1819 if (rc < 0)
1820 return rc;
1821
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001822 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001823
1824 if (f->fmt.sliced.service_set == 0)
1825 return -EINVAL;
1826
1827 return 0;
1828}
1829
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001830/* RAW VBI ioctls */
1831
1832static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1833 struct v4l2_format *format)
1834{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001835 struct em28xx_fh *fh = priv;
1836 struct em28xx *dev = fh->dev;
1837
1838 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001839 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1840 format->fmt.vbi.offset = 0;
1841 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001842 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1843 format->fmt.vbi.count[0] = dev->vbi_height;
1844 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001845
1846 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001847 if (dev->norm & V4L2_STD_525_60) {
1848 /* NTSC */
1849 format->fmt.vbi.start[0] = 10;
1850 format->fmt.vbi.start[1] = 273;
1851 } else if (dev->norm & V4L2_STD_625_50) {
1852 /* PAL */
1853 format->fmt.vbi.start[0] = 6;
1854 format->fmt.vbi.start[1] = 318;
1855 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001856
1857 return 0;
1858}
1859
1860static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1861 struct v4l2_format *format)
1862{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001863 struct em28xx_fh *fh = priv;
1864 struct em28xx *dev = fh->dev;
1865
1866 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001867 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1868 format->fmt.vbi.offset = 0;
1869 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001870 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1871 format->fmt.vbi.count[0] = dev->vbi_height;
1872 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001873
1874 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001875 if (dev->norm & V4L2_STD_525_60) {
1876 /* NTSC */
1877 format->fmt.vbi.start[0] = 10;
1878 format->fmt.vbi.start[1] = 273;
1879 } else if (dev->norm & V4L2_STD_625_50) {
1880 /* PAL */
1881 format->fmt.vbi.start[0] = 6;
1882 format->fmt.vbi.start[1] = 318;
1883 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001884
1885 return 0;
1886}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001887
1888static int vidioc_reqbufs(struct file *file, void *priv,
1889 struct v4l2_requestbuffers *rb)
1890{
1891 struct em28xx_fh *fh = priv;
1892 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001893 int rc;
1894
1895 rc = check_dev(dev);
1896 if (rc < 0)
1897 return rc;
1898
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001899 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1900 return videobuf_reqbufs(&fh->vb_vidq, rb);
1901 else
1902 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001903}
1904
1905static int vidioc_querybuf(struct file *file, void *priv,
1906 struct v4l2_buffer *b)
1907{
1908 struct em28xx_fh *fh = priv;
1909 struct em28xx *dev = fh->dev;
1910 int rc;
1911
1912 rc = check_dev(dev);
1913 if (rc < 0)
1914 return rc;
1915
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001916 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1917 return videobuf_querybuf(&fh->vb_vidq, b);
1918 else {
1919 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1920 the videobuf framework, but we probably shouldn't be
1921 returning a buffer larger than that which was asked for.
1922 At a minimum, it causes a crash in zvbi since it does
1923 a memcpy based on the source buffer length */
1924 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001925 b->length = dev->vbi_width * dev->vbi_height * 2;
1926
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001927 return result;
1928 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001929}
1930
1931static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1932{
1933 struct em28xx_fh *fh = priv;
1934 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001935 int rc;
1936
1937 rc = check_dev(dev);
1938 if (rc < 0)
1939 return rc;
1940
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001941 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1942 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001943 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001944 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001945}
1946
1947static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1948{
1949 struct em28xx_fh *fh = priv;
1950 struct em28xx *dev = fh->dev;
1951 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001952
1953 rc = check_dev(dev);
1954 if (rc < 0)
1955 return rc;
1956
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001957 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1958 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1959 O_NONBLOCK);
1960 else
1961 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1962 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001963}
1964
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001965/* ----------------------------------------------------------- */
1966/* RADIO ESPECIFIC IOCTLS */
1967/* ----------------------------------------------------------- */
1968
1969static int radio_querycap(struct file *file, void *priv,
1970 struct v4l2_capability *cap)
1971{
1972 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1973
1974 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1975 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001976 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001977
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001978 cap->capabilities = V4L2_CAP_TUNER;
1979 return 0;
1980}
1981
1982static int radio_g_tuner(struct file *file, void *priv,
1983 struct v4l2_tuner *t)
1984{
1985 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1986
1987 if (unlikely(t->index > 0))
1988 return -EINVAL;
1989
1990 strcpy(t->name, "Radio");
1991 t->type = V4L2_TUNER_RADIO;
1992
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001993 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001994
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001995 return 0;
1996}
1997
1998static int radio_enum_input(struct file *file, void *priv,
1999 struct v4l2_input *i)
2000{
2001 if (i->index != 0)
2002 return -EINVAL;
2003 strcpy(i->name, "Radio");
2004 i->type = V4L2_INPUT_TYPE_TUNER;
2005
2006 return 0;
2007}
2008
2009static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2010{
2011 if (unlikely(a->index))
2012 return -EINVAL;
2013
2014 strcpy(a->name, "Radio");
2015 return 0;
2016}
2017
2018static int radio_s_tuner(struct file *file, void *priv,
2019 struct v4l2_tuner *t)
2020{
2021 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2022
2023 if (0 != t->index)
2024 return -EINVAL;
2025
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002026 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002027
2028 return 0;
2029}
2030
2031static int radio_s_audio(struct file *file, void *fh,
2032 struct v4l2_audio *a)
2033{
2034 return 0;
2035}
2036
2037static int radio_s_input(struct file *file, void *fh, unsigned int i)
2038{
2039 return 0;
2040}
2041
2042static int radio_queryctrl(struct file *file, void *priv,
2043 struct v4l2_queryctrl *qc)
2044{
2045 int i;
2046
2047 if (qc->id < V4L2_CID_BASE ||
2048 qc->id >= V4L2_CID_LASTP1)
2049 return -EINVAL;
2050
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002051 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2052 if (qc->id && qc->id == ac97_qctrl[i].id) {
2053 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002054 return 0;
2055 }
2056 }
2057
2058 return -EINVAL;
2059}
2060
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002061/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002062 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002063 * inits the device and starts isoc transfer
2064 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002065static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002066{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002067 int errCode = 0, radio = 0;
2068 struct video_device *vdev = video_devdata(filp);
2069 struct em28xx *dev = video_drvdata(filp);
2070 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002071 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002072 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002073
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002074 switch (vdev->vfl_type) {
2075 case VFL_TYPE_GRABBER:
2076 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2077 break;
2078 case VFL_TYPE_VBI:
2079 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2080 break;
2081 case VFL_TYPE_RADIO:
2082 radio = 1;
2083 break;
2084 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002085
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002086 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2087 video_device_node_name(vdev), v4l2_type_names[fh_type],
2088 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002089
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002090
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002091 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002092 if (!fh) {
2093 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2094 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002096 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002097 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002098 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002099 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002100
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002101 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002102 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002103 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002104 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002106 /* Needed, since GPIO might have disabled power of
2107 some i2c device
2108 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002109 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002110
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002111 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002112 if (fh->radio) {
2113 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002114 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002115 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116
2117 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002119 if (dev->progressive)
2120 field = V4L2_FIELD_NONE;
2121 else
2122 field = V4L2_FIELD_INTERLACED;
2123
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002124 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2125 NULL, &dev->slock,
2126 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002127 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002128
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002129 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2130 NULL, &dev->slock,
2131 V4L2_BUF_TYPE_VBI_CAPTURE,
2132 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002133 sizeof(struct em28xx_buffer), fh, &dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002134
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002135 return errCode;
2136}
2137
2138/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002139 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002140 * unregisters the v4l2,i2c and usb devices
2141 * called when the device gets disconected or at module unload
2142*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002143void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002144{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002146 /*FIXME: I2C IR should be disconnected */
2147
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002148 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002149 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002150 video_unregister_device(dev->radio_dev);
2151 else
2152 video_device_release(dev->radio_dev);
2153 dev->radio_dev = NULL;
2154 }
2155 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002156 em28xx_info("V4L2 device %s deregistered\n",
2157 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002158 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002159 video_unregister_device(dev->vbi_dev);
2160 else
2161 video_device_release(dev->vbi_dev);
2162 dev->vbi_dev = NULL;
2163 }
2164 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002165 em28xx_info("V4L2 device %s deregistered\n",
2166 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002167 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002168 video_unregister_device(dev->vdev);
2169 else
2170 video_device_release(dev->vdev);
2171 dev->vdev = NULL;
2172 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173}
2174
2175/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002176 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002177 * stops streaming and deallocates all resources allocated by the v4l2
2178 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002179 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002180static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002182 struct em28xx_fh *fh = filp->private_data;
2183 struct em28xx *dev = fh->dev;
2184 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002186 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002187
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002188 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002189 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002190 res_free(fh, EM28XX_RESOURCE_VIDEO);
2191 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002192
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002193 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2194 videobuf_stop(&fh->vb_vbiq);
2195 res_free(fh, EM28XX_RESOURCE_VBI);
2196 }
2197
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002198 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002199 /* the device is already disconnect,
2200 free the remaining resources */
2201 if (dev->state & DEV_DISCONNECTED) {
2202 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002203 kfree(dev);
2204 return 0;
2205 }
2206
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002207 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002208 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002209
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002210 /* do this before setting alternate! */
2211 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002212 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002213
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002214 /* set alternate 0 */
2215 dev->alt = 0;
2216 em28xx_videodbg("setting alternate 0\n");
2217 errCode = usb_set_interface(dev->udev, 0, 0);
2218 if (errCode < 0) {
2219 em28xx_errdev("cannot change alternate number to "
2220 "0 (error=%i)\n", errCode);
2221 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002223
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002224 videobuf_mmap_free(&fh->vb_vidq);
2225 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002226 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227 dev->users--;
2228 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002229 return 0;
2230}
2231
2232/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002233 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002234 * will allocate buffers when called for the first time
2235 */
2236static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002237em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002238 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002240 struct em28xx_fh *fh = filp->private_data;
2241 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002242 int rc;
2243
2244 rc = check_dev(dev);
2245 if (rc < 0)
2246 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002248 /* FIXME: read() is not prepared to allow changing the video
2249 resolution while streaming. Seems a bug at em28xx_set_fmt
2250 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002251
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002252 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002253 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2254 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002255
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002256 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2257 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002258 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002259
2260
2261 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002262 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2263 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002264
2265 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2266 filp->f_flags & O_NONBLOCK);
2267 }
2268
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002269 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270}
2271
2272/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002273 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002274 * will allocate buffers when called for the first time
2275 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002276static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002277{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002278 struct em28xx_fh *fh = filp->private_data;
2279 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002280 int rc;
2281
2282 rc = check_dev(dev);
2283 if (rc < 0)
2284 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002285
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002286 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2287 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2288 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002289 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002290 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2291 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2292 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002293 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002294 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002295 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002296 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297}
2298
2299/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002300 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002301 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002302static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002304 struct em28xx_fh *fh = filp->private_data;
2305 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002306 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002307
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002308 rc = check_dev(dev);
2309 if (rc < 0)
2310 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002311
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002312 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2313 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2314 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2315 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002316
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002317 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2318 (unsigned long)vma->vm_start,
2319 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2320 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002321
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002322 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002323}
2324
Hans Verkuilbec43662008-12-30 06:58:20 -03002325static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002326 .owner = THIS_MODULE,
2327 .open = em28xx_v4l2_open,
2328 .release = em28xx_v4l2_close,
2329 .read = em28xx_v4l2_read,
2330 .poll = em28xx_v4l2_poll,
2331 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002332 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002333};
2334
Hans Verkuila3998102008-07-21 02:57:38 -03002335static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002336 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002337 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2338 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2339 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2340 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002341 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2342 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002343 .vidioc_g_audio = vidioc_g_audio,
2344 .vidioc_s_audio = vidioc_s_audio,
2345 .vidioc_cropcap = vidioc_cropcap,
2346
Hans Verkuil78b526a2008-05-28 12:16:41 -03002347 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2348 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2349 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002350
2351 .vidioc_reqbufs = vidioc_reqbufs,
2352 .vidioc_querybuf = vidioc_querybuf,
2353 .vidioc_qbuf = vidioc_qbuf,
2354 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002355 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002356 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002357 .vidioc_g_parm = vidioc_g_parm,
2358 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002359 .vidioc_enum_input = vidioc_enum_input,
2360 .vidioc_g_input = vidioc_g_input,
2361 .vidioc_s_input = vidioc_s_input,
2362 .vidioc_queryctrl = vidioc_queryctrl,
2363 .vidioc_g_ctrl = vidioc_g_ctrl,
2364 .vidioc_s_ctrl = vidioc_s_ctrl,
2365 .vidioc_streamon = vidioc_streamon,
2366 .vidioc_streamoff = vidioc_streamoff,
2367 .vidioc_g_tuner = vidioc_g_tuner,
2368 .vidioc_s_tuner = vidioc_s_tuner,
2369 .vidioc_g_frequency = vidioc_g_frequency,
2370 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002371#ifdef CONFIG_VIDEO_ADV_DEBUG
2372 .vidioc_g_register = vidioc_g_register,
2373 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002374 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002375#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002376};
2377
2378static const struct video_device em28xx_video_template = {
2379 .fops = &em28xx_v4l_fops,
2380 .release = video_device_release,
2381 .ioctl_ops = &video_ioctl_ops,
2382
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002383 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002384 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002385};
2386
Hans Verkuilbec43662008-12-30 06:58:20 -03002387static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002388 .owner = THIS_MODULE,
2389 .open = em28xx_v4l2_open,
2390 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002391 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002392};
2393
2394static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002395 .vidioc_querycap = radio_querycap,
2396 .vidioc_g_tuner = radio_g_tuner,
2397 .vidioc_enum_input = radio_enum_input,
2398 .vidioc_g_audio = radio_g_audio,
2399 .vidioc_s_tuner = radio_s_tuner,
2400 .vidioc_s_audio = radio_s_audio,
2401 .vidioc_s_input = radio_s_input,
2402 .vidioc_queryctrl = radio_queryctrl,
2403 .vidioc_g_ctrl = vidioc_g_ctrl,
2404 .vidioc_s_ctrl = vidioc_s_ctrl,
2405 .vidioc_g_frequency = vidioc_g_frequency,
2406 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002407#ifdef CONFIG_VIDEO_ADV_DEBUG
2408 .vidioc_g_register = vidioc_g_register,
2409 .vidioc_s_register = vidioc_s_register,
2410#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002411};
2412
Hans Verkuila3998102008-07-21 02:57:38 -03002413static struct video_device em28xx_radio_template = {
2414 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002415 .fops = &radio_fops,
2416 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002417};
2418
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002419/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002421
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002422
Adrian Bunk532fe652008-01-28 22:10:48 -03002423static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002424 const struct video_device *template,
2425 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002426{
2427 struct video_device *vfd;
2428
2429 vfd = video_device_alloc();
2430 if (NULL == vfd)
2431 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002432
2433 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002434 vfd->v4l2_dev = &dev->v4l2_dev;
2435 vfd->release = video_device_release;
2436 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002437 vfd->lock = &dev->lock;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002438
2439 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2440 dev->name, type_name);
2441
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002442 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002443 return vfd;
2444}
2445
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002446int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002447{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002448 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002449 int ret;
2450
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002451 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2452 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002453
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002454 /* set default norm */
2455 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002456 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002457 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002458 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002459
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002460 /* Analog specific initialization */
2461 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002462 em28xx_set_video_format(dev, format[0].fourcc,
2463 norm_maxw(dev), norm_maxh(dev));
2464
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002465 video_mux(dev, dev->ctl_input);
2466
2467 /* Audio defaults */
2468 dev->mute = 1;
2469 dev->volume = 0x1f;
2470
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002471/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002472 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2473 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2474 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002475
2476 em28xx_set_outfmt(dev);
2477 em28xx_colorlevels_set_default(dev);
2478 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002479
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002480 /* allocate and fill video video_device struct */
2481 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2482 if (!dev->vdev) {
2483 em28xx_errdev("cannot allocate video_device.\n");
2484 return -ENODEV;
2485 }
2486
2487 /* register v4l2 video video_device */
2488 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2489 video_nr[dev->devno]);
2490 if (ret) {
2491 em28xx_errdev("unable to register video device (error=%i).\n",
2492 ret);
2493 return ret;
2494 }
2495
2496 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002497 if (em28xx_vbi_supported(dev) == 1) {
2498 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2499 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002500
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002501 /* register v4l2 vbi video_device */
2502 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2503 vbi_nr[dev->devno]);
2504 if (ret < 0) {
2505 em28xx_errdev("unable to register vbi device\n");
2506 return ret;
2507 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002508 }
2509
2510 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002511 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2512 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002513 if (!dev->radio_dev) {
2514 em28xx_errdev("cannot allocate video_device.\n");
2515 return -ENODEV;
2516 }
2517 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2518 radio_nr[dev->devno]);
2519 if (ret < 0) {
2520 em28xx_errdev("can't register radio device\n");
2521 return ret;
2522 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002523 em28xx_info("Registered radio device as %s\n",
2524 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002525 }
2526
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002527 em28xx_info("V4L2 video device registered as %s\n",
2528 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002529
2530 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002531 em28xx_info("V4L2 VBI device registered as %s\n",
2532 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002533
2534 return 0;
2535}