blob: 0fe20110bfd679404337acded2f2c20df7517d63 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030053#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
75
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020076static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030077static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020080module_param_array(video_nr, int, NULL, 0444);
81module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083MODULE_PARM_DESC(video_nr, "video device numbers");
84MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
85MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080086
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030087static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030088module_param(video_debug, int, 0644);
89MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080090
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030091/* supported video standards */
92static struct em28xx_fmt format[] = {
93 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030094 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030095 .fourcc = V4L2_PIX_FMT_YUYV,
96 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030097 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030098 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030099 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300102 .reg = EM28XX_OUTFMT_RGB_16_656,
103 }, {
104 .name = "8 bpp Bayer BGBG..GRGR",
105 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .depth = 8,
107 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 }, {
109 .name = "8 bpp Bayer GRGR..BGBG",
110 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .depth = 8,
112 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 }, {
114 .name = "8 bpp Bayer GBGB..RGRG",
115 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .depth = 8,
117 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 }, {
119 .name = "12 bpp YUV411",
120 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .depth = 12,
122 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300123 },
124};
125
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800126/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200127/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300128static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800129 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200130 .id = V4L2_CID_AUDIO_VOLUME,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Volume",
133 .minimum = 0x0,
134 .maximum = 0x1f,
135 .step = 0x1,
136 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300137 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300138 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200139 .id = V4L2_CID_AUDIO_MUTE,
140 .type = V4L2_CTRL_TYPE_BOOLEAN,
141 .name = "Mute",
142 .minimum = 0,
143 .maximum = 1,
144 .step = 1,
145 .default_value = 1,
146 .flags = 0,
147 }
148};
149
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300150/* ------------------------------------------------------------------
151 DMA and thread functions
152 ------------------------------------------------------------------*/
153
154/*
155 * Announces that a buffer were filled and request the next
156 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158 struct em28xx_dmaqueue *dma_q,
159 struct em28xx_buffer *buf)
160{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 /* Advice that buffer was filled */
162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
163 buf->vb.state = VIDEOBUF_DONE;
164 buf->vb.field_count++;
165 do_gettimeofday(&buf->vb.ts);
166
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300167 dev->isoc_ctl.vid_buf = NULL;
168
169 list_del(&buf->vb.queue);
170 wake_up(&buf->vb.done);
171}
172
173static inline void vbi_buffer_filled(struct em28xx *dev,
174 struct em28xx_dmaqueue *dma_q,
175 struct em28xx_buffer *buf)
176{
177 /* Advice that buffer was filled */
178 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179
180 buf->vb.state = VIDEOBUF_DONE;
181 buf->vb.field_count++;
182 do_gettimeofday(&buf->vb.ts);
183
184 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300185
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 list_del(&buf->vb.queue);
187 wake_up(&buf->vb.done);
188}
189
190/*
191 * Identify the buffer header type and properly handles
192 */
193static void em28xx_copy_video(struct em28xx *dev,
194 struct em28xx_dmaqueue *dma_q,
195 struct em28xx_buffer *buf,
196 unsigned char *p,
197 unsigned char *outp, unsigned long len)
198{
199 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300200 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202
203 if (dma_q->pos + len > buf->vb.size)
204 len = buf->vb.size - dma_q->pos;
205
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300206 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 em28xx_isocdbg("frame is not complete\n");
208 len += 4;
209 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300210 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211
212 startread = p;
213 remain = len;
214
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300215 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300217 else {
218 /* Interlaces two half frames */
219 if (buf->top_field)
220 fieldstart = outp;
221 else
222 fieldstart = outp + bytesperline;
223 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300225 linesdone = dma_q->pos / bytesperline;
226 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300227
228 if (dev->progressive)
229 offset = linesdone * bytesperline + currlinedone;
230 else
231 offset = linesdone * bytesperline * 2 + currlinedone;
232
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300233 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300234 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300235 lencopy = lencopy > remain ? remain : lencopy;
236
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300237 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300238 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300239 ((char *)startwrite + lencopy) -
240 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300241 remain = (char *)outp + buf->vb.size - (char *)startwrite;
242 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300243 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300244 if (lencopy <= 0)
245 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300246 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247
248 remain -= lencopy;
249
250 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300251 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300252 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300253 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300254 lencopy = remain;
255 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300256 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300257
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300258 if ((char *)startwrite + lencopy > (char *)outp +
259 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300260 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
261 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300262 ((char *)startwrite + lencopy) -
263 ((char *)outp + buf->vb.size));
264 lencopy = remain = (char *)outp + buf->vb.size -
265 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300266 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300267 if (lencopy <= 0)
268 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300269
270 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300271
272 remain -= lencopy;
273 }
274
275 dma_q->pos += len;
276}
277
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300278static void em28xx_copy_vbi(struct em28xx *dev,
279 struct em28xx_dmaqueue *dma_q,
280 struct em28xx_buffer *buf,
281 unsigned char *p,
282 unsigned char *outp, unsigned long len)
283{
284 void *startwrite, *startread;
285 int offset;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300286 int bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300287
288 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300289 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300290 return;
291 }
292
293 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300294 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300295 return;
296 }
297 if (buf == NULL) {
298 return;
299 }
300 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300301 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300302 return;
303 }
304 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300305 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300306 return;
307 }
308
309 if (dma_q->pos + len > buf->vb.size)
310 len = buf->vb.size - dma_q->pos;
311
312 if ((p[0] == 0x33 && p[1] == 0x95) ||
313 (p[0] == 0x88 && p[1] == 0x88)) {
314 /* Header field, advance past it */
315 p += 4;
316 } else {
317 len += 4;
318 }
319
320 startread = p;
321
322 startwrite = outp + dma_q->pos;
323 offset = dma_q->pos;
324
325 /* Make sure the bottom field populates the second half of the frame */
326 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300327 startwrite += bytesperline * dev->vbi_height;
328 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300329 }
330
331 memcpy(startwrite, startread, len);
332 dma_q->pos += len;
333}
334
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300335static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300336 int packet, int status)
337{
338 char *errmsg = "Unknown";
339
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300340 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341 case -ENOENT:
342 errmsg = "unlinked synchronuously";
343 break;
344 case -ECONNRESET:
345 errmsg = "unlinked asynchronuously";
346 break;
347 case -ENOSR:
348 errmsg = "Buffer error (overrun)";
349 break;
350 case -EPIPE:
351 errmsg = "Stalled (device not responding)";
352 break;
353 case -EOVERFLOW:
354 errmsg = "Babble (bad cable?)";
355 break;
356 case -EPROTO:
357 errmsg = "Bit-stuff error (bad cable?)";
358 break;
359 case -EILSEQ:
360 errmsg = "CRC/Timeout (could be anything)";
361 break;
362 case -ETIME:
363 errmsg = "Device does not respond";
364 break;
365 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300366 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300367 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
368 } else {
369 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
370 packet, status, errmsg);
371 }
372}
373
374/*
375 * video-buf generic routine to get the next available buffer
376 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300377static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300378 struct em28xx_buffer **buf)
379{
380 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300382
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300383 if (list_empty(&dma_q->active)) {
384 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300386 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300387 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300388 }
389
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300390 /* Get the next buffer */
391 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
392
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300393 /* Cleans up buffer - Usefull for testing for frame/URB loss */
394 outp = videobuf_to_vmalloc(&(*buf)->vb);
395 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300396
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300397 dev->isoc_ctl.vid_buf = *buf;
398
399 return;
400}
401
402/*
403 * video-buf generic routine to get the next available VBI buffer
404 */
405static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
406 struct em28xx_buffer **buf)
407{
408 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
409 char *outp;
410
411 if (list_empty(&dma_q->active)) {
412 em28xx_isocdbg("No active queue to serve\n");
413 dev->isoc_ctl.vbi_buf = NULL;
414 *buf = NULL;
415 return;
416 }
417
418 /* Get the next buffer */
419 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
420 /* Cleans up buffer - Usefull for testing for frame/URB loss */
421 outp = videobuf_to_vmalloc(&(*buf)->vb);
422 memset(outp, 0x00, (*buf)->vb.size);
423
424 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300425
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300426 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300427}
428
429/*
430 * Controls the isoc copy of each urb packet
431 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300432static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300433{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300434 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300435 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300436 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300438 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439
440 if (!dev)
441 return 0;
442
443 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
444 return 0;
445
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300446 if (urb->status < 0) {
447 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300448 if (urb->status == -ENOENT)
449 return 0;
450 }
451
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300452 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300453 if (buf != NULL)
454 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300455
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 for (i = 0; i < urb->number_of_packets; i++) {
457 int status = urb->iso_frame_desc[i].status;
458
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300459 if (status < 0) {
460 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 if (urb->iso_frame_desc[i].status != -EPROTO)
462 continue;
463 }
464
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466
467 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300468 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300469 continue;
470 }
471 if (urb->iso_frame_desc[i].actual_length >
472 dev->max_pkt_size) {
473 em28xx_isocdbg("packet bigger than packet size");
474 continue;
475 }
476
477 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300478
479 /* FIXME: incomplete buffer checks where removed to make
480 logic simpler. Impacts of those changes should be evaluated
481 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300482 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
483 em28xx_isocdbg("VBI HEADER!!!\n");
484 /* FIXME: Should add vbi copy */
485 continue;
486 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300487 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300488 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300489 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300490
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300491 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300492 if (buf != NULL)
493 buffer_filled(dev, dma_q, buf);
494 get_next_buf(dma_q, &buf);
495 if (buf == NULL)
496 outp = NULL;
497 else
498 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300499 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300500
501 if (buf != NULL) {
502 if (p[2] & 1)
503 buf->top_field = 0;
504 else
505 buf->top_field = 1;
506 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300507
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300509 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300510 if (buf != NULL)
511 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300512 }
513 return rc;
514}
515
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300516/* Version of isoc handler that takes into account a mixture of video and
517 VBI data */
518static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
519{
520 struct em28xx_buffer *buf, *vbi_buf;
521 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300522 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300523 unsigned char *outp = NULL;
524 unsigned char *vbioutp = NULL;
525 int i, len = 0, rc = 1;
526 unsigned char *p;
527 int vbi_size;
528
529 if (!dev)
530 return 0;
531
532 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
533 return 0;
534
535 if (urb->status < 0) {
536 print_err_status(dev, -1, urb->status);
537 if (urb->status == -ENOENT)
538 return 0;
539 }
540
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300541 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300542 if (buf != NULL)
543 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300544
545 vbi_buf = dev->isoc_ctl.vbi_buf;
546 if (vbi_buf != NULL)
547 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
548
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300549 for (i = 0; i < urb->number_of_packets; i++) {
550 int status = urb->iso_frame_desc[i].status;
551
552 if (status < 0) {
553 print_err_status(dev, i, status);
554 if (urb->iso_frame_desc[i].status != -EPROTO)
555 continue;
556 }
557
558 len = urb->iso_frame_desc[i].actual_length - 4;
559
560 if (urb->iso_frame_desc[i].actual_length <= 0) {
561 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
562 continue;
563 }
564 if (urb->iso_frame_desc[i].actual_length >
565 dev->max_pkt_size) {
566 em28xx_isocdbg("packet bigger than packet size");
567 continue;
568 }
569
570 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
571
572 /* capture type 0 = vbi start
573 capture type 1 = video start
574 capture type 2 = video in progress */
575 if (p[0] == 0x33 && p[1] == 0x95) {
576 dev->capture_type = 0;
577 dev->vbi_read = 0;
578 em28xx_isocdbg("VBI START HEADER!!!\n");
579 dev->cur_field = p[2];
580 }
581
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300582 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300583
584 if (dev->capture_type == 0) {
585 if (dev->vbi_read >= vbi_size) {
586 /* We've already read all the VBI data, so
587 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300588 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300589 } else if ((dev->vbi_read + len) < vbi_size) {
590 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300591 if (dev->vbi_read == 0 &&
592 (!(dev->cur_field & 1))) {
593 /* Brand new frame */
594 if (vbi_buf != NULL)
595 vbi_buffer_filled(dev,
596 vbi_dma_q,
597 vbi_buf);
598 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
599 if (vbi_buf == NULL)
600 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300601 else
602 vbioutp = videobuf_to_vmalloc(
603 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300604 }
605
606 if (dev->vbi_read == 0) {
607 vbi_dma_q->pos = 0;
608 if (vbi_buf != NULL) {
609 if (dev->cur_field & 1)
610 vbi_buf->top_field = 0;
611 else
612 vbi_buf->top_field = 1;
613 }
614 }
615
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300616 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300617 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
618 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300619 } else {
620 /* Some of this frame is VBI data and some is
621 video data */
622 int vbi_data_len = vbi_size - dev->vbi_read;
623 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300624 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
625 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300626 dev->capture_type = 1;
627 p += vbi_data_len;
628 len -= vbi_data_len;
629 }
630 }
631
632 if (dev->capture_type == 1) {
633 dev->capture_type = 2;
634 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
635 len, (p[2] & 1) ? "odd" : "even");
636
637 if (dev->progressive || !(dev->cur_field & 1)) {
638 if (buf != NULL)
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
641 if (buf == NULL)
642 outp = NULL;
643 else
644 outp = videobuf_to_vmalloc(&buf->vb);
645 }
646 if (buf != NULL) {
647 if (dev->cur_field & 1)
648 buf->top_field = 0;
649 else
650 buf->top_field = 1;
651 }
652
653 dma_q->pos = 0;
654 }
655 if (buf != NULL && dev->capture_type == 2)
656 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
657 }
658 return rc;
659}
660
661
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300662/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300663 Videobuf operations
664 ------------------------------------------------------------------*/
665
666static int
667buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
668{
669 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300670 struct em28xx *dev = fh->dev;
671 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300672
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300673 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
674 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300675
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300676 if (0 == *count)
677 *count = EM28XX_DEF_BUF;
678
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300679 if (*count < EM28XX_MIN_BUF)
680 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300682 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300683 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300684 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300685 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300686
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300687 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300688
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689 return 0;
690}
691
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300692/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300693static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
694{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300695 struct em28xx_fh *fh = vq->priv_data;
696 struct em28xx *dev = fh->dev;
697 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698 if (in_interrupt())
699 BUG();
700
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300701 /* We used to wait for the buffer to finish here, but this didn't work
702 because, as we were keeping the state as VIDEOBUF_QUEUED,
703 videobuf_queue_cancel marked it as finished for us.
704 (Also, it could wedge forever if the hardware was misconfigured.)
705
706 This should be safe; by the time we get here, the buffer isn't
707 queued anymore. If we ever start marking the buffers as
708 VIDEOBUF_ACTIVE, it won't be, though.
709 */
710 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300711 if (dev->isoc_ctl.vid_buf == buf)
712 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300713 spin_unlock_irqrestore(&dev->slock, flags);
714
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
717}
718
719static int
720buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
721 enum v4l2_field field)
722{
723 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300724 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300725 struct em28xx *dev = fh->dev;
726 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300727
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300728 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
729 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300730
731 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
732 return -EINVAL;
733
Brandon Philips05612972008-04-13 14:57:01 -0300734 buf->vb.width = dev->width;
735 buf->vb.height = dev->height;
736 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300737
738 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300739 rc = videobuf_iolock(vq, &buf->vb, NULL);
740 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300742 }
743
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300745 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746
747 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300748 if (em28xx_vbi_supported(dev) == 1)
749 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
750 EM28XX_NUM_BUFS,
751 dev->max_pkt_size,
752 em28xx_isoc_copy_vbi);
753 else
754 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
755 EM28XX_NUM_BUFS,
756 dev->max_pkt_size,
757 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300758 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759 goto fail;
760 }
761
762 buf->vb.state = VIDEOBUF_PREPARED;
763 return 0;
764
765fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300766 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 return rc;
768}
769
770static void
771buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
772{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300773 struct em28xx_buffer *buf = container_of(vb,
774 struct em28xx_buffer,
775 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300776 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300777 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300778 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300779
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300780 buf->vb.state = VIDEOBUF_QUEUED;
781 list_add_tail(&buf->vb.queue, &vidq->active);
782
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783}
784
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300785static void buffer_release(struct videobuf_queue *vq,
786 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300787{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300788 struct em28xx_buffer *buf = container_of(vb,
789 struct em28xx_buffer,
790 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300791 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300792 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300793
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300794 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300796 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797}
798
799static struct videobuf_queue_ops em28xx_video_qops = {
800 .buf_setup = buffer_setup,
801 .buf_prepare = buffer_prepare,
802 .buf_queue = buffer_queue,
803 .buf_release = buffer_release,
804};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800805
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300806/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800807
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800808static void video_mux(struct em28xx *dev, int index)
809{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800810 dev->ctl_input = index;
811 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300812 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800813
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300814 if (!dev->ctl_aoutput)
815 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
816
Hans Verkuil5325b422009-04-02 11:26:22 -0300817 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
818 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800819
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300820 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300821 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300822 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
823 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300824 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300825 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300826 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
827 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800828 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300829
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300830 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300831 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
832 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300833 }
834
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300835 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800836}
837
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300838/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300839static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300840{
841 struct em28xx *dev = fh->dev;
842
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300843 if (fh->resources & bit)
844 /* have it already allocated */
845 return 1;
846
847 /* is it free? */
848 mutex_lock(&dev->lock);
849 if (dev->resources & bit) {
850 /* no, someone else uses it */
851 mutex_unlock(&dev->lock);
852 return 0;
853 }
854 /* it's free, grab it */
855 fh->resources |= bit;
856 dev->resources |= bit;
857 em28xx_videodbg("res: get %d\n", bit);
858 mutex_unlock(&dev->lock);
859 return 1;
860}
861
862static int res_check(struct em28xx_fh *fh, unsigned int bit)
863{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300864 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300865}
866
867static int res_locked(struct em28xx *dev, unsigned int bit)
868{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300869 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300870}
871
872static void res_free(struct em28xx_fh *fh, unsigned int bits)
873{
874 struct em28xx *dev = fh->dev;
875
876 BUG_ON((fh->resources & bits) != bits);
877
878 mutex_lock(&dev->lock);
879 fh->resources &= ~bits;
880 dev->resources &= ~bits;
881 em28xx_videodbg("res: put %d\n", bits);
882 mutex_unlock(&dev->lock);
883}
884
885static int get_ressource(struct em28xx_fh *fh)
886{
887 switch (fh->type) {
888 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
889 return EM28XX_RESOURCE_VIDEO;
890 case V4L2_BUF_TYPE_VBI_CAPTURE:
891 return EM28XX_RESOURCE_VBI;
892 default:
893 BUG();
894 return 0;
895 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300896}
897
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800898/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300899 * ac97_queryctrl()
900 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300901 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300902static int ac97_queryctrl(struct v4l2_queryctrl *qc)
903{
904 int i;
905
906 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
907 if (qc->id && qc->id == ac97_qctrl[i].id) {
908 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
909 return 0;
910 }
911 }
912
913 /* Control is not ac97 related */
914 return 1;
915}
916
917/*
918 * ac97_get_ctrl()
919 * return the current values for ac97 mute and volume
920 */
921static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300922{
923 switch (ctrl->id) {
924 case V4L2_CID_AUDIO_MUTE:
925 ctrl->value = dev->mute;
926 return 0;
927 case V4L2_CID_AUDIO_VOLUME:
928 ctrl->value = dev->volume;
929 return 0;
930 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300931 /* Control is not ac97 related */
932 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933 }
934}
935
936/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300937 * ac97_set_ctrl()
938 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300940static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300942 int i;
943
944 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
945 if (ctrl->id == ac97_qctrl[i].id)
946 goto handle;
947
948 /* Announce that hasn't handle it */
949 return 1;
950
951handle:
952 if (ctrl->value < ac97_qctrl[i].minimum ||
953 ctrl->value > ac97_qctrl[i].maximum)
954 return -ERANGE;
955
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 switch (ctrl->id) {
957 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300958 dev->mute = ctrl->value;
959 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960 case V4L2_CID_AUDIO_VOLUME:
961 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300962 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300964
965 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966}
967
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968static int check_dev(struct em28xx *dev)
969{
970 if (dev->state & DEV_DISCONNECTED) {
971 em28xx_errdev("v4l2 ioctl: device not present\n");
972 return -ENODEV;
973 }
974
975 if (dev->state & DEV_MISCONFIGURED) {
976 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
977 "close and open it again\n");
978 return -EIO;
979 }
980 return 0;
981}
982
983static void get_scale(struct em28xx *dev,
984 unsigned int width, unsigned int height,
985 unsigned int *hscale, unsigned int *vscale)
986{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300987 unsigned int maxw = norm_maxw(dev);
988 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300989
990 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
991 if (*hscale >= 0x4000)
992 *hscale = 0x3fff;
993
994 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
995 if (*vscale >= 0x4000)
996 *vscale = 0x3fff;
997}
998
999/* ------------------------------------------------------------------
1000 IOCTL vidioc handling
1001 ------------------------------------------------------------------*/
1002
Hans Verkuil78b526a2008-05-28 12:16:41 -03001003static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 struct v4l2_format *f)
1005{
1006 struct em28xx_fh *fh = priv;
1007 struct em28xx *dev = fh->dev;
1008
1009 mutex_lock(&dev->lock);
1010
1011 f->fmt.pix.width = dev->width;
1012 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001013 f->fmt.pix.pixelformat = dev->format->fourcc;
1014 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001015 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001016 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1017
1018 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001019 if (dev->progressive)
1020 f->fmt.pix.field = V4L2_FIELD_NONE;
1021 else
1022 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1024
1025 mutex_unlock(&dev->lock);
1026 return 0;
1027}
1028
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001029static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1030{
1031 unsigned int i;
1032
1033 for (i = 0; i < ARRAY_SIZE(format); i++)
1034 if (format[i].fourcc == fourcc)
1035 return &format[i];
1036
1037 return NULL;
1038}
1039
Hans Verkuil78b526a2008-05-28 12:16:41 -03001040static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001041 struct v4l2_format *f)
1042{
1043 struct em28xx_fh *fh = priv;
1044 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001045 unsigned int width = f->fmt.pix.width;
1046 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001047 unsigned int maxw = norm_maxw(dev);
1048 unsigned int maxh = norm_maxh(dev);
1049 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001050 struct em28xx_fmt *fmt;
1051
1052 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1053 if (!fmt) {
1054 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1055 f->fmt.pix.pixelformat);
1056 return -EINVAL;
1057 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001058
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001059 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001060 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001061 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1062 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001063 } else {
1064 /* width must even because of the YUYV format
1065 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001066 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1067 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068 }
1069
1070 get_scale(dev, width, height, &hscale, &vscale);
1071
1072 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1073 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1074
1075 f->fmt.pix.width = width;
1076 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001077 f->fmt.pix.pixelformat = fmt->fourcc;
1078 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1079 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001080 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001081 if (dev->progressive)
1082 f->fmt.pix.field = V4L2_FIELD_NONE;
1083 else
1084 f->fmt.pix.field = dev->interlaced ?
1085 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001086
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001087 return 0;
1088}
1089
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001090static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1091 unsigned width, unsigned height)
1092{
1093 struct em28xx_fmt *fmt;
1094
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001095 fmt = format_by_fourcc(fourcc);
1096 if (!fmt)
1097 return -EINVAL;
1098
1099 dev->format = fmt;
1100 dev->width = width;
1101 dev->height = height;
1102
1103 /* set new image size */
1104 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1105
1106 em28xx_set_alternate(dev);
1107 em28xx_resolution_set(dev);
1108
1109 return 0;
1110}
1111
Hans Verkuil78b526a2008-05-28 12:16:41 -03001112static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001113 struct v4l2_format *f)
1114{
1115 struct em28xx_fh *fh = priv;
1116 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001117 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001118
1119 rc = check_dev(dev);
1120 if (rc < 0)
1121 return rc;
1122
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001123 mutex_lock(&dev->lock);
1124
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001125 vidioc_try_fmt_vid_cap(file, priv, f);
1126
Brandon Philips05612972008-04-13 14:57:01 -03001127 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1128 em28xx_errdev("%s queue busy\n", __func__);
1129 rc = -EBUSY;
1130 goto out;
1131 }
1132
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001133 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1134 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -03001135
1136out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001137 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -03001138 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001139}
1140
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001141static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1142{
1143 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001145 int rc;
1146
1147 rc = check_dev(dev);
1148 if (rc < 0)
1149 return rc;
1150
1151 *norm = dev->norm;
1152
1153 return 0;
1154}
1155
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001156static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157{
1158 struct em28xx_fh *fh = priv;
1159 struct em28xx *dev = fh->dev;
1160 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001161 int rc;
1162
1163 rc = check_dev(dev);
1164 if (rc < 0)
1165 return rc;
1166
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001167 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001168 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169
1170 /* Adjusts width/height, if needed */
1171 f.fmt.pix.width = dev->width;
1172 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001173 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001174
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001175 /* set new image size */
1176 dev->width = f.fmt.pix.width;
1177 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001178 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1179
1180 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001181 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001182
1183 mutex_unlock(&dev->lock);
1184 return 0;
1185}
1186
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001187static int vidioc_g_parm(struct file *file, void *priv,
1188 struct v4l2_streamparm *p)
1189{
1190 struct em28xx_fh *fh = priv;
1191 struct em28xx *dev = fh->dev;
1192 int rc = 0;
1193
1194 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1195 return -EINVAL;
1196
1197 if (dev->board.is_webcam)
1198 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1199 video, g_parm, p);
1200 else
1201 v4l2_video_std_frame_period(dev->norm,
1202 &p->parm.capture.timeperframe);
1203
1204 return rc;
1205}
1206
1207static int vidioc_s_parm(struct file *file, void *priv,
1208 struct v4l2_streamparm *p)
1209{
1210 struct em28xx_fh *fh = priv;
1211 struct em28xx *dev = fh->dev;
1212
1213 if (!dev->board.is_webcam)
1214 return -EINVAL;
1215
1216 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL;
1218
1219 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1220}
1221
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001222static const char *iname[] = {
1223 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1224 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1225 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1226 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1227 [EM28XX_VMUX_SVIDEO] = "S-Video",
1228 [EM28XX_VMUX_TELEVISION] = "Television",
1229 [EM28XX_VMUX_CABLE] = "Cable TV",
1230 [EM28XX_VMUX_DVB] = "DVB",
1231 [EM28XX_VMUX_DEBUG] = "for debug only",
1232};
1233
1234static int vidioc_enum_input(struct file *file, void *priv,
1235 struct v4l2_input *i)
1236{
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1239 unsigned int n;
1240
1241 n = i->index;
1242 if (n >= MAX_EM28XX_INPUT)
1243 return -EINVAL;
1244 if (0 == INPUT(n)->type)
1245 return -EINVAL;
1246
1247 i->index = n;
1248 i->type = V4L2_INPUT_TYPE_CAMERA;
1249
1250 strcpy(i->name, iname[INPUT(n)->type]);
1251
1252 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1253 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1254 i->type = V4L2_INPUT_TYPE_TUNER;
1255
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001256 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001257
1258 return 0;
1259}
1260
1261static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1262{
1263 struct em28xx_fh *fh = priv;
1264 struct em28xx *dev = fh->dev;
1265
1266 *i = dev->ctl_input;
1267
1268 return 0;
1269}
1270
1271static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1272{
1273 struct em28xx_fh *fh = priv;
1274 struct em28xx *dev = fh->dev;
1275 int rc;
1276
1277 rc = check_dev(dev);
1278 if (rc < 0)
1279 return rc;
1280
1281 if (i >= MAX_EM28XX_INPUT)
1282 return -EINVAL;
1283 if (0 == INPUT(i)->type)
1284 return -EINVAL;
1285
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001286 dev->ctl_input = i;
1287
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001288 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001289 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 mutex_unlock(&dev->lock);
1291 return 0;
1292}
1293
1294static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1295{
1296 struct em28xx_fh *fh = priv;
1297 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001298
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001299 if (!dev->audio_mode.has_audio)
1300 return -EINVAL;
1301
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001302 switch (a->index) {
1303 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001304 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001305 break;
1306 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001308 break;
1309 case EM28XX_AMUX_VIDEO2:
1310 strcpy(a->name, "Television alt");
1311 break;
1312 case EM28XX_AMUX_PHONE:
1313 strcpy(a->name, "Phone");
1314 break;
1315 case EM28XX_AMUX_MIC:
1316 strcpy(a->name, "Mic");
1317 break;
1318 case EM28XX_AMUX_CD:
1319 strcpy(a->name, "CD");
1320 break;
1321 case EM28XX_AMUX_AUX:
1322 strcpy(a->name, "Aux");
1323 break;
1324 case EM28XX_AMUX_PCM_OUT:
1325 strcpy(a->name, "PCM");
1326 break;
1327 default:
1328 return -EINVAL;
1329 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001330
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001331 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001333
1334 return 0;
1335}
1336
1337static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1338{
1339 struct em28xx_fh *fh = priv;
1340 struct em28xx *dev = fh->dev;
1341
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001342
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001343 if (!dev->audio_mode.has_audio)
1344 return -EINVAL;
1345
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001346 if (a->index >= MAX_EM28XX_INPUT)
1347 return -EINVAL;
1348 if (0 == INPUT(a->index)->type)
1349 return -EINVAL;
1350
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001351 mutex_lock(&dev->lock);
1352
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001353 dev->ctl_ainput = INPUT(a->index)->amux;
1354 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001355
1356 if (!dev->ctl_aoutput)
1357 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001358
1359 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001360 return 0;
1361}
1362
1363static int vidioc_queryctrl(struct file *file, void *priv,
1364 struct v4l2_queryctrl *qc)
1365{
1366 struct em28xx_fh *fh = priv;
1367 struct em28xx *dev = fh->dev;
1368 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001369 int rc;
1370
1371 rc = check_dev(dev);
1372 if (rc < 0)
1373 return rc;
1374
1375 memset(qc, 0, sizeof(*qc));
1376
1377 qc->id = id;
1378
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001379 /* enumberate AC97 controls */
1380 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1381 rc = ac97_queryctrl(qc);
1382 if (!rc)
1383 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001385
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001386 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001388 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389 mutex_unlock(&dev->lock);
1390
1391 if (qc->type)
1392 return 0;
1393 else
1394 return -EINVAL;
1395}
1396
1397static int vidioc_g_ctrl(struct file *file, void *priv,
1398 struct v4l2_control *ctrl)
1399{
1400 struct em28xx_fh *fh = priv;
1401 struct em28xx *dev = fh->dev;
1402 int rc;
1403
1404 rc = check_dev(dev);
1405 if (rc < 0)
1406 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001407 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001408
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001409 mutex_lock(&dev->lock);
1410
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001411 /* Set an AC97 control */
1412 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1413 rc = ac97_get_ctrl(dev, ctrl);
1414 else
1415 rc = 1;
1416
1417 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1418 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001419 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001420 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001421 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001422
1423 mutex_unlock(&dev->lock);
1424 return rc;
1425}
1426
1427static int vidioc_s_ctrl(struct file *file, void *priv,
1428 struct v4l2_control *ctrl)
1429{
1430 struct em28xx_fh *fh = priv;
1431 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432 int rc;
1433
1434 rc = check_dev(dev);
1435 if (rc < 0)
1436 return rc;
1437
1438 mutex_lock(&dev->lock);
1439
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001440 /* Set an AC97 control */
1441 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1442 rc = ac97_set_ctrl(dev, ctrl);
1443 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001444 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001445
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001446 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001447 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001448 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001449
1450 /*
1451 * In the case of non-AC97 volume controls, we still need
1452 * to do some setups at em28xx, in order to mute/unmute
1453 * and to adjust audio volume. However, the value ranges
1454 * should be checked by the corresponding V4L subdriver.
1455 */
1456 switch (ctrl->id) {
1457 case V4L2_CID_AUDIO_MUTE:
1458 dev->mute = ctrl->value;
1459 rc = em28xx_audio_analog_set(dev);
1460 break;
1461 case V4L2_CID_AUDIO_VOLUME:
1462 dev->volume = ctrl->value;
1463 rc = em28xx_audio_analog_set(dev);
1464 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001465 }
1466
1467 mutex_unlock(&dev->lock);
1468 return rc;
1469}
1470
1471static int vidioc_g_tuner(struct file *file, void *priv,
1472 struct v4l2_tuner *t)
1473{
1474 struct em28xx_fh *fh = priv;
1475 struct em28xx *dev = fh->dev;
1476 int rc;
1477
1478 rc = check_dev(dev);
1479 if (rc < 0)
1480 return rc;
1481
1482 if (0 != t->index)
1483 return -EINVAL;
1484
1485 strcpy(t->name, "Tuner");
1486
1487 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001488 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001489 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001490
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001491 return 0;
1492}
1493
1494static int vidioc_s_tuner(struct file *file, void *priv,
1495 struct v4l2_tuner *t)
1496{
1497 struct em28xx_fh *fh = priv;
1498 struct em28xx *dev = fh->dev;
1499 int rc;
1500
1501 rc = check_dev(dev);
1502 if (rc < 0)
1503 return rc;
1504
1505 if (0 != t->index)
1506 return -EINVAL;
1507
1508 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001509 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001510 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001511
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001512 return 0;
1513}
1514
1515static int vidioc_g_frequency(struct file *file, void *priv,
1516 struct v4l2_frequency *f)
1517{
1518 struct em28xx_fh *fh = priv;
1519 struct em28xx *dev = fh->dev;
1520
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001521 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001522 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001523 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001524 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001525
1526 return 0;
1527}
1528
1529static int vidioc_s_frequency(struct file *file, void *priv,
1530 struct v4l2_frequency *f)
1531{
1532 struct em28xx_fh *fh = priv;
1533 struct em28xx *dev = fh->dev;
1534 int rc;
1535
1536 rc = check_dev(dev);
1537 if (rc < 0)
1538 return rc;
1539
1540 if (0 != f->tuner)
1541 return -EINVAL;
1542
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001543 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1544 return -EINVAL;
1545 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546 return -EINVAL;
1547
1548 mutex_lock(&dev->lock);
1549
1550 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001551 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001552
1553 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001554
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555 return 0;
1556}
1557
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001558#ifdef CONFIG_VIDEO_ADV_DEBUG
1559static int em28xx_reg_len(int reg)
1560{
1561 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001562 case EM28XX_R40_AC97LSB:
1563 case EM28XX_R30_HSCALELOW:
1564 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001565 return 2;
1566 default:
1567 return 1;
1568 }
1569}
1570
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001571static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001572 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001573{
1574 struct em28xx_fh *fh = priv;
1575 struct em28xx *dev = fh->dev;
1576
1577 chip->ident = V4L2_IDENT_NONE;
1578 chip->revision = 0;
1579
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001580 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001581
1582 return 0;
1583}
1584
1585
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001586static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001587 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001588{
1589 struct em28xx_fh *fh = priv;
1590 struct em28xx *dev = fh->dev;
1591 int ret;
1592
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001593 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001594 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001595 mutex_lock(&dev->lock);
1596 ret = em28xx_read_ac97(dev, reg->reg);
1597 mutex_unlock(&dev->lock);
1598 if (ret < 0)
1599 return ret;
1600
1601 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001602 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001603 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001604 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001605 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001606 return 0;
1607 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001608 /* TODO: is this correct? */
1609 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1610 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001611 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001612 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001613 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001614 }
1615
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001616 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001617 reg->size = em28xx_reg_len(reg->reg);
1618 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001619 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001620 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001621 mutex_unlock(&dev->lock);
1622
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001623 if (ret < 0)
1624 return ret;
1625
1626 reg->val = ret;
1627 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001628 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001629 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001630 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1631 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001632 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001633 if (ret < 0)
1634 return ret;
1635
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001636 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001637 }
1638
1639 return 0;
1640}
1641
1642static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001643 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001644{
1645 struct em28xx_fh *fh = priv;
1646 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001647 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001648 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001649
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001650 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001651 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001652 mutex_lock(&dev->lock);
1653 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1654 mutex_unlock(&dev->lock);
1655
1656 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001657 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001658 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001659 return 0;
1660 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001661 /* TODO: is this correct? */
1662 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1663 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001664 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001665 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001666 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001667 }
1668
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001669 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001670 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001671
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001672 mutex_lock(&dev->lock);
1673 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1674 em28xx_reg_len(reg->reg));
1675 mutex_unlock(&dev->lock);
1676
1677 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001678}
1679#endif
1680
1681
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001682static int vidioc_cropcap(struct file *file, void *priv,
1683 struct v4l2_cropcap *cc)
1684{
1685 struct em28xx_fh *fh = priv;
1686 struct em28xx *dev = fh->dev;
1687
1688 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1689 return -EINVAL;
1690
1691 cc->bounds.left = 0;
1692 cc->bounds.top = 0;
1693 cc->bounds.width = dev->width;
1694 cc->bounds.height = dev->height;
1695 cc->defrect = cc->bounds;
1696 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1697 cc->pixelaspect.denominator = 59;
1698
1699 return 0;
1700}
1701
1702static int vidioc_streamon(struct file *file, void *priv,
1703 enum v4l2_buf_type type)
1704{
1705 struct em28xx_fh *fh = priv;
1706 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001707 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001708
1709 rc = check_dev(dev);
1710 if (rc < 0)
1711 return rc;
1712
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001713 if (unlikely(type != fh->type))
1714 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001715
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001716 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1717 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001718
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001719 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001720 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001721
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001722 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1723 rc = videobuf_streamon(&fh->vb_vidq);
1724 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1725 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001726
1727 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001728}
1729
1730static int vidioc_streamoff(struct file *file, void *priv,
1731 enum v4l2_buf_type type)
1732{
1733 struct em28xx_fh *fh = priv;
1734 struct em28xx *dev = fh->dev;
1735 int rc;
1736
1737 rc = check_dev(dev);
1738 if (rc < 0)
1739 return rc;
1740
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001741 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1742 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001743 return -EINVAL;
1744 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001745 return -EINVAL;
1746
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001747 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1748 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001749
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001750 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001751 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001752 res_free(fh, EM28XX_RESOURCE_VIDEO);
1753 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001754 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001755 res_free(fh, EM28XX_RESOURCE_VBI);
1756 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001757
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001758 return 0;
1759}
1760
1761static int vidioc_querycap(struct file *file, void *priv,
1762 struct v4l2_capability *cap)
1763{
1764 struct em28xx_fh *fh = priv;
1765 struct em28xx *dev = fh->dev;
1766
1767 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1768 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001769 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001770
1771 cap->version = EM28XX_VERSION_CODE;
1772
1773 cap->capabilities =
1774 V4L2_CAP_SLICED_VBI_CAPTURE |
1775 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001776 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1777
Devin Heitmueller04146142009-09-11 00:08:44 -03001778 if (dev->vbi_dev)
1779 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1780
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001781 if (dev->audio_mode.has_audio)
1782 cap->capabilities |= V4L2_CAP_AUDIO;
1783
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001784 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785 cap->capabilities |= V4L2_CAP_TUNER;
1786
1787 return 0;
1788}
1789
Hans Verkuil78b526a2008-05-28 12:16:41 -03001790static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001791 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001793 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001794 return -EINVAL;
1795
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001796 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1797 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001798
1799 return 0;
1800}
1801
1802/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001803static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804 struct v4l2_format *f)
1805{
1806 struct em28xx_fh *fh = priv;
1807 struct em28xx *dev = fh->dev;
1808 int rc;
1809
1810 rc = check_dev(dev);
1811 if (rc < 0)
1812 return rc;
1813
1814 mutex_lock(&dev->lock);
1815
1816 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001817 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001818
1819 if (f->fmt.sliced.service_set == 0)
1820 rc = -EINVAL;
1821
1822 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001823
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824 return rc;
1825}
1826
Hans Verkuil78b526a2008-05-28 12:16:41 -03001827static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001828 struct v4l2_format *f)
1829{
1830 struct em28xx_fh *fh = priv;
1831 struct em28xx *dev = fh->dev;
1832 int rc;
1833
1834 rc = check_dev(dev);
1835 if (rc < 0)
1836 return rc;
1837
1838 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001839 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001840 mutex_unlock(&dev->lock);
1841
1842 if (f->fmt.sliced.service_set == 0)
1843 return -EINVAL;
1844
1845 return 0;
1846}
1847
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001848/* RAW VBI ioctls */
1849
1850static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1851 struct v4l2_format *format)
1852{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001853 struct em28xx_fh *fh = priv;
1854 struct em28xx *dev = fh->dev;
1855
1856 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001857 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1858 format->fmt.vbi.offset = 0;
1859 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001860 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1861 format->fmt.vbi.count[0] = dev->vbi_height;
1862 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001863
1864 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001865 if (dev->norm & V4L2_STD_525_60) {
1866 /* NTSC */
1867 format->fmt.vbi.start[0] = 10;
1868 format->fmt.vbi.start[1] = 273;
1869 } else if (dev->norm & V4L2_STD_625_50) {
1870 /* PAL */
1871 format->fmt.vbi.start[0] = 6;
1872 format->fmt.vbi.start[1] = 318;
1873 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001874
1875 return 0;
1876}
1877
1878static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1879 struct v4l2_format *format)
1880{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001881 struct em28xx_fh *fh = priv;
1882 struct em28xx *dev = fh->dev;
1883
1884 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001885 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1886 format->fmt.vbi.offset = 0;
1887 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001888 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1889 format->fmt.vbi.count[0] = dev->vbi_height;
1890 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001891
1892 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001893 if (dev->norm & V4L2_STD_525_60) {
1894 /* NTSC */
1895 format->fmt.vbi.start[0] = 10;
1896 format->fmt.vbi.start[1] = 273;
1897 } else if (dev->norm & V4L2_STD_625_50) {
1898 /* PAL */
1899 format->fmt.vbi.start[0] = 6;
1900 format->fmt.vbi.start[1] = 318;
1901 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001902
1903 return 0;
1904}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001905
1906static int vidioc_reqbufs(struct file *file, void *priv,
1907 struct v4l2_requestbuffers *rb)
1908{
1909 struct em28xx_fh *fh = priv;
1910 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001911 int rc;
1912
1913 rc = check_dev(dev);
1914 if (rc < 0)
1915 return rc;
1916
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001917 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1918 return videobuf_reqbufs(&fh->vb_vidq, rb);
1919 else
1920 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001921}
1922
1923static int vidioc_querybuf(struct file *file, void *priv,
1924 struct v4l2_buffer *b)
1925{
1926 struct em28xx_fh *fh = priv;
1927 struct em28xx *dev = fh->dev;
1928 int rc;
1929
1930 rc = check_dev(dev);
1931 if (rc < 0)
1932 return rc;
1933
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001934 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1935 return videobuf_querybuf(&fh->vb_vidq, b);
1936 else {
1937 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1938 the videobuf framework, but we probably shouldn't be
1939 returning a buffer larger than that which was asked for.
1940 At a minimum, it causes a crash in zvbi since it does
1941 a memcpy based on the source buffer length */
1942 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001943 b->length = dev->vbi_width * dev->vbi_height * 2;
1944
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001945 return result;
1946 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001947}
1948
1949static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1950{
1951 struct em28xx_fh *fh = priv;
1952 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001953 int rc;
1954
1955 rc = check_dev(dev);
1956 if (rc < 0)
1957 return rc;
1958
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001959 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1960 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001961 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001962 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001963}
1964
1965static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1966{
1967 struct em28xx_fh *fh = priv;
1968 struct em28xx *dev = fh->dev;
1969 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001970
1971 rc = check_dev(dev);
1972 if (rc < 0)
1973 return rc;
1974
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001975 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1976 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1977 O_NONBLOCK);
1978 else
1979 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1980 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001981}
1982
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001983#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001984static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001985{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001986 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001987
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001988 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1989 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1990 else
1991 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001992}
1993#endif
1994
1995
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001996/* ----------------------------------------------------------- */
1997/* RADIO ESPECIFIC IOCTLS */
1998/* ----------------------------------------------------------- */
1999
2000static int radio_querycap(struct file *file, void *priv,
2001 struct v4l2_capability *cap)
2002{
2003 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2004
2005 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2006 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002007 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002008
2009 cap->version = EM28XX_VERSION_CODE;
2010 cap->capabilities = V4L2_CAP_TUNER;
2011 return 0;
2012}
2013
2014static int radio_g_tuner(struct file *file, void *priv,
2015 struct v4l2_tuner *t)
2016{
2017 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2018
2019 if (unlikely(t->index > 0))
2020 return -EINVAL;
2021
2022 strcpy(t->name, "Radio");
2023 t->type = V4L2_TUNER_RADIO;
2024
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002025 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002026 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002027 mutex_unlock(&dev->lock);
2028
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002029 return 0;
2030}
2031
2032static int radio_enum_input(struct file *file, void *priv,
2033 struct v4l2_input *i)
2034{
2035 if (i->index != 0)
2036 return -EINVAL;
2037 strcpy(i->name, "Radio");
2038 i->type = V4L2_INPUT_TYPE_TUNER;
2039
2040 return 0;
2041}
2042
2043static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2044{
2045 if (unlikely(a->index))
2046 return -EINVAL;
2047
2048 strcpy(a->name, "Radio");
2049 return 0;
2050}
2051
2052static int radio_s_tuner(struct file *file, void *priv,
2053 struct v4l2_tuner *t)
2054{
2055 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2056
2057 if (0 != t->index)
2058 return -EINVAL;
2059
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002060 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002061 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002062 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002063
2064 return 0;
2065}
2066
2067static int radio_s_audio(struct file *file, void *fh,
2068 struct v4l2_audio *a)
2069{
2070 return 0;
2071}
2072
2073static int radio_s_input(struct file *file, void *fh, unsigned int i)
2074{
2075 return 0;
2076}
2077
2078static int radio_queryctrl(struct file *file, void *priv,
2079 struct v4l2_queryctrl *qc)
2080{
2081 int i;
2082
2083 if (qc->id < V4L2_CID_BASE ||
2084 qc->id >= V4L2_CID_LASTP1)
2085 return -EINVAL;
2086
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002087 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2088 if (qc->id && qc->id == ac97_qctrl[i].id) {
2089 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002090 return 0;
2091 }
2092 }
2093
2094 return -EINVAL;
2095}
2096
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002097/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002098 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099 * inits the device and starts isoc transfer
2100 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002101static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002103 int errCode = 0, radio = 0;
2104 struct video_device *vdev = video_devdata(filp);
2105 struct em28xx *dev = video_drvdata(filp);
2106 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002107 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002108 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002109
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002110 switch (vdev->vfl_type) {
2111 case VFL_TYPE_GRABBER:
2112 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2113 break;
2114 case VFL_TYPE_VBI:
2115 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2116 break;
2117 case VFL_TYPE_RADIO:
2118 radio = 1;
2119 break;
2120 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002121
2122 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08002123
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002124 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2125 video_device_node_name(vdev), v4l2_type_names[fh_type],
2126 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002129 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002130 if (!fh) {
2131 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002132 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002133 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002135 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002136 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002137 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002138 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002140 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002141 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002142 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002143 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002144
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002145 /* Needed, since GPIO might have disabled power of
2146 some i2c device
2147 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002148 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002149
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002150 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002151 if (fh->radio) {
2152 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002153 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002154 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002155
2156 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002158 if (dev->progressive)
2159 field = V4L2_FIELD_NONE;
2160 else
2161 field = V4L2_FIELD_INTERLACED;
2162
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002163 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2164 NULL, &dev->slock,
2165 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2166 sizeof(struct em28xx_buffer), fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002167
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002168 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2169 NULL, &dev->slock,
2170 V4L2_BUF_TYPE_VBI_CAPTURE,
2171 V4L2_FIELD_SEQ_TB,
2172 sizeof(struct em28xx_buffer), fh);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002173
Ingo Molnar3593cab2006-02-07 06:49:14 -02002174 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002175
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002176 return errCode;
2177}
2178
2179/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002180 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181 * unregisters the v4l2,i2c and usb devices
2182 * called when the device gets disconected or at module unload
2183*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002184void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002187 /*FIXME: I2C IR should be disconnected */
2188
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002189 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002190 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002191 video_unregister_device(dev->radio_dev);
2192 else
2193 video_device_release(dev->radio_dev);
2194 dev->radio_dev = NULL;
2195 }
2196 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002197 em28xx_info("V4L2 device %s deregistered\n",
2198 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002199 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002200 video_unregister_device(dev->vbi_dev);
2201 else
2202 video_device_release(dev->vbi_dev);
2203 dev->vbi_dev = NULL;
2204 }
2205 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002206 em28xx_info("V4L2 device %s deregistered\n",
2207 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002208 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002209 video_unregister_device(dev->vdev);
2210 else
2211 video_device_release(dev->vdev);
2212 dev->vdev = NULL;
2213 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214}
2215
2216/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002217 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002218 * stops streaming and deallocates all resources allocated by the v4l2
2219 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002220 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002221static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002223 struct em28xx_fh *fh = filp->private_data;
2224 struct em28xx *dev = fh->dev;
2225 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002226
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002227 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002228
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002229 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002230 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002231 res_free(fh, EM28XX_RESOURCE_VIDEO);
2232 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002234 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2235 videobuf_stop(&fh->vb_vbiq);
2236 res_free(fh, EM28XX_RESOURCE_VBI);
2237 }
2238
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002239 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002240 /* the device is already disconnect,
2241 free the remaining resources */
2242 if (dev->state & DEV_DISCONNECTED) {
2243 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002244 kfree(dev);
2245 return 0;
2246 }
2247
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002248 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002249 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002250
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002251 /* do this before setting alternate! */
2252 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002253 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002254
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002255 /* set alternate 0 */
2256 dev->alt = 0;
2257 em28xx_videodbg("setting alternate 0\n");
2258 errCode = usb_set_interface(dev->udev, 0, 0);
2259 if (errCode < 0) {
2260 em28xx_errdev("cannot change alternate number to "
2261 "0 (error=%i)\n", errCode);
2262 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002263 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002264
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002265 videobuf_mmap_free(&fh->vb_vidq);
2266 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002267 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002268 dev->users--;
2269 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270 return 0;
2271}
2272
2273/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002274 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002275 * will allocate buffers when called for the first time
2276 */
2277static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002278em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002279 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002281 struct em28xx_fh *fh = filp->private_data;
2282 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002283 int rc;
2284
2285 rc = check_dev(dev);
2286 if (rc < 0)
2287 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002289 /* FIXME: read() is not prepared to allow changing the video
2290 resolution while streaming. Seems a bug at em28xx_set_fmt
2291 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002292
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002293 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002294 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2295 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002296
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002297 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2298 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002299 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002300
2301
2302 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002303 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2304 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002305
2306 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2307 filp->f_flags & O_NONBLOCK);
2308 }
2309
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002310 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002311}
2312
2313/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002314 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002315 * will allocate buffers when called for the first time
2316 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002317static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002318{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002319 struct em28xx_fh *fh = filp->private_data;
2320 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002321 int rc;
2322
2323 rc = check_dev(dev);
2324 if (rc < 0)
2325 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002326
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002327 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2328 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2329 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002330 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002331 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2332 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2333 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002334 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002335 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002336 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002337 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002338}
2339
2340/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002341 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002342 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002343static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002344{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002345 struct em28xx_fh *fh = filp->private_data;
2346 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002347 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002348
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002349 rc = check_dev(dev);
2350 if (rc < 0)
2351 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002352
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002353 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2354 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2355 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2356 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002357
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002358 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2359 (unsigned long)vma->vm_start,
2360 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2361 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002362
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002363 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002364}
2365
Hans Verkuilbec43662008-12-30 06:58:20 -03002366static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002367 .owner = THIS_MODULE,
2368 .open = em28xx_v4l2_open,
2369 .release = em28xx_v4l2_close,
2370 .read = em28xx_v4l2_read,
2371 .poll = em28xx_v4l2_poll,
2372 .mmap = em28xx_v4l2_mmap,
2373 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002374};
2375
Hans Verkuila3998102008-07-21 02:57:38 -03002376static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002377 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002378 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2379 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2380 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2381 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002382 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2383 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002384 .vidioc_g_audio = vidioc_g_audio,
2385 .vidioc_s_audio = vidioc_s_audio,
2386 .vidioc_cropcap = vidioc_cropcap,
2387
Hans Verkuil78b526a2008-05-28 12:16:41 -03002388 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2389 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2390 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002391
2392 .vidioc_reqbufs = vidioc_reqbufs,
2393 .vidioc_querybuf = vidioc_querybuf,
2394 .vidioc_qbuf = vidioc_qbuf,
2395 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002396 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002397 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002398 .vidioc_g_parm = vidioc_g_parm,
2399 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002400 .vidioc_enum_input = vidioc_enum_input,
2401 .vidioc_g_input = vidioc_g_input,
2402 .vidioc_s_input = vidioc_s_input,
2403 .vidioc_queryctrl = vidioc_queryctrl,
2404 .vidioc_g_ctrl = vidioc_g_ctrl,
2405 .vidioc_s_ctrl = vidioc_s_ctrl,
2406 .vidioc_streamon = vidioc_streamon,
2407 .vidioc_streamoff = vidioc_streamoff,
2408 .vidioc_g_tuner = vidioc_g_tuner,
2409 .vidioc_s_tuner = vidioc_s_tuner,
2410 .vidioc_g_frequency = vidioc_g_frequency,
2411 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002412#ifdef CONFIG_VIDEO_ADV_DEBUG
2413 .vidioc_g_register = vidioc_g_register,
2414 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002415 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002416#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002417#ifdef CONFIG_VIDEO_V4L1_COMPAT
2418 .vidiocgmbuf = vidiocgmbuf,
2419#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002420};
2421
2422static const struct video_device em28xx_video_template = {
2423 .fops = &em28xx_v4l_fops,
2424 .release = video_device_release,
2425 .ioctl_ops = &video_ioctl_ops,
2426
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002427 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002428 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002429};
2430
Hans Verkuilbec43662008-12-30 06:58:20 -03002431static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002432 .owner = THIS_MODULE,
2433 .open = em28xx_v4l2_open,
2434 .release = em28xx_v4l2_close,
2435 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002436};
2437
2438static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002439 .vidioc_querycap = radio_querycap,
2440 .vidioc_g_tuner = radio_g_tuner,
2441 .vidioc_enum_input = radio_enum_input,
2442 .vidioc_g_audio = radio_g_audio,
2443 .vidioc_s_tuner = radio_s_tuner,
2444 .vidioc_s_audio = radio_s_audio,
2445 .vidioc_s_input = radio_s_input,
2446 .vidioc_queryctrl = radio_queryctrl,
2447 .vidioc_g_ctrl = vidioc_g_ctrl,
2448 .vidioc_s_ctrl = vidioc_s_ctrl,
2449 .vidioc_g_frequency = vidioc_g_frequency,
2450 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002451#ifdef CONFIG_VIDEO_ADV_DEBUG
2452 .vidioc_g_register = vidioc_g_register,
2453 .vidioc_s_register = vidioc_s_register,
2454#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002455};
2456
Hans Verkuila3998102008-07-21 02:57:38 -03002457static struct video_device em28xx_radio_template = {
2458 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002459 .fops = &radio_fops,
2460 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002461};
2462
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002463/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002464
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002465
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002466
Adrian Bunk532fe652008-01-28 22:10:48 -03002467static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002468 const struct video_device *template,
2469 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002470{
2471 struct video_device *vfd;
2472
2473 vfd = video_device_alloc();
2474 if (NULL == vfd)
2475 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002476
2477 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002478 vfd->v4l2_dev = &dev->v4l2_dev;
2479 vfd->release = video_device_release;
2480 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002481
2482 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2483 dev->name, type_name);
2484
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002485 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002486 return vfd;
2487}
2488
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002489int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002490{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002491 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002492 int ret;
2493
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002494 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2495 dev->name,
2496 (EM28XX_VERSION_CODE >> 16) & 0xff,
2497 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2498
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002499 /* set default norm */
2500 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002501 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002502 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002503
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002504 /* Analog specific initialization */
2505 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002506 em28xx_set_video_format(dev, format[0].fourcc,
2507 norm_maxw(dev), norm_maxh(dev));
2508
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002509 video_mux(dev, dev->ctl_input);
2510
2511 /* Audio defaults */
2512 dev->mute = 1;
2513 dev->volume = 0x1f;
2514
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002515/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002516 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2517 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2518 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002519
2520 em28xx_set_outfmt(dev);
2521 em28xx_colorlevels_set_default(dev);
2522 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002523
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002524 /* allocate and fill video video_device struct */
2525 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2526 if (!dev->vdev) {
2527 em28xx_errdev("cannot allocate video_device.\n");
2528 return -ENODEV;
2529 }
2530
2531 /* register v4l2 video video_device */
2532 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2533 video_nr[dev->devno]);
2534 if (ret) {
2535 em28xx_errdev("unable to register video device (error=%i).\n",
2536 ret);
2537 return ret;
2538 }
2539
2540 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002541 if (em28xx_vbi_supported(dev) == 1) {
2542 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2543 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002544
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002545 /* register v4l2 vbi video_device */
2546 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2547 vbi_nr[dev->devno]);
2548 if (ret < 0) {
2549 em28xx_errdev("unable to register vbi device\n");
2550 return ret;
2551 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002552 }
2553
2554 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002555 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2556 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002557 if (!dev->radio_dev) {
2558 em28xx_errdev("cannot allocate video_device.\n");
2559 return -ENODEV;
2560 }
2561 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2562 radio_nr[dev->devno]);
2563 if (ret < 0) {
2564 em28xx_errdev("can't register radio device\n");
2565 return ret;
2566 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002567 em28xx_info("Registered radio device as %s\n",
2568 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002569 }
2570
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002571 em28xx_info("V4L2 video device registered as %s\n",
2572 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002573
2574 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002575 em28xx_info("V4L2 VBI device registered as %s\n",
2576 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002577
2578 return 0;
2579}