blob: 50f5f4fc2148ce76db99619930137345e3d4b0a3 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030052
53#define EM28XX_VERSION "0.1.3"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080054
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080055#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080056 if (video_debug) \
57 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030058 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080059
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030061module_param(isoc_debug, int, 0644);
62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030063
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030064#define em28xx_isocdbg(fmt, arg...) \
65do {\
66 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030067 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030068 dev->name, __func__ , ##arg); \
69 } \
70 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030071
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030075MODULE_VERSION(EM28XX_VERSION);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080076
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030083module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084MODULE_PARM_DESC(video_nr, "video device numbers");
85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080087
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030088static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030089module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080091
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030092/* supported video standards */
93static struct em28xx_fmt format[] = {
94 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030095 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030096 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030098 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300100 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300101 .fourcc = V4L2_PIX_FMT_RGB565,
102 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300103 .reg = EM28XX_OUTFMT_RGB_16_656,
104 }, {
105 .name = "8 bpp Bayer BGBG..GRGR",
106 .fourcc = V4L2_PIX_FMT_SBGGR8,
107 .depth = 8,
108 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
109 }, {
110 .name = "8 bpp Bayer GRGR..BGBG",
111 .fourcc = V4L2_PIX_FMT_SGRBG8,
112 .depth = 8,
113 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
114 }, {
115 .name = "8 bpp Bayer GBGB..RGRG",
116 .fourcc = V4L2_PIX_FMT_SGBRG8,
117 .depth = 8,
118 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
119 }, {
120 .name = "12 bpp YUV411",
121 .fourcc = V4L2_PIX_FMT_YUV411P,
122 .depth = 12,
123 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300124 },
125};
126
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800127/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200128/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300129static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800130 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300138 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300139 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300151/* ------------------------------------------------------------------
152 DMA and thread functions
153 ------------------------------------------------------------------*/
154
155/*
156 * Announces that a buffer were filled and request the next
157 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 struct em28xx_dmaqueue *dma_q,
160 struct em28xx_buffer *buf)
161{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300162 /* Advice that buffer was filled */
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
166 do_gettimeofday(&buf->vb.ts);
167
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300168 dev->isoc_ctl.vid_buf = NULL;
169
170 list_del(&buf->vb.queue);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
183 do_gettimeofday(&buf->vb.ts);
184
185 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300186
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189}
190
191/*
192 * Identify the buffer header type and properly handles
193 */
194static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf,
197 unsigned char *p,
198 unsigned char *outp, unsigned long len)
199{
200 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300201 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
204 if (dma_q->pos + len > buf->vb.size)
205 len = buf->vb.size - dma_q->pos;
206
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 startread = p;
208 remain = len;
209
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300210 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300212 else {
213 /* Interlaces two half frames */
214 if (buf->top_field)
215 fieldstart = outp;
216 else
217 fieldstart = outp + bytesperline;
218 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300219
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300220 linesdone = dma_q->pos / bytesperline;
221 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300222
223 if (dev->progressive)
224 offset = linesdone * bytesperline + currlinedone;
225 else
226 offset = linesdone * bytesperline * 2 + currlinedone;
227
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300228 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300229 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 lencopy = lencopy > remain ? remain : lencopy;
231
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300232 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300236 remain = (char *)outp + buf->vb.size - (char *)startwrite;
237 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300238 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300239 if (lencopy <= 0)
240 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300241 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300242
243 remain -= lencopy;
244
245 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300246 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300247 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300248 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300249 lencopy = remain;
250 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300251 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300252
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300253 if ((char *)startwrite + lencopy > (char *)outp +
254 buf->vb.size) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300255 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300257 ((char *)startwrite + lencopy) -
258 ((char *)outp + buf->vb.size));
259 lencopy = remain = (char *)outp + buf->vb.size -
260 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300261 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300262 if (lencopy <= 0)
263 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300264
265 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266
267 remain -= lencopy;
268 }
269
270 dma_q->pos += len;
271}
272
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300273static void em28xx_copy_vbi(struct em28xx *dev,
274 struct em28xx_dmaqueue *dma_q,
275 struct em28xx_buffer *buf,
276 unsigned char *p,
277 unsigned char *outp, unsigned long len)
278{
279 void *startwrite, *startread;
280 int offset;
Julia Lawall6b81bef2010-08-27 02:57:18 -0300281 int bytesperline;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300282
283 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300284 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300285 return;
286 }
Julia Lawall6b81bef2010-08-27 02:57:18 -0300287 bytesperline = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300288
289 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300290 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300291 return;
292 }
293 if (buf == NULL) {
294 return;
295 }
296 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300297 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300298 return;
299 }
300 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300301 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300302 return;
303 }
304
305 if (dma_q->pos + len > buf->vb.size)
306 len = buf->vb.size - dma_q->pos;
307
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300308 startread = p;
309
310 startwrite = outp + dma_q->pos;
311 offset = dma_q->pos;
312
313 /* Make sure the bottom field populates the second half of the frame */
314 if (buf->top_field == 0) {
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300315 startwrite += bytesperline * dev->vbi_height;
316 offset += bytesperline * dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300317 }
318
319 memcpy(startwrite, startread, len);
320 dma_q->pos += len;
321}
322
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300323static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300324 int packet, int status)
325{
326 char *errmsg = "Unknown";
327
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300328 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 case -ENOENT:
330 errmsg = "unlinked synchronuously";
331 break;
332 case -ECONNRESET:
333 errmsg = "unlinked asynchronuously";
334 break;
335 case -ENOSR:
336 errmsg = "Buffer error (overrun)";
337 break;
338 case -EPIPE:
339 errmsg = "Stalled (device not responding)";
340 break;
341 case -EOVERFLOW:
342 errmsg = "Babble (bad cable?)";
343 break;
344 case -EPROTO:
345 errmsg = "Bit-stuff error (bad cable?)";
346 break;
347 case -EILSEQ:
348 errmsg = "CRC/Timeout (could be anything)";
349 break;
350 case -ETIME:
351 errmsg = "Device does not respond";
352 break;
353 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300354 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300355 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
356 } else {
357 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
358 packet, status, errmsg);
359 }
360}
361
362/*
363 * video-buf generic routine to get the next available buffer
364 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300365static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366 struct em28xx_buffer **buf)
367{
368 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300369 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300371 if (list_empty(&dma_q->active)) {
372 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300373 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300374 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300375 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300376 }
377
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300378 /* Get the next buffer */
379 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
380
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300381 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300382 outp = videobuf_to_vmalloc(&(*buf)->vb);
383 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300384
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300385 dev->isoc_ctl.vid_buf = *buf;
386
387 return;
388}
389
390/*
391 * video-buf generic routine to get the next available VBI buffer
392 */
393static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
394 struct em28xx_buffer **buf)
395{
396 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
397 char *outp;
398
399 if (list_empty(&dma_q->active)) {
400 em28xx_isocdbg("No active queue to serve\n");
401 dev->isoc_ctl.vbi_buf = NULL;
402 *buf = NULL;
403 return;
404 }
405
406 /* Get the next buffer */
407 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300408 /* Cleans up buffer - Useful for testing for frame/URB loss */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300409 outp = videobuf_to_vmalloc(&(*buf)->vb);
410 memset(outp, 0x00, (*buf)->vb.size);
411
412 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300415}
416
417/*
418 * Controls the isoc copy of each urb packet
419 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300420static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300421{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300422 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300423 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300424 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300426 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300427
428 if (!dev)
429 return 0;
430
431 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
432 return 0;
433
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300434 if (urb->status < 0) {
435 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 if (urb->status == -ENOENT)
437 return 0;
438 }
439
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300440 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300441 if (buf != NULL)
442 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444 for (i = 0; i < urb->number_of_packets; i++) {
445 int status = urb->iso_frame_desc[i].status;
446
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300447 if (status < 0) {
448 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300449 if (urb->iso_frame_desc[i].status != -EPROTO)
450 continue;
451 }
452
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300453 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454
455 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300456 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 continue;
458 }
459 if (urb->iso_frame_desc[i].actual_length >
460 dev->max_pkt_size) {
461 em28xx_isocdbg("packet bigger than packet size");
462 continue;
463 }
464
465 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466
467 /* FIXME: incomplete buffer checks where removed to make
468 logic simpler. Impacts of those changes should be evaluated
469 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300470 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
471 em28xx_isocdbg("VBI HEADER!!!\n");
472 /* FIXME: Should add vbi copy */
473 continue;
474 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300475 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300476 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300477 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300478
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300479 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300480 if (buf != NULL)
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
483 if (buf == NULL)
484 outp = NULL;
485 else
486 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300487 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300488
489 if (buf != NULL) {
490 if (p[2] & 1)
491 buf->top_field = 0;
492 else
493 buf->top_field = 1;
494 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300495
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300498 if (buf != NULL) {
499 if (p[0] != 0x88 && p[0] != 0x22) {
500 em28xx_isocdbg("frame is not complete\n");
501 len += 4;
502 } else {
503 p += 4;
504 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300505 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300506 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 }
508 return rc;
509}
510
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300511/* Version of isoc handler that takes into account a mixture of video and
512 VBI data */
513static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
514{
515 struct em28xx_buffer *buf, *vbi_buf;
516 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300517 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300518 unsigned char *outp = NULL;
519 unsigned char *vbioutp = NULL;
520 int i, len = 0, rc = 1;
521 unsigned char *p;
522 int vbi_size;
523
524 if (!dev)
525 return 0;
526
527 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
528 return 0;
529
530 if (urb->status < 0) {
531 print_err_status(dev, -1, urb->status);
532 if (urb->status == -ENOENT)
533 return 0;
534 }
535
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300536 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300537 if (buf != NULL)
538 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300539
540 vbi_buf = dev->isoc_ctl.vbi_buf;
541 if (vbi_buf != NULL)
542 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
543
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300544 for (i = 0; i < urb->number_of_packets; i++) {
545 int status = urb->iso_frame_desc[i].status;
546
547 if (status < 0) {
548 print_err_status(dev, i, status);
549 if (urb->iso_frame_desc[i].status != -EPROTO)
550 continue;
551 }
552
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300553 len = urb->iso_frame_desc[i].actual_length;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300554 if (urb->iso_frame_desc[i].actual_length <= 0) {
555 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
556 continue;
557 }
558 if (urb->iso_frame_desc[i].actual_length >
559 dev->max_pkt_size) {
560 em28xx_isocdbg("packet bigger than packet size");
561 continue;
562 }
563
564 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
565
566 /* capture type 0 = vbi start
567 capture type 1 = video start
568 capture type 2 = video in progress */
569 if (p[0] == 0x33 && p[1] == 0x95) {
570 dev->capture_type = 0;
571 dev->vbi_read = 0;
572 em28xx_isocdbg("VBI START HEADER!!!\n");
573 dev->cur_field = p[2];
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300574 p += 4;
575 len -= 4;
576 } else if (p[0] == 0x88 && p[1] == 0x88 &&
577 p[2] == 0x88 && p[3] == 0x88) {
578 /* continuation */
579 p += 4;
580 len -= 4;
581 } else if (p[0] == 0x22 && p[1] == 0x5a) {
582 /* start video */
583 p += 4;
584 len -= 4;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300585 }
586
Devin Heitmueller66d9cba2009-11-24 23:17:25 -0300587 vbi_size = dev->vbi_width * dev->vbi_height;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300588
589 if (dev->capture_type == 0) {
590 if (dev->vbi_read >= vbi_size) {
591 /* We've already read all the VBI data, so
592 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300593 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300594 } else if ((dev->vbi_read + len) < vbi_size) {
595 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300596 if (dev->vbi_read == 0 &&
597 (!(dev->cur_field & 1))) {
598 /* Brand new frame */
599 if (vbi_buf != NULL)
600 vbi_buffer_filled(dev,
601 vbi_dma_q,
602 vbi_buf);
603 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
604 if (vbi_buf == NULL)
605 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300606 else
607 vbioutp = videobuf_to_vmalloc(
608 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300609 }
610
611 if (dev->vbi_read == 0) {
612 vbi_dma_q->pos = 0;
613 if (vbi_buf != NULL) {
614 if (dev->cur_field & 1)
615 vbi_buf->top_field = 0;
616 else
617 vbi_buf->top_field = 1;
618 }
619 }
620
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300621 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300622 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
623 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300624 } else {
625 /* Some of this frame is VBI data and some is
626 video data */
627 int vbi_data_len = vbi_size - dev->vbi_read;
628 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300631 dev->capture_type = 1;
632 p += vbi_data_len;
633 len -= vbi_data_len;
634 }
635 }
636
637 if (dev->capture_type == 1) {
638 dev->capture_type = 2;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300639 if (dev->progressive || !(dev->cur_field & 1)) {
640 if (buf != NULL)
641 buffer_filled(dev, dma_q, buf);
642 get_next_buf(dma_q, &buf);
643 if (buf == NULL)
644 outp = NULL;
645 else
646 outp = videobuf_to_vmalloc(&buf->vb);
647 }
648 if (buf != NULL) {
649 if (dev->cur_field & 1)
650 buf->top_field = 0;
651 else
652 buf->top_field = 1;
653 }
654
655 dma_q->pos = 0;
656 }
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300657
658 if (buf != NULL && dev->capture_type == 2) {
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300659 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300660 p[2] == 0x88 && p[3] == 0x88) {
661 p += 4;
662 len -= 4;
663 }
Devin Heitmueller2584bc42010-06-13 17:00:23 -0300664 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
Devin Heitmueller5fee3342010-01-22 02:34:32 -0300665 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
666 p[2], len, (p[2] & 1) ?
667 "odd" : "even");
668 p += 4;
669 len -= 4;
670 }
671
672 if (len > 0)
673 em28xx_copy_video(dev, dma_q, buf, p, outp,
674 len);
675 }
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300676 }
677 return rc;
678}
679
680
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300682 Videobuf operations
683 ------------------------------------------------------------------*/
684
685static int
686buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
687{
688 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300689 struct em28xx *dev = fh->dev;
690 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300691
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300692 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
693 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300694
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300695 if (0 == *count)
696 *count = EM28XX_DEF_BUF;
697
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300698 if (*count < EM28XX_MIN_BUF)
699 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300700
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300701 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300702 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300703 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300704 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300705
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300706 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300707
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300708 return 0;
709}
710
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300711/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300712static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
713{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300714 struct em28xx_fh *fh = vq->priv_data;
715 struct em28xx *dev = fh->dev;
716 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300717 if (in_interrupt())
718 BUG();
719
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300720 /* We used to wait for the buffer to finish here, but this didn't work
721 because, as we were keeping the state as VIDEOBUF_QUEUED,
722 videobuf_queue_cancel marked it as finished for us.
723 (Also, it could wedge forever if the hardware was misconfigured.)
724
725 This should be safe; by the time we get here, the buffer isn't
726 queued anymore. If we ever start marking the buffers as
727 VIDEOBUF_ACTIVE, it won't be, though.
728 */
729 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300730 if (dev->isoc_ctl.vid_buf == buf)
731 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300732 spin_unlock_irqrestore(&dev->slock, flags);
733
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300734 videobuf_vmalloc_free(&buf->vb);
735 buf->vb.state = VIDEOBUF_NEEDS_INIT;
736}
737
738static int
739buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
740 enum v4l2_field field)
741{
742 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300743 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744 struct em28xx *dev = fh->dev;
745 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300747 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
748 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300749
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
751 return -EINVAL;
752
Brandon Philips05612972008-04-13 14:57:01 -0300753 buf->vb.width = dev->width;
754 buf->vb.height = dev->height;
755 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756
757 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300758 rc = videobuf_iolock(vq, &buf->vb, NULL);
759 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300760 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300761 }
762
Gianluca Gennari86d38d12012-02-13 13:59:22 -0300763 if (!dev->isoc_ctl.analog_bufs.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300764 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300765
766 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300767 if (em28xx_vbi_supported(dev) == 1)
Gianluca Gennari86d38d12012-02-13 13:59:22 -0300768 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
769 EM28XX_NUM_PACKETS,
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300770 EM28XX_NUM_BUFS,
771 dev->max_pkt_size,
772 em28xx_isoc_copy_vbi);
773 else
Gianluca Gennari86d38d12012-02-13 13:59:22 -0300774 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
775 EM28XX_NUM_PACKETS,
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300776 EM28XX_NUM_BUFS,
777 dev->max_pkt_size,
778 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300779 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300780 goto fail;
781 }
782
783 buf->vb.state = VIDEOBUF_PREPARED;
784 return 0;
785
786fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300787 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300788 return rc;
789}
790
791static void
792buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
793{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300794 struct em28xx_buffer *buf = container_of(vb,
795 struct em28xx_buffer,
796 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300798 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300799 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300800
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300801 buf->vb.state = VIDEOBUF_QUEUED;
802 list_add_tail(&buf->vb.queue, &vidq->active);
803
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300804}
805
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300806static void buffer_release(struct videobuf_queue *vq,
807 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300808{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300809 struct em28xx_buffer *buf = container_of(vb,
810 struct em28xx_buffer,
811 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300812 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300813 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300814
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300815 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300816
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300817 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300818}
819
820static struct videobuf_queue_ops em28xx_video_qops = {
821 .buf_setup = buffer_setup,
822 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue,
824 .buf_release = buffer_release,
825};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800826
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300827/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800828
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800829static void video_mux(struct em28xx *dev, int index)
830{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800831 dev->ctl_input = index;
832 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300833 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800834
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300835 if (!dev->ctl_aoutput)
836 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
837
Hans Verkuil5325b422009-04-02 11:26:22 -0300838 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
839 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800840
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300841 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300842 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300843 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
844 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300845 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300846 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300847 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
848 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800849 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300850
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300851 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300852 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
853 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300854 }
855
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300856 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800857}
858
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300859/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300860static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300861{
862 struct em28xx *dev = fh->dev;
863
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300864 if (fh->resources & bit)
865 /* have it already allocated */
866 return 1;
867
868 /* is it free? */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300869 if (dev->resources & bit) {
870 /* no, someone else uses it */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300871 return 0;
872 }
873 /* it's free, grab it */
874 fh->resources |= bit;
875 dev->resources |= bit;
876 em28xx_videodbg("res: get %d\n", bit);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300877 return 1;
878}
879
880static int res_check(struct em28xx_fh *fh, unsigned int bit)
881{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300882 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300883}
884
885static int res_locked(struct em28xx *dev, unsigned int bit)
886{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300887 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300888}
889
890static void res_free(struct em28xx_fh *fh, unsigned int bits)
891{
892 struct em28xx *dev = fh->dev;
893
894 BUG_ON((fh->resources & bits) != bits);
895
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 em28xx_videodbg("res: put %d\n", bits);
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300899}
900
901static int get_ressource(struct em28xx_fh *fh)
902{
903 switch (fh->type) {
904 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
905 return EM28XX_RESOURCE_VIDEO;
906 case V4L2_BUF_TYPE_VBI_CAPTURE:
907 return EM28XX_RESOURCE_VBI;
908 default:
909 BUG();
910 return 0;
911 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300912}
913
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800914/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300915 * ac97_queryctrl()
916 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300917 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300918static int ac97_queryctrl(struct v4l2_queryctrl *qc)
919{
920 int i;
921
922 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
923 if (qc->id && qc->id == ac97_qctrl[i].id) {
924 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
925 return 0;
926 }
927 }
928
929 /* Control is not ac97 related */
930 return 1;
931}
932
933/*
934 * ac97_get_ctrl()
935 * return the current values for ac97 mute and volume
936 */
937static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938{
939 switch (ctrl->id) {
940 case V4L2_CID_AUDIO_MUTE:
941 ctrl->value = dev->mute;
942 return 0;
943 case V4L2_CID_AUDIO_VOLUME:
944 ctrl->value = dev->volume;
945 return 0;
946 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300947 /* Control is not ac97 related */
948 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300949 }
950}
951
952/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300953 * ac97_set_ctrl()
954 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300955 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300956static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300957{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300958 int i;
959
960 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
961 if (ctrl->id == ac97_qctrl[i].id)
962 goto handle;
963
964 /* Announce that hasn't handle it */
965 return 1;
966
967handle:
968 if (ctrl->value < ac97_qctrl[i].minimum ||
969 ctrl->value > ac97_qctrl[i].maximum)
970 return -ERANGE;
971
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300972 switch (ctrl->id) {
973 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300974 dev->mute = ctrl->value;
975 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300976 case V4L2_CID_AUDIO_VOLUME:
977 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300978 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300980
981 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300982}
983
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984static int check_dev(struct em28xx *dev)
985{
986 if (dev->state & DEV_DISCONNECTED) {
987 em28xx_errdev("v4l2 ioctl: device not present\n");
988 return -ENODEV;
989 }
990
991 if (dev->state & DEV_MISCONFIGURED) {
992 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
993 "close and open it again\n");
994 return -EIO;
995 }
996 return 0;
997}
998
999static void get_scale(struct em28xx *dev,
1000 unsigned int width, unsigned int height,
1001 unsigned int *hscale, unsigned int *vscale)
1002{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001003 unsigned int maxw = norm_maxw(dev);
1004 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005
1006 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
1007 if (*hscale >= 0x4000)
1008 *hscale = 0x3fff;
1009
1010 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
1011 if (*vscale >= 0x4000)
1012 *vscale = 0x3fff;
1013}
1014
1015/* ------------------------------------------------------------------
1016 IOCTL vidioc handling
1017 ------------------------------------------------------------------*/
1018
Hans Verkuil78b526a2008-05-28 12:16:41 -03001019static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001020 struct v4l2_format *f)
1021{
1022 struct em28xx_fh *fh = priv;
1023 struct em28xx *dev = fh->dev;
1024
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001025 f->fmt.pix.width = dev->width;
1026 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001027 f->fmt.pix.pixelformat = dev->format->fourcc;
1028 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001029 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1031
1032 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001033 if (dev->progressive)
1034 f->fmt.pix.field = V4L2_FIELD_NONE;
1035 else
1036 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001037 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038 return 0;
1039}
1040
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001041static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1042{
1043 unsigned int i;
1044
1045 for (i = 0; i < ARRAY_SIZE(format); i++)
1046 if (format[i].fourcc == fourcc)
1047 return &format[i];
1048
1049 return NULL;
1050}
1051
Hans Verkuil78b526a2008-05-28 12:16:41 -03001052static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001053 struct v4l2_format *f)
1054{
1055 struct em28xx_fh *fh = priv;
1056 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001057 unsigned int width = f->fmt.pix.width;
1058 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059 unsigned int maxw = norm_maxw(dev);
1060 unsigned int maxh = norm_maxh(dev);
1061 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001062 struct em28xx_fmt *fmt;
1063
1064 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1065 if (!fmt) {
1066 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1067 f->fmt.pix.pixelformat);
1068 return -EINVAL;
1069 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001070
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001071 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001072 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001073 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1074 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
Sascha Sommer1020d132012-01-08 16:54:28 -03001075 /* MaxPacketSize for em2800 is too small to capture at full resolution
1076 * use half of maxw as the scaler can only scale to 50% */
1077 if (width == maxw && height == maxh)
1078 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001079 } else {
1080 /* width must even because of the YUYV format
1081 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001082 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1083 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001084 }
1085
1086 get_scale(dev, width, height, &hscale, &vscale);
1087
1088 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1089 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1090
1091 f->fmt.pix.width = width;
1092 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001093 f->fmt.pix.pixelformat = fmt->fourcc;
1094 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1095 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001097 if (dev->progressive)
1098 f->fmt.pix.field = V4L2_FIELD_NONE;
1099 else
1100 f->fmt.pix.field = dev->interlaced ?
1101 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001102
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001103 return 0;
1104}
1105
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001106static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1107 unsigned width, unsigned height)
1108{
1109 struct em28xx_fmt *fmt;
1110
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001111 fmt = format_by_fourcc(fourcc);
1112 if (!fmt)
1113 return -EINVAL;
1114
1115 dev->format = fmt;
1116 dev->width = width;
1117 dev->height = height;
1118
1119 /* set new image size */
1120 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1121
1122 em28xx_set_alternate(dev);
1123 em28xx_resolution_set(dev);
1124
1125 return 0;
1126}
1127
Hans Verkuil78b526a2008-05-28 12:16:41 -03001128static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129 struct v4l2_format *f)
1130{
1131 struct em28xx_fh *fh = priv;
1132 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001133 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134
1135 rc = check_dev(dev);
1136 if (rc < 0)
1137 return rc;
1138
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001139 vidioc_try_fmt_vid_cap(file, priv, f);
1140
Brandon Philips05612972008-04-13 14:57:01 -03001141 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1142 em28xx_errdev("%s queue busy\n", __func__);
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001143 return -EBUSY;
Brandon Philips05612972008-04-13 14:57:01 -03001144 }
1145
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001146 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001147 f->fmt.pix.width, f->fmt.pix.height);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001148}
1149
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001150static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1151{
1152 struct em28xx_fh *fh = priv;
1153 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001154 int rc;
1155
1156 rc = check_dev(dev);
1157 if (rc < 0)
1158 return rc;
1159
1160 *norm = dev->norm;
1161
1162 return 0;
1163}
1164
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03001165static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1166{
1167 struct em28xx_fh *fh = priv;
1168 struct em28xx *dev = fh->dev;
1169 int rc;
1170
1171 rc = check_dev(dev);
1172 if (rc < 0)
1173 return rc;
1174
1175 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
1176
1177 return 0;
1178}
1179
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001180static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181{
1182 struct em28xx_fh *fh = priv;
1183 struct em28xx *dev = fh->dev;
1184 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001185 int rc;
1186
1187 rc = check_dev(dev);
1188 if (rc < 0)
1189 return rc;
1190
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001191 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001192
1193 /* Adjusts width/height, if needed */
1194 f.fmt.pix.width = dev->width;
1195 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001196 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198 /* set new image size */
1199 dev->width = f.fmt.pix.width;
1200 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001201 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1202
1203 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001204 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001205
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206 return 0;
1207}
1208
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001209static int vidioc_g_parm(struct file *file, void *priv,
1210 struct v4l2_streamparm *p)
1211{
1212 struct em28xx_fh *fh = priv;
1213 struct em28xx *dev = fh->dev;
1214 int rc = 0;
1215
1216 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL;
1218
1219 if (dev->board.is_webcam)
1220 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1221 video, g_parm, p);
1222 else
1223 v4l2_video_std_frame_period(dev->norm,
1224 &p->parm.capture.timeperframe);
1225
1226 return rc;
1227}
1228
1229static int vidioc_s_parm(struct file *file, void *priv,
1230 struct v4l2_streamparm *p)
1231{
1232 struct em28xx_fh *fh = priv;
1233 struct em28xx *dev = fh->dev;
1234
1235 if (!dev->board.is_webcam)
1236 return -EINVAL;
1237
1238 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1239 return -EINVAL;
1240
1241 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1242}
1243
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001244static const char *iname[] = {
1245 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1246 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1247 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1248 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1249 [EM28XX_VMUX_SVIDEO] = "S-Video",
1250 [EM28XX_VMUX_TELEVISION] = "Television",
1251 [EM28XX_VMUX_CABLE] = "Cable TV",
1252 [EM28XX_VMUX_DVB] = "DVB",
1253 [EM28XX_VMUX_DEBUG] = "for debug only",
1254};
1255
1256static int vidioc_enum_input(struct file *file, void *priv,
1257 struct v4l2_input *i)
1258{
1259 struct em28xx_fh *fh = priv;
1260 struct em28xx *dev = fh->dev;
1261 unsigned int n;
1262
1263 n = i->index;
1264 if (n >= MAX_EM28XX_INPUT)
1265 return -EINVAL;
1266 if (0 == INPUT(n)->type)
1267 return -EINVAL;
1268
1269 i->index = n;
1270 i->type = V4L2_INPUT_TYPE_CAMERA;
1271
1272 strcpy(i->name, iname[INPUT(n)->type]);
1273
1274 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1275 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1276 i->type = V4L2_INPUT_TYPE_TUNER;
1277
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001278 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001279
1280 return 0;
1281}
1282
1283static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1284{
1285 struct em28xx_fh *fh = priv;
1286 struct em28xx *dev = fh->dev;
1287
1288 *i = dev->ctl_input;
1289
1290 return 0;
1291}
1292
1293static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1294{
1295 struct em28xx_fh *fh = priv;
1296 struct em28xx *dev = fh->dev;
1297 int rc;
1298
1299 rc = check_dev(dev);
1300 if (rc < 0)
1301 return rc;
1302
1303 if (i >= MAX_EM28XX_INPUT)
1304 return -EINVAL;
1305 if (0 == INPUT(i)->type)
1306 return -EINVAL;
1307
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03001308 video_mux(dev, i);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309 return 0;
1310}
1311
1312static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1313{
1314 struct em28xx_fh *fh = priv;
1315 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001316
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001317 if (!dev->audio_mode.has_audio)
1318 return -EINVAL;
1319
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001320 switch (a->index) {
1321 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001322 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001323 break;
1324 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001325 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001326 break;
1327 case EM28XX_AMUX_VIDEO2:
1328 strcpy(a->name, "Television alt");
1329 break;
1330 case EM28XX_AMUX_PHONE:
1331 strcpy(a->name, "Phone");
1332 break;
1333 case EM28XX_AMUX_MIC:
1334 strcpy(a->name, "Mic");
1335 break;
1336 case EM28XX_AMUX_CD:
1337 strcpy(a->name, "CD");
1338 break;
1339 case EM28XX_AMUX_AUX:
1340 strcpy(a->name, "Aux");
1341 break;
1342 case EM28XX_AMUX_PCM_OUT:
1343 strcpy(a->name, "PCM");
1344 break;
1345 default:
1346 return -EINVAL;
1347 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001348
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001349 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001350 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001351
1352 return 0;
1353}
1354
1355static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1356{
1357 struct em28xx_fh *fh = priv;
1358 struct em28xx *dev = fh->dev;
1359
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001360
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001361 if (!dev->audio_mode.has_audio)
1362 return -EINVAL;
1363
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001364 if (a->index >= MAX_EM28XX_INPUT)
1365 return -EINVAL;
1366 if (0 == INPUT(a->index)->type)
1367 return -EINVAL;
1368
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001369 dev->ctl_ainput = INPUT(a->index)->amux;
1370 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001371
1372 if (!dev->ctl_aoutput)
1373 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001374
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001375 return 0;
1376}
1377
1378static int vidioc_queryctrl(struct file *file, void *priv,
1379 struct v4l2_queryctrl *qc)
1380{
1381 struct em28xx_fh *fh = priv;
1382 struct em28xx *dev = fh->dev;
1383 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384 int rc;
1385
1386 rc = check_dev(dev);
1387 if (rc < 0)
1388 return rc;
1389
1390 memset(qc, 0, sizeof(*qc));
1391
1392 qc->id = id;
1393
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001394 /* enumerate AC97 controls */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001395 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1396 rc = ac97_queryctrl(qc);
1397 if (!rc)
1398 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001399 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001400
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001401 /* enumerate V4L2 device controls */
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001402 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001403
1404 if (qc->type)
1405 return 0;
1406 else
1407 return -EINVAL;
1408}
1409
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001410/*
1411 * FIXME: This is an indirect way to check if a control exists at a
1412 * subdev. Instead of that hack, maybe the better would be to change all
1413 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1414 */
1415static int check_subdev_ctrl(struct em28xx *dev, int id)
1416{
1417 struct v4l2_queryctrl qc;
1418
1419 memset(&qc, 0, sizeof(qc));
1420 qc.id = id;
1421
1422 /* enumerate V4L2 device controls */
1423 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1424
1425 if (qc.type)
1426 return 0;
1427 else
1428 return -EINVAL;
1429}
1430
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001431static int vidioc_g_ctrl(struct file *file, void *priv,
1432 struct v4l2_control *ctrl)
1433{
1434 struct em28xx_fh *fh = priv;
1435 struct em28xx *dev = fh->dev;
1436 int rc;
1437
1438 rc = check_dev(dev);
1439 if (rc < 0)
1440 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001441 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001442
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001443 /* Set an AC97 control */
1444 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1445 rc = ac97_get_ctrl(dev, ctrl);
1446 else
1447 rc = 1;
1448
1449 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1450 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001451 if (check_subdev_ctrl(dev, ctrl->id))
1452 return -EINVAL;
1453
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001454 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001455 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001456 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001457
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001458 return rc;
1459}
1460
1461static int vidioc_s_ctrl(struct file *file, void *priv,
1462 struct v4l2_control *ctrl)
1463{
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466 int rc;
1467
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
1471
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001472 /* Set an AC97 control */
1473 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1474 rc = ac97_set_ctrl(dev, ctrl);
1475 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001476 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001477
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001478 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001479 if (rc == 1) {
Mauro Carvalho Chehabfb8decf2011-02-22 01:00:58 -03001480 rc = check_subdev_ctrl(dev, ctrl->id);
1481 if (!rc)
1482 v4l2_device_call_all(&dev->v4l2_dev, 0,
1483 core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001484 /*
1485 * In the case of non-AC97 volume controls, we still need
1486 * to do some setups at em28xx, in order to mute/unmute
1487 * and to adjust audio volume. However, the value ranges
1488 * should be checked by the corresponding V4L subdriver.
1489 */
1490 switch (ctrl->id) {
1491 case V4L2_CID_AUDIO_MUTE:
1492 dev->mute = ctrl->value;
1493 rc = em28xx_audio_analog_set(dev);
1494 break;
1495 case V4L2_CID_AUDIO_VOLUME:
1496 dev->volume = ctrl->value;
1497 rc = em28xx_audio_analog_set(dev);
1498 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001499 }
Mauro Carvalho Chehab78e51562011-02-22 00:27:41 -03001500 return (rc < 0) ? rc : 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001501}
1502
1503static int vidioc_g_tuner(struct file *file, void *priv,
1504 struct v4l2_tuner *t)
1505{
1506 struct em28xx_fh *fh = priv;
1507 struct em28xx *dev = fh->dev;
1508 int rc;
1509
1510 rc = check_dev(dev);
1511 if (rc < 0)
1512 return rc;
1513
1514 if (0 != t->index)
1515 return -EINVAL;
1516
1517 strcpy(t->name, "Tuner");
Hans Verkuil0eed42e2010-05-01 18:06:50 -03001518 t->type = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001519
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001520 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001521 return 0;
1522}
1523
1524static int vidioc_s_tuner(struct file *file, void *priv,
1525 struct v4l2_tuner *t)
1526{
1527 struct em28xx_fh *fh = priv;
1528 struct em28xx *dev = fh->dev;
1529 int rc;
1530
1531 rc = check_dev(dev);
1532 if (rc < 0)
1533 return rc;
1534
1535 if (0 != t->index)
1536 return -EINVAL;
1537
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001538 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001539 return 0;
1540}
1541
1542static int vidioc_g_frequency(struct file *file, void *priv,
1543 struct v4l2_frequency *f)
1544{
1545 struct em28xx_fh *fh = priv;
1546 struct em28xx *dev = fh->dev;
1547
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001548 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001549 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001550 return 0;
1551}
1552
1553static int vidioc_s_frequency(struct file *file, void *priv,
1554 struct v4l2_frequency *f)
1555{
1556 struct em28xx_fh *fh = priv;
1557 struct em28xx *dev = fh->dev;
1558 int rc;
1559
1560 rc = check_dev(dev);
1561 if (rc < 0)
1562 return rc;
1563
1564 if (0 != f->tuner)
1565 return -EINVAL;
1566
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001567 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1568 return -EINVAL;
1569 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001570 return -EINVAL;
1571
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001572 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001573 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001574
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575 return 0;
1576}
1577
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001578#ifdef CONFIG_VIDEO_ADV_DEBUG
1579static int em28xx_reg_len(int reg)
1580{
1581 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001582 case EM28XX_R40_AC97LSB:
1583 case EM28XX_R30_HSCALELOW:
1584 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001585 return 2;
1586 default:
1587 return 1;
1588 }
1589}
1590
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001591static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001592 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001593{
1594 struct em28xx_fh *fh = priv;
1595 struct em28xx *dev = fh->dev;
1596
1597 chip->ident = V4L2_IDENT_NONE;
1598 chip->revision = 0;
1599
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001600 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001601
1602 return 0;
1603}
1604
1605
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001606static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001607 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001608{
1609 struct em28xx_fh *fh = priv;
1610 struct em28xx *dev = fh->dev;
1611 int ret;
1612
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001613 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001614 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001615 ret = em28xx_read_ac97(dev, reg->reg);
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001616 if (ret < 0)
1617 return ret;
1618
1619 reg->val = ret;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001620 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001621 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001622 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001623 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001624 return 0;
1625 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001626 /* TODO: is this correct? */
1627 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1628 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001629 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001630 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001631 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001632 }
1633
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001634 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001635 reg->size = em28xx_reg_len(reg->reg);
1636 if (reg->size == 1) {
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001637 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001638
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001639 if (ret < 0)
1640 return ret;
1641
1642 reg->val = ret;
1643 } else {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001644 __le16 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001645 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1646 reg->reg, (char *)&val, 2);
1647 if (ret < 0)
1648 return ret;
1649
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001650 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001651 }
1652
1653 return 0;
1654}
1655
1656static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001657 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001658{
1659 struct em28xx_fh *fh = priv;
1660 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001661 __le16 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001662
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001663 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001664 case V4L2_CHIP_MATCH_AC97:
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001665 return em28xx_write_ac97(dev, reg->reg, reg->val);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001666 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001667 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001668 return 0;
1669 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001670 /* TODO: is this correct? */
1671 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1672 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001673 default:
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001674 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001675 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001676 }
1677
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001678 /* Match host */
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001679 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001680
Hans Verkuil0499a5a2010-09-26 07:34:45 -03001681 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001682 em28xx_reg_len(reg->reg));
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001683}
1684#endif
1685
1686
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001687static int vidioc_cropcap(struct file *file, void *priv,
1688 struct v4l2_cropcap *cc)
1689{
1690 struct em28xx_fh *fh = priv;
1691 struct em28xx *dev = fh->dev;
1692
1693 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1694 return -EINVAL;
1695
1696 cc->bounds.left = 0;
1697 cc->bounds.top = 0;
1698 cc->bounds.width = dev->width;
1699 cc->bounds.height = dev->height;
1700 cc->defrect = cc->bounds;
1701 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1702 cc->pixelaspect.denominator = 59;
1703
1704 return 0;
1705}
1706
1707static int vidioc_streamon(struct file *file, void *priv,
1708 enum v4l2_buf_type type)
1709{
1710 struct em28xx_fh *fh = priv;
1711 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001712 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001713
1714 rc = check_dev(dev);
1715 if (rc < 0)
1716 return rc;
1717
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001718 if (unlikely(type != fh->type))
1719 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001720
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001721 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1722 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001723
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001724 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001725 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001726
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001727 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1728 rc = videobuf_streamon(&fh->vb_vidq);
1729 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1730 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001731
1732 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001733}
1734
1735static int vidioc_streamoff(struct file *file, void *priv,
1736 enum v4l2_buf_type type)
1737{
1738 struct em28xx_fh *fh = priv;
1739 struct em28xx *dev = fh->dev;
1740 int rc;
1741
1742 rc = check_dev(dev);
1743 if (rc < 0)
1744 return rc;
1745
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001746 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1747 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001748 return -EINVAL;
1749 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001750 return -EINVAL;
1751
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001752 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1753 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001754
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001755 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001756 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1757 videobuf_streamoff(&fh->vb_vidq);
1758 res_free(fh, EM28XX_RESOURCE_VIDEO);
1759 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001760 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Hans Verkuil3ea2b672010-12-29 14:28:13 -03001761 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1762 videobuf_streamoff(&fh->vb_vbiq);
1763 res_free(fh, EM28XX_RESOURCE_VBI);
1764 }
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001765 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001766
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001767 return 0;
1768}
1769
1770static int vidioc_querycap(struct file *file, void *priv,
1771 struct v4l2_capability *cap)
1772{
1773 struct em28xx_fh *fh = priv;
1774 struct em28xx *dev = fh->dev;
1775
1776 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1777 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001778 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001779
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001780 cap->capabilities =
1781 V4L2_CAP_SLICED_VBI_CAPTURE |
1782 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001783 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1784
Devin Heitmueller04146142009-09-11 00:08:44 -03001785 if (dev->vbi_dev)
1786 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1787
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001788 if (dev->audio_mode.has_audio)
1789 cap->capabilities |= V4L2_CAP_AUDIO;
1790
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001791 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 cap->capabilities |= V4L2_CAP_TUNER;
1793
1794 return 0;
1795}
1796
Hans Verkuil78b526a2008-05-28 12:16:41 -03001797static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001798 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001799{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001800 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001801 return -EINVAL;
1802
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001803 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1804 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001805
1806 return 0;
1807}
1808
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02001809static int vidioc_enum_framesizes(struct file *file, void *priv,
1810 struct v4l2_frmsizeenum *fsize)
1811{
1812 struct em28xx_fh *fh = priv;
1813 struct em28xx *dev = fh->dev;
1814 struct em28xx_fmt *fmt;
1815 unsigned int maxw = norm_maxw(dev);
1816 unsigned int maxh = norm_maxh(dev);
1817
1818 fmt = format_by_fourcc(fsize->pixel_format);
1819 if (!fmt) {
1820 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1821 fsize->pixel_format);
1822 return -EINVAL;
1823 }
1824
1825 if (dev->board.is_em2800) {
1826 if (fsize->index > 1)
1827 return -EINVAL;
1828 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1829 fsize->discrete.width = maxw / (1 + fsize->index);
1830 fsize->discrete.height = maxh / (1 + fsize->index);
1831 return 0;
1832 }
1833
1834 if (fsize->index != 0)
1835 return -EINVAL;
1836
1837 /* Report a continuous range */
1838 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1839 fsize->stepwise.min_width = 48;
1840 fsize->stepwise.min_height = 32;
1841 fsize->stepwise.max_width = maxw;
1842 fsize->stepwise.max_height = maxh;
1843 fsize->stepwise.step_width = 1;
1844 fsize->stepwise.step_height = 1;
1845 return 0;
1846}
1847
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001848/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001849static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001850 struct v4l2_format *f)
1851{
1852 struct em28xx_fh *fh = priv;
1853 struct em28xx *dev = fh->dev;
1854 int rc;
1855
1856 rc = check_dev(dev);
1857 if (rc < 0)
1858 return rc;
1859
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860 f->fmt.sliced.service_set = 0;
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001861 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001862
1863 if (f->fmt.sliced.service_set == 0)
1864 rc = -EINVAL;
1865
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001866 return rc;
1867}
1868
Hans Verkuil78b526a2008-05-28 12:16:41 -03001869static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001870 struct v4l2_format *f)
1871{
1872 struct em28xx_fh *fh = priv;
1873 struct em28xx *dev = fh->dev;
1874 int rc;
1875
1876 rc = check_dev(dev);
1877 if (rc < 0)
1878 return rc;
1879
Hans Verkuil4a61ecb2010-03-14 12:39:27 -03001880 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001881
1882 if (f->fmt.sliced.service_set == 0)
1883 return -EINVAL;
1884
1885 return 0;
1886}
1887
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001888/* RAW VBI ioctls */
1889
1890static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1891 struct v4l2_format *format)
1892{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001893 struct em28xx_fh *fh = priv;
1894 struct em28xx *dev = fh->dev;
1895
1896 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001897 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1898 format->fmt.vbi.offset = 0;
1899 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001900 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1901 format->fmt.vbi.count[0] = dev->vbi_height;
1902 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001903
1904 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001905 if (dev->norm & V4L2_STD_525_60) {
1906 /* NTSC */
1907 format->fmt.vbi.start[0] = 10;
1908 format->fmt.vbi.start[1] = 273;
1909 } else if (dev->norm & V4L2_STD_625_50) {
1910 /* PAL */
1911 format->fmt.vbi.start[0] = 6;
1912 format->fmt.vbi.start[1] = 318;
1913 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001914
1915 return 0;
1916}
1917
1918static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1919 struct v4l2_format *format)
1920{
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001921 struct em28xx_fh *fh = priv;
1922 struct em28xx *dev = fh->dev;
1923
1924 format->fmt.vbi.samples_per_line = dev->vbi_width;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001925 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1926 format->fmt.vbi.offset = 0;
1927 format->fmt.vbi.flags = 0;
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001928 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1929 format->fmt.vbi.count[0] = dev->vbi_height;
1930 format->fmt.vbi.count[1] = dev->vbi_height;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001931
1932 /* Varies by video standard (NTSC, PAL, etc.) */
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001933 if (dev->norm & V4L2_STD_525_60) {
1934 /* NTSC */
1935 format->fmt.vbi.start[0] = 10;
1936 format->fmt.vbi.start[1] = 273;
1937 } else if (dev->norm & V4L2_STD_625_50) {
1938 /* PAL */
1939 format->fmt.vbi.start[0] = 6;
1940 format->fmt.vbi.start[1] = 318;
1941 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001942
1943 return 0;
1944}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001945
1946static int vidioc_reqbufs(struct file *file, void *priv,
1947 struct v4l2_requestbuffers *rb)
1948{
1949 struct em28xx_fh *fh = priv;
1950 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001951 int rc;
1952
1953 rc = check_dev(dev);
1954 if (rc < 0)
1955 return rc;
1956
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001957 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1958 return videobuf_reqbufs(&fh->vb_vidq, rb);
1959 else
1960 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001961}
1962
1963static int vidioc_querybuf(struct file *file, void *priv,
1964 struct v4l2_buffer *b)
1965{
1966 struct em28xx_fh *fh = priv;
1967 struct em28xx *dev = fh->dev;
1968 int rc;
1969
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_querybuf(&fh->vb_vidq, b);
1976 else {
1977 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1978 the videobuf framework, but we probably shouldn't be
1979 returning a buffer larger than that which was asked for.
1980 At a minimum, it causes a crash in zvbi since it does
1981 a memcpy based on the source buffer length */
1982 int result = videobuf_querybuf(&fh->vb_vbiq, b);
Devin Heitmueller66d9cba2009-11-24 23:17:25 -03001983 b->length = dev->vbi_width * dev->vbi_height * 2;
1984
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001985 return result;
1986 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001987}
1988
1989static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1990{
1991 struct em28xx_fh *fh = priv;
1992 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001993 int rc;
1994
1995 rc = check_dev(dev);
1996 if (rc < 0)
1997 return rc;
1998
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001999 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2000 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002001 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002002 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002003}
2004
2005static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2006{
2007 struct em28xx_fh *fh = priv;
2008 struct em28xx *dev = fh->dev;
2009 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002010
2011 rc = check_dev(dev);
2012 if (rc < 0)
2013 return rc;
2014
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002015 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2016 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2017 O_NONBLOCK);
2018 else
2019 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2020 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002021}
2022
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002023/* ----------------------------------------------------------- */
2024/* RADIO ESPECIFIC IOCTLS */
2025/* ----------------------------------------------------------- */
2026
2027static int radio_querycap(struct file *file, void *priv,
2028 struct v4l2_capability *cap)
2029{
2030 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2031
2032 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2033 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03002034 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002035
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002036 cap->capabilities = V4L2_CAP_TUNER;
2037 return 0;
2038}
2039
2040static int radio_g_tuner(struct file *file, void *priv,
2041 struct v4l2_tuner *t)
2042{
2043 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2044
2045 if (unlikely(t->index > 0))
2046 return -EINVAL;
2047
2048 strcpy(t->name, "Radio");
2049 t->type = V4L2_TUNER_RADIO;
2050
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002051 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002052
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002053 return 0;
2054}
2055
2056static int radio_enum_input(struct file *file, void *priv,
2057 struct v4l2_input *i)
2058{
2059 if (i->index != 0)
2060 return -EINVAL;
2061 strcpy(i->name, "Radio");
2062 i->type = V4L2_INPUT_TYPE_TUNER;
2063
2064 return 0;
2065}
2066
2067static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2068{
2069 if (unlikely(a->index))
2070 return -EINVAL;
2071
2072 strcpy(a->name, "Radio");
2073 return 0;
2074}
2075
2076static int radio_s_tuner(struct file *file, void *priv,
2077 struct v4l2_tuner *t)
2078{
2079 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2080
2081 if (0 != t->index)
2082 return -EINVAL;
2083
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002084 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002085
2086 return 0;
2087}
2088
2089static int radio_s_audio(struct file *file, void *fh,
2090 struct v4l2_audio *a)
2091{
2092 return 0;
2093}
2094
2095static int radio_s_input(struct file *file, void *fh, unsigned int i)
2096{
2097 return 0;
2098}
2099
2100static int radio_queryctrl(struct file *file, void *priv,
2101 struct v4l2_queryctrl *qc)
2102{
2103 int i;
2104
2105 if (qc->id < V4L2_CID_BASE ||
2106 qc->id >= V4L2_CID_LASTP1)
2107 return -EINVAL;
2108
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002109 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2110 if (qc->id && qc->id == ac97_qctrl[i].id) {
2111 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002112 return 0;
2113 }
2114 }
2115
2116 return -EINVAL;
2117}
2118
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002119/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002120 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002121 * inits the device and starts isoc transfer
2122 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002123static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002124{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002125 int errCode = 0, radio = 0;
2126 struct video_device *vdev = video_devdata(filp);
2127 struct em28xx *dev = video_drvdata(filp);
2128 enum v4l2_buf_type fh_type = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002129 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002130 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002131
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002132 switch (vdev->vfl_type) {
2133 case VFL_TYPE_GRABBER:
2134 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2135 break;
2136 case VFL_TYPE_VBI:
2137 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2138 break;
2139 case VFL_TYPE_RADIO:
2140 radio = 1;
2141 break;
2142 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002143
Laurent Pinchart50462eb2009-12-10 11:47:13 -02002144 em28xx_videodbg("open dev=%s type=%s users=%d\n",
2145 video_device_node_name(vdev), v4l2_type_names[fh_type],
2146 dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002147
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002148
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002149 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002150 if (!fh) {
2151 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
2152 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002154 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002155 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002156 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002157 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002159 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002160 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002161 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002162 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002163
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002164 /* Needed, since GPIO might have disabled power of
2165 some i2c device
2166 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002167 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002168
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002169 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002170 if (fh->radio) {
2171 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002172 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002173 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174
2175 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002176
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002177 if (dev->progressive)
2178 field = V4L2_FIELD_NONE;
2179 else
2180 field = V4L2_FIELD_INTERLACED;
2181
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002182 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2183 NULL, &dev->slock,
2184 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002185 sizeof(struct em28xx_buffer), fh, &dev->lock);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002186
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002187 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2188 NULL, &dev->slock,
2189 V4L2_BUF_TYPE_VBI_CAPTURE,
2190 V4L2_FIELD_SEQ_TB,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002191 sizeof(struct em28xx_buffer), fh, &dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002192
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002193 return errCode;
2194}
2195
2196/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002197 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198 * unregisters the v4l2,i2c and usb devices
2199 * called when the device gets disconected or at module unload
2200*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002201void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002204 /*FIXME: I2C IR should be disconnected */
2205
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002206 if (dev->radio_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002207 if (video_is_registered(dev->radio_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002208 video_unregister_device(dev->radio_dev);
2209 else
2210 video_device_release(dev->radio_dev);
2211 dev->radio_dev = NULL;
2212 }
2213 if (dev->vbi_dev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002214 em28xx_info("V4L2 device %s deregistered\n",
2215 video_device_node_name(dev->vbi_dev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002216 if (video_is_registered(dev->vbi_dev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002217 video_unregister_device(dev->vbi_dev);
2218 else
2219 video_device_release(dev->vbi_dev);
2220 dev->vbi_dev = NULL;
2221 }
2222 if (dev->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002223 em28xx_info("V4L2 device %s deregistered\n",
2224 video_device_node_name(dev->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03002225 if (video_is_registered(dev->vdev))
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002226 video_unregister_device(dev->vdev);
2227 else
2228 video_device_release(dev->vdev);
2229 dev->vdev = NULL;
2230 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002231}
2232
2233/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002234 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002235 * stops streaming and deallocates all resources allocated by the v4l2
2236 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002238static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002239{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002240 struct em28xx_fh *fh = filp->private_data;
2241 struct em28xx *dev = fh->dev;
2242 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002243
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002244 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002245
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002246 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002247 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002248 res_free(fh, EM28XX_RESOURCE_VIDEO);
2249 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002250
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002251 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2252 videobuf_stop(&fh->vb_vbiq);
2253 res_free(fh, EM28XX_RESOURCE_VBI);
2254 }
2255
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002256 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002257 /* the device is already disconnect,
2258 free the remaining resources */
2259 if (dev->state & DEV_DISCONNECTED) {
2260 em28xx_release_resources(dev);
Chris Rankin16e3d2f2011-09-04 15:38:14 -03002261 kfree(dev->alt_max_pkt_size);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002262 kfree(dev);
Ezequiel GarcĂ­adedb8cb2012-05-05 16:13:22 -03002263 kfree(fh);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002264 return 0;
2265 }
2266
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002267 /* Save some power by putting tuner to sleep */
Laurent Pinchart622b8282009-10-05 10:48:17 -03002268 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002269
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002270 /* do this before setting alternate! */
Gianluca Gennari86d38d12012-02-13 13:59:22 -03002271 em28xx_uninit_isoc(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002272 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002273
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002274 /* set alternate 0 */
2275 dev->alt = 0;
2276 em28xx_videodbg("setting alternate 0\n");
2277 errCode = usb_set_interface(dev->udev, 0, 0);
2278 if (errCode < 0) {
2279 em28xx_errdev("cannot change alternate number to "
2280 "0 (error=%i)\n", errCode);
2281 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002282 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002283
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002284 videobuf_mmap_free(&fh->vb_vidq);
2285 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002286 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287 dev->users--;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288 return 0;
2289}
2290
2291/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002292 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 * will allocate buffers when called for the first time
2294 */
2295static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002296em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002297 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002298{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002299 struct em28xx_fh *fh = filp->private_data;
2300 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002301 int rc;
2302
2303 rc = check_dev(dev);
2304 if (rc < 0)
2305 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002306
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002307 /* FIXME: read() is not prepared to allow changing the video
2308 resolution while streaming. Seems a bug at em28xx_set_fmt
2309 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002310
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002311 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002312 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2313 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002314
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002315 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2316 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002317 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002318
2319
2320 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002321 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2322 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002323
2324 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2325 filp->f_flags & O_NONBLOCK);
2326 }
2327
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002328 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002329}
2330
2331/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002332 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002333 * will allocate buffers when called for the first time
2334 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002335static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002336{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002337 struct em28xx_fh *fh = filp->private_data;
2338 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002339 int rc;
2340
2341 rc = check_dev(dev);
2342 if (rc < 0)
2343 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002344
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002345 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2346 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2347 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002348 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002349 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2350 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2351 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002352 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002353 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002354 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002355 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002356}
2357
2358/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002359 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002360 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002361static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002362{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002363 struct em28xx_fh *fh = filp->private_data;
2364 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002365 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002366
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002367 rc = check_dev(dev);
2368 if (rc < 0)
2369 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002370
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002371 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2372 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2373 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2374 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002375
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002376 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2377 (unsigned long)vma->vm_start,
2378 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2379 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002380
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002381 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002382}
2383
Hans Verkuilbec43662008-12-30 06:58:20 -03002384static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002385 .owner = THIS_MODULE,
2386 .open = em28xx_v4l2_open,
2387 .release = em28xx_v4l2_close,
2388 .read = em28xx_v4l2_read,
2389 .poll = em28xx_v4l2_poll,
2390 .mmap = em28xx_v4l2_mmap,
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002391 .unlocked_ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002392};
2393
Hans Verkuila3998102008-07-21 02:57:38 -03002394static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002395 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002396 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2397 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2398 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2399 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002400 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2401 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab1c5c5062011-10-16 13:52:43 -02002402 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002403 .vidioc_g_audio = vidioc_g_audio,
2404 .vidioc_s_audio = vidioc_s_audio,
2405 .vidioc_cropcap = vidioc_cropcap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002406 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2407 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2408 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002409
2410 .vidioc_reqbufs = vidioc_reqbufs,
2411 .vidioc_querybuf = vidioc_querybuf,
2412 .vidioc_qbuf = vidioc_qbuf,
2413 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002414 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehabd56ae6f2011-10-04 09:53:00 -03002415 .vidioc_querystd = vidioc_querystd,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002416 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002417 .vidioc_g_parm = vidioc_g_parm,
2418 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002419 .vidioc_enum_input = vidioc_enum_input,
2420 .vidioc_g_input = vidioc_g_input,
2421 .vidioc_s_input = vidioc_s_input,
2422 .vidioc_queryctrl = vidioc_queryctrl,
2423 .vidioc_g_ctrl = vidioc_g_ctrl,
2424 .vidioc_s_ctrl = vidioc_s_ctrl,
2425 .vidioc_streamon = vidioc_streamon,
2426 .vidioc_streamoff = vidioc_streamoff,
2427 .vidioc_g_tuner = vidioc_g_tuner,
2428 .vidioc_s_tuner = vidioc_s_tuner,
2429 .vidioc_g_frequency = vidioc_g_frequency,
2430 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002431#ifdef CONFIG_VIDEO_ADV_DEBUG
2432 .vidioc_g_register = vidioc_g_register,
2433 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002434 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002435#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002436};
2437
2438static const struct video_device em28xx_video_template = {
2439 .fops = &em28xx_v4l_fops,
2440 .release = video_device_release,
2441 .ioctl_ops = &video_ioctl_ops,
2442
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002443 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002444 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002445};
2446
Hans Verkuilbec43662008-12-30 06:58:20 -03002447static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002448 .owner = THIS_MODULE,
2449 .open = em28xx_v4l2_open,
2450 .release = em28xx_v4l2_close,
Hans Verkuil8fd0bda2010-12-18 09:59:51 -03002451 .unlocked_ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002452};
2453
2454static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002455 .vidioc_querycap = radio_querycap,
2456 .vidioc_g_tuner = radio_g_tuner,
2457 .vidioc_enum_input = radio_enum_input,
2458 .vidioc_g_audio = radio_g_audio,
2459 .vidioc_s_tuner = radio_s_tuner,
2460 .vidioc_s_audio = radio_s_audio,
2461 .vidioc_s_input = radio_s_input,
2462 .vidioc_queryctrl = radio_queryctrl,
2463 .vidioc_g_ctrl = vidioc_g_ctrl,
2464 .vidioc_s_ctrl = vidioc_s_ctrl,
2465 .vidioc_g_frequency = vidioc_g_frequency,
2466 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002467#ifdef CONFIG_VIDEO_ADV_DEBUG
2468 .vidioc_g_register = vidioc_g_register,
2469 .vidioc_s_register = vidioc_s_register,
2470#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002471};
2472
Hans Verkuila3998102008-07-21 02:57:38 -03002473static struct video_device em28xx_radio_template = {
2474 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002475 .fops = &radio_fops,
2476 .ioctl_ops = &radio_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03002477};
2478
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002479/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002480
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002481
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002482
Adrian Bunk532fe652008-01-28 22:10:48 -03002483static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002484 const struct video_device *template,
2485 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002486{
2487 struct video_device *vfd;
2488
2489 vfd = video_device_alloc();
2490 if (NULL == vfd)
2491 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002492
2493 *vfd = *template;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002494 vfd->v4l2_dev = &dev->v4l2_dev;
2495 vfd->release = video_device_release;
2496 vfd->debug = video_debug;
Hans Verkuil0499a5a2010-09-26 07:34:45 -03002497 vfd->lock = &dev->lock;
Hans Verkuil5126f252012-05-10 04:57:22 -03002498 /* Locking in file operations other than ioctl should be done
2499 by the driver, not the V4L2 core.
2500 This driver needs auditing so that this flag can be removed. */
2501 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002502
2503 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2504 dev->name, type_name);
2505
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -02002506 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002507 return vfd;
2508}
2509
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002510int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002511{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002512 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002513 int ret;
Sascha Sommer1020d132012-01-08 16:54:28 -03002514 unsigned int maxw;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002515
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -03002516 printk(KERN_INFO "%s: v4l2 driver version %s\n",
2517 dev->name, EM28XX_VERSION);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002518
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002519 /* set default norm */
2520 dev->norm = em28xx_video_template.current_norm;
Hans Verkuild5906dd2010-09-26 07:45:15 -03002521 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002522 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002523
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002524 /* Analog specific initialization */
2525 dev->format = &format[0];
Sascha Sommer1020d132012-01-08 16:54:28 -03002526
2527 maxw = norm_maxw(dev);
2528 /* MaxPacketSize for em2800 is too small to capture at full resolution
2529 * use half of maxw as the scaler can only scale to 50% */
2530 if (dev->board.is_em2800)
2531 maxw /= 2;
2532
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002533 em28xx_set_video_format(dev, format[0].fourcc,
Sascha Sommer1020d132012-01-08 16:54:28 -03002534 maxw, norm_maxh(dev));
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002535
Ezequiel GarcĂ­a96371fc2012-03-23 18:09:34 -03002536 video_mux(dev, 0);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002537
2538 /* Audio defaults */
2539 dev->mute = 1;
2540 dev->volume = 0x1f;
2541
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002542/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002543 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2544 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2545 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002546
2547 em28xx_set_outfmt(dev);
2548 em28xx_colorlevels_set_default(dev);
2549 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002550
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002551 /* allocate and fill video video_device struct */
2552 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2553 if (!dev->vdev) {
2554 em28xx_errdev("cannot allocate video_device.\n");
2555 return -ENODEV;
2556 }
2557
2558 /* register v4l2 video video_device */
2559 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2560 video_nr[dev->devno]);
2561 if (ret) {
2562 em28xx_errdev("unable to register video device (error=%i).\n",
2563 ret);
2564 return ret;
2565 }
2566
2567 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002568 if (em28xx_vbi_supported(dev) == 1) {
2569 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2570 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002571
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002572 /* register v4l2 vbi video_device */
2573 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2574 vbi_nr[dev->devno]);
2575 if (ret < 0) {
2576 em28xx_errdev("unable to register vbi device\n");
2577 return ret;
2578 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002579 }
2580
2581 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002582 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2583 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002584 if (!dev->radio_dev) {
2585 em28xx_errdev("cannot allocate video_device.\n");
2586 return -ENODEV;
2587 }
2588 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2589 radio_nr[dev->devno]);
2590 if (ret < 0) {
2591 em28xx_errdev("can't register radio device\n");
2592 return ret;
2593 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002594 em28xx_info("Registered radio device as %s\n",
2595 video_device_node_name(dev->radio_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002596 }
2597
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002598 em28xx_info("V4L2 video device registered as %s\n",
2599 video_device_node_name(dev->vdev));
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002600
2601 if (dev->vbi_dev)
Laurent Pinchart38c7c032009-11-27 13:57:15 -03002602 em28xx_info("V4L2 VBI device registered as %s\n",
2603 video_device_node_name(dev->vbi_dev));
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002604
2605 return 0;
2606}