blob: ac2bd935927e713e19e0ee67ef861f0154e33a86 [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>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080055 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030057 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030059static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030060module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030063#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030067 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030070
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020075static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030076static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030081module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082MODULE_PARM_DESC(video_nr, "video device numbers");
83MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080085
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030086static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030087module_param(video_debug, int, 0644);
88MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080089
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030090/* supported video standards */
91static struct em28xx_fmt format[] = {
92 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030093 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030094 .fourcc = V4L2_PIX_FMT_YUYV,
95 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030096 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030097 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030098 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .reg = EM28XX_OUTFMT_RGB_16_656,
102 }, {
103 .name = "8 bpp Bayer BGBG..GRGR",
104 .fourcc = V4L2_PIX_FMT_SBGGR8,
105 .depth = 8,
106 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
107 }, {
108 .name = "8 bpp Bayer GRGR..BGBG",
109 .fourcc = V4L2_PIX_FMT_SGRBG8,
110 .depth = 8,
111 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
112 }, {
113 .name = "8 bpp Bayer GBGB..RGRG",
114 .fourcc = V4L2_PIX_FMT_SGBRG8,
115 .depth = 8,
116 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
117 }, {
118 .name = "12 bpp YUV411",
119 .fourcc = V4L2_PIX_FMT_YUV411P,
120 .depth = 12,
121 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300122 },
123};
124
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800125/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200126/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300127static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129 .id = V4L2_CID_AUDIO_VOLUME,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "Volume",
132 .minimum = 0x0,
133 .maximum = 0x1f,
134 .step = 0x1,
135 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300136 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300137 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200138 .id = V4L2_CID_AUDIO_MUTE,
139 .type = V4L2_CTRL_TYPE_BOOLEAN,
140 .name = "Mute",
141 .minimum = 0,
142 .maximum = 1,
143 .step = 1,
144 .default_value = 1,
145 .flags = 0,
146 }
147};
148
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300149/* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
152
153/*
154 * Announces that a buffer were filled and request the next
155 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
159{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
162 buf->vb.state = VIDEOBUF_DONE;
163 buf->vb.field_count++;
164 do_gettimeofday(&buf->vb.ts);
165
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300166 dev->isoc_ctl.vid_buf = NULL;
167
168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
170}
171
172static inline void vbi_buffer_filled(struct em28xx *dev,
173 struct em28xx_dmaqueue *dma_q,
174 struct em28xx_buffer *buf)
175{
176 /* Advice that buffer was filled */
177 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
178
179 buf->vb.state = VIDEOBUF_DONE;
180 buf->vb.field_count++;
181 do_gettimeofday(&buf->vb.ts);
182
183 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300184
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300185 list_del(&buf->vb.queue);
186 wake_up(&buf->vb.done);
187}
188
189/*
190 * Identify the buffer header type and properly handles
191 */
192static void em28xx_copy_video(struct em28xx *dev,
193 struct em28xx_dmaqueue *dma_q,
194 struct em28xx_buffer *buf,
195 unsigned char *p,
196 unsigned char *outp, unsigned long len)
197{
198 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300199 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300200 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201
202 if (dma_q->pos + len > buf->vb.size)
203 len = buf->vb.size - dma_q->pos;
204
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300205 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206 em28xx_isocdbg("frame is not complete\n");
207 len += 4;
208 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300209 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210
211 startread = p;
212 remain = len;
213
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300214 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300216 else {
217 /* Interlaces two half frames */
218 if (buf->top_field)
219 fieldstart = outp;
220 else
221 fieldstart = outp + bytesperline;
222 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300224 linesdone = dma_q->pos / bytesperline;
225 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300226
227 if (dev->progressive)
228 offset = linesdone * bytesperline + currlinedone;
229 else
230 offset = linesdone * bytesperline * 2 + currlinedone;
231
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300232 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300233 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300234 lencopy = lencopy > remain ? remain : lencopy;
235
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300236 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300237 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300238 ((char *)startwrite + lencopy) -
239 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300240 remain = (char *)outp + buf->vb.size - (char *)startwrite;
241 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300242 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300243 if (lencopy <= 0)
244 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300245 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246
247 remain -= lencopy;
248
249 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300252 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300253 lencopy = remain;
254 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300255 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300256
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300257 if ((char *)startwrite + lencopy > (char *)outp +
258 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300259 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
260 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 ((char *)startwrite + lencopy) -
262 ((char *)outp + buf->vb.size));
263 lencopy = remain = (char *)outp + buf->vb.size -
264 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300265 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300266 if (lencopy <= 0)
267 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300268
269 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270
271 remain -= lencopy;
272 }
273
274 dma_q->pos += len;
275}
276
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300277static void em28xx_copy_vbi(struct em28xx *dev,
278 struct em28xx_dmaqueue *dma_q,
279 struct em28xx_buffer *buf,
280 unsigned char *p,
281 unsigned char *outp, unsigned long len)
282{
283 void *startwrite, *startread;
284 int offset;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300285 int bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300286
287 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300288 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300289 return;
290 }
291
292 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300293 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300294 return;
295 }
296 if (buf == NULL) {
297 return;
298 }
299 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300304 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300305 return;
306 }
307
308 if (dma_q->pos + len > buf->vb.size)
309 len = buf->vb.size - dma_q->pos;
310
311 if ((p[0] == 0x33 && p[1] == 0x95) ||
312 (p[0] == 0x88 && p[1] == 0x88)) {
313 /* Header field, advance past it */
314 p += 4;
315 } else {
316 len += 4;
317 }
318
319 startread = p;
320
321 startwrite = outp + dma_q->pos;
322 offset = dma_q->pos;
323
324 /* Make sure the bottom field populates the second half of the frame */
325 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300326 startwrite += bytesperline * dev->vbi_height;
327 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300328 }
329
330 memcpy(startwrite, startread, len);
331 dma_q->pos += len;
332}
333
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300334static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300335 int packet, int status)
336{
337 char *errmsg = "Unknown";
338
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300339 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340 case -ENOENT:
341 errmsg = "unlinked synchronuously";
342 break;
343 case -ECONNRESET:
344 errmsg = "unlinked asynchronuously";
345 break;
346 case -ENOSR:
347 errmsg = "Buffer error (overrun)";
348 break;
349 case -EPIPE:
350 errmsg = "Stalled (device not responding)";
351 break;
352 case -EOVERFLOW:
353 errmsg = "Babble (bad cable?)";
354 break;
355 case -EPROTO:
356 errmsg = "Bit-stuff error (bad cable?)";
357 break;
358 case -EILSEQ:
359 errmsg = "CRC/Timeout (could be anything)";
360 break;
361 case -ETIME:
362 errmsg = "Device does not respond";
363 break;
364 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300365 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
367 } else {
368 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
369 packet, status, errmsg);
370 }
371}
372
373/*
374 * video-buf generic routine to get the next available buffer
375 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300376static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377 struct em28xx_buffer **buf)
378{
379 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300381
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300382 if (list_empty(&dma_q->active)) {
383 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300385 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300386 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300387 }
388
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300389 /* Get the next buffer */
390 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
391
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300392 /* Cleans up buffer - Usefull for testing for frame/URB loss */
393 outp = videobuf_to_vmalloc(&(*buf)->vb);
394 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300395
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300396 dev->isoc_ctl.vid_buf = *buf;
397
398 return;
399}
400
401/*
402 * video-buf generic routine to get the next available VBI buffer
403 */
404static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
405 struct em28xx_buffer **buf)
406{
407 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
408 char *outp;
409
410 if (list_empty(&dma_q->active)) {
411 em28xx_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.vbi_buf = NULL;
413 *buf = NULL;
414 return;
415 }
416
417 /* Get the next buffer */
418 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
419 /* Cleans up buffer - Usefull for testing for frame/URB loss */
420 outp = videobuf_to_vmalloc(&(*buf)->vb);
421 memset(outp, 0x00, (*buf)->vb.size);
422
423 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300424
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300425 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426}
427
428/*
429 * Controls the isoc copy of each urb packet
430 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300431static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300432{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300433 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300434 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300435 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300437 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438
439 if (!dev)
440 return 0;
441
442 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
443 return 0;
444
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300445 if (urb->status < 0) {
446 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447 if (urb->status == -ENOENT)
448 return 0;
449 }
450
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300451 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300452 if (buf != NULL)
453 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 for (i = 0; i < urb->number_of_packets; i++) {
456 int status = urb->iso_frame_desc[i].status;
457
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 if (status < 0) {
459 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460 if (urb->iso_frame_desc[i].status != -EPROTO)
461 continue;
462 }
463
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300464 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465
466 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300467 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300468 continue;
469 }
470 if (urb->iso_frame_desc[i].actual_length >
471 dev->max_pkt_size) {
472 em28xx_isocdbg("packet bigger than packet size");
473 continue;
474 }
475
476 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300477
478 /* FIXME: incomplete buffer checks where removed to make
479 logic simpler. Impacts of those changes should be evaluated
480 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300481 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
482 em28xx_isocdbg("VBI HEADER!!!\n");
483 /* FIXME: Should add vbi copy */
484 continue;
485 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300486 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300487 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300488 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300490 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300491 if (buf != NULL)
492 buffer_filled(dev, dma_q, buf);
493 get_next_buf(dma_q, &buf);
494 if (buf == NULL)
495 outp = NULL;
496 else
497 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300498 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300499
500 if (buf != NULL) {
501 if (p[2] & 1)
502 buf->top_field = 0;
503 else
504 buf->top_field = 1;
505 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300506
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300509 if (buf != NULL)
510 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300511 }
512 return rc;
513}
514
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300515/* Version of isoc handler that takes into account a mixture of video and
516 VBI data */
517static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
518{
519 struct em28xx_buffer *buf, *vbi_buf;
520 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300521 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300522 unsigned char *outp = NULL;
523 unsigned char *vbioutp = NULL;
524 int i, len = 0, rc = 1;
525 unsigned char *p;
526 int vbi_size;
527
528 if (!dev)
529 return 0;
530
531 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
532 return 0;
533
534 if (urb->status < 0) {
535 print_err_status(dev, -1, urb->status);
536 if (urb->status == -ENOENT)
537 return 0;
538 }
539
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300540 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300541 if (buf != NULL)
542 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300543
544 vbi_buf = dev->isoc_ctl.vbi_buf;
545 if (vbi_buf != NULL)
546 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
547
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300548 for (i = 0; i < urb->number_of_packets; i++) {
549 int status = urb->iso_frame_desc[i].status;
550
551 if (status < 0) {
552 print_err_status(dev, i, status);
553 if (urb->iso_frame_desc[i].status != -EPROTO)
554 continue;
555 }
556
557 len = urb->iso_frame_desc[i].actual_length - 4;
558
559 if (urb->iso_frame_desc[i].actual_length <= 0) {
560 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
561 continue;
562 }
563 if (urb->iso_frame_desc[i].actual_length >
564 dev->max_pkt_size) {
565 em28xx_isocdbg("packet bigger than packet size");
566 continue;
567 }
568
569 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
570
571 /* capture type 0 = vbi start
572 capture type 1 = video start
573 capture type 2 = video in progress */
574 if (p[0] == 0x33 && p[1] == 0x95) {
575 dev->capture_type = 0;
576 dev->vbi_read = 0;
577 em28xx_isocdbg("VBI START HEADER!!!\n");
578 dev->cur_field = p[2];
579 }
580
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300581 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300582
583 if (dev->capture_type == 0) {
584 if (dev->vbi_read >= vbi_size) {
585 /* We've already read all the VBI data, so
586 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300587 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300588 } else if ((dev->vbi_read + len) < vbi_size) {
589 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300590 if (dev->vbi_read == 0 &&
591 (!(dev->cur_field & 1))) {
592 /* Brand new frame */
593 if (vbi_buf != NULL)
594 vbi_buffer_filled(dev,
595 vbi_dma_q,
596 vbi_buf);
597 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
598 if (vbi_buf == NULL)
599 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300600 else
601 vbioutp = videobuf_to_vmalloc(
602 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300603 }
604
605 if (dev->vbi_read == 0) {
606 vbi_dma_q->pos = 0;
607 if (vbi_buf != NULL) {
608 if (dev->cur_field & 1)
609 vbi_buf->top_field = 0;
610 else
611 vbi_buf->top_field = 1;
612 }
613 }
614
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300615 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300616 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
617 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300618 } else {
619 /* Some of this frame is VBI data and some is
620 video data */
621 int vbi_data_len = vbi_size - dev->vbi_read;
622 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300623 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
624 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300625 dev->capture_type = 1;
626 p += vbi_data_len;
627 len -= vbi_data_len;
628 }
629 }
630
631 if (dev->capture_type == 1) {
632 dev->capture_type = 2;
633 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
634 len, (p[2] & 1) ? "odd" : "even");
635
636 if (dev->progressive || !(dev->cur_field & 1)) {
637 if (buf != NULL)
638 buffer_filled(dev, dma_q, buf);
639 get_next_buf(dma_q, &buf);
640 if (buf == NULL)
641 outp = NULL;
642 else
643 outp = videobuf_to_vmalloc(&buf->vb);
644 }
645 if (buf != NULL) {
646 if (dev->cur_field & 1)
647 buf->top_field = 0;
648 else
649 buf->top_field = 1;
650 }
651
652 dma_q->pos = 0;
653 }
654 if (buf != NULL && dev->capture_type == 2)
655 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
656 }
657 return rc;
658}
659
660
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300662 Videobuf operations
663 ------------------------------------------------------------------*/
664
665static int
666buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
667{
668 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300669 struct em28xx *dev = fh->dev;
670 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300671
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300672 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
673 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300674
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300675 if (0 == *count)
676 *count = EM28XX_DEF_BUF;
677
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300678 if (*count < EM28XX_MIN_BUF)
679 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300680
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300681 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300682 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300683 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300684 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300685
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300686 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300687
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300688 return 0;
689}
690
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300691/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300692static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
693{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300694 struct em28xx_fh *fh = vq->priv_data;
695 struct em28xx *dev = fh->dev;
696 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300697 if (in_interrupt())
698 BUG();
699
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300700 /* We used to wait for the buffer to finish here, but this didn't work
701 because, as we were keeping the state as VIDEOBUF_QUEUED,
702 videobuf_queue_cancel marked it as finished for us.
703 (Also, it could wedge forever if the hardware was misconfigured.)
704
705 This should be safe; by the time we get here, the buffer isn't
706 queued anymore. If we ever start marking the buffers as
707 VIDEOBUF_ACTIVE, it won't be, though.
708 */
709 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300710 if (dev->isoc_ctl.vid_buf == buf)
711 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300712 spin_unlock_irqrestore(&dev->slock, flags);
713
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300714 videobuf_vmalloc_free(&buf->vb);
715 buf->vb.state = VIDEOBUF_NEEDS_INIT;
716}
717
718static int
719buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
720 enum v4l2_field field)
721{
722 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300723 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300724 struct em28xx *dev = fh->dev;
725 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300726
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300727 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
728 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300729
730 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
731 return -EINVAL;
732
Brandon Philips05612972008-04-13 14:57:01 -0300733 buf->vb.width = dev->width;
734 buf->vb.height = dev->height;
735 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300736
737 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300738 rc = videobuf_iolock(vq, &buf->vb, NULL);
739 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300740 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 }
742
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300743 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300744 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300745
746 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300747 if (em28xx_vbi_supported(dev) == 1)
748 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
749 EM28XX_NUM_BUFS,
750 dev->max_pkt_size,
751 em28xx_isoc_copy_vbi);
752 else
753 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
754 EM28XX_NUM_BUFS,
755 dev->max_pkt_size,
756 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300757 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300758 goto fail;
759 }
760
761 buf->vb.state = VIDEOBUF_PREPARED;
762 return 0;
763
764fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300765 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300766 return rc;
767}
768
769static void
770buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
771{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300772 struct em28xx_buffer *buf = container_of(vb,
773 struct em28xx_buffer,
774 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300775 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300776 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300777 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300778
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300779 buf->vb.state = VIDEOBUF_QUEUED;
780 list_add_tail(&buf->vb.queue, &vidq->active);
781
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300782}
783
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300784static void buffer_release(struct videobuf_queue *vq,
785 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300786{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300787 struct em28xx_buffer *buf = container_of(vb,
788 struct em28xx_buffer,
789 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300790 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300791 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300792
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300793 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300794
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300795 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300796}
797
798static struct videobuf_queue_ops em28xx_video_qops = {
799 .buf_setup = buffer_setup,
800 .buf_prepare = buffer_prepare,
801 .buf_queue = buffer_queue,
802 .buf_release = buffer_release,
803};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800804
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300805/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800806
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800807static void video_mux(struct em28xx *dev, int index)
808{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800809 dev->ctl_input = index;
810 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300811 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800812
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300813 if (!dev->ctl_aoutput)
814 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
815
Hans Verkuil5325b422009-04-02 11:26:22 -0300816 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
817 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800818
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300819 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300820 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300821 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
822 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300823 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300824 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300825 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
826 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800827 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300828
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300829 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300830 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
831 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300832 }
833
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300834 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800835}
836
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300837/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300838static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300839{
840 struct em28xx *dev = fh->dev;
841
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300842 if (fh->resources & bit)
843 /* have it already allocated */
844 return 1;
845
846 /* is it free? */
847 mutex_lock(&dev->lock);
848 if (dev->resources & bit) {
849 /* no, someone else uses it */
850 mutex_unlock(&dev->lock);
851 return 0;
852 }
853 /* it's free, grab it */
854 fh->resources |= bit;
855 dev->resources |= bit;
856 em28xx_videodbg("res: get %d\n", bit);
857 mutex_unlock(&dev->lock);
858 return 1;
859}
860
861static int res_check(struct em28xx_fh *fh, unsigned int bit)
862{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300863 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300864}
865
866static int res_locked(struct em28xx *dev, unsigned int bit)
867{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300868 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869}
870
871static void res_free(struct em28xx_fh *fh, unsigned int bits)
872{
873 struct em28xx *dev = fh->dev;
874
875 BUG_ON((fh->resources & bits) != bits);
876
877 mutex_lock(&dev->lock);
878 fh->resources &= ~bits;
879 dev->resources &= ~bits;
880 em28xx_videodbg("res: put %d\n", bits);
881 mutex_unlock(&dev->lock);
882}
883
884static int get_ressource(struct em28xx_fh *fh)
885{
886 switch (fh->type) {
887 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
888 return EM28XX_RESOURCE_VIDEO;
889 case V4L2_BUF_TYPE_VBI_CAPTURE:
890 return EM28XX_RESOURCE_VBI;
891 default:
892 BUG();
893 return 0;
894 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300895}
896
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800897/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300898 * ac97_queryctrl()
899 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300900 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300901static int ac97_queryctrl(struct v4l2_queryctrl *qc)
902{
903 int i;
904
905 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
906 if (qc->id && qc->id == ac97_qctrl[i].id) {
907 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
908 return 0;
909 }
910 }
911
912 /* Control is not ac97 related */
913 return 1;
914}
915
916/*
917 * ac97_get_ctrl()
918 * return the current values for ac97 mute and volume
919 */
920static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300921{
922 switch (ctrl->id) {
923 case V4L2_CID_AUDIO_MUTE:
924 ctrl->value = dev->mute;
925 return 0;
926 case V4L2_CID_AUDIO_VOLUME:
927 ctrl->value = dev->volume;
928 return 0;
929 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300930 /* Control is not ac97 related */
931 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932 }
933}
934
935/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300936 * ac97_set_ctrl()
937 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300939static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300940{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300941 int i;
942
943 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
944 if (ctrl->id == ac97_qctrl[i].id)
945 goto handle;
946
947 /* Announce that hasn't handle it */
948 return 1;
949
950handle:
951 if (ctrl->value < ac97_qctrl[i].minimum ||
952 ctrl->value > ac97_qctrl[i].maximum)
953 return -ERANGE;
954
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955 switch (ctrl->id) {
956 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300957 dev->mute = ctrl->value;
958 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300959 case V4L2_CID_AUDIO_VOLUME:
960 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300961 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300963
964 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965}
966
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300967static int check_dev(struct em28xx *dev)
968{
969 if (dev->state & DEV_DISCONNECTED) {
970 em28xx_errdev("v4l2 ioctl: device not present\n");
971 return -ENODEV;
972 }
973
974 if (dev->state & DEV_MISCONFIGURED) {
975 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
976 "close and open it again\n");
977 return -EIO;
978 }
979 return 0;
980}
981
982static void get_scale(struct em28xx *dev,
983 unsigned int width, unsigned int height,
984 unsigned int *hscale, unsigned int *vscale)
985{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300986 unsigned int maxw = norm_maxw(dev);
987 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988
989 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
990 if (*hscale >= 0x4000)
991 *hscale = 0x3fff;
992
993 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
994 if (*vscale >= 0x4000)
995 *vscale = 0x3fff;
996}
997
998/* ------------------------------------------------------------------
999 IOCTL vidioc handling
1000 ------------------------------------------------------------------*/
1001
Hans Verkuil78b526a2008-05-28 12:16:41 -03001002static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001003 struct v4l2_format *f)
1004{
1005 struct em28xx_fh *fh = priv;
1006 struct em28xx *dev = fh->dev;
1007
1008 mutex_lock(&dev->lock);
1009
1010 f->fmt.pix.width = dev->width;
1011 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001012 f->fmt.pix.pixelformat = dev->format->fourcc;
1013 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001014 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001015 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1016
1017 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001018 if (dev->progressive)
1019 f->fmt.pix.field = V4L2_FIELD_NONE;
1020 else
1021 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001022 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1023
1024 mutex_unlock(&dev->lock);
1025 return 0;
1026}
1027
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001028static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1029{
1030 unsigned int i;
1031
1032 for (i = 0; i < ARRAY_SIZE(format); i++)
1033 if (format[i].fourcc == fourcc)
1034 return &format[i];
1035
1036 return NULL;
1037}
1038
Hans Verkuil78b526a2008-05-28 12:16:41 -03001039static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001040 struct v4l2_format *f)
1041{
1042 struct em28xx_fh *fh = priv;
1043 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001044 unsigned int width = f->fmt.pix.width;
1045 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001046 unsigned int maxw = norm_maxw(dev);
1047 unsigned int maxh = norm_maxh(dev);
1048 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001049 struct em28xx_fmt *fmt;
1050
1051 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1052 if (!fmt) {
1053 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1054 f->fmt.pix.pixelformat);
1055 return -EINVAL;
1056 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001057
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001058 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001060 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1061 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001062 } else {
1063 /* width must even because of the YUYV format
1064 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001065 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1066 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001067 }
1068
1069 get_scale(dev, width, height, &hscale, &vscale);
1070
1071 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1072 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1073
1074 f->fmt.pix.width = width;
1075 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001076 f->fmt.pix.pixelformat = fmt->fourcc;
1077 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1078 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001079 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001080 if (dev->progressive)
1081 f->fmt.pix.field = V4L2_FIELD_NONE;
1082 else
1083 f->fmt.pix.field = dev->interlaced ?
1084 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001085
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001086 return 0;
1087}
1088
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001089static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1090 unsigned width, unsigned height)
1091{
1092 struct em28xx_fmt *fmt;
1093
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001094 fmt = format_by_fourcc(fourcc);
1095 if (!fmt)
1096 return -EINVAL;
1097
1098 dev->format = fmt;
1099 dev->width = width;
1100 dev->height = height;
1101
1102 /* set new image size */
1103 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1104
1105 em28xx_set_alternate(dev);
1106 em28xx_resolution_set(dev);
1107
1108 return 0;
1109}
1110
Hans Verkuil78b526a2008-05-28 12:16:41 -03001111static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001112 struct v4l2_format *f)
1113{
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001116 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001117
1118 rc = check_dev(dev);
1119 if (rc < 0)
1120 return rc;
1121
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001122 mutex_lock(&dev->lock);
1123
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001124 vidioc_try_fmt_vid_cap(file, priv, f);
1125
Brandon Philips05612972008-04-13 14:57:01 -03001126 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1127 em28xx_errdev("%s queue busy\n", __func__);
1128 rc = -EBUSY;
1129 goto out;
1130 }
1131
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001132 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1133 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -03001134
1135out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001136 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -03001137 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001138}
1139
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001140static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1141{
1142 struct em28xx_fh *fh = priv;
1143 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001144 int rc;
1145
1146 rc = check_dev(dev);
1147 if (rc < 0)
1148 return rc;
1149
1150 *norm = dev->norm;
1151
1152 return 0;
1153}
1154
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001155static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001156{
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160 int rc;
1161
1162 rc = check_dev(dev);
1163 if (rc < 0)
1164 return rc;
1165
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001166 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001167 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001168
1169 /* Adjusts width/height, if needed */
1170 f.fmt.pix.width = dev->width;
1171 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001172 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001173
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001174 /* set new image size */
1175 dev->width = f.fmt.pix.width;
1176 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001177 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1178
1179 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001180 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181
1182 mutex_unlock(&dev->lock);
1183 return 0;
1184}
1185
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001186static int vidioc_g_parm(struct file *file, void *priv,
1187 struct v4l2_streamparm *p)
1188{
1189 struct em28xx_fh *fh = priv;
1190 struct em28xx *dev = fh->dev;
1191 int rc = 0;
1192
1193 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1194 return -EINVAL;
1195
1196 if (dev->board.is_webcam)
1197 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1198 video, g_parm, p);
1199 else
1200 v4l2_video_std_frame_period(dev->norm,
1201 &p->parm.capture.timeperframe);
1202
1203 return rc;
1204}
1205
1206static int vidioc_s_parm(struct file *file, void *priv,
1207 struct v4l2_streamparm *p)
1208{
1209 struct em28xx_fh *fh = priv;
1210 struct em28xx *dev = fh->dev;
1211
1212 if (!dev->board.is_webcam)
1213 return -EINVAL;
1214
1215 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1216 return -EINVAL;
1217
1218 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1219}
1220
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001221static const char *iname[] = {
1222 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1223 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1224 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1225 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1226 [EM28XX_VMUX_SVIDEO] = "S-Video",
1227 [EM28XX_VMUX_TELEVISION] = "Television",
1228 [EM28XX_VMUX_CABLE] = "Cable TV",
1229 [EM28XX_VMUX_DVB] = "DVB",
1230 [EM28XX_VMUX_DEBUG] = "for debug only",
1231};
1232
1233static int vidioc_enum_input(struct file *file, void *priv,
1234 struct v4l2_input *i)
1235{
1236 struct em28xx_fh *fh = priv;
1237 struct em28xx *dev = fh->dev;
1238 unsigned int n;
1239
1240 n = i->index;
1241 if (n >= MAX_EM28XX_INPUT)
1242 return -EINVAL;
1243 if (0 == INPUT(n)->type)
1244 return -EINVAL;
1245
1246 i->index = n;
1247 i->type = V4L2_INPUT_TYPE_CAMERA;
1248
1249 strcpy(i->name, iname[INPUT(n)->type]);
1250
1251 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1252 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1253 i->type = V4L2_INPUT_TYPE_TUNER;
1254
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001255 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001256
1257 return 0;
1258}
1259
1260static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1261{
1262 struct em28xx_fh *fh = priv;
1263 struct em28xx *dev = fh->dev;
1264
1265 *i = dev->ctl_input;
1266
1267 return 0;
1268}
1269
1270static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1271{
1272 struct em28xx_fh *fh = priv;
1273 struct em28xx *dev = fh->dev;
1274 int rc;
1275
1276 rc = check_dev(dev);
1277 if (rc < 0)
1278 return rc;
1279
1280 if (i >= MAX_EM28XX_INPUT)
1281 return -EINVAL;
1282 if (0 == INPUT(i)->type)
1283 return -EINVAL;
1284
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001285 dev->ctl_input = i;
1286
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001287 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001288 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001289 mutex_unlock(&dev->lock);
1290 return 0;
1291}
1292
1293static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1294{
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001298 if (!dev->audio_mode.has_audio)
1299 return -EINVAL;
1300
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001301 switch (a->index) {
1302 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001303 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001304 break;
1305 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001307 break;
1308 case EM28XX_AMUX_VIDEO2:
1309 strcpy(a->name, "Television alt");
1310 break;
1311 case EM28XX_AMUX_PHONE:
1312 strcpy(a->name, "Phone");
1313 break;
1314 case EM28XX_AMUX_MIC:
1315 strcpy(a->name, "Mic");
1316 break;
1317 case EM28XX_AMUX_CD:
1318 strcpy(a->name, "CD");
1319 break;
1320 case EM28XX_AMUX_AUX:
1321 strcpy(a->name, "Aux");
1322 break;
1323 case EM28XX_AMUX_PCM_OUT:
1324 strcpy(a->name, "PCM");
1325 break;
1326 default:
1327 return -EINVAL;
1328 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001329
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001330 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332
1333 return 0;
1334}
1335
1336static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1337{
1338 struct em28xx_fh *fh = priv;
1339 struct em28xx *dev = fh->dev;
1340
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001341
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001342 if (!dev->audio_mode.has_audio)
1343 return -EINVAL;
1344
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001345 if (a->index >= MAX_EM28XX_INPUT)
1346 return -EINVAL;
1347 if (0 == INPUT(a->index)->type)
1348 return -EINVAL;
1349
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001350 mutex_lock(&dev->lock);
1351
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001352 dev->ctl_ainput = INPUT(a->index)->amux;
1353 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001354
1355 if (!dev->ctl_aoutput)
1356 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001357
1358 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001359 return 0;
1360}
1361
1362static int vidioc_queryctrl(struct file *file, void *priv,
1363 struct v4l2_queryctrl *qc)
1364{
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
1367 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368 int rc;
1369
1370 rc = check_dev(dev);
1371 if (rc < 0)
1372 return rc;
1373
1374 memset(qc, 0, sizeof(*qc));
1375
1376 qc->id = id;
1377
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001378 /* enumberate AC97 controls */
1379 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1380 rc = ac97_queryctrl(qc);
1381 if (!rc)
1382 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001384
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001385 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001386 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001387 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001388 mutex_unlock(&dev->lock);
1389
1390 if (qc->type)
1391 return 0;
1392 else
1393 return -EINVAL;
1394}
1395
1396static int vidioc_g_ctrl(struct file *file, void *priv,
1397 struct v4l2_control *ctrl)
1398{
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1401 int rc;
1402
1403 rc = check_dev(dev);
1404 if (rc < 0)
1405 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001406 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001407
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408 mutex_lock(&dev->lock);
1409
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001410 /* Set an AC97 control */
1411 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1412 rc = ac97_get_ctrl(dev, ctrl);
1413 else
1414 rc = 1;
1415
1416 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1417 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001418 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001419 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001420 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421
1422 mutex_unlock(&dev->lock);
1423 return rc;
1424}
1425
1426static int vidioc_s_ctrl(struct file *file, void *priv,
1427 struct v4l2_control *ctrl)
1428{
1429 struct em28xx_fh *fh = priv;
1430 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001431 int rc;
1432
1433 rc = check_dev(dev);
1434 if (rc < 0)
1435 return rc;
1436
1437 mutex_lock(&dev->lock);
1438
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001439 /* Set an AC97 control */
1440 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1441 rc = ac97_set_ctrl(dev, ctrl);
1442 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001444
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001445 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001446 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001447 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001448
1449 /*
1450 * In the case of non-AC97 volume controls, we still need
1451 * to do some setups at em28xx, in order to mute/unmute
1452 * and to adjust audio volume. However, the value ranges
1453 * should be checked by the corresponding V4L subdriver.
1454 */
1455 switch (ctrl->id) {
1456 case V4L2_CID_AUDIO_MUTE:
1457 dev->mute = ctrl->value;
1458 rc = em28xx_audio_analog_set(dev);
1459 break;
1460 case V4L2_CID_AUDIO_VOLUME:
1461 dev->volume = ctrl->value;
1462 rc = em28xx_audio_analog_set(dev);
1463 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001464 }
1465
1466 mutex_unlock(&dev->lock);
1467 return rc;
1468}
1469
1470static int vidioc_g_tuner(struct file *file, void *priv,
1471 struct v4l2_tuner *t)
1472{
1473 struct em28xx_fh *fh = priv;
1474 struct em28xx *dev = fh->dev;
1475 int rc;
1476
1477 rc = check_dev(dev);
1478 if (rc < 0)
1479 return rc;
1480
1481 if (0 != t->index)
1482 return -EINVAL;
1483
1484 strcpy(t->name, "Tuner");
1485
1486 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001487 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001489
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001490 return 0;
1491}
1492
1493static int vidioc_s_tuner(struct file *file, void *priv,
1494 struct v4l2_tuner *t)
1495{
1496 struct em28xx_fh *fh = priv;
1497 struct em28xx *dev = fh->dev;
1498 int rc;
1499
1500 rc = check_dev(dev);
1501 if (rc < 0)
1502 return rc;
1503
1504 if (0 != t->index)
1505 return -EINVAL;
1506
1507 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001508 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001509 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001510
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001511 return 0;
1512}
1513
1514static int vidioc_g_frequency(struct file *file, void *priv,
1515 struct v4l2_frequency *f)
1516{
1517 struct em28xx_fh *fh = priv;
1518 struct em28xx *dev = fh->dev;
1519
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001520 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001521 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001522 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001523 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001524
1525 return 0;
1526}
1527
1528static int vidioc_s_frequency(struct file *file, void *priv,
1529 struct v4l2_frequency *f)
1530{
1531 struct em28xx_fh *fh = priv;
1532 struct em28xx *dev = fh->dev;
1533 int rc;
1534
1535 rc = check_dev(dev);
1536 if (rc < 0)
1537 return rc;
1538
1539 if (0 != f->tuner)
1540 return -EINVAL;
1541
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001542 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1543 return -EINVAL;
1544 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001545 return -EINVAL;
1546
1547 mutex_lock(&dev->lock);
1548
1549 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001550 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001551
1552 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001553
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001554 return 0;
1555}
1556
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001557#ifdef CONFIG_VIDEO_ADV_DEBUG
1558static int em28xx_reg_len(int reg)
1559{
1560 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001561 case EM28XX_R40_AC97LSB:
1562 case EM28XX_R30_HSCALELOW:
1563 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001564 return 2;
1565 default:
1566 return 1;
1567 }
1568}
1569
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001570static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001571 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001572{
1573 struct em28xx_fh *fh = priv;
1574 struct em28xx *dev = fh->dev;
1575
1576 chip->ident = V4L2_IDENT_NONE;
1577 chip->revision = 0;
1578
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001579 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001580
1581 return 0;
1582}
1583
1584
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001585static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001586 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001587{
1588 struct em28xx_fh *fh = priv;
1589 struct em28xx *dev = fh->dev;
1590 int ret;
1591
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001592 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001593 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001594 mutex_lock(&dev->lock);
1595 ret = em28xx_read_ac97(dev, reg->reg);
1596 mutex_unlock(&dev->lock);
1597 if (ret < 0)
1598 return ret;
1599
1600 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001601 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001602 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001603 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001604 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001605 return 0;
1606 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001607 /* TODO: is this correct? */
1608 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1609 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001610 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001611 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001612 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001613 }
1614
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001615 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001616 reg->size = em28xx_reg_len(reg->reg);
1617 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001618 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001619 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001620 mutex_unlock(&dev->lock);
1621
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001622 if (ret < 0)
1623 return ret;
1624
1625 reg->val = ret;
1626 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001627 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001628 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001629 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1630 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001631 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001632 if (ret < 0)
1633 return ret;
1634
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001635 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001636 }
1637
1638 return 0;
1639}
1640
1641static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001642 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643{
1644 struct em28xx_fh *fh = priv;
1645 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001646 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001647 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001648
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001649 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001650 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001651 mutex_lock(&dev->lock);
1652 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1653 mutex_unlock(&dev->lock);
1654
1655 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001656 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001657 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001658 return 0;
1659 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001660 /* TODO: is this correct? */
1661 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1662 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001663 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001664 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001665 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001666 }
1667
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001668 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001669 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001670
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001671 mutex_lock(&dev->lock);
1672 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1673 em28xx_reg_len(reg->reg));
1674 mutex_unlock(&dev->lock);
1675
1676 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001677}
1678#endif
1679
1680
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001681static int vidioc_cropcap(struct file *file, void *priv,
1682 struct v4l2_cropcap *cc)
1683{
1684 struct em28xx_fh *fh = priv;
1685 struct em28xx *dev = fh->dev;
1686
1687 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1688 return -EINVAL;
1689
1690 cc->bounds.left = 0;
1691 cc->bounds.top = 0;
1692 cc->bounds.width = dev->width;
1693 cc->bounds.height = dev->height;
1694 cc->defrect = cc->bounds;
1695 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1696 cc->pixelaspect.denominator = 59;
1697
1698 return 0;
1699}
1700
1701static int vidioc_streamon(struct file *file, void *priv,
1702 enum v4l2_buf_type type)
1703{
1704 struct em28xx_fh *fh = priv;
1705 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001706 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001707
1708 rc = check_dev(dev);
1709 if (rc < 0)
1710 return rc;
1711
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001712 if (unlikely(type != fh->type))
1713 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001714
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001715 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1716 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001717
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001718 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001720
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001721 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1722 rc = videobuf_streamon(&fh->vb_vidq);
1723 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1724 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001725
1726 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001727}
1728
1729static int vidioc_streamoff(struct file *file, void *priv,
1730 enum v4l2_buf_type type)
1731{
1732 struct em28xx_fh *fh = priv;
1733 struct em28xx *dev = fh->dev;
1734 int rc;
1735
1736 rc = check_dev(dev);
1737 if (rc < 0)
1738 return rc;
1739
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001740 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1741 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001742 return -EINVAL;
1743 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001744 return -EINVAL;
1745
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001746 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1747 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001748
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001749 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001750 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001751 res_free(fh, EM28XX_RESOURCE_VIDEO);
1752 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001753 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001754 res_free(fh, EM28XX_RESOURCE_VBI);
1755 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001756
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001757 return 0;
1758}
1759
1760static int vidioc_querycap(struct file *file, void *priv,
1761 struct v4l2_capability *cap)
1762{
1763 struct em28xx_fh *fh = priv;
1764 struct em28xx *dev = fh->dev;
1765
1766 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1767 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001768 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001769
1770 cap->version = EM28XX_VERSION_CODE;
1771
1772 cap->capabilities =
1773 V4L2_CAP_SLICED_VBI_CAPTURE |
1774 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1776
Devin Heitmueller04146142009-09-11 00:08:44 -03001777 if (dev->vbi_dev)
1778 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1779
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001780 if (dev->audio_mode.has_audio)
1781 cap->capabilities |= V4L2_CAP_AUDIO;
1782
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001783 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 cap->capabilities |= V4L2_CAP_TUNER;
1785
1786 return 0;
1787}
1788
Hans Verkuil78b526a2008-05-28 12:16:41 -03001789static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001790 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001792 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793 return -EINVAL;
1794
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001795 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1796 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001797
1798 return 0;
1799}
1800
1801/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001802static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001803 struct v4l2_format *f)
1804{
1805 struct em28xx_fh *fh = priv;
1806 struct em28xx *dev = fh->dev;
1807 int rc;
1808
1809 rc = check_dev(dev);
1810 if (rc < 0)
1811 return rc;
1812
1813 mutex_lock(&dev->lock);
1814
1815 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001816 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817
1818 if (f->fmt.sliced.service_set == 0)
1819 rc = -EINVAL;
1820
1821 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001822
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001823 return rc;
1824}
1825
Hans Verkuil78b526a2008-05-28 12:16:41 -03001826static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001827 struct v4l2_format *f)
1828{
1829 struct em28xx_fh *fh = priv;
1830 struct em28xx *dev = fh->dev;
1831 int rc;
1832
1833 rc = check_dev(dev);
1834 if (rc < 0)
1835 return rc;
1836
1837 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001838 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001839 mutex_unlock(&dev->lock);
1840
1841 if (f->fmt.sliced.service_set == 0)
1842 return -EINVAL;
1843
1844 return 0;
1845}
1846
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001847/* RAW VBI ioctls */
1848
1849static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1850 struct v4l2_format *format)
1851{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001852 struct em28xx_fh *fh = priv;
1853 struct em28xx *dev = fh->dev;
1854
1855 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001856 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1857 format->fmt.vbi.offset = 0;
1858 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001859 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1860 format->fmt.vbi.count[0] = dev->vbi_height;
1861 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001862
1863 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001864 if (dev->norm & V4L2_STD_525_60) {
1865 /* NTSC */
1866 format->fmt.vbi.start[0] = 10;
1867 format->fmt.vbi.start[1] = 273;
1868 } else if (dev->norm & V4L2_STD_625_50) {
1869 /* PAL */
1870 format->fmt.vbi.start[0] = 6;
1871 format->fmt.vbi.start[1] = 318;
1872 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001873
1874 return 0;
1875}
1876
1877static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1878 struct v4l2_format *format)
1879{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001880 struct em28xx_fh *fh = priv;
1881 struct em28xx *dev = fh->dev;
1882
1883 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001884 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1885 format->fmt.vbi.offset = 0;
1886 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001887 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1888 format->fmt.vbi.count[0] = dev->vbi_height;
1889 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001890
1891 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001892 if (dev->norm & V4L2_STD_525_60) {
1893 /* NTSC */
1894 format->fmt.vbi.start[0] = 10;
1895 format->fmt.vbi.start[1] = 273;
1896 } else if (dev->norm & V4L2_STD_625_50) {
1897 /* PAL */
1898 format->fmt.vbi.start[0] = 6;
1899 format->fmt.vbi.start[1] = 318;
1900 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001901
1902 return 0;
1903}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001904
1905static int vidioc_reqbufs(struct file *file, void *priv,
1906 struct v4l2_requestbuffers *rb)
1907{
1908 struct em28xx_fh *fh = priv;
1909 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001910 int rc;
1911
1912 rc = check_dev(dev);
1913 if (rc < 0)
1914 return rc;
1915
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001916 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1917 return videobuf_reqbufs(&fh->vb_vidq, rb);
1918 else
1919 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001920}
1921
1922static int vidioc_querybuf(struct file *file, void *priv,
1923 struct v4l2_buffer *b)
1924{
1925 struct em28xx_fh *fh = priv;
1926 struct em28xx *dev = fh->dev;
1927 int rc;
1928
1929 rc = check_dev(dev);
1930 if (rc < 0)
1931 return rc;
1932
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001933 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1934 return videobuf_querybuf(&fh->vb_vidq, b);
1935 else {
1936 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1937 the videobuf framework, but we probably shouldn't be
1938 returning a buffer larger than that which was asked for.
1939 At a minimum, it causes a crash in zvbi since it does
1940 a memcpy based on the source buffer length */
1941 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001942 b->length = dev->vbi_width * dev->vbi_height * 2;
1943
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001944 return result;
1945 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001946}
1947
1948static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1949{
1950 struct em28xx_fh *fh = priv;
1951 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001952 int rc;
1953
1954 rc = check_dev(dev);
1955 if (rc < 0)
1956 return rc;
1957
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001958 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1959 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001960 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001961 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001962}
1963
1964static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1965{
1966 struct em28xx_fh *fh = priv;
1967 struct em28xx *dev = fh->dev;
1968 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001969
1970 rc = check_dev(dev);
1971 if (rc < 0)
1972 return rc;
1973
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001974 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1975 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1976 O_NONBLOCK);
1977 else
1978 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1979 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001980}
1981
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001982#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001983static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001984{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001985 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001986
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001987 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1988 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1989 else
1990 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001991}
1992#endif
1993
1994
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001995/* ----------------------------------------------------------- */
1996/* RADIO ESPECIFIC IOCTLS */
1997/* ----------------------------------------------------------- */
1998
1999static int radio_querycap(struct file *file, void *priv,
2000 struct v4l2_capability *cap)
2001{
2002 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2003
2004 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2005 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002006 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002007
2008 cap->version = EM28XX_VERSION_CODE;
2009 cap->capabilities = V4L2_CAP_TUNER;
2010 return 0;
2011}
2012
2013static int radio_g_tuner(struct file *file, void *priv,
2014 struct v4l2_tuner *t)
2015{
2016 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2017
2018 if (unlikely(t->index > 0))
2019 return -EINVAL;
2020
2021 strcpy(t->name, "Radio");
2022 t->type = V4L2_TUNER_RADIO;
2023
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002024 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002025 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002026 mutex_unlock(&dev->lock);
2027
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002028 return 0;
2029}
2030
2031static int radio_enum_input(struct file *file, void *priv,
2032 struct v4l2_input *i)
2033{
2034 if (i->index != 0)
2035 return -EINVAL;
2036 strcpy(i->name, "Radio");
2037 i->type = V4L2_INPUT_TYPE_TUNER;
2038
2039 return 0;
2040}
2041
2042static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2043{
2044 if (unlikely(a->index))
2045 return -EINVAL;
2046
2047 strcpy(a->name, "Radio");
2048 return 0;
2049}
2050
2051static int radio_s_tuner(struct file *file, void *priv,
2052 struct v4l2_tuner *t)
2053{
2054 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2055
2056 if (0 != t->index)
2057 return -EINVAL;
2058
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002059 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002060 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002061 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002062
2063 return 0;
2064}
2065
2066static int radio_s_audio(struct file *file, void *fh,
2067 struct v4l2_audio *a)
2068{
2069 return 0;
2070}
2071
2072static int radio_s_input(struct file *file, void *fh, unsigned int i)
2073{
2074 return 0;
2075}
2076
2077static int radio_queryctrl(struct file *file, void *priv,
2078 struct v4l2_queryctrl *qc)
2079{
2080 int i;
2081
2082 if (qc->id < V4L2_CID_BASE ||
2083 qc->id >= V4L2_CID_LASTP1)
2084 return -EINVAL;
2085
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002086 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2087 if (qc->id && qc->id == ac97_qctrl[i].id) {
2088 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002089 return 0;
2090 }
2091 }
2092
2093 return -EINVAL;
2094}
2095
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002096/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002097 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002098 * inits the device and starts isoc transfer
2099 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002100static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002101{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002102 int errCode = 0, radio = 0;
2103 struct video_device *vdev = video_devdata(filp);
2104 struct em28xx *dev = video_drvdata(filp);
2105 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002106 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002107 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002108
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002109 switch (vdev->vfl_type) {
2110 case VFL_TYPE_GRABBER:
2111 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2112 break;
2113 case VFL_TYPE_VBI:
2114 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2115 break;
2116 case VFL_TYPE_RADIO:
2117 radio = 1;
2118 break;
2119 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002120
2121 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08002122
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002123 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2124 video_device_node_name(vdev), v4l2_type_names[fh_type],
2125 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002128 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002129 if (!fh) {
2130 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002131 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002132 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002134 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002135 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002136 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002137 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002138
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002139 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002140 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002141 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002142 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002143
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002144 /* Needed, since GPIO might have disabled power of
2145 some i2c device
2146 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002147 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002148
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002149 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002150 if (fh->radio) {
2151 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002152 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002153 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002154
2155 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002156
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002157 if (dev->progressive)
2158 field = V4L2_FIELD_NONE;
2159 else
2160 field = V4L2_FIELD_INTERLACED;
2161
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002162 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2163 NULL, &dev->slock,
2164 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2165 sizeof(struct em28xx_buffer), fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002166
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002167 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2168 NULL, &dev->slock,
2169 V4L2_BUF_TYPE_VBI_CAPTURE,
2170 V4L2_FIELD_SEQ_TB,
2171 sizeof(struct em28xx_buffer), fh);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002172
Ingo Molnar3593cab2006-02-07 06:49:14 -02002173 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002174
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002175 return errCode;
2176}
2177
2178/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002179 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002180 * unregisters the v4l2,i2c and usb devices
2181 * called when the device gets disconected or at module unload
2182*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002183void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002184{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002186 /*FIXME: I2C IR should be disconnected */
2187
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002188 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002189 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002190 video_unregister_device(dev->radio_dev);
2191 else
2192 video_device_release(dev->radio_dev);
2193 dev->radio_dev = NULL;
2194 }
2195 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002196 em28xx_info("V4L2 device %s deregistered\n",
2197 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002198 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002199 video_unregister_device(dev->vbi_dev);
2200 else
2201 video_device_release(dev->vbi_dev);
2202 dev->vbi_dev = NULL;
2203 }
2204 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002205 em28xx_info("V4L2 device %s deregistered\n",
2206 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002207 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002208 video_unregister_device(dev->vdev);
2209 else
2210 video_device_release(dev->vdev);
2211 dev->vdev = NULL;
2212 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213}
2214
2215/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002216 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002217 * stops streaming and deallocates all resources allocated by the v4l2
2218 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002219 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002220static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002221{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002222 struct em28xx_fh *fh = filp->private_data;
2223 struct em28xx *dev = fh->dev;
2224 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002225
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002226 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002228 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002229 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002230 res_free(fh, EM28XX_RESOURCE_VIDEO);
2231 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002233 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2234 videobuf_stop(&fh->vb_vbiq);
2235 res_free(fh, EM28XX_RESOURCE_VBI);
2236 }
2237
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002238 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002239 /* the device is already disconnect,
2240 free the remaining resources */
2241 if (dev->state & DEV_DISCONNECTED) {
2242 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002243 kfree(dev);
2244 return 0;
2245 }
2246
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002247 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002248 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002249
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002250 /* do this before setting alternate! */
2251 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002252 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002253
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002254 /* set alternate 0 */
2255 dev->alt = 0;
2256 em28xx_videodbg("setting alternate 0\n");
2257 errCode = usb_set_interface(dev->udev, 0, 0);
2258 if (errCode < 0) {
2259 em28xx_errdev("cannot change alternate number to "
2260 "0 (error=%i)\n", errCode);
2261 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002263
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002264 videobuf_mmap_free(&fh->vb_vidq);
2265 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002266 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002267 dev->users--;
2268 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002269 return 0;
2270}
2271
2272/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002273 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002274 * will allocate buffers when called for the first time
2275 */
2276static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002277em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002278 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002279{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002280 struct em28xx_fh *fh = filp->private_data;
2281 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002282 int rc;
2283
2284 rc = check_dev(dev);
2285 if (rc < 0)
2286 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002288 /* FIXME: read() is not prepared to allow changing the video
2289 resolution while streaming. Seems a bug at em28xx_set_fmt
2290 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002291
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002292 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002293 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2294 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002295
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002296 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2297 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002298 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002299
2300
2301 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002302 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2303 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002304
2305 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2306 filp->f_flags & O_NONBLOCK);
2307 }
2308
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002309 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310}
2311
2312/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002313 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002314 * will allocate buffers when called for the first time
2315 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002316static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002317{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002318 struct em28xx_fh *fh = filp->private_data;
2319 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002320 int rc;
2321
2322 rc = check_dev(dev);
2323 if (rc < 0)
2324 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002325
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002326 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2327 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2328 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002329 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002330 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2331 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2332 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002333 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002334 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002335 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002336 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002337}
2338
2339/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002340 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002341 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002342static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002344 struct em28xx_fh *fh = filp->private_data;
2345 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002346 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002347
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002348 rc = check_dev(dev);
2349 if (rc < 0)
2350 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002351
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002352 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2353 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2354 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2355 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002356
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002357 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2358 (unsigned long)vma->vm_start,
2359 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2360 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002361
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002362 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002363}
2364
Hans Verkuilbec43662008-12-30 06:58:20 -03002365static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002366 .owner = THIS_MODULE,
2367 .open = em28xx_v4l2_open,
2368 .release = em28xx_v4l2_close,
2369 .read = em28xx_v4l2_read,
2370 .poll = em28xx_v4l2_poll,
2371 .mmap = em28xx_v4l2_mmap,
2372 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002373};
2374
Hans Verkuila3998102008-07-21 02:57:38 -03002375static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002376 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002377 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2378 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2379 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2380 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002381 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2382 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002383 .vidioc_g_audio = vidioc_g_audio,
2384 .vidioc_s_audio = vidioc_s_audio,
2385 .vidioc_cropcap = vidioc_cropcap,
2386
Hans Verkuil78b526a2008-05-28 12:16:41 -03002387 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2388 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2389 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002390
2391 .vidioc_reqbufs = vidioc_reqbufs,
2392 .vidioc_querybuf = vidioc_querybuf,
2393 .vidioc_qbuf = vidioc_qbuf,
2394 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002395 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002396 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002397 .vidioc_g_parm = vidioc_g_parm,
2398 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002399 .vidioc_enum_input = vidioc_enum_input,
2400 .vidioc_g_input = vidioc_g_input,
2401 .vidioc_s_input = vidioc_s_input,
2402 .vidioc_queryctrl = vidioc_queryctrl,
2403 .vidioc_g_ctrl = vidioc_g_ctrl,
2404 .vidioc_s_ctrl = vidioc_s_ctrl,
2405 .vidioc_streamon = vidioc_streamon,
2406 .vidioc_streamoff = vidioc_streamoff,
2407 .vidioc_g_tuner = vidioc_g_tuner,
2408 .vidioc_s_tuner = vidioc_s_tuner,
2409 .vidioc_g_frequency = vidioc_g_frequency,
2410 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002411#ifdef CONFIG_VIDEO_ADV_DEBUG
2412 .vidioc_g_register = vidioc_g_register,
2413 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002414 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002415#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002416#ifdef CONFIG_VIDEO_V4L1_COMPAT
2417 .vidiocgmbuf = vidiocgmbuf,
2418#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002419};
2420
2421static const struct video_device em28xx_video_template = {
2422 .fops = &em28xx_v4l_fops,
2423 .release = video_device_release,
2424 .ioctl_ops = &video_ioctl_ops,
2425
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002426 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002427 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002428};
2429
Hans Verkuilbec43662008-12-30 06:58:20 -03002430static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002431 .owner = THIS_MODULE,
2432 .open = em28xx_v4l2_open,
2433 .release = em28xx_v4l2_close,
2434 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002435};
2436
2437static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002438 .vidioc_querycap = radio_querycap,
2439 .vidioc_g_tuner = radio_g_tuner,
2440 .vidioc_enum_input = radio_enum_input,
2441 .vidioc_g_audio = radio_g_audio,
2442 .vidioc_s_tuner = radio_s_tuner,
2443 .vidioc_s_audio = radio_s_audio,
2444 .vidioc_s_input = radio_s_input,
2445 .vidioc_queryctrl = radio_queryctrl,
2446 .vidioc_g_ctrl = vidioc_g_ctrl,
2447 .vidioc_s_ctrl = vidioc_s_ctrl,
2448 .vidioc_g_frequency = vidioc_g_frequency,
2449 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002450#ifdef CONFIG_VIDEO_ADV_DEBUG
2451 .vidioc_g_register = vidioc_g_register,
2452 .vidioc_s_register = vidioc_s_register,
2453#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002454};
2455
Hans Verkuila3998102008-07-21 02:57:38 -03002456static struct video_device em28xx_radio_template = {
2457 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002458 .fops = &radio_fops,
2459 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002460};
2461
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002462/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002463
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002464
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002465
Adrian Bunk532fe652008-01-28 22:10:48 -03002466static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002467 const struct video_device *template,
2468 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002469{
2470 struct video_device *vfd;
2471
2472 vfd = video_device_alloc();
2473 if (NULL == vfd)
2474 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002475
2476 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002477 vfd->v4l2_dev = &dev->v4l2_dev;
2478 vfd->release = video_device_release;
2479 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002480
2481 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2482 dev->name, type_name);
2483
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002484 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002485 return vfd;
2486}
2487
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002488int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002489{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002490 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002491 int ret;
2492
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002493 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2494 dev->name,
2495 (EM28XX_VERSION_CODE >> 16) & 0xff,
2496 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2497
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002498 /* set default norm */
2499 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002500 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002501 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002502
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002503 /* Analog specific initialization */
2504 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002505 em28xx_set_video_format(dev, format[0].fourcc,
2506 norm_maxw(dev), norm_maxh(dev));
2507
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002508 video_mux(dev, dev->ctl_input);
2509
2510 /* Audio defaults */
2511 dev->mute = 1;
2512 dev->volume = 0x1f;
2513
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002514/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002515 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2516 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2517 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002518
2519 em28xx_set_outfmt(dev);
2520 em28xx_colorlevels_set_default(dev);
2521 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002522
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002523 /* allocate and fill video video_device struct */
2524 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2525 if (!dev->vdev) {
2526 em28xx_errdev("cannot allocate video_device.\n");
2527 return -ENODEV;
2528 }
2529
2530 /* register v4l2 video video_device */
2531 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2532 video_nr[dev->devno]);
2533 if (ret) {
2534 em28xx_errdev("unable to register video device (error=%i).\n",
2535 ret);
2536 return ret;
2537 }
2538
2539 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002540 if (em28xx_vbi_supported(dev) == 1) {
2541 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2542 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002543
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002544 /* register v4l2 vbi video_device */
2545 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2546 vbi_nr[dev->devno]);
2547 if (ret < 0) {
2548 em28xx_errdev("unable to register vbi device\n");
2549 return ret;
2550 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002551 }
2552
2553 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002554 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2555 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002556 if (!dev->radio_dev) {
2557 em28xx_errdev("cannot allocate video_device.\n");
2558 return -ENODEV;
2559 }
2560 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2561 radio_nr[dev->devno]);
2562 if (ret < 0) {
2563 em28xx_errdev("can't register radio device\n");
2564 return ret;
2565 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002566 em28xx_info("Registered radio device as %s\n",
2567 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002568 }
2569
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002570 em28xx_info("V4L2 video device registered as %s\n",
2571 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002572
2573 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002574 em28xx_info("V4L2 VBI device registered as %s\n",
2575 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002576
2577 return 0;
2578}