blob: b6ac99de97d2ed6d72ec194e49eb97b483c62997 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080039
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080040#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020041#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030043#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030044#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030045#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
48 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030049 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080052#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030053#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
75
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020076static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030077static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020080module_param_array(video_nr, int, NULL, 0444);
81module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083MODULE_PARM_DESC(video_nr, "video device numbers");
84MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
85MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080086
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030087static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030088module_param(video_debug, int, 0644);
89MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080090
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030091/* supported video standards */
92static struct em28xx_fmt format[] = {
93 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030094 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030095 .fourcc = V4L2_PIX_FMT_YUYV,
96 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030097 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030098 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030099 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300102 .reg = EM28XX_OUTFMT_RGB_16_656,
103 }, {
104 .name = "8 bpp Bayer BGBG..GRGR",
105 .fourcc = V4L2_PIX_FMT_SBGGR8,
106 .depth = 8,
107 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
108 }, {
109 .name = "8 bpp Bayer GRGR..BGBG",
110 .fourcc = V4L2_PIX_FMT_SGRBG8,
111 .depth = 8,
112 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
113 }, {
114 .name = "8 bpp Bayer GBGB..RGRG",
115 .fourcc = V4L2_PIX_FMT_SGBRG8,
116 .depth = 8,
117 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
118 }, {
119 .name = "12 bpp YUV411",
120 .fourcc = V4L2_PIX_FMT_YUV411P,
121 .depth = 12,
122 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300123 },
124};
125
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800126/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200127/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300128static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800129 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200130 .id = V4L2_CID_AUDIO_VOLUME,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Volume",
133 .minimum = 0x0,
134 .maximum = 0x1f,
135 .step = 0x1,
136 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300137 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300138 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200139 .id = V4L2_CID_AUDIO_MUTE,
140 .type = V4L2_CTRL_TYPE_BOOLEAN,
141 .name = "Mute",
142 .minimum = 0,
143 .maximum = 1,
144 .step = 1,
145 .default_value = 1,
146 .flags = 0,
147 }
148};
149
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300150/* ------------------------------------------------------------------
151 DMA and thread functions
152 ------------------------------------------------------------------*/
153
154/*
155 * Announces that a buffer were filled and request the next
156 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158 struct em28xx_dmaqueue *dma_q,
159 struct em28xx_buffer *buf)
160{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 /* Advice that buffer was filled */
162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
163 buf->vb.state = VIDEOBUF_DONE;
164 buf->vb.field_count++;
165 do_gettimeofday(&buf->vb.ts);
166
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300167 dev->isoc_ctl.vid_buf = NULL;
168
169 list_del(&buf->vb.queue);
170 wake_up(&buf->vb.done);
171}
172
173static inline void vbi_buffer_filled(struct em28xx *dev,
174 struct em28xx_dmaqueue *dma_q,
175 struct em28xx_buffer *buf)
176{
177 /* Advice that buffer was filled */
178 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
179
180 buf->vb.state = VIDEOBUF_DONE;
181 buf->vb.field_count++;
182 do_gettimeofday(&buf->vb.ts);
183
184 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300185
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 list_del(&buf->vb.queue);
187 wake_up(&buf->vb.done);
188}
189
190/*
191 * Identify the buffer header type and properly handles
192 */
193static void em28xx_copy_video(struct em28xx *dev,
194 struct em28xx_dmaqueue *dma_q,
195 struct em28xx_buffer *buf,
196 unsigned char *p,
197 unsigned char *outp, unsigned long len)
198{
199 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300200 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202
203 if (dma_q->pos + len > buf->vb.size)
204 len = buf->vb.size - dma_q->pos;
205
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206 startread = p;
207 remain = len;
208
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300209 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300211 else {
212 /* Interlaces two half frames */
213 if (buf->top_field)
214 fieldstart = outp;
215 else
216 fieldstart = outp + bytesperline;
217 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300218
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300219 linesdone = dma_q->pos / bytesperline;
220 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300221
222 if (dev->progressive)
223 offset = linesdone * bytesperline + currlinedone;
224 else
225 offset = linesdone * bytesperline * 2 + currlinedone;
226
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300227 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300228 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229 lencopy = lencopy > remain ? remain : lencopy;
230
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300231 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300232 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300233 ((char *)startwrite + lencopy) -
234 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300235 remain = (char *)outp + buf->vb.size - (char *)startwrite;
236 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300237 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300238 if (lencopy <= 0)
239 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300240 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300241
242 remain -= lencopy;
243
244 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300245 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300247 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300248 lencopy = remain;
249 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300252 if ((char *)startwrite + lencopy > (char *)outp +
253 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300254 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
255 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 ((char *)startwrite + lencopy) -
257 ((char *)outp + buf->vb.size));
258 lencopy = remain = (char *)outp + buf->vb.size -
259 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (lencopy <= 0)
262 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300263
264 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
266 remain -= lencopy;
267 }
268
269 dma_q->pos += len;
270}
271
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300272static void em28xx_copy_vbi(struct em28xx *dev,
273 struct em28xx_dmaqueue *dma_q,
274 struct em28xx_buffer *buf,
275 unsigned char *p,
276 unsigned char *outp, unsigned long len)
277{
278 void *startwrite, *startread;
279 int offset;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300280 int bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300281
282 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300283 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300284 return;
285 }
286
287 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300288 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300289 return;
290 }
291 if (buf == NULL) {
292 return;
293 }
294 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300295 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300296 return;
297 }
298 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300299 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300300 return;
301 }
302
303 if (dma_q->pos + len > buf->vb.size)
304 len = buf->vb.size - dma_q->pos;
305
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300306 startread = p;
307
308 startwrite = outp + dma_q->pos;
309 offset = dma_q->pos;
310
311 /* Make sure the bottom field populates the second half of the frame */
312 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300313 startwrite += bytesperline * dev->vbi_height;
314 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300315 }
316
317 memcpy(startwrite, startread, len);
318 dma_q->pos += len;
319}
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 int packet, int status)
323{
324 char *errmsg = "Unknown";
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327 case -ENOENT:
328 errmsg = "unlinked synchronuously";
329 break;
330 case -ECONNRESET:
331 errmsg = "unlinked asynchronuously";
332 break;
333 case -ENOSR:
334 errmsg = "Buffer error (overrun)";
335 break;
336 case -EPIPE:
337 errmsg = "Stalled (device not responding)";
338 break;
339 case -EOVERFLOW:
340 errmsg = "Babble (bad cable?)";
341 break;
342 case -EPROTO:
343 errmsg = "Bit-stuff error (bad cable?)";
344 break;
345 case -EILSEQ:
346 errmsg = "CRC/Timeout (could be anything)";
347 break;
348 case -ETIME:
349 errmsg = "Device does not respond";
350 break;
351 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300352 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300353 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
354 } else {
355 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
356 packet, status, errmsg);
357 }
358}
359
360/*
361 * video-buf generic routine to get the next available buffer
362 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300363static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364 struct em28xx_buffer **buf)
365{
366 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300367 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300369 if (list_empty(&dma_q->active)) {
370 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300371 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300372 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300373 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300374 }
375
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300376 /* Get the next buffer */
377 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
378
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300379 /* Cleans up buffer - Usefull for testing for frame/URB loss */
380 outp = videobuf_to_vmalloc(&(*buf)->vb);
381 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300382
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300383 dev->isoc_ctl.vid_buf = *buf;
384
385 return;
386}
387
388/*
389 * video-buf generic routine to get the next available VBI buffer
390 */
391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
392 struct em28xx_buffer **buf)
393{
394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
395 char *outp;
396
397 if (list_empty(&dma_q->active)) {
398 em28xx_isocdbg("No active queue to serve\n");
399 dev->isoc_ctl.vbi_buf = NULL;
400 *buf = NULL;
401 return;
402 }
403
404 /* Get the next buffer */
405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
406 /* Cleans up buffer - Usefull for testing for frame/URB loss */
407 outp = videobuf_to_vmalloc(&(*buf)->vb);
408 memset(outp, 0x00, (*buf)->vb.size);
409
410 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300411
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300412 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300413}
414
415/*
416 * Controls the isoc copy of each urb packet
417 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300418static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300419{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300420 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300421 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300422 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300423 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300424 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425
426 if (!dev)
427 return 0;
428
429 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
430 return 0;
431
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300432 if (urb->status < 0) {
433 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300434 if (urb->status == -ENOENT)
435 return 0;
436 }
437
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300438 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300439 if (buf != NULL)
440 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300441
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300442 for (i = 0; i < urb->number_of_packets; i++) {
443 int status = urb->iso_frame_desc[i].status;
444
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300445 if (status < 0) {
446 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447 if (urb->iso_frame_desc[i].status != -EPROTO)
448 continue;
449 }
450
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300451 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452
453 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 continue;
456 }
457 if (urb->iso_frame_desc[i].actual_length >
458 dev->max_pkt_size) {
459 em28xx_isocdbg("packet bigger than packet size");
460 continue;
461 }
462
463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464
465 /* FIXME: incomplete buffer checks where removed to make
466 logic simpler. Impacts of those changes should be evaluated
467 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300468 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
469 em28xx_isocdbg("VBI HEADER!!!\n");
470 /* FIXME: Should add vbi copy */
471 continue;
472 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300473 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300474 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300475 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300476
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300477 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300478 if (buf != NULL)
479 buffer_filled(dev, dma_q, buf);
480 get_next_buf(dma_q, &buf);
481 if (buf == NULL)
482 outp = NULL;
483 else
484 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300485 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300486
487 if (buf != NULL) {
488 if (p[2] & 1)
489 buf->top_field = 0;
490 else
491 buf->top_field = 1;
492 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300493
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300496 if (buf != NULL) {
497 if (p[0] != 0x88 && p[0] != 0x22) {
498 em28xx_isocdbg("frame is not complete\n");
499 len += 4;
500 } else {
501 p += 4;
502 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300503 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300504 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300505 }
506 return rc;
507}
508
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300509/* Version of isoc handler that takes into account a mixture of video and
510 VBI data */
511static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
512{
513 struct em28xx_buffer *buf, *vbi_buf;
514 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300515 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300516 unsigned char *outp = NULL;
517 unsigned char *vbioutp = NULL;
518 int i, len = 0, rc = 1;
519 unsigned char *p;
520 int vbi_size;
521
522 if (!dev)
523 return 0;
524
525 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
526 return 0;
527
528 if (urb->status < 0) {
529 print_err_status(dev, -1, urb->status);
530 if (urb->status == -ENOENT)
531 return 0;
532 }
533
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300534 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300535 if (buf != NULL)
536 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300537
538 vbi_buf = dev->isoc_ctl.vbi_buf;
539 if (vbi_buf != NULL)
540 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
541
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300542 for (i = 0; i < urb->number_of_packets; i++) {
543 int status = urb->iso_frame_desc[i].status;
544
545 if (status < 0) {
546 print_err_status(dev, i, status);
547 if (urb->iso_frame_desc[i].status != -EPROTO)
548 continue;
549 }
550
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300551 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300552 if (urb->iso_frame_desc[i].actual_length <= 0) {
553 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
554 continue;
555 }
556 if (urb->iso_frame_desc[i].actual_length >
557 dev->max_pkt_size) {
558 em28xx_isocdbg("packet bigger than packet size");
559 continue;
560 }
561
562 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
563
564 /* capture type 0 = vbi start
565 capture type 1 = video start
566 capture type 2 = video in progress */
567 if (p[0] == 0x33 && p[1] == 0x95) {
568 dev->capture_type = 0;
569 dev->vbi_read = 0;
570 em28xx_isocdbg("VBI START HEADER!!!\n");
571 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300572 p += 4;
573 len -= 4;
574 } else if (p[0] == 0x88 && p[1] == 0x88 &&
575 p[2] == 0x88 && p[3] == 0x88) {
576 /* continuation */
577 p += 4;
578 len -= 4;
579 } else if (p[0] == 0x22 && p[1] == 0x5a) {
580 /* start video */
581 p += 4;
582 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300583 }
584
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300585 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300586
587 if (dev->capture_type == 0) {
588 if (dev->vbi_read >= vbi_size) {
589 /* We've already read all the VBI data, so
590 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300591 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300592 } else if ((dev->vbi_read + len) < vbi_size) {
593 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300594 if (dev->vbi_read == 0 &&
595 (!(dev->cur_field & 1))) {
596 /* Brand new frame */
597 if (vbi_buf != NULL)
598 vbi_buffer_filled(dev,
599 vbi_dma_q,
600 vbi_buf);
601 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
602 if (vbi_buf == NULL)
603 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300604 else
605 vbioutp = videobuf_to_vmalloc(
606 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300607 }
608
609 if (dev->vbi_read == 0) {
610 vbi_dma_q->pos = 0;
611 if (vbi_buf != NULL) {
612 if (dev->cur_field & 1)
613 vbi_buf->top_field = 0;
614 else
615 vbi_buf->top_field = 1;
616 }
617 }
618
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300619 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300620 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
621 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300622 } else {
623 /* Some of this frame is VBI data and some is
624 video data */
625 int vbi_data_len = vbi_size - dev->vbi_read;
626 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300627 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300629 dev->capture_type = 1;
630 p += vbi_data_len;
631 len -= vbi_data_len;
632 }
633 }
634
635 if (dev->capture_type == 1) {
636 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300637 if (dev->progressive || !(dev->cur_field & 1)) {
638 if (buf != NULL)
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
641 if (buf == NULL)
642 outp = NULL;
643 else
644 outp = videobuf_to_vmalloc(&buf->vb);
645 }
646 if (buf != NULL) {
647 if (dev->cur_field & 1)
648 buf->top_field = 0;
649 else
650 buf->top_field = 1;
651 }
652
653 dma_q->pos = 0;
654 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300655
656 if (buf != NULL && dev->capture_type == 2) {
657 if (len > 4 && p[0] == 0x88 && p[1] == 0x88 &&
658 p[2] == 0x88 && p[3] == 0x88) {
659 p += 4;
660 len -= 4;
661 }
662 if (len > 4 && p[0] == 0x22 && p[1] == 0x5a) {
663 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
664 p[2], len, (p[2] & 1) ?
665 "odd" : "even");
666 p += 4;
667 len -= 4;
668 }
669
670 if (len > 0)
671 em28xx_copy_video(dev, dma_q, buf, p, outp,
672 len);
673 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300674 }
675 return rc;
676}
677
678
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680 Videobuf operations
681 ------------------------------------------------------------------*/
682
683static int
684buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
685{
686 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300687 struct em28xx *dev = fh->dev;
688 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300690 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
691 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300692
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300693 if (0 == *count)
694 *count = EM28XX_DEF_BUF;
695
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300696 if (*count < EM28XX_MIN_BUF)
697 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300699 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300700 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300701 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300702 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300703
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300704 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300705
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300706 return 0;
707}
708
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300709/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300710static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
711{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300712 struct em28xx_fh *fh = vq->priv_data;
713 struct em28xx *dev = fh->dev;
714 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 if (in_interrupt())
716 BUG();
717
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300718 /* We used to wait for the buffer to finish here, but this didn't work
719 because, as we were keeping the state as VIDEOBUF_QUEUED,
720 videobuf_queue_cancel marked it as finished for us.
721 (Also, it could wedge forever if the hardware was misconfigured.)
722
723 This should be safe; by the time we get here, the buffer isn't
724 queued anymore. If we ever start marking the buffers as
725 VIDEOBUF_ACTIVE, it won't be, though.
726 */
727 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300728 if (dev->isoc_ctl.vid_buf == buf)
729 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300730 spin_unlock_irqrestore(&dev->slock, flags);
731
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300732 videobuf_vmalloc_free(&buf->vb);
733 buf->vb.state = VIDEOBUF_NEEDS_INIT;
734}
735
736static int
737buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
738 enum v4l2_field field)
739{
740 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300741 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300742 struct em28xx *dev = fh->dev;
743 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300745 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
746 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300747
748 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
749 return -EINVAL;
750
Brandon Philips05612972008-04-13 14:57:01 -0300751 buf->vb.width = dev->width;
752 buf->vb.height = dev->height;
753 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300754
755 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300756 rc = videobuf_iolock(vq, &buf->vb, NULL);
757 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300758 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759 }
760
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300761 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300762 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300763
764 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300765 if (em28xx_vbi_supported(dev) == 1)
766 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
767 EM28XX_NUM_BUFS,
768 dev->max_pkt_size,
769 em28xx_isoc_copy_vbi);
770 else
771 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
772 EM28XX_NUM_BUFS,
773 dev->max_pkt_size,
774 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300775 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300776 goto fail;
777 }
778
779 buf->vb.state = VIDEOBUF_PREPARED;
780 return 0;
781
782fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300783 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300784 return rc;
785}
786
787static void
788buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
789{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300790 struct em28xx_buffer *buf = container_of(vb,
791 struct em28xx_buffer,
792 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300793 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300794 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300796
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300797 buf->vb.state = VIDEOBUF_QUEUED;
798 list_add_tail(&buf->vb.queue, &vidq->active);
799
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300800}
801
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300802static void buffer_release(struct videobuf_queue *vq,
803 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300804{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300805 struct em28xx_buffer *buf = container_of(vb,
806 struct em28xx_buffer,
807 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300808 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300809 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300811 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300812
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300813 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300814}
815
816static struct videobuf_queue_ops em28xx_video_qops = {
817 .buf_setup = buffer_setup,
818 .buf_prepare = buffer_prepare,
819 .buf_queue = buffer_queue,
820 .buf_release = buffer_release,
821};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800822
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300823/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800824
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800825static void video_mux(struct em28xx *dev, int index)
826{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800827 dev->ctl_input = index;
828 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300829 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800830
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300831 if (!dev->ctl_aoutput)
832 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
833
Hans Verkuil5325b422009-04-02 11:26:22 -0300834 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
835 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800836
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300837 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300838 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300839 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
840 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300841 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300842 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300843 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
844 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800845 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300846
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300847 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300848 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
849 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300850 }
851
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300852 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800853}
854
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300855/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300856static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300857{
858 struct em28xx *dev = fh->dev;
859
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300860 if (fh->resources & bit)
861 /* have it already allocated */
862 return 1;
863
864 /* is it free? */
865 mutex_lock(&dev->lock);
866 if (dev->resources & bit) {
867 /* no, someone else uses it */
868 mutex_unlock(&dev->lock);
869 return 0;
870 }
871 /* it's free, grab it */
872 fh->resources |= bit;
873 dev->resources |= bit;
874 em28xx_videodbg("res: get %d\n", bit);
875 mutex_unlock(&dev->lock);
876 return 1;
877}
878
879static int res_check(struct em28xx_fh *fh, unsigned int bit)
880{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300881 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300882}
883
884static int res_locked(struct em28xx *dev, unsigned int bit)
885{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300886 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300887}
888
889static void res_free(struct em28xx_fh *fh, unsigned int bits)
890{
891 struct em28xx *dev = fh->dev;
892
893 BUG_ON((fh->resources & bits) != bits);
894
895 mutex_lock(&dev->lock);
896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 em28xx_videodbg("res: put %d\n", bits);
899 mutex_unlock(&dev->lock);
900}
901
902static int get_ressource(struct em28xx_fh *fh)
903{
904 switch (fh->type) {
905 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
906 return EM28XX_RESOURCE_VIDEO;
907 case V4L2_BUF_TYPE_VBI_CAPTURE:
908 return EM28XX_RESOURCE_VBI;
909 default:
910 BUG();
911 return 0;
912 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300913}
914
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800915/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300916 * ac97_queryctrl()
917 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300918 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300919static int ac97_queryctrl(struct v4l2_queryctrl *qc)
920{
921 int i;
922
923 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
924 if (qc->id && qc->id == ac97_qctrl[i].id) {
925 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
926 return 0;
927 }
928 }
929
930 /* Control is not ac97 related */
931 return 1;
932}
933
934/*
935 * ac97_get_ctrl()
936 * return the current values for ac97 mute and volume
937 */
938static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939{
940 switch (ctrl->id) {
941 case V4L2_CID_AUDIO_MUTE:
942 ctrl->value = dev->mute;
943 return 0;
944 case V4L2_CID_AUDIO_VOLUME:
945 ctrl->value = dev->volume;
946 return 0;
947 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300948 /* Control is not ac97 related */
949 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300950 }
951}
952
953/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300954 * ac97_set_ctrl()
955 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300957static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300958{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300959 int i;
960
961 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
962 if (ctrl->id == ac97_qctrl[i].id)
963 goto handle;
964
965 /* Announce that hasn't handle it */
966 return 1;
967
968handle:
969 if (ctrl->value < ac97_qctrl[i].minimum ||
970 ctrl->value > ac97_qctrl[i].maximum)
971 return -ERANGE;
972
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300973 switch (ctrl->id) {
974 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300975 dev->mute = ctrl->value;
976 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300977 case V4L2_CID_AUDIO_VOLUME:
978 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300979 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300980 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300981
982 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300983}
984
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300985static int check_dev(struct em28xx *dev)
986{
987 if (dev->state & DEV_DISCONNECTED) {
988 em28xx_errdev("v4l2 ioctl: device not present\n");
989 return -ENODEV;
990 }
991
992 if (dev->state & DEV_MISCONFIGURED) {
993 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
994 "close and open it again\n");
995 return -EIO;
996 }
997 return 0;
998}
999
1000static void get_scale(struct em28xx *dev,
1001 unsigned int width, unsigned int height,
1002 unsigned int *hscale, unsigned int *vscale)
1003{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001004 unsigned int maxw = norm_maxw(dev);
1005 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001006
1007 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1008 if (*hscale >= 0x4000)
1009 *hscale = 0x3fff;
1010
1011 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1012 if (*vscale >= 0x4000)
1013 *vscale = 0x3fff;
1014}
1015
1016/* ------------------------------------------------------------------
1017 IOCTL vidioc handling
1018 ------------------------------------------------------------------*/
1019
Hans Verkuil78b526a2008-05-28 12:16:41 -03001020static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001021 struct v4l2_format *f)
1022{
1023 struct em28xx_fh *fh = priv;
1024 struct em28xx *dev = fh->dev;
1025
1026 mutex_lock(&dev->lock);
1027
1028 f->fmt.pix.width = dev->width;
1029 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001030 f->fmt.pix.pixelformat = dev->format->fourcc;
1031 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001032 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001033 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1034
1035 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001036 if (dev->progressive)
1037 f->fmt.pix.field = V4L2_FIELD_NONE;
1038 else
1039 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001040 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1041
1042 mutex_unlock(&dev->lock);
1043 return 0;
1044}
1045
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001046static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1047{
1048 unsigned int i;
1049
1050 for (i = 0; i < ARRAY_SIZE(format); i++)
1051 if (format[i].fourcc == fourcc)
1052 return &format[i];
1053
1054 return NULL;
1055}
1056
Hans Verkuil78b526a2008-05-28 12:16:41 -03001057static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001058 struct v4l2_format *f)
1059{
1060 struct em28xx_fh *fh = priv;
1061 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001062 unsigned int width = f->fmt.pix.width;
1063 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064 unsigned int maxw = norm_maxw(dev);
1065 unsigned int maxh = norm_maxh(dev);
1066 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001067 struct em28xx_fmt *fmt;
1068
1069 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1070 if (!fmt) {
1071 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1072 f->fmt.pix.pixelformat);
1073 return -EINVAL;
1074 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001075
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001076 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001077 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001078 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1079 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001080 } else {
1081 /* width must even because of the YUYV format
1082 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001083 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1084 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001085 }
1086
1087 get_scale(dev, width, height, &hscale, &vscale);
1088
1089 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1090 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1091
1092 f->fmt.pix.width = width;
1093 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001094 f->fmt.pix.pixelformat = fmt->fourcc;
1095 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1096 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001097 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001098 if (dev->progressive)
1099 f->fmt.pix.field = V4L2_FIELD_NONE;
1100 else
1101 f->fmt.pix.field = dev->interlaced ?
1102 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001103
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001104 return 0;
1105}
1106
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001107static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1108 unsigned width, unsigned height)
1109{
1110 struct em28xx_fmt *fmt;
1111
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001112 fmt = format_by_fourcc(fourcc);
1113 if (!fmt)
1114 return -EINVAL;
1115
1116 dev->format = fmt;
1117 dev->width = width;
1118 dev->height = height;
1119
1120 /* set new image size */
1121 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1122
1123 em28xx_set_alternate(dev);
1124 em28xx_resolution_set(dev);
1125
1126 return 0;
1127}
1128
Hans Verkuil78b526a2008-05-28 12:16:41 -03001129static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001130 struct v4l2_format *f)
1131{
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001134 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001135
1136 rc = check_dev(dev);
1137 if (rc < 0)
1138 return rc;
1139
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001140 mutex_lock(&dev->lock);
1141
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001142 vidioc_try_fmt_vid_cap(file, priv, f);
1143
Brandon Philips05612972008-04-13 14:57:01 -03001144 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1145 em28xx_errdev("%s queue busy\n", __func__);
1146 rc = -EBUSY;
1147 goto out;
1148 }
1149
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001150 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1151 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -03001152
1153out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -03001155 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001156}
1157
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001158static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1159{
1160 struct em28xx_fh *fh = priv;
1161 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001162 int rc;
1163
1164 rc = check_dev(dev);
1165 if (rc < 0)
1166 return rc;
1167
1168 *norm = dev->norm;
1169
1170 return 0;
1171}
1172
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001173static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001174{
1175 struct em28xx_fh *fh = priv;
1176 struct em28xx *dev = fh->dev;
1177 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001178 int rc;
1179
1180 rc = check_dev(dev);
1181 if (rc < 0)
1182 return rc;
1183
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001185 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001186
1187 /* Adjusts width/height, if needed */
1188 f.fmt.pix.width = dev->width;
1189 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001190 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001191
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001192 /* set new image size */
1193 dev->width = f.fmt.pix.width;
1194 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001195 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1196
1197 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001198 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001199
1200 mutex_unlock(&dev->lock);
1201 return 0;
1202}
1203
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001204static int vidioc_g_parm(struct file *file, void *priv,
1205 struct v4l2_streamparm *p)
1206{
1207 struct em28xx_fh *fh = priv;
1208 struct em28xx *dev = fh->dev;
1209 int rc = 0;
1210
1211 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1212 return -EINVAL;
1213
1214 if (dev->board.is_webcam)
1215 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1216 video, g_parm, p);
1217 else
1218 v4l2_video_std_frame_period(dev->norm,
1219 &p->parm.capture.timeperframe);
1220
1221 return rc;
1222}
1223
1224static int vidioc_s_parm(struct file *file, void *priv,
1225 struct v4l2_streamparm *p)
1226{
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1229
1230 if (!dev->board.is_webcam)
1231 return -EINVAL;
1232
1233 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1234 return -EINVAL;
1235
1236 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1237}
1238
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001239static const char *iname[] = {
1240 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1241 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1242 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1243 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1244 [EM28XX_VMUX_SVIDEO] = "S-Video",
1245 [EM28XX_VMUX_TELEVISION] = "Television",
1246 [EM28XX_VMUX_CABLE] = "Cable TV",
1247 [EM28XX_VMUX_DVB] = "DVB",
1248 [EM28XX_VMUX_DEBUG] = "for debug only",
1249};
1250
1251static int vidioc_enum_input(struct file *file, void *priv,
1252 struct v4l2_input *i)
1253{
1254 struct em28xx_fh *fh = priv;
1255 struct em28xx *dev = fh->dev;
1256 unsigned int n;
1257
1258 n = i->index;
1259 if (n >= MAX_EM28XX_INPUT)
1260 return -EINVAL;
1261 if (0 == INPUT(n)->type)
1262 return -EINVAL;
1263
1264 i->index = n;
1265 i->type = V4L2_INPUT_TYPE_CAMERA;
1266
1267 strcpy(i->name, iname[INPUT(n)->type]);
1268
1269 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1270 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1271 i->type = V4L2_INPUT_TYPE_TUNER;
1272
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001273 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001274
1275 return 0;
1276}
1277
1278static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1279{
1280 struct em28xx_fh *fh = priv;
1281 struct em28xx *dev = fh->dev;
1282
1283 *i = dev->ctl_input;
1284
1285 return 0;
1286}
1287
1288static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1289{
1290 struct em28xx_fh *fh = priv;
1291 struct em28xx *dev = fh->dev;
1292 int rc;
1293
1294 rc = check_dev(dev);
1295 if (rc < 0)
1296 return rc;
1297
1298 if (i >= MAX_EM28XX_INPUT)
1299 return -EINVAL;
1300 if (0 == INPUT(i)->type)
1301 return -EINVAL;
1302
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001303 dev->ctl_input = i;
1304
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001305 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001306 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307 mutex_unlock(&dev->lock);
1308 return 0;
1309}
1310
1311static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001315
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001316 if (!dev->audio_mode.has_audio)
1317 return -EINVAL;
1318
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001319 switch (a->index) {
1320 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001322 break;
1323 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001324 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001325 break;
1326 case EM28XX_AMUX_VIDEO2:
1327 strcpy(a->name, "Television alt");
1328 break;
1329 case EM28XX_AMUX_PHONE:
1330 strcpy(a->name, "Phone");
1331 break;
1332 case EM28XX_AMUX_MIC:
1333 strcpy(a->name, "Mic");
1334 break;
1335 case EM28XX_AMUX_CD:
1336 strcpy(a->name, "CD");
1337 break;
1338 case EM28XX_AMUX_AUX:
1339 strcpy(a->name, "Aux");
1340 break;
1341 case EM28XX_AMUX_PCM_OUT:
1342 strcpy(a->name, "PCM");
1343 break;
1344 default:
1345 return -EINVAL;
1346 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001347
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001348 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001349 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001350
1351 return 0;
1352}
1353
1354static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1355{
1356 struct em28xx_fh *fh = priv;
1357 struct em28xx *dev = fh->dev;
1358
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001359
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001360 if (!dev->audio_mode.has_audio)
1361 return -EINVAL;
1362
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001363 if (a->index >= MAX_EM28XX_INPUT)
1364 return -EINVAL;
1365 if (0 == INPUT(a->index)->type)
1366 return -EINVAL;
1367
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001368 mutex_lock(&dev->lock);
1369
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001370 dev->ctl_ainput = INPUT(a->index)->amux;
1371 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001372
1373 if (!dev->ctl_aoutput)
1374 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001375
1376 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377 return 0;
1378}
1379
1380static int vidioc_queryctrl(struct file *file, void *priv,
1381 struct v4l2_queryctrl *qc)
1382{
1383 struct em28xx_fh *fh = priv;
1384 struct em28xx *dev = fh->dev;
1385 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001386 int rc;
1387
1388 rc = check_dev(dev);
1389 if (rc < 0)
1390 return rc;
1391
1392 memset(qc, 0, sizeof(*qc));
1393
1394 qc->id = id;
1395
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001396 /* enumberate AC97 controls */
1397 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1398 rc = ac97_queryctrl(qc);
1399 if (!rc)
1400 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001402
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001403 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001404 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001405 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001406 mutex_unlock(&dev->lock);
1407
1408 if (qc->type)
1409 return 0;
1410 else
1411 return -EINVAL;
1412}
1413
1414static int vidioc_g_ctrl(struct file *file, void *priv,
1415 struct v4l2_control *ctrl)
1416{
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1419 int rc;
1420
1421 rc = check_dev(dev);
1422 if (rc < 0)
1423 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001424 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001425
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001426 mutex_lock(&dev->lock);
1427
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001428 /* Set an AC97 control */
1429 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1430 rc = ac97_get_ctrl(dev, ctrl);
1431 else
1432 rc = 1;
1433
1434 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1435 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001436 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001437 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001438 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439
1440 mutex_unlock(&dev->lock);
1441 return rc;
1442}
1443
1444static int vidioc_s_ctrl(struct file *file, void *priv,
1445 struct v4l2_control *ctrl)
1446{
1447 struct em28xx_fh *fh = priv;
1448 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001449 int rc;
1450
1451 rc = check_dev(dev);
1452 if (rc < 0)
1453 return rc;
1454
1455 mutex_lock(&dev->lock);
1456
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001457 /* Set an AC97 control */
1458 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1459 rc = ac97_set_ctrl(dev, ctrl);
1460 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001461 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001463 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001465 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001466
1467 /*
1468 * In the case of non-AC97 volume controls, we still need
1469 * to do some setups at em28xx, in order to mute/unmute
1470 * and to adjust audio volume. However, the value ranges
1471 * should be checked by the corresponding V4L subdriver.
1472 */
1473 switch (ctrl->id) {
1474 case V4L2_CID_AUDIO_MUTE:
1475 dev->mute = ctrl->value;
1476 rc = em28xx_audio_analog_set(dev);
1477 break;
1478 case V4L2_CID_AUDIO_VOLUME:
1479 dev->volume = ctrl->value;
1480 rc = em28xx_audio_analog_set(dev);
1481 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482 }
1483
1484 mutex_unlock(&dev->lock);
1485 return rc;
1486}
1487
1488static int vidioc_g_tuner(struct file *file, void *priv,
1489 struct v4l2_tuner *t)
1490{
1491 struct em28xx_fh *fh = priv;
1492 struct em28xx *dev = fh->dev;
1493 int rc;
1494
1495 rc = check_dev(dev);
1496 if (rc < 0)
1497 return rc;
1498
1499 if (0 != t->index)
1500 return -EINVAL;
1501
1502 strcpy(t->name, "Tuner");
1503
1504 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001505 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001506 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001507
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001508 return 0;
1509}
1510
1511static int vidioc_s_tuner(struct file *file, void *priv,
1512 struct v4l2_tuner *t)
1513{
1514 struct em28xx_fh *fh = priv;
1515 struct em28xx *dev = fh->dev;
1516 int rc;
1517
1518 rc = check_dev(dev);
1519 if (rc < 0)
1520 return rc;
1521
1522 if (0 != t->index)
1523 return -EINVAL;
1524
1525 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001526 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001527 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001528
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001529 return 0;
1530}
1531
1532static int vidioc_g_frequency(struct file *file, void *priv,
1533 struct v4l2_frequency *f)
1534{
1535 struct em28xx_fh *fh = priv;
1536 struct em28xx *dev = fh->dev;
1537
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001538 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001539 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001540 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001541 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001542
1543 return 0;
1544}
1545
1546static int vidioc_s_frequency(struct file *file, void *priv,
1547 struct v4l2_frequency *f)
1548{
1549 struct em28xx_fh *fh = priv;
1550 struct em28xx *dev = fh->dev;
1551 int rc;
1552
1553 rc = check_dev(dev);
1554 if (rc < 0)
1555 return rc;
1556
1557 if (0 != f->tuner)
1558 return -EINVAL;
1559
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001560 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1561 return -EINVAL;
1562 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001563 return -EINVAL;
1564
1565 mutex_lock(&dev->lock);
1566
1567 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001568 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001569
1570 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001571
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001572 return 0;
1573}
1574
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001575#ifdef CONFIG_VIDEO_ADV_DEBUG
1576static int em28xx_reg_len(int reg)
1577{
1578 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001579 case EM28XX_R40_AC97LSB:
1580 case EM28XX_R30_HSCALELOW:
1581 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001582 return 2;
1583 default:
1584 return 1;
1585 }
1586}
1587
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001588static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001589 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001590{
1591 struct em28xx_fh *fh = priv;
1592 struct em28xx *dev = fh->dev;
1593
1594 chip->ident = V4L2_IDENT_NONE;
1595 chip->revision = 0;
1596
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001597 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001598
1599 return 0;
1600}
1601
1602
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001603static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001604 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001605{
1606 struct em28xx_fh *fh = priv;
1607 struct em28xx *dev = fh->dev;
1608 int ret;
1609
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001610 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001611 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001612 mutex_lock(&dev->lock);
1613 ret = em28xx_read_ac97(dev, reg->reg);
1614 mutex_unlock(&dev->lock);
1615 if (ret < 0)
1616 return ret;
1617
1618 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001619 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001620 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001621 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001622 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001623 return 0;
1624 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001625 /* TODO: is this correct? */
1626 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1627 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001628 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001629 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001630 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001631 }
1632
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001633 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001634 reg->size = em28xx_reg_len(reg->reg);
1635 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001636 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001637 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001638 mutex_unlock(&dev->lock);
1639
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001640 if (ret < 0)
1641 return ret;
1642
1643 reg->val = ret;
1644 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001645 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001646 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001647 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1648 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001649 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001650 if (ret < 0)
1651 return ret;
1652
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001653 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001654 }
1655
1656 return 0;
1657}
1658
1659static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001660 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001661{
1662 struct em28xx_fh *fh = priv;
1663 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001664 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001665 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001666
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001667 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001668 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001669 mutex_lock(&dev->lock);
1670 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1671 mutex_unlock(&dev->lock);
1672
1673 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001674 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001675 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001676 return 0;
1677 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001678 /* TODO: is this correct? */
1679 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1680 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001681 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001682 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001683 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001684 }
1685
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001686 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001687 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001688
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001689 mutex_lock(&dev->lock);
1690 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1691 em28xx_reg_len(reg->reg));
1692 mutex_unlock(&dev->lock);
1693
1694 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001695}
1696#endif
1697
1698
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001699static int vidioc_cropcap(struct file *file, void *priv,
1700 struct v4l2_cropcap *cc)
1701{
1702 struct em28xx_fh *fh = priv;
1703 struct em28xx *dev = fh->dev;
1704
1705 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1706 return -EINVAL;
1707
1708 cc->bounds.left = 0;
1709 cc->bounds.top = 0;
1710 cc->bounds.width = dev->width;
1711 cc->bounds.height = dev->height;
1712 cc->defrect = cc->bounds;
1713 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1714 cc->pixelaspect.denominator = 59;
1715
1716 return 0;
1717}
1718
1719static int vidioc_streamon(struct file *file, void *priv,
1720 enum v4l2_buf_type type)
1721{
1722 struct em28xx_fh *fh = priv;
1723 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001724 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001725
1726 rc = check_dev(dev);
1727 if (rc < 0)
1728 return rc;
1729
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001730 if (unlikely(type != fh->type))
1731 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001732
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001733 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1734 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001735
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001736 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001737 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001738
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001739 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1740 rc = videobuf_streamon(&fh->vb_vidq);
1741 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1742 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001743
1744 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001745}
1746
1747static int vidioc_streamoff(struct file *file, void *priv,
1748 enum v4l2_buf_type type)
1749{
1750 struct em28xx_fh *fh = priv;
1751 struct em28xx *dev = fh->dev;
1752 int rc;
1753
1754 rc = check_dev(dev);
1755 if (rc < 0)
1756 return rc;
1757
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001758 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1759 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001760 return -EINVAL;
1761 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001762 return -EINVAL;
1763
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001764 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1765 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001766
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001767 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001768 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001769 res_free(fh, EM28XX_RESOURCE_VIDEO);
1770 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001771 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001772 res_free(fh, EM28XX_RESOURCE_VBI);
1773 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001774
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 return 0;
1776}
1777
1778static int vidioc_querycap(struct file *file, void *priv,
1779 struct v4l2_capability *cap)
1780{
1781 struct em28xx_fh *fh = priv;
1782 struct em28xx *dev = fh->dev;
1783
1784 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1785 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001786 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001787
1788 cap->version = EM28XX_VERSION_CODE;
1789
1790 cap->capabilities =
1791 V4L2_CAP_SLICED_VBI_CAPTURE |
1792 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1794
Devin Heitmueller04146142009-09-11 00:08:44 -03001795 if (dev->vbi_dev)
1796 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1797
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001798 if (dev->audio_mode.has_audio)
1799 cap->capabilities |= V4L2_CAP_AUDIO;
1800
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001801 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001802 cap->capabilities |= V4L2_CAP_TUNER;
1803
1804 return 0;
1805}
1806
Hans Verkuil78b526a2008-05-28 12:16:41 -03001807static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001808 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001809{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001810 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001811 return -EINVAL;
1812
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001813 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1814 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001815
1816 return 0;
1817}
1818
1819/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001820static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001821 struct v4l2_format *f)
1822{
1823 struct em28xx_fh *fh = priv;
1824 struct em28xx *dev = fh->dev;
1825 int rc;
1826
1827 rc = check_dev(dev);
1828 if (rc < 0)
1829 return rc;
1830
1831 mutex_lock(&dev->lock);
1832
1833 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001834 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001835
1836 if (f->fmt.sliced.service_set == 0)
1837 rc = -EINVAL;
1838
1839 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001840
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001841 return rc;
1842}
1843
Hans Verkuil78b526a2008-05-28 12:16:41 -03001844static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001845 struct v4l2_format *f)
1846{
1847 struct em28xx_fh *fh = priv;
1848 struct em28xx *dev = fh->dev;
1849 int rc;
1850
1851 rc = check_dev(dev);
1852 if (rc < 0)
1853 return rc;
1854
1855 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001856 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001857 mutex_unlock(&dev->lock);
1858
1859 if (f->fmt.sliced.service_set == 0)
1860 return -EINVAL;
1861
1862 return 0;
1863}
1864
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001865/* RAW VBI ioctls */
1866
1867static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1868 struct v4l2_format *format)
1869{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001870 struct em28xx_fh *fh = priv;
1871 struct em28xx *dev = fh->dev;
1872
1873 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001874 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1875 format->fmt.vbi.offset = 0;
1876 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001877 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1878 format->fmt.vbi.count[0] = dev->vbi_height;
1879 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001880
1881 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001882 if (dev->norm & V4L2_STD_525_60) {
1883 /* NTSC */
1884 format->fmt.vbi.start[0] = 10;
1885 format->fmt.vbi.start[1] = 273;
1886 } else if (dev->norm & V4L2_STD_625_50) {
1887 /* PAL */
1888 format->fmt.vbi.start[0] = 6;
1889 format->fmt.vbi.start[1] = 318;
1890 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001891
1892 return 0;
1893}
1894
1895static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1896 struct v4l2_format *format)
1897{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001898 struct em28xx_fh *fh = priv;
1899 struct em28xx *dev = fh->dev;
1900
1901 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001902 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1903 format->fmt.vbi.offset = 0;
1904 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001905 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1906 format->fmt.vbi.count[0] = dev->vbi_height;
1907 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001908
1909 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001910 if (dev->norm & V4L2_STD_525_60) {
1911 /* NTSC */
1912 format->fmt.vbi.start[0] = 10;
1913 format->fmt.vbi.start[1] = 273;
1914 } else if (dev->norm & V4L2_STD_625_50) {
1915 /* PAL */
1916 format->fmt.vbi.start[0] = 6;
1917 format->fmt.vbi.start[1] = 318;
1918 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001919
1920 return 0;
1921}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001922
1923static int vidioc_reqbufs(struct file *file, void *priv,
1924 struct v4l2_requestbuffers *rb)
1925{
1926 struct em28xx_fh *fh = priv;
1927 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001928 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_reqbufs(&fh->vb_vidq, rb);
1936 else
1937 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001938}
1939
1940static int vidioc_querybuf(struct file *file, void *priv,
1941 struct v4l2_buffer *b)
1942{
1943 struct em28xx_fh *fh = priv;
1944 struct em28xx *dev = fh->dev;
1945 int rc;
1946
1947 rc = check_dev(dev);
1948 if (rc < 0)
1949 return rc;
1950
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001951 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1952 return videobuf_querybuf(&fh->vb_vidq, b);
1953 else {
1954 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1955 the videobuf framework, but we probably shouldn't be
1956 returning a buffer larger than that which was asked for.
1957 At a minimum, it causes a crash in zvbi since it does
1958 a memcpy based on the source buffer length */
1959 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001960 b->length = dev->vbi_width * dev->vbi_height * 2;
1961
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001962 return result;
1963 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001964}
1965
1966static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1967{
1968 struct em28xx_fh *fh = priv;
1969 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001970 int rc;
1971
1972 rc = check_dev(dev);
1973 if (rc < 0)
1974 return rc;
1975
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001976 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1977 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001978 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001979 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001980}
1981
1982static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1983{
1984 struct em28xx_fh *fh = priv;
1985 struct em28xx *dev = fh->dev;
1986 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001987
1988 rc = check_dev(dev);
1989 if (rc < 0)
1990 return rc;
1991
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001992 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1993 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1994 O_NONBLOCK);
1995 else
1996 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1997 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001998}
1999
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002000#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002001static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002002{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002003 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002004
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002005 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2006 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
2007 else
2008 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002009}
2010#endif
2011
2012
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002013/* ----------------------------------------------------------- */
2014/* RADIO ESPECIFIC IOCTLS */
2015/* ----------------------------------------------------------- */
2016
2017static int radio_querycap(struct file *file, void *priv,
2018 struct v4l2_capability *cap)
2019{
2020 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2021
2022 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2023 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002024 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002025
2026 cap->version = EM28XX_VERSION_CODE;
2027 cap->capabilities = V4L2_CAP_TUNER;
2028 return 0;
2029}
2030
2031static int radio_g_tuner(struct file *file, void *priv,
2032 struct v4l2_tuner *t)
2033{
2034 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2035
2036 if (unlikely(t->index > 0))
2037 return -EINVAL;
2038
2039 strcpy(t->name, "Radio");
2040 t->type = V4L2_TUNER_RADIO;
2041
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002042 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002043 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002044 mutex_unlock(&dev->lock);
2045
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002046 return 0;
2047}
2048
2049static int radio_enum_input(struct file *file, void *priv,
2050 struct v4l2_input *i)
2051{
2052 if (i->index != 0)
2053 return -EINVAL;
2054 strcpy(i->name, "Radio");
2055 i->type = V4L2_INPUT_TYPE_TUNER;
2056
2057 return 0;
2058}
2059
2060static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2061{
2062 if (unlikely(a->index))
2063 return -EINVAL;
2064
2065 strcpy(a->name, "Radio");
2066 return 0;
2067}
2068
2069static int radio_s_tuner(struct file *file, void *priv,
2070 struct v4l2_tuner *t)
2071{
2072 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2073
2074 if (0 != t->index)
2075 return -EINVAL;
2076
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002077 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002078 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002079 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002080
2081 return 0;
2082}
2083
2084static int radio_s_audio(struct file *file, void *fh,
2085 struct v4l2_audio *a)
2086{
2087 return 0;
2088}
2089
2090static int radio_s_input(struct file *file, void *fh, unsigned int i)
2091{
2092 return 0;
2093}
2094
2095static int radio_queryctrl(struct file *file, void *priv,
2096 struct v4l2_queryctrl *qc)
2097{
2098 int i;
2099
2100 if (qc->id < V4L2_CID_BASE ||
2101 qc->id >= V4L2_CID_LASTP1)
2102 return -EINVAL;
2103
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002104 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2105 if (qc->id && qc->id == ac97_qctrl[i].id) {
2106 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002107 return 0;
2108 }
2109 }
2110
2111 return -EINVAL;
2112}
2113
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002114/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002115 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116 * inits the device and starts isoc transfer
2117 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002118static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002119{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002120 int errCode = 0, radio = 0;
2121 struct video_device *vdev = video_devdata(filp);
2122 struct em28xx *dev = video_drvdata(filp);
2123 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002124 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002125 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002126
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002127 switch (vdev->vfl_type) {
2128 case VFL_TYPE_GRABBER:
2129 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2130 break;
2131 case VFL_TYPE_VBI:
2132 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2133 break;
2134 case VFL_TYPE_RADIO:
2135 radio = 1;
2136 break;
2137 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002138
2139 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08002140
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002141 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2142 video_device_node_name(vdev), v4l2_type_names[fh_type],
2143 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002144
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002146 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002147 if (!fh) {
2148 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002149 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002150 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002151 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002152 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002153 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002154 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002155 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002156
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002157 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002158 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002159 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002160 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002161
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002162 /* Needed, since GPIO might have disabled power of
2163 some i2c device
2164 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002165 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002166
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002167 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002168 if (fh->radio) {
2169 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002170 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002171 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002172
2173 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002175 if (dev->progressive)
2176 field = V4L2_FIELD_NONE;
2177 else
2178 field = V4L2_FIELD_INTERLACED;
2179
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002180 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2181 NULL, &dev->slock,
2182 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2183 sizeof(struct em28xx_buffer), fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002184
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002185 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2186 NULL, &dev->slock,
2187 V4L2_BUF_TYPE_VBI_CAPTURE,
2188 V4L2_FIELD_SEQ_TB,
2189 sizeof(struct em28xx_buffer), fh);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002190
Ingo Molnar3593cab2006-02-07 06:49:14 -02002191 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002192
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002193 return errCode;
2194}
2195
2196/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002197 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198 * unregisters the v4l2,i2c and usb devices
2199 * called when the device gets disconected or at module unload
2200*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002201void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002204 /*FIXME: I2C IR should be disconnected */
2205
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002206 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002207 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002208 video_unregister_device(dev->radio_dev);
2209 else
2210 video_device_release(dev->radio_dev);
2211 dev->radio_dev = NULL;
2212 }
2213 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002214 em28xx_info("V4L2 device %s deregistered\n",
2215 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002216 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002217 video_unregister_device(dev->vbi_dev);
2218 else
2219 video_device_release(dev->vbi_dev);
2220 dev->vbi_dev = NULL;
2221 }
2222 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002223 em28xx_info("V4L2 device %s deregistered\n",
2224 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002225 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002226 video_unregister_device(dev->vdev);
2227 else
2228 video_device_release(dev->vdev);
2229 dev->vdev = NULL;
2230 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002231}
2232
2233/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002234 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002235 * stops streaming and deallocates all resources allocated by the v4l2
2236 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002238static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002240 struct em28xx_fh *fh = filp->private_data;
2241 struct em28xx *dev = fh->dev;
2242 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002243
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002244 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002245
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002246 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002247 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002248 res_free(fh, EM28XX_RESOURCE_VIDEO);
2249 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002250
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002251 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2252 videobuf_stop(&fh->vb_vbiq);
2253 res_free(fh, EM28XX_RESOURCE_VBI);
2254 }
2255
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002256 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002257 /* the device is already disconnect,
2258 free the remaining resources */
2259 if (dev->state & DEV_DISCONNECTED) {
2260 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002261 kfree(dev);
2262 return 0;
2263 }
2264
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002265 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002266 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002267
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002268 /* do this before setting alternate! */
2269 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002270 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002271
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002272 /* set alternate 0 */
2273 dev->alt = 0;
2274 em28xx_videodbg("setting alternate 0\n");
2275 errCode = usb_set_interface(dev->udev, 0, 0);
2276 if (errCode < 0) {
2277 em28xx_errdev("cannot change alternate number to "
2278 "0 (error=%i)\n", errCode);
2279 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002281
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002282 videobuf_mmap_free(&fh->vb_vidq);
2283 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002284 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002285 dev->users--;
2286 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287 return 0;
2288}
2289
2290/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002292 * will allocate buffers when called for the first time
2293 */
2294static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002295em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002296 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002298 struct em28xx_fh *fh = filp->private_data;
2299 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002300 int rc;
2301
2302 rc = check_dev(dev);
2303 if (rc < 0)
2304 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002306 /* FIXME: read() is not prepared to allow changing the video
2307 resolution while streaming. Seems a bug at em28xx_set_fmt
2308 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002309
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002310 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002311 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2312 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002313
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002314 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2315 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002316 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002317
2318
2319 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002320 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2321 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002322
2323 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2324 filp->f_flags & O_NONBLOCK);
2325 }
2326
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002327 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002328}
2329
2330/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002331 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002332 * will allocate buffers when called for the first time
2333 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002334static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002335{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002336 struct em28xx_fh *fh = filp->private_data;
2337 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002338 int rc;
2339
2340 rc = check_dev(dev);
2341 if (rc < 0)
2342 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002344 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2345 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2346 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002347 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002348 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2349 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2350 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002351 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002352 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002353 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002354 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002355}
2356
2357/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002358 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002359 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002360static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002361{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002362 struct em28xx_fh *fh = filp->private_data;
2363 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002364 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002365
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002366 rc = check_dev(dev);
2367 if (rc < 0)
2368 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002369
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002370 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2371 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2372 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2373 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002374
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002375 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2376 (unsigned long)vma->vm_start,
2377 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2378 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002379
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002380 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002381}
2382
Hans Verkuilbec43662008-12-30 06:58:20 -03002383static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002384 .owner = THIS_MODULE,
2385 .open = em28xx_v4l2_open,
2386 .release = em28xx_v4l2_close,
2387 .read = em28xx_v4l2_read,
2388 .poll = em28xx_v4l2_poll,
2389 .mmap = em28xx_v4l2_mmap,
2390 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002391};
2392
Hans Verkuila3998102008-07-21 02:57:38 -03002393static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002394 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002395 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2396 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2397 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2398 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002399 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2400 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002401 .vidioc_g_audio = vidioc_g_audio,
2402 .vidioc_s_audio = vidioc_s_audio,
2403 .vidioc_cropcap = vidioc_cropcap,
2404
Hans Verkuil78b526a2008-05-28 12:16:41 -03002405 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2406 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2407 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002408
2409 .vidioc_reqbufs = vidioc_reqbufs,
2410 .vidioc_querybuf = vidioc_querybuf,
2411 .vidioc_qbuf = vidioc_qbuf,
2412 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002413 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002414 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002415 .vidioc_g_parm = vidioc_g_parm,
2416 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002417 .vidioc_enum_input = vidioc_enum_input,
2418 .vidioc_g_input = vidioc_g_input,
2419 .vidioc_s_input = vidioc_s_input,
2420 .vidioc_queryctrl = vidioc_queryctrl,
2421 .vidioc_g_ctrl = vidioc_g_ctrl,
2422 .vidioc_s_ctrl = vidioc_s_ctrl,
2423 .vidioc_streamon = vidioc_streamon,
2424 .vidioc_streamoff = vidioc_streamoff,
2425 .vidioc_g_tuner = vidioc_g_tuner,
2426 .vidioc_s_tuner = vidioc_s_tuner,
2427 .vidioc_g_frequency = vidioc_g_frequency,
2428 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002429#ifdef CONFIG_VIDEO_ADV_DEBUG
2430 .vidioc_g_register = vidioc_g_register,
2431 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002432 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002433#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002434#ifdef CONFIG_VIDEO_V4L1_COMPAT
2435 .vidiocgmbuf = vidiocgmbuf,
2436#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002437};
2438
2439static const struct video_device em28xx_video_template = {
2440 .fops = &em28xx_v4l_fops,
2441 .release = video_device_release,
2442 .ioctl_ops = &video_ioctl_ops,
2443
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002444 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002445 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002446};
2447
Hans Verkuilbec43662008-12-30 06:58:20 -03002448static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002449 .owner = THIS_MODULE,
2450 .open = em28xx_v4l2_open,
2451 .release = em28xx_v4l2_close,
2452 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002453};
2454
2455static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002456 .vidioc_querycap = radio_querycap,
2457 .vidioc_g_tuner = radio_g_tuner,
2458 .vidioc_enum_input = radio_enum_input,
2459 .vidioc_g_audio = radio_g_audio,
2460 .vidioc_s_tuner = radio_s_tuner,
2461 .vidioc_s_audio = radio_s_audio,
2462 .vidioc_s_input = radio_s_input,
2463 .vidioc_queryctrl = radio_queryctrl,
2464 .vidioc_g_ctrl = vidioc_g_ctrl,
2465 .vidioc_s_ctrl = vidioc_s_ctrl,
2466 .vidioc_g_frequency = vidioc_g_frequency,
2467 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002468#ifdef CONFIG_VIDEO_ADV_DEBUG
2469 .vidioc_g_register = vidioc_g_register,
2470 .vidioc_s_register = vidioc_s_register,
2471#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002472};
2473
Hans Verkuila3998102008-07-21 02:57:38 -03002474static struct video_device em28xx_radio_template = {
2475 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002476 .fops = &radio_fops,
2477 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002478};
2479
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002480/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002481
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002482
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002483
Adrian Bunk532fe652008-01-28 22:10:48 -03002484static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002485 const struct video_device *template,
2486 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002487{
2488 struct video_device *vfd;
2489
2490 vfd = video_device_alloc();
2491 if (NULL == vfd)
2492 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002493
2494 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002495 vfd->v4l2_dev = &dev->v4l2_dev;
2496 vfd->release = video_device_release;
2497 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002498
2499 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2500 dev->name, type_name);
2501
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002502 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002503 return vfd;
2504}
2505
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002506int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002507{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002508 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002509 int ret;
2510
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002511 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2512 dev->name,
2513 (EM28XX_VERSION_CODE >> 16) & 0xff,
2514 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2515
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002516 /* set default norm */
2517 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002518 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002519 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002520
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002521 /* Analog specific initialization */
2522 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002523 em28xx_set_video_format(dev, format[0].fourcc,
2524 norm_maxw(dev), norm_maxh(dev));
2525
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002526 video_mux(dev, dev->ctl_input);
2527
2528 /* Audio defaults */
2529 dev->mute = 1;
2530 dev->volume = 0x1f;
2531
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002532/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002533 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2534 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2535 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002536
2537 em28xx_set_outfmt(dev);
2538 em28xx_colorlevels_set_default(dev);
2539 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002540
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002541 /* allocate and fill video video_device struct */
2542 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2543 if (!dev->vdev) {
2544 em28xx_errdev("cannot allocate video_device.\n");
2545 return -ENODEV;
2546 }
2547
2548 /* register v4l2 video video_device */
2549 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2550 video_nr[dev->devno]);
2551 if (ret) {
2552 em28xx_errdev("unable to register video device (error=%i).\n",
2553 ret);
2554 return ret;
2555 }
2556
2557 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002558 if (em28xx_vbi_supported(dev) == 1) {
2559 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2560 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002561
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002562 /* register v4l2 vbi video_device */
2563 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2564 vbi_nr[dev->devno]);
2565 if (ret < 0) {
2566 em28xx_errdev("unable to register vbi device\n");
2567 return ret;
2568 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002569 }
2570
2571 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002572 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2573 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002574 if (!dev->radio_dev) {
2575 em28xx_errdev("cannot allocate video_device.\n");
2576 return -ENODEV;
2577 }
2578 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2579 radio_nr[dev->devno]);
2580 if (ret < 0) {
2581 em28xx_errdev("can't register radio device\n");
2582 return ret;
2583 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002584 em28xx_info("Registered radio device as %s\n",
2585 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002586 }
2587
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002588 em28xx_info("V4L2 video device registered as %s\n",
2589 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002590
2591 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002592 em28xx_info("V4L2 VBI device registered as %s\n",
2593 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002594
2595 return 0;
2596}