blob: f3ee001f21ca98a71cfca6cd6a3b921ced1ab56c [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080055 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030057 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030059static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030060module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030063#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030067 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030070
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020075static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030076static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030081module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082MODULE_PARM_DESC(video_nr, "video device numbers");
83MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080085
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030086static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030087module_param(video_debug, int, 0644);
88MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080089
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030090/* supported video standards */
91static struct em28xx_fmt format[] = {
92 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030093 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030094 .fourcc = V4L2_PIX_FMT_YUYV,
95 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030096 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030097 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030098 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .reg = EM28XX_OUTFMT_RGB_16_656,
102 }, {
103 .name = "8 bpp Bayer BGBG..GRGR",
104 .fourcc = V4L2_PIX_FMT_SBGGR8,
105 .depth = 8,
106 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
107 }, {
108 .name = "8 bpp Bayer GRGR..BGBG",
109 .fourcc = V4L2_PIX_FMT_SGRBG8,
110 .depth = 8,
111 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
112 }, {
113 .name = "8 bpp Bayer GBGB..RGRG",
114 .fourcc = V4L2_PIX_FMT_SGBRG8,
115 .depth = 8,
116 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
117 }, {
118 .name = "12 bpp YUV411",
119 .fourcc = V4L2_PIX_FMT_YUV411P,
120 .depth = 12,
121 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300122 },
123};
124
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800125/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200126/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300127static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129 .id = V4L2_CID_AUDIO_VOLUME,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "Volume",
132 .minimum = 0x0,
133 .maximum = 0x1f,
134 .step = 0x1,
135 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300136 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300137 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200138 .id = V4L2_CID_AUDIO_MUTE,
139 .type = V4L2_CTRL_TYPE_BOOLEAN,
140 .name = "Mute",
141 .minimum = 0,
142 .maximum = 1,
143 .step = 1,
144 .default_value = 1,
145 .flags = 0,
146 }
147};
148
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300149/* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
152
153/*
154 * Announces that a buffer were filled and request the next
155 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
159{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
162 buf->vb.state = VIDEOBUF_DONE;
163 buf->vb.field_count++;
164 do_gettimeofday(&buf->vb.ts);
165
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300166 dev->isoc_ctl.buf = NULL;
167
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
170}
171
172/*
173 * Identify the buffer header type and properly handles
174 */
175static void em28xx_copy_video(struct em28xx *dev,
176 struct em28xx_dmaqueue *dma_q,
177 struct em28xx_buffer *buf,
178 unsigned char *p,
179 unsigned char *outp, unsigned long len)
180{
181 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300182 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300183 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184
185 if (dma_q->pos + len > buf->vb.size)
186 len = buf->vb.size - dma_q->pos;
187
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300188 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300189 em28xx_isocdbg("frame is not complete\n");
190 len += 4;
191 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300192 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300193
194 startread = p;
195 remain = len;
196
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300197 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300198 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300199 else {
200 /* Interlaces two half frames */
201 if (buf->top_field)
202 fieldstart = outp;
203 else
204 fieldstart = outp + bytesperline;
205 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300207 linesdone = dma_q->pos / bytesperline;
208 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300209
210 if (dev->progressive)
211 offset = linesdone * bytesperline + currlinedone;
212 else
213 offset = linesdone * bytesperline * 2 + currlinedone;
214
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300216 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300217 lencopy = lencopy > remain ? remain : lencopy;
218
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300219 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300220 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300221 ((char *)startwrite + lencopy) -
222 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300223 remain = (char *)outp + buf->vb.size - (char *)startwrite;
224 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300225 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300226 if (lencopy <= 0)
227 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300228 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229
230 remain -= lencopy;
231
232 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300233 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300234 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300235 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300236 lencopy = remain;
237 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300238 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300239
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300240 if ((char *)startwrite + lencopy > (char *)outp +
241 buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300242 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300243 ((char *)startwrite + lencopy) -
244 ((char *)outp + buf->vb.size));
245 lencopy = remain = (char *)outp + buf->vb.size -
246 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300247 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300248 if (lencopy <= 0)
249 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300250
251 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300252
253 remain -= lencopy;
254 }
255
256 dma_q->pos += len;
257}
258
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300259static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300260 int packet, int status)
261{
262 char *errmsg = "Unknown";
263
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300264 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265 case -ENOENT:
266 errmsg = "unlinked synchronuously";
267 break;
268 case -ECONNRESET:
269 errmsg = "unlinked asynchronuously";
270 break;
271 case -ENOSR:
272 errmsg = "Buffer error (overrun)";
273 break;
274 case -EPIPE:
275 errmsg = "Stalled (device not responding)";
276 break;
277 case -EOVERFLOW:
278 errmsg = "Babble (bad cable?)";
279 break;
280 case -EPROTO:
281 errmsg = "Bit-stuff error (bad cable?)";
282 break;
283 case -EILSEQ:
284 errmsg = "CRC/Timeout (could be anything)";
285 break;
286 case -ETIME:
287 errmsg = "Device does not respond";
288 break;
289 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300290 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300291 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
292 } else {
293 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
294 packet, status, errmsg);
295 }
296}
297
298/*
299 * video-buf generic routine to get the next available buffer
300 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300301static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300302 struct em28xx_buffer **buf)
303{
304 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300305 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300306
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300307 if (list_empty(&dma_q->active)) {
308 em28xx_isocdbg("No active queue to serve\n");
309 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300310 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300311 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300312 }
313
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300314 /* Get the next buffer */
315 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
316
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300317 /* Cleans up buffer - Usefull for testing for frame/URB loss */
318 outp = videobuf_to_vmalloc(&(*buf)->vb);
319 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300320
321 dev->isoc_ctl.buf = *buf;
322
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300323 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300324}
325
326/*
327 * Controls the isoc copy of each urb packet
328 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300329static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300331 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300332 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300333 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300334 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300335 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300336
337 if (!dev)
338 return 0;
339
340 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
341 return 0;
342
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300343 if (urb->status < 0) {
344 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300345 if (urb->status == -ENOENT)
346 return 0;
347 }
348
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300349 buf = dev->isoc_ctl.buf;
350 if (buf != NULL)
351 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300352
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300353 for (i = 0; i < urb->number_of_packets; i++) {
354 int status = urb->iso_frame_desc[i].status;
355
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300356 if (status < 0) {
357 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300358 if (urb->iso_frame_desc[i].status != -EPROTO)
359 continue;
360 }
361
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300362 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300363
364 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300365 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366 continue;
367 }
368 if (urb->iso_frame_desc[i].actual_length >
369 dev->max_pkt_size) {
370 em28xx_isocdbg("packet bigger than packet size");
371 continue;
372 }
373
374 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300375
376 /* FIXME: incomplete buffer checks where removed to make
377 logic simpler. Impacts of those changes should be evaluated
378 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300379 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
380 em28xx_isocdbg("VBI HEADER!!!\n");
381 /* FIXME: Should add vbi copy */
382 continue;
383 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300384 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300385 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300386 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300387
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300388 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300389 if (buf != NULL)
390 buffer_filled(dev, dma_q, buf);
391 get_next_buf(dma_q, &buf);
392 if (buf == NULL)
393 outp = NULL;
394 else
395 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300396 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300397
398 if (buf != NULL) {
399 if (p[2] & 1)
400 buf->top_field = 0;
401 else
402 buf->top_field = 1;
403 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300404
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300407 if (buf != NULL)
408 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300409 }
410 return rc;
411}
412
413/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414 Videobuf operations
415 ------------------------------------------------------------------*/
416
417static int
418buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
419{
420 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300421 struct em28xx *dev = fh->dev;
422 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300423
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300424 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
425
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426 if (0 == *count)
427 *count = EM28XX_DEF_BUF;
428
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300429 if (*count < EM28XX_MIN_BUF)
430 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300431
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300432 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300433 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300434 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300435 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300436
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300437 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300438
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439 return 0;
440}
441
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300442/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
444{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300445 struct em28xx_fh *fh = vq->priv_data;
446 struct em28xx *dev = fh->dev;
447 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300448 if (in_interrupt())
449 BUG();
450
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300451 /* We used to wait for the buffer to finish here, but this didn't work
452 because, as we were keeping the state as VIDEOBUF_QUEUED,
453 videobuf_queue_cancel marked it as finished for us.
454 (Also, it could wedge forever if the hardware was misconfigured.)
455
456 This should be safe; by the time we get here, the buffer isn't
457 queued anymore. If we ever start marking the buffers as
458 VIDEOBUF_ACTIVE, it won't be, though.
459 */
460 spin_lock_irqsave(&dev->slock, flags);
461 if (dev->isoc_ctl.buf == buf)
462 dev->isoc_ctl.buf = NULL;
463 spin_unlock_irqrestore(&dev->slock, flags);
464
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465 videobuf_vmalloc_free(&buf->vb);
466 buf->vb.state = VIDEOBUF_NEEDS_INIT;
467}
468
469static int
470buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
471 enum v4l2_field field)
472{
473 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300474 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300475 struct em28xx *dev = fh->dev;
476 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300477
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300478 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479
480 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
481 return -EINVAL;
482
Brandon Philips05612972008-04-13 14:57:01 -0300483 buf->vb.width = dev->width;
484 buf->vb.height = dev->height;
485 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300486
487 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300488 rc = videobuf_iolock(vq, &buf->vb, NULL);
489 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300490 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491 }
492
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495
496 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300497 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
498 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300499 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300500 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300501 goto fail;
502 }
503
504 buf->vb.state = VIDEOBUF_PREPARED;
505 return 0;
506
507fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300508 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300509 return rc;
510}
511
512static void
513buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
514{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300515 struct em28xx_buffer *buf = container_of(vb,
516 struct em28xx_buffer,
517 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300518 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300519 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300520 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300521
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300522 buf->vb.state = VIDEOBUF_QUEUED;
523 list_add_tail(&buf->vb.queue, &vidq->active);
524
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300525}
526
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300527static void buffer_release(struct videobuf_queue *vq,
528 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300529{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300530 struct em28xx_buffer *buf = container_of(vb,
531 struct em28xx_buffer,
532 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300533 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300534 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300535
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300536 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300537
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300538 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300539}
540
541static struct videobuf_queue_ops em28xx_video_qops = {
542 .buf_setup = buffer_setup,
543 .buf_prepare = buffer_prepare,
544 .buf_queue = buffer_queue,
545 .buf_release = buffer_release,
546};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800547
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300548/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800549
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800550static void video_mux(struct em28xx *dev, int index)
551{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800552 dev->ctl_input = index;
553 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300554 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800555
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300556 if (!dev->ctl_aoutput)
557 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
558
Hans Verkuil5325b422009-04-02 11:26:22 -0300559 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
560 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800561
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300562 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300563 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300564 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
565 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300566 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300567 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300568 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
569 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800570 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300571
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300572 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300573 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
574 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300575 }
576
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300577 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800578}
579
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300580/* Usage lock check functions */
581static int res_get(struct em28xx_fh *fh)
582{
583 struct em28xx *dev = fh->dev;
584 int rc = 0;
585
586 /* This instance already has stream_on */
587 if (fh->stream_on)
588 return rc;
589
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300590 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300591 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300592
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300593 dev->stream_on = 1;
594 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300595 return rc;
596}
597
598static int res_check(struct em28xx_fh *fh)
599{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300600 return fh->stream_on;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300601}
602
603static void res_free(struct em28xx_fh *fh)
604{
605 struct em28xx *dev = fh->dev;
606
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300607 fh->stream_on = 0;
608 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300609}
610
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800611/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300612 * ac97_queryctrl()
613 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300614 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300615static int ac97_queryctrl(struct v4l2_queryctrl *qc)
616{
617 int i;
618
619 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
620 if (qc->id && qc->id == ac97_qctrl[i].id) {
621 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
622 return 0;
623 }
624 }
625
626 /* Control is not ac97 related */
627 return 1;
628}
629
630/*
631 * ac97_get_ctrl()
632 * return the current values for ac97 mute and volume
633 */
634static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300635{
636 switch (ctrl->id) {
637 case V4L2_CID_AUDIO_MUTE:
638 ctrl->value = dev->mute;
639 return 0;
640 case V4L2_CID_AUDIO_VOLUME:
641 ctrl->value = dev->volume;
642 return 0;
643 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300644 /* Control is not ac97 related */
645 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300646 }
647}
648
649/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300650 * ac97_set_ctrl()
651 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300652 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300653static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300654{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300655 int i;
656
657 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
658 if (ctrl->id == ac97_qctrl[i].id)
659 goto handle;
660
661 /* Announce that hasn't handle it */
662 return 1;
663
664handle:
665 if (ctrl->value < ac97_qctrl[i].minimum ||
666 ctrl->value > ac97_qctrl[i].maximum)
667 return -ERANGE;
668
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300669 switch (ctrl->id) {
670 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300671 dev->mute = ctrl->value;
672 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300673 case V4L2_CID_AUDIO_VOLUME:
674 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300675 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300676 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300677
678 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300679}
680
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300681static int check_dev(struct em28xx *dev)
682{
683 if (dev->state & DEV_DISCONNECTED) {
684 em28xx_errdev("v4l2 ioctl: device not present\n");
685 return -ENODEV;
686 }
687
688 if (dev->state & DEV_MISCONFIGURED) {
689 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
690 "close and open it again\n");
691 return -EIO;
692 }
693 return 0;
694}
695
696static void get_scale(struct em28xx *dev,
697 unsigned int width, unsigned int height,
698 unsigned int *hscale, unsigned int *vscale)
699{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300700 unsigned int maxw = norm_maxw(dev);
701 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300702
703 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
704 if (*hscale >= 0x4000)
705 *hscale = 0x3fff;
706
707 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
708 if (*vscale >= 0x4000)
709 *vscale = 0x3fff;
710}
711
712/* ------------------------------------------------------------------
713 IOCTL vidioc handling
714 ------------------------------------------------------------------*/
715
Hans Verkuil78b526a2008-05-28 12:16:41 -0300716static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300717 struct v4l2_format *f)
718{
719 struct em28xx_fh *fh = priv;
720 struct em28xx *dev = fh->dev;
721
722 mutex_lock(&dev->lock);
723
724 f->fmt.pix.width = dev->width;
725 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300726 f->fmt.pix.pixelformat = dev->format->fourcc;
727 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300728 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300729 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
730
731 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300732 if (dev->progressive)
733 f->fmt.pix.field = V4L2_FIELD_NONE;
734 else
735 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300736 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
737
738 mutex_unlock(&dev->lock);
739 return 0;
740}
741
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300742static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
743{
744 unsigned int i;
745
746 for (i = 0; i < ARRAY_SIZE(format); i++)
747 if (format[i].fourcc == fourcc)
748 return &format[i];
749
750 return NULL;
751}
752
Hans Verkuil78b526a2008-05-28 12:16:41 -0300753static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300754 struct v4l2_format *f)
755{
756 struct em28xx_fh *fh = priv;
757 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300758 unsigned int width = f->fmt.pix.width;
759 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300760 unsigned int maxw = norm_maxw(dev);
761 unsigned int maxh = norm_maxh(dev);
762 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300763 struct em28xx_fmt *fmt;
764
765 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
766 if (!fmt) {
767 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
768 f->fmt.pix.pixelformat);
769 return -EINVAL;
770 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300771
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300772 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300773 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300774 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
775 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
776 /* According to empiatech support the MaxPacketSize is too small
777 * to support framesizes larger than 640x480 @ 30 fps or 640x576
778 * @ 25 fps. As this would cut of a part of the image we prefer
779 * 360x576 or 360x480 for now */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300780 if (width == maxw && height == maxh)
781 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300782 } else {
783 /* width must even because of the YUYV format
784 height must be even because of interlacing */
785 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300786 }
787
788 get_scale(dev, width, height, &hscale, &vscale);
789
790 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
791 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
792
793 f->fmt.pix.width = width;
794 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300795 f->fmt.pix.pixelformat = fmt->fourcc;
796 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
797 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300798 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300799 if (dev->progressive)
800 f->fmt.pix.field = V4L2_FIELD_NONE;
801 else
802 f->fmt.pix.field = dev->interlaced ?
803 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805 return 0;
806}
807
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300808static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
809 unsigned width, unsigned height)
810{
811 struct em28xx_fmt *fmt;
812
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300813 fmt = format_by_fourcc(fourcc);
814 if (!fmt)
815 return -EINVAL;
816
817 dev->format = fmt;
818 dev->width = width;
819 dev->height = height;
820
821 /* set new image size */
822 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
823
824 em28xx_set_alternate(dev);
825 em28xx_resolution_set(dev);
826
827 return 0;
828}
829
Hans Verkuil78b526a2008-05-28 12:16:41 -0300830static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300831 struct v4l2_format *f)
832{
833 struct em28xx_fh *fh = priv;
834 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300835 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300836
837 rc = check_dev(dev);
838 if (rc < 0)
839 return rc;
840
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841 mutex_lock(&dev->lock);
842
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300843 vidioc_try_fmt_vid_cap(file, priv, f);
844
Brandon Philips05612972008-04-13 14:57:01 -0300845 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
846 em28xx_errdev("%s queue busy\n", __func__);
847 rc = -EBUSY;
848 goto out;
849 }
850
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300851 if (dev->stream_on && !fh->stream_on) {
852 em28xx_errdev("%s device in use by another fh\n", __func__);
853 rc = -EBUSY;
854 goto out;
855 }
856
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300857 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
858 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -0300859
860out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300861 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300862 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300863}
864
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300865static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866{
867 struct em28xx_fh *fh = priv;
868 struct em28xx *dev = fh->dev;
869 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300870 int rc;
871
872 rc = check_dev(dev);
873 if (rc < 0)
874 return rc;
875
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300876 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300877 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300878
879 /* Adjusts width/height, if needed */
880 f.fmt.pix.width = dev->width;
881 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300882 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300883
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300884 /* set new image size */
885 dev->width = f.fmt.pix.width;
886 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300887 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
888
889 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300890 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891
892 mutex_unlock(&dev->lock);
893 return 0;
894}
895
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -0300896static int vidioc_g_parm(struct file *file, void *priv,
897 struct v4l2_streamparm *p)
898{
899 struct em28xx_fh *fh = priv;
900 struct em28xx *dev = fh->dev;
901 int rc = 0;
902
903 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 return -EINVAL;
905
906 if (dev->board.is_webcam)
907 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
908 video, g_parm, p);
909 else
910 v4l2_video_std_frame_period(dev->norm,
911 &p->parm.capture.timeperframe);
912
913 return rc;
914}
915
916static int vidioc_s_parm(struct file *file, void *priv,
917 struct v4l2_streamparm *p)
918{
919 struct em28xx_fh *fh = priv;
920 struct em28xx *dev = fh->dev;
921
922 if (!dev->board.is_webcam)
923 return -EINVAL;
924
925 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 return -EINVAL;
927
928 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
929}
930
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300931static const char *iname[] = {
932 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
933 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
934 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
935 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
936 [EM28XX_VMUX_SVIDEO] = "S-Video",
937 [EM28XX_VMUX_TELEVISION] = "Television",
938 [EM28XX_VMUX_CABLE] = "Cable TV",
939 [EM28XX_VMUX_DVB] = "DVB",
940 [EM28XX_VMUX_DEBUG] = "for debug only",
941};
942
943static int vidioc_enum_input(struct file *file, void *priv,
944 struct v4l2_input *i)
945{
946 struct em28xx_fh *fh = priv;
947 struct em28xx *dev = fh->dev;
948 unsigned int n;
949
950 n = i->index;
951 if (n >= MAX_EM28XX_INPUT)
952 return -EINVAL;
953 if (0 == INPUT(n)->type)
954 return -EINVAL;
955
956 i->index = n;
957 i->type = V4L2_INPUT_TYPE_CAMERA;
958
959 strcpy(i->name, iname[INPUT(n)->type]);
960
961 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
962 (EM28XX_VMUX_CABLE == INPUT(n)->type))
963 i->type = V4L2_INPUT_TYPE_TUNER;
964
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300965 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966
967 return 0;
968}
969
970static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
971{
972 struct em28xx_fh *fh = priv;
973 struct em28xx *dev = fh->dev;
974
975 *i = dev->ctl_input;
976
977 return 0;
978}
979
980static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
981{
982 struct em28xx_fh *fh = priv;
983 struct em28xx *dev = fh->dev;
984 int rc;
985
986 rc = check_dev(dev);
987 if (rc < 0)
988 return rc;
989
990 if (i >= MAX_EM28XX_INPUT)
991 return -EINVAL;
992 if (0 == INPUT(i)->type)
993 return -EINVAL;
994
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300995 dev->ctl_input = i;
996
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300998 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300999 mutex_unlock(&dev->lock);
1000 return 0;
1001}
1002
1003static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1004{
1005 struct em28xx_fh *fh = priv;
1006 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001008 if (!dev->audio_mode.has_audio)
1009 return -EINVAL;
1010
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001011 switch (a->index) {
1012 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001014 break;
1015 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001016 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001017 break;
1018 case EM28XX_AMUX_VIDEO2:
1019 strcpy(a->name, "Television alt");
1020 break;
1021 case EM28XX_AMUX_PHONE:
1022 strcpy(a->name, "Phone");
1023 break;
1024 case EM28XX_AMUX_MIC:
1025 strcpy(a->name, "Mic");
1026 break;
1027 case EM28XX_AMUX_CD:
1028 strcpy(a->name, "CD");
1029 break;
1030 case EM28XX_AMUX_AUX:
1031 strcpy(a->name, "Aux");
1032 break;
1033 case EM28XX_AMUX_PCM_OUT:
1034 strcpy(a->name, "PCM");
1035 break;
1036 default:
1037 return -EINVAL;
1038 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001039
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001040 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001041 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001042
1043 return 0;
1044}
1045
1046static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1047{
1048 struct em28xx_fh *fh = priv;
1049 struct em28xx *dev = fh->dev;
1050
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001051
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001052 if (!dev->audio_mode.has_audio)
1053 return -EINVAL;
1054
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001055 if (a->index >= MAX_EM28XX_INPUT)
1056 return -EINVAL;
1057 if (0 == INPUT(a->index)->type)
1058 return -EINVAL;
1059
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001060 mutex_lock(&dev->lock);
1061
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001062 dev->ctl_ainput = INPUT(a->index)->amux;
1063 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001064
1065 if (!dev->ctl_aoutput)
1066 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001067
1068 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001069 return 0;
1070}
1071
1072static int vidioc_queryctrl(struct file *file, void *priv,
1073 struct v4l2_queryctrl *qc)
1074{
1075 struct em28xx_fh *fh = priv;
1076 struct em28xx *dev = fh->dev;
1077 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 int rc;
1079
1080 rc = check_dev(dev);
1081 if (rc < 0)
1082 return rc;
1083
1084 memset(qc, 0, sizeof(*qc));
1085
1086 qc->id = id;
1087
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001088 /* enumberate AC97 controls */
1089 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1090 rc = ac97_queryctrl(qc);
1091 if (!rc)
1092 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001094
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001095 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001096 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001097 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001098 mutex_unlock(&dev->lock);
1099
1100 if (qc->type)
1101 return 0;
1102 else
1103 return -EINVAL;
1104}
1105
1106static int vidioc_g_ctrl(struct file *file, void *priv,
1107 struct v4l2_control *ctrl)
1108{
1109 struct em28xx_fh *fh = priv;
1110 struct em28xx *dev = fh->dev;
1111 int rc;
1112
1113 rc = check_dev(dev);
1114 if (rc < 0)
1115 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001116 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001117
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001118 mutex_lock(&dev->lock);
1119
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001120 /* Set an AC97 control */
1121 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1122 rc = ac97_get_ctrl(dev, ctrl);
1123 else
1124 rc = 1;
1125
1126 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1127 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001128 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001129 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001130 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001131
1132 mutex_unlock(&dev->lock);
1133 return rc;
1134}
1135
1136static int vidioc_s_ctrl(struct file *file, void *priv,
1137 struct v4l2_control *ctrl)
1138{
1139 struct em28xx_fh *fh = priv;
1140 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141 int rc;
1142
1143 rc = check_dev(dev);
1144 if (rc < 0)
1145 return rc;
1146
1147 mutex_lock(&dev->lock);
1148
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001149 /* Set an AC97 control */
1150 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1151 rc = ac97_set_ctrl(dev, ctrl);
1152 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001153 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001154
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001155 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001156 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001157 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001158
1159 /*
1160 * In the case of non-AC97 volume controls, we still need
1161 * to do some setups at em28xx, in order to mute/unmute
1162 * and to adjust audio volume. However, the value ranges
1163 * should be checked by the corresponding V4L subdriver.
1164 */
1165 switch (ctrl->id) {
1166 case V4L2_CID_AUDIO_MUTE:
1167 dev->mute = ctrl->value;
1168 rc = em28xx_audio_analog_set(dev);
1169 break;
1170 case V4L2_CID_AUDIO_VOLUME:
1171 dev->volume = ctrl->value;
1172 rc = em28xx_audio_analog_set(dev);
1173 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001174 }
1175
1176 mutex_unlock(&dev->lock);
1177 return rc;
1178}
1179
1180static int vidioc_g_tuner(struct file *file, void *priv,
1181 struct v4l2_tuner *t)
1182{
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
1185 int rc;
1186
1187 rc = check_dev(dev);
1188 if (rc < 0)
1189 return rc;
1190
1191 if (0 != t->index)
1192 return -EINVAL;
1193
1194 strcpy(t->name, "Tuner");
1195
1196 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001197 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001199
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001200 return 0;
1201}
1202
1203static int vidioc_s_tuner(struct file *file, void *priv,
1204 struct v4l2_tuner *t)
1205{
1206 struct em28xx_fh *fh = priv;
1207 struct em28xx *dev = fh->dev;
1208 int rc;
1209
1210 rc = check_dev(dev);
1211 if (rc < 0)
1212 return rc;
1213
1214 if (0 != t->index)
1215 return -EINVAL;
1216
1217 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001218 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001219 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001220
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001221 return 0;
1222}
1223
1224static int vidioc_g_frequency(struct file *file, void *priv,
1225 struct v4l2_frequency *f)
1226{
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1229
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001230 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001231 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001232 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001233 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001234
1235 return 0;
1236}
1237
1238static int vidioc_s_frequency(struct file *file, void *priv,
1239 struct v4l2_frequency *f)
1240{
1241 struct em28xx_fh *fh = priv;
1242 struct em28xx *dev = fh->dev;
1243 int rc;
1244
1245 rc = check_dev(dev);
1246 if (rc < 0)
1247 return rc;
1248
1249 if (0 != f->tuner)
1250 return -EINVAL;
1251
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001252 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1253 return -EINVAL;
1254 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001255 return -EINVAL;
1256
1257 mutex_lock(&dev->lock);
1258
1259 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001260 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001261
1262 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001263
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001264 return 0;
1265}
1266
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001267#ifdef CONFIG_VIDEO_ADV_DEBUG
1268static int em28xx_reg_len(int reg)
1269{
1270 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001271 case EM28XX_R40_AC97LSB:
1272 case EM28XX_R30_HSCALELOW:
1273 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001274 return 2;
1275 default:
1276 return 1;
1277 }
1278}
1279
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001280static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001281 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001282{
1283 struct em28xx_fh *fh = priv;
1284 struct em28xx *dev = fh->dev;
1285
1286 chip->ident = V4L2_IDENT_NONE;
1287 chip->revision = 0;
1288
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001289 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001290
1291 return 0;
1292}
1293
1294
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001295static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001296 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001297{
1298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
1300 int ret;
1301
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001302 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001303 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001304 mutex_lock(&dev->lock);
1305 ret = em28xx_read_ac97(dev, reg->reg);
1306 mutex_unlock(&dev->lock);
1307 if (ret < 0)
1308 return ret;
1309
1310 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001311 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001312 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001313 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001314 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001315 return 0;
1316 case V4L2_CHIP_MATCH_I2C_ADDR:
1317 /* Not supported yet */
1318 return -EINVAL;
1319 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001320 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001321 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001322 }
1323
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001324 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001325 reg->size = em28xx_reg_len(reg->reg);
1326 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001327 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001328 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001329 mutex_unlock(&dev->lock);
1330
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001331 if (ret < 0)
1332 return ret;
1333
1334 reg->val = ret;
1335 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001336 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001337 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001338 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1339 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001340 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001341 if (ret < 0)
1342 return ret;
1343
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001344 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001345 }
1346
1347 return 0;
1348}
1349
1350static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001351 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001352{
1353 struct em28xx_fh *fh = priv;
1354 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001355 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001356 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001357
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001358 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001359 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001360 mutex_lock(&dev->lock);
1361 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1362 mutex_unlock(&dev->lock);
1363
1364 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001365 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001366 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001367 return 0;
1368 case V4L2_CHIP_MATCH_I2C_ADDR:
1369 /* Not supported yet */
1370 return -EINVAL;
1371 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001372 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001373 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001374 }
1375
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001376 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001377 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001378
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001379 mutex_lock(&dev->lock);
1380 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1381 em28xx_reg_len(reg->reg));
1382 mutex_unlock(&dev->lock);
1383
1384 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001385}
1386#endif
1387
1388
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389static int vidioc_cropcap(struct file *file, void *priv,
1390 struct v4l2_cropcap *cc)
1391{
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1394
1395 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1396 return -EINVAL;
1397
1398 cc->bounds.left = 0;
1399 cc->bounds.top = 0;
1400 cc->bounds.width = dev->width;
1401 cc->bounds.height = dev->height;
1402 cc->defrect = cc->bounds;
1403 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1404 cc->pixelaspect.denominator = 59;
1405
1406 return 0;
1407}
1408
1409static int vidioc_streamon(struct file *file, void *priv,
1410 enum v4l2_buf_type type)
1411{
1412 struct em28xx_fh *fh = priv;
1413 struct em28xx *dev = fh->dev;
1414 int rc;
1415
1416 rc = check_dev(dev);
1417 if (rc < 0)
1418 return rc;
1419
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001420
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001421 mutex_lock(&dev->lock);
1422 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001423
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001424 if (likely(rc >= 0))
1425 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001426
1427 mutex_unlock(&dev->lock);
1428
1429 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001430}
1431
1432static int vidioc_streamoff(struct file *file, void *priv,
1433 enum v4l2_buf_type type)
1434{
1435 struct em28xx_fh *fh = priv;
1436 struct em28xx *dev = fh->dev;
1437 int rc;
1438
1439 rc = check_dev(dev);
1440 if (rc < 0)
1441 return rc;
1442
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001443 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1444 return -EINVAL;
1445 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001446 return -EINVAL;
1447
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001448 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001449
1450 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001451 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001452
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001453 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001454
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001455 return 0;
1456}
1457
1458static int vidioc_querycap(struct file *file, void *priv,
1459 struct v4l2_capability *cap)
1460{
1461 struct em28xx_fh *fh = priv;
1462 struct em28xx *dev = fh->dev;
1463
1464 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1465 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001466 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001467
1468 cap->version = EM28XX_VERSION_CODE;
1469
1470 cap->capabilities =
1471 V4L2_CAP_SLICED_VBI_CAPTURE |
1472 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1474
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001475 if (dev->audio_mode.has_audio)
1476 cap->capabilities |= V4L2_CAP_AUDIO;
1477
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001478 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001479 cap->capabilities |= V4L2_CAP_TUNER;
1480
1481 return 0;
1482}
1483
Hans Verkuil78b526a2008-05-28 12:16:41 -03001484static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001485 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001486{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001487 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488 return -EINVAL;
1489
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001490 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1491 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001492
1493 return 0;
1494}
1495
1496/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001497static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001498 struct v4l2_format *f)
1499{
1500 struct em28xx_fh *fh = priv;
1501 struct em28xx *dev = fh->dev;
1502 int rc;
1503
1504 rc = check_dev(dev);
1505 if (rc < 0)
1506 return rc;
1507
1508 mutex_lock(&dev->lock);
1509
1510 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001511 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001512
1513 if (f->fmt.sliced.service_set == 0)
1514 rc = -EINVAL;
1515
1516 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001517
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001518 return rc;
1519}
1520
Hans Verkuil78b526a2008-05-28 12:16:41 -03001521static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001522 struct v4l2_format *f)
1523{
1524 struct em28xx_fh *fh = priv;
1525 struct em28xx *dev = fh->dev;
1526 int rc;
1527
1528 rc = check_dev(dev);
1529 if (rc < 0)
1530 return rc;
1531
1532 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001533 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001534 mutex_unlock(&dev->lock);
1535
1536 if (f->fmt.sliced.service_set == 0)
1537 return -EINVAL;
1538
1539 return 0;
1540}
1541
1542
1543static int vidioc_reqbufs(struct file *file, void *priv,
1544 struct v4l2_requestbuffers *rb)
1545{
1546 struct em28xx_fh *fh = priv;
1547 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001548 int rc;
1549
1550 rc = check_dev(dev);
1551 if (rc < 0)
1552 return rc;
1553
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001554 return videobuf_reqbufs(&fh->vb_vidq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555}
1556
1557static int vidioc_querybuf(struct file *file, void *priv,
1558 struct v4l2_buffer *b)
1559{
1560 struct em28xx_fh *fh = priv;
1561 struct em28xx *dev = fh->dev;
1562 int rc;
1563
1564 rc = check_dev(dev);
1565 if (rc < 0)
1566 return rc;
1567
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001568 return videobuf_querybuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001569}
1570
1571static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1572{
1573 struct em28xx_fh *fh = priv;
1574 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001575 int rc;
1576
1577 rc = check_dev(dev);
1578 if (rc < 0)
1579 return rc;
1580
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001581 return videobuf_qbuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001582}
1583
1584static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1585{
1586 struct em28xx_fh *fh = priv;
1587 struct em28xx *dev = fh->dev;
1588 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001589
1590 rc = check_dev(dev);
1591 if (rc < 0)
1592 return rc;
1593
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001594 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001595}
1596
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001597#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001598static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001599{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001600 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001601
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001602 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001603}
1604#endif
1605
1606
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001607/* ----------------------------------------------------------- */
1608/* RADIO ESPECIFIC IOCTLS */
1609/* ----------------------------------------------------------- */
1610
1611static int radio_querycap(struct file *file, void *priv,
1612 struct v4l2_capability *cap)
1613{
1614 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1615
1616 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1617 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001618 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001619
1620 cap->version = EM28XX_VERSION_CODE;
1621 cap->capabilities = V4L2_CAP_TUNER;
1622 return 0;
1623}
1624
1625static int radio_g_tuner(struct file *file, void *priv,
1626 struct v4l2_tuner *t)
1627{
1628 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1629
1630 if (unlikely(t->index > 0))
1631 return -EINVAL;
1632
1633 strcpy(t->name, "Radio");
1634 t->type = V4L2_TUNER_RADIO;
1635
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001636 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001637 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001638 mutex_unlock(&dev->lock);
1639
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001640 return 0;
1641}
1642
1643static int radio_enum_input(struct file *file, void *priv,
1644 struct v4l2_input *i)
1645{
1646 if (i->index != 0)
1647 return -EINVAL;
1648 strcpy(i->name, "Radio");
1649 i->type = V4L2_INPUT_TYPE_TUNER;
1650
1651 return 0;
1652}
1653
1654static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1655{
1656 if (unlikely(a->index))
1657 return -EINVAL;
1658
1659 strcpy(a->name, "Radio");
1660 return 0;
1661}
1662
1663static int radio_s_tuner(struct file *file, void *priv,
1664 struct v4l2_tuner *t)
1665{
1666 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1667
1668 if (0 != t->index)
1669 return -EINVAL;
1670
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001671 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001672 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001673 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001674
1675 return 0;
1676}
1677
1678static int radio_s_audio(struct file *file, void *fh,
1679 struct v4l2_audio *a)
1680{
1681 return 0;
1682}
1683
1684static int radio_s_input(struct file *file, void *fh, unsigned int i)
1685{
1686 return 0;
1687}
1688
1689static int radio_queryctrl(struct file *file, void *priv,
1690 struct v4l2_queryctrl *qc)
1691{
1692 int i;
1693
1694 if (qc->id < V4L2_CID_BASE ||
1695 qc->id >= V4L2_CID_LASTP1)
1696 return -EINVAL;
1697
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03001698 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
1699 if (qc->id && qc->id == ac97_qctrl[i].id) {
1700 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001701 return 0;
1702 }
1703 }
1704
1705 return -EINVAL;
1706}
1707
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001708/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001709 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710 * inits the device and starts isoc transfer
1711 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001712static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713{
Hans Verkuilbec43662008-12-30 06:58:20 -03001714 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001715 int errCode = 0, radio;
1716 struct em28xx *dev;
1717 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001718 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001719 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08001720
Hans Verkuilbec43662008-12-30 06:58:20 -03001721 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001722
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001723 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001724 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001725
1726 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001727
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001728 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001729 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001732 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001733 if (!fh) {
1734 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001735 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001736 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001738 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001739 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001740 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001741 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001743 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001744 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001745 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001746 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001748 /* Needed, since GPIO might have disabled power of
1749 some i2c device
1750 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001751 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001752
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001753 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001754 if (fh->radio) {
1755 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001756 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001757 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001758
1759 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001761 if (dev->progressive)
1762 field = V4L2_FIELD_NONE;
1763 else
1764 field = V4L2_FIELD_INTERLACED;
1765
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001766 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001767 NULL, &dev->slock, fh->type, field,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001768 sizeof(struct em28xx_buffer), fh);
1769
Ingo Molnar3593cab2006-02-07 06:49:14 -02001770 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001771
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772 return errCode;
1773}
1774
1775/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001776 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001777 * unregisters the v4l2,i2c and usb devices
1778 * called when the device gets disconected or at module unload
1779*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001780void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001782
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001783 /*FIXME: I2C IR should be disconnected */
1784
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001785 if (dev->radio_dev) {
1786 if (-1 != dev->radio_dev->minor)
1787 video_unregister_device(dev->radio_dev);
1788 else
1789 video_device_release(dev->radio_dev);
1790 dev->radio_dev = NULL;
1791 }
1792 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001793 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1794 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001795 if (-1 != dev->vbi_dev->minor)
1796 video_unregister_device(dev->vbi_dev);
1797 else
1798 video_device_release(dev->vbi_dev);
1799 dev->vbi_dev = NULL;
1800 }
1801 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001802 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1803 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001804 if (-1 != dev->vdev->minor)
1805 video_unregister_device(dev->vdev);
1806 else
1807 video_device_release(dev->vdev);
1808 dev->vdev = NULL;
1809 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810}
1811
1812/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001813 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001814 * stops streaming and deallocates all resources allocated by the v4l2
1815 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001816 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001817static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001818{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001819 struct em28xx_fh *fh = filp->private_data;
1820 struct em28xx *dev = fh->dev;
1821 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001823 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001824
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001825
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001826 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001827 if (res_check(fh))
1828 res_free(fh);
1829
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001830 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001831 videobuf_stop(&fh->vb_vidq);
1832 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001833
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001834 /* the device is already disconnect,
1835 free the remaining resources */
1836 if (dev->state & DEV_DISCONNECTED) {
1837 em28xx_release_resources(dev);
1838 mutex_unlock(&dev->lock);
1839 kfree(dev);
1840 return 0;
1841 }
1842
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001843 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03001844 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001845
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001846 /* do this before setting alternate! */
1847 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001848 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001849
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001850 /* set alternate 0 */
1851 dev->alt = 0;
1852 em28xx_videodbg("setting alternate 0\n");
1853 errCode = usb_set_interface(dev->udev, 0, 0);
1854 if (errCode < 0) {
1855 em28xx_errdev("cannot change alternate number to "
1856 "0 (error=%i)\n", errCode);
1857 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001858 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001859 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860 dev->users--;
1861 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001862 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001863 return 0;
1864}
1865
1866/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001867 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001868 * will allocate buffers when called for the first time
1869 */
1870static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001871em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001872 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001873{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001874 struct em28xx_fh *fh = filp->private_data;
1875 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001876 int rc;
1877
1878 rc = check_dev(dev);
1879 if (rc < 0)
1880 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001881
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001882 /* FIXME: read() is not prepared to allow changing the video
1883 resolution while streaming. Seems a bug at em28xx_set_fmt
1884 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001885
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001886 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001887 mutex_lock(&dev->lock);
1888 rc = res_get(fh);
1889 mutex_unlock(&dev->lock);
1890
1891 if (unlikely(rc < 0))
1892 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001893
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001894 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1895 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001896 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001897 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001898}
1899
1900/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001901 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902 * will allocate buffers when called for the first time
1903 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001904static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001905{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001906 struct em28xx_fh *fh = filp->private_data;
1907 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001908 int rc;
1909
1910 rc = check_dev(dev);
1911 if (rc < 0)
1912 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001913
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001914 mutex_lock(&dev->lock);
1915 rc = res_get(fh);
1916 mutex_unlock(&dev->lock);
1917
1918 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001919 return POLLERR;
1920
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001921 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1922 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001923
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001924 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001925}
1926
1927/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001928 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001929 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001930static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001931{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001932 struct em28xx_fh *fh = filp->private_data;
1933 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001934 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001935
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001936 rc = check_dev(dev);
1937 if (rc < 0)
1938 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001939
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001940 mutex_lock(&dev->lock);
1941 rc = res_get(fh);
1942 mutex_unlock(&dev->lock);
1943
1944 if (unlikely(rc < 0))
1945 return rc;
1946
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001947 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001948
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001949 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1950 (unsigned long)vma->vm_start,
1951 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1952 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001953
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001954 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001955}
1956
Hans Verkuilbec43662008-12-30 06:58:20 -03001957static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001958 .owner = THIS_MODULE,
1959 .open = em28xx_v4l2_open,
1960 .release = em28xx_v4l2_close,
1961 .read = em28xx_v4l2_read,
1962 .poll = em28xx_v4l2_poll,
1963 .mmap = em28xx_v4l2_mmap,
1964 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001965};
1966
Hans Verkuila3998102008-07-21 02:57:38 -03001967static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001968 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001969 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1970 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1971 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1972 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001973 .vidioc_g_audio = vidioc_g_audio,
1974 .vidioc_s_audio = vidioc_s_audio,
1975 .vidioc_cropcap = vidioc_cropcap,
1976
Hans Verkuil78b526a2008-05-28 12:16:41 -03001977 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1978 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1979 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001980
1981 .vidioc_reqbufs = vidioc_reqbufs,
1982 .vidioc_querybuf = vidioc_querybuf,
1983 .vidioc_qbuf = vidioc_qbuf,
1984 .vidioc_dqbuf = vidioc_dqbuf,
1985 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001986 .vidioc_g_parm = vidioc_g_parm,
1987 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001988 .vidioc_enum_input = vidioc_enum_input,
1989 .vidioc_g_input = vidioc_g_input,
1990 .vidioc_s_input = vidioc_s_input,
1991 .vidioc_queryctrl = vidioc_queryctrl,
1992 .vidioc_g_ctrl = vidioc_g_ctrl,
1993 .vidioc_s_ctrl = vidioc_s_ctrl,
1994 .vidioc_streamon = vidioc_streamon,
1995 .vidioc_streamoff = vidioc_streamoff,
1996 .vidioc_g_tuner = vidioc_g_tuner,
1997 .vidioc_s_tuner = vidioc_s_tuner,
1998 .vidioc_g_frequency = vidioc_g_frequency,
1999 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002000#ifdef CONFIG_VIDEO_ADV_DEBUG
2001 .vidioc_g_register = vidioc_g_register,
2002 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002003 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002004#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002005#ifdef CONFIG_VIDEO_V4L1_COMPAT
2006 .vidiocgmbuf = vidiocgmbuf,
2007#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002008};
2009
2010static const struct video_device em28xx_video_template = {
2011 .fops = &em28xx_v4l_fops,
2012 .release = video_device_release,
2013 .ioctl_ops = &video_ioctl_ops,
2014
2015 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002016
2017 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002018 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002019};
2020
Hans Verkuilbec43662008-12-30 06:58:20 -03002021static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002022 .owner = THIS_MODULE,
2023 .open = em28xx_v4l2_open,
2024 .release = em28xx_v4l2_close,
2025 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002026};
2027
2028static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002029 .vidioc_querycap = radio_querycap,
2030 .vidioc_g_tuner = radio_g_tuner,
2031 .vidioc_enum_input = radio_enum_input,
2032 .vidioc_g_audio = radio_g_audio,
2033 .vidioc_s_tuner = radio_s_tuner,
2034 .vidioc_s_audio = radio_s_audio,
2035 .vidioc_s_input = radio_s_input,
2036 .vidioc_queryctrl = radio_queryctrl,
2037 .vidioc_g_ctrl = vidioc_g_ctrl,
2038 .vidioc_s_ctrl = vidioc_s_ctrl,
2039 .vidioc_g_frequency = vidioc_g_frequency,
2040 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002041#ifdef CONFIG_VIDEO_ADV_DEBUG
2042 .vidioc_g_register = vidioc_g_register,
2043 .vidioc_s_register = vidioc_s_register,
2044#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002045};
2046
Hans Verkuila3998102008-07-21 02:57:38 -03002047static struct video_device em28xx_radio_template = {
2048 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002049 .fops = &radio_fops,
2050 .ioctl_ops = &radio_ioctl_ops,
2051 .minor = -1,
2052};
2053
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002054/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002055
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002056
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002057
Adrian Bunk532fe652008-01-28 22:10:48 -03002058static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002059 const struct video_device *template,
2060 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002061{
2062 struct video_device *vfd;
2063
2064 vfd = video_device_alloc();
2065 if (NULL == vfd)
2066 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002067
2068 *vfd = *template;
2069 vfd->minor = -1;
2070 vfd->v4l2_dev = &dev->v4l2_dev;
2071 vfd->release = video_device_release;
2072 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002073
2074 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2075 dev->name, type_name);
2076
2077 return vfd;
2078}
2079
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002080int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002081{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002082 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002083 int ret;
2084
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002085 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2086 dev->name,
2087 (EM28XX_VERSION_CODE >> 16) & 0xff,
2088 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2089
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002090 /* set default norm */
2091 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002092 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002093 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002094
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002095 /* Analog specific initialization */
2096 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002097 em28xx_set_video_format(dev, format[0].fourcc,
2098 norm_maxw(dev), norm_maxh(dev));
2099
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002100 video_mux(dev, dev->ctl_input);
2101
2102 /* Audio defaults */
2103 dev->mute = 1;
2104 dev->volume = 0x1f;
2105
2106 /* enable vbi capturing */
2107
2108/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002109 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2110 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2111 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002112 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2113
2114 em28xx_set_outfmt(dev);
2115 em28xx_colorlevels_set_default(dev);
2116 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002117
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002118 /* allocate and fill video video_device struct */
2119 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2120 if (!dev->vdev) {
2121 em28xx_errdev("cannot allocate video_device.\n");
2122 return -ENODEV;
2123 }
2124
2125 /* register v4l2 video video_device */
2126 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2127 video_nr[dev->devno]);
2128 if (ret) {
2129 em28xx_errdev("unable to register video device (error=%i).\n",
2130 ret);
2131 return ret;
2132 }
2133
2134 /* Allocate and fill vbi video_device struct */
2135 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2136
2137 /* register v4l2 vbi video_device */
2138 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2139 vbi_nr[dev->devno]);
2140 if (ret < 0) {
2141 em28xx_errdev("unable to register vbi device\n");
2142 return ret;
2143 }
2144
2145 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002146 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2147 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002148 if (!dev->radio_dev) {
2149 em28xx_errdev("cannot allocate video_device.\n");
2150 return -ENODEV;
2151 }
2152 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2153 radio_nr[dev->devno]);
2154 if (ret < 0) {
2155 em28xx_errdev("can't register radio device\n");
2156 return ret;
2157 }
2158 em28xx_info("Registered radio device as /dev/radio%d\n",
2159 dev->radio_dev->num);
2160 }
2161
2162 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2163 dev->vdev->num, dev->vbi_dev->num);
2164
2165 return 0;
2166}