blob: b96a138d9d03bd9e93edc0b6895fd440056f9adf [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 {
93 .name = "16bpp YUY2, 4:2:2, packed",
94 .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 }, {
98 .name = "16 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
101 .reg = EM28XX_OUTFMT_YUV211,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300102 },
103};
104
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800105/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200106/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800107static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800108 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200109 .id = V4L2_CID_AUDIO_VOLUME,
110 .type = V4L2_CTRL_TYPE_INTEGER,
111 .name = "Volume",
112 .minimum = 0x0,
113 .maximum = 0x1f,
114 .step = 0x1,
115 .default_value = 0x1f,
116 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300117 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200118 .id = V4L2_CID_AUDIO_MUTE,
119 .type = V4L2_CTRL_TYPE_BOOLEAN,
120 .name = "Mute",
121 .minimum = 0,
122 .maximum = 1,
123 .step = 1,
124 .default_value = 1,
125 .flags = 0,
126 }
127};
128
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300129/* ------------------------------------------------------------------
130 DMA and thread functions
131 ------------------------------------------------------------------*/
132
133/*
134 * Announces that a buffer were filled and request the next
135 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300136static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300137 struct em28xx_dmaqueue *dma_q,
138 struct em28xx_buffer *buf)
139{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300140 /* Advice that buffer was filled */
141 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
142 buf->vb.state = VIDEOBUF_DONE;
143 buf->vb.field_count++;
144 do_gettimeofday(&buf->vb.ts);
145
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300146 dev->isoc_ctl.buf = NULL;
147
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300148 list_del(&buf->vb.queue);
149 wake_up(&buf->vb.done);
150}
151
152/*
153 * Identify the buffer header type and properly handles
154 */
155static void em28xx_copy_video(struct em28xx *dev,
156 struct em28xx_dmaqueue *dma_q,
157 struct em28xx_buffer *buf,
158 unsigned char *p,
159 unsigned char *outp, unsigned long len)
160{
161 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300162 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300163 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300164
165 if (dma_q->pos + len > buf->vb.size)
166 len = buf->vb.size - dma_q->pos;
167
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300168 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169 em28xx_isocdbg("frame is not complete\n");
170 len += 4;
171 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300172 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300173
174 startread = p;
175 remain = len;
176
177 /* Interlaces frame */
178 if (buf->top_field)
179 fieldstart = outp;
180 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300181 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300183 linesdone = dma_q->pos / bytesperline;
184 currlinedone = dma_q->pos % bytesperline;
185 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300186 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300187 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188 lencopy = lencopy > remain ? remain : lencopy;
189
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300190 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300191 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300192 ((char *)startwrite + lencopy) -
193 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300194 remain = (char *)outp + buf->vb.size - (char *)startwrite;
195 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300196 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300197 if (lencopy <= 0)
198 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300199 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200
201 remain -= lencopy;
202
203 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300204 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300206 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207 lencopy = remain;
208 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300209 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300211 if ((char *)startwrite + lencopy > (char *)outp +
212 buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300213 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300214 ((char *)startwrite + lencopy) -
215 ((char *)outp + buf->vb.size));
216 lencopy = remain = (char *)outp + buf->vb.size -
217 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300218 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300219 if (lencopy <= 0)
220 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300221
222 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223
224 remain -= lencopy;
225 }
226
227 dma_q->pos += len;
228}
229
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300231 int packet, int status)
232{
233 char *errmsg = "Unknown";
234
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300235 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300236 case -ENOENT:
237 errmsg = "unlinked synchronuously";
238 break;
239 case -ECONNRESET:
240 errmsg = "unlinked asynchronuously";
241 break;
242 case -ENOSR:
243 errmsg = "Buffer error (overrun)";
244 break;
245 case -EPIPE:
246 errmsg = "Stalled (device not responding)";
247 break;
248 case -EOVERFLOW:
249 errmsg = "Babble (bad cable?)";
250 break;
251 case -EPROTO:
252 errmsg = "Bit-stuff error (bad cable?)";
253 break;
254 case -EILSEQ:
255 errmsg = "CRC/Timeout (could be anything)";
256 break;
257 case -ETIME:
258 errmsg = "Device does not respond";
259 break;
260 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300262 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
263 } else {
264 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
265 packet, status, errmsg);
266 }
267}
268
269/*
270 * video-buf generic routine to get the next available buffer
271 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300272static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300273 struct em28xx_buffer **buf)
274{
275 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300277
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300278 if (list_empty(&dma_q->active)) {
279 em28xx_isocdbg("No active queue to serve\n");
280 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300281 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300282 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300283 }
284
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300285 /* Get the next buffer */
286 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
287
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300288 /* Cleans up buffer - Usefull for testing for frame/URB loss */
289 outp = videobuf_to_vmalloc(&(*buf)->vb);
290 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300291
292 dev->isoc_ctl.buf = *buf;
293
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300294 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295}
296
297/*
298 * Controls the isoc copy of each urb packet
299 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300300static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300302 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300303 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300304 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300305 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300306 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300307
308 if (!dev)
309 return 0;
310
311 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
312 return 0;
313
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300314 if (urb->status < 0) {
315 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300316 if (urb->status == -ENOENT)
317 return 0;
318 }
319
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300320 buf = dev->isoc_ctl.buf;
321 if (buf != NULL)
322 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300323
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300324 for (i = 0; i < urb->number_of_packets; i++) {
325 int status = urb->iso_frame_desc[i].status;
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 if (status < 0) {
328 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 if (urb->iso_frame_desc[i].status != -EPROTO)
330 continue;
331 }
332
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300333 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300334
335 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300336 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300337 continue;
338 }
339 if (urb->iso_frame_desc[i].actual_length >
340 dev->max_pkt_size) {
341 em28xx_isocdbg("packet bigger than packet size");
342 continue;
343 }
344
345 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300346
347 /* FIXME: incomplete buffer checks where removed to make
348 logic simpler. Impacts of those changes should be evaluated
349 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300350 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
351 em28xx_isocdbg("VBI HEADER!!!\n");
352 /* FIXME: Should add vbi copy */
353 continue;
354 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300355 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300356 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300357 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300358
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300359 if (!(p[2] & 1)) {
360 if (buf != NULL)
361 buffer_filled(dev, dma_q, buf);
362 get_next_buf(dma_q, &buf);
363 if (buf == NULL)
364 outp = NULL;
365 else
366 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300367 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300368
369 if (buf != NULL) {
370 if (p[2] & 1)
371 buf->top_field = 0;
372 else
373 buf->top_field = 1;
374 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300375
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300376 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300378 if (buf != NULL)
379 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300380 }
381 return rc;
382}
383
384/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300385 Videobuf operations
386 ------------------------------------------------------------------*/
387
388static int
389buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
390{
391 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300392 struct em28xx *dev = fh->dev;
393 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300394
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300395 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
396
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300397 if (0 == *count)
398 *count = EM28XX_DEF_BUF;
399
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300400 if (*count < EM28XX_MIN_BUF)
401 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300403 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300404 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300405 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300406 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300407
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300408 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300409
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300410 return 0;
411}
412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
415{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300416 struct em28xx_fh *fh = vq->priv_data;
417 struct em28xx *dev = fh->dev;
418 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300419 if (in_interrupt())
420 BUG();
421
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300422 /* We used to wait for the buffer to finish here, but this didn't work
423 because, as we were keeping the state as VIDEOBUF_QUEUED,
424 videobuf_queue_cancel marked it as finished for us.
425 (Also, it could wedge forever if the hardware was misconfigured.)
426
427 This should be safe; by the time we get here, the buffer isn't
428 queued anymore. If we ever start marking the buffers as
429 VIDEOBUF_ACTIVE, it won't be, though.
430 */
431 spin_lock_irqsave(&dev->slock, flags);
432 if (dev->isoc_ctl.buf == buf)
433 dev->isoc_ctl.buf = NULL;
434 spin_unlock_irqrestore(&dev->slock, flags);
435
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 videobuf_vmalloc_free(&buf->vb);
437 buf->vb.state = VIDEOBUF_NEEDS_INIT;
438}
439
440static int
441buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
442 enum v4l2_field field)
443{
444 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300445 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300446 struct em28xx *dev = fh->dev;
447 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300448
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300449 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450
451 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
452 return -EINVAL;
453
Brandon Philips05612972008-04-13 14:57:01 -0300454 buf->vb.width = dev->width;
455 buf->vb.height = dev->height;
456 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457
458 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300459 rc = videobuf_iolock(vq, &buf->vb, NULL);
460 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300462 }
463
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466
467 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300468 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
469 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300470 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300471 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300472 goto fail;
473 }
474
475 buf->vb.state = VIDEOBUF_PREPARED;
476 return 0;
477
478fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300479 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300480 return rc;
481}
482
483static void
484buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
485{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300486 struct em28xx_buffer *buf = container_of(vb,
487 struct em28xx_buffer,
488 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300490 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300493 buf->vb.state = VIDEOBUF_QUEUED;
494 list_add_tail(&buf->vb.queue, &vidq->active);
495
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496}
497
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300498static void buffer_release(struct videobuf_queue *vq,
499 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300501 struct em28xx_buffer *buf = container_of(vb,
502 struct em28xx_buffer,
503 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300505 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300506
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300507 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300509 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300510}
511
512static struct videobuf_queue_ops em28xx_video_qops = {
513 .buf_setup = buffer_setup,
514 .buf_prepare = buffer_prepare,
515 .buf_queue = buffer_queue,
516 .buf_release = buffer_release,
517};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800518
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300519/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800521static void video_mux(struct em28xx *dev, int index)
522{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800523 dev->ctl_input = index;
524 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300525 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800526
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300527 if (!dev->ctl_aoutput)
528 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
529
Hans Verkuil5325b422009-04-02 11:26:22 -0300530 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
531 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800532
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300533 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300534 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300535 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
536 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300537 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300538 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300539 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
540 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800541 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300542
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300543 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300544 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
545 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300546 }
547
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300548 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800549}
550
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300551/* Usage lock check functions */
552static int res_get(struct em28xx_fh *fh)
553{
554 struct em28xx *dev = fh->dev;
555 int rc = 0;
556
557 /* This instance already has stream_on */
558 if (fh->stream_on)
559 return rc;
560
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300561 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300562 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300563
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300564 dev->stream_on = 1;
565 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300566 return rc;
567}
568
569static int res_check(struct em28xx_fh *fh)
570{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300571 return fh->stream_on;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300572}
573
574static void res_free(struct em28xx_fh *fh)
575{
576 struct em28xx *dev = fh->dev;
577
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300578 fh->stream_on = 0;
579 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300580}
581
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800582/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300583 * em28xx_get_ctrl()
584 * return the current saturation, brightness or contrast, mute state
585 */
586static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
587{
588 switch (ctrl->id) {
589 case V4L2_CID_AUDIO_MUTE:
590 ctrl->value = dev->mute;
591 return 0;
592 case V4L2_CID_AUDIO_VOLUME:
593 ctrl->value = dev->volume;
594 return 0;
595 default:
596 return -EINVAL;
597 }
598}
599
600/*
601 * em28xx_set_ctrl()
602 * mute or set new saturation, brightness or contrast
603 */
604static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
605{
606 switch (ctrl->id) {
607 case V4L2_CID_AUDIO_MUTE:
608 if (ctrl->value != dev->mute) {
609 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300610 return em28xx_audio_analog_set(dev);
611 }
612 return 0;
613 case V4L2_CID_AUDIO_VOLUME:
614 dev->volume = ctrl->value;
615 return em28xx_audio_analog_set(dev);
616 default:
617 return -EINVAL;
618 }
619}
620
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300621static int check_dev(struct em28xx *dev)
622{
623 if (dev->state & DEV_DISCONNECTED) {
624 em28xx_errdev("v4l2 ioctl: device not present\n");
625 return -ENODEV;
626 }
627
628 if (dev->state & DEV_MISCONFIGURED) {
629 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
630 "close and open it again\n");
631 return -EIO;
632 }
633 return 0;
634}
635
636static void get_scale(struct em28xx *dev,
637 unsigned int width, unsigned int height,
638 unsigned int *hscale, unsigned int *vscale)
639{
640 unsigned int maxw = norm_maxw(dev);
641 unsigned int maxh = norm_maxh(dev);
642
643 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
644 if (*hscale >= 0x4000)
645 *hscale = 0x3fff;
646
647 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
648 if (*vscale >= 0x4000)
649 *vscale = 0x3fff;
650}
651
652/* ------------------------------------------------------------------
653 IOCTL vidioc handling
654 ------------------------------------------------------------------*/
655
Hans Verkuil78b526a2008-05-28 12:16:41 -0300656static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300657 struct v4l2_format *f)
658{
659 struct em28xx_fh *fh = priv;
660 struct em28xx *dev = fh->dev;
661
662 mutex_lock(&dev->lock);
663
664 f->fmt.pix.width = dev->width;
665 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300666 f->fmt.pix.pixelformat = dev->format->fourcc;
667 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300668 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300669 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
670
671 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
672 f->fmt.pix.field = dev->interlaced ?
673 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
674
675 mutex_unlock(&dev->lock);
676 return 0;
677}
678
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300679static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
680{
681 unsigned int i;
682
683 for (i = 0; i < ARRAY_SIZE(format); i++)
684 if (format[i].fourcc == fourcc)
685 return &format[i];
686
687 return NULL;
688}
689
Hans Verkuil78b526a2008-05-28 12:16:41 -0300690static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300691 struct v4l2_format *f)
692{
693 struct em28xx_fh *fh = priv;
694 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -0300695 unsigned int width = f->fmt.pix.width;
696 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300697 unsigned int maxw = norm_maxw(dev);
698 unsigned int maxh = norm_maxh(dev);
699 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300700 struct em28xx_fmt *fmt;
701
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300702 /* FIXME: This is the only supported fmt */
703 if (dev->board.is_27xx)
704 f->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
705
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300706 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
707 if (!fmt) {
708 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
709 f->fmt.pix.pixelformat);
710 return -EINVAL;
711 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300712
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300713 if (dev->board.is_27xx) {
714 /* FIXME: This is the only supported fmt */
715 width = 640;
716 height = 480;
717 } else if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300718 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -0300719 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
720 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
721 /* According to empiatech support the MaxPacketSize is too small
722 * to support framesizes larger than 640x480 @ 30 fps or 640x576
723 * @ 25 fps. As this would cut of a part of the image we prefer
724 * 360x576 or 360x480 for now */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300725 if (width == maxw && height == maxh)
726 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -0300727 } else {
728 /* width must even because of the YUYV format
729 height must be even because of interlacing */
730 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300731 }
732
733 get_scale(dev, width, height, &hscale, &vscale);
734
735 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
736 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
737
738 f->fmt.pix.width = width;
739 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300740 f->fmt.pix.pixelformat = fmt->fourcc;
741 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
742 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300743 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
744 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
745
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300746 return 0;
747}
748
Hans Verkuil78b526a2008-05-28 12:16:41 -0300749static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300750 struct v4l2_format *f)
751{
752 struct em28xx_fh *fh = priv;
753 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300754 int rc;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300755 struct em28xx_fmt *fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300756
757 rc = check_dev(dev);
758 if (rc < 0)
759 return rc;
760
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300761 mutex_lock(&dev->lock);
762
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -0300763 /* FIXME: This is the only supported fmt */
764 if (dev->board.is_27xx)
765 f->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
766
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300767 vidioc_try_fmt_vid_cap(file, priv, f);
768
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300769 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -0300770 if (!fmt) {
771 rc = -EINVAL;
772 goto out;
773 }
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300774
Brandon Philips05612972008-04-13 14:57:01 -0300775 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
776 em28xx_errdev("%s queue busy\n", __func__);
777 rc = -EBUSY;
778 goto out;
779 }
780
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300781 if (dev->stream_on && !fh->stream_on) {
782 em28xx_errdev("%s device in use by another fh\n", __func__);
783 rc = -EBUSY;
784 goto out;
785 }
786
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300787 /* set new image size */
788 dev->width = f->fmt.pix.width;
789 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300790 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300791 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
792
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300794 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795
Brandon Philips05612972008-04-13 14:57:01 -0300796 rc = 0;
797
798out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300800 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300801}
802
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300803static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804{
805 struct em28xx_fh *fh = priv;
806 struct em28xx *dev = fh->dev;
807 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 int rc;
809
810 rc = check_dev(dev);
811 if (rc < 0)
812 return rc;
813
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300815 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816
817 /* Adjusts width/height, if needed */
818 f.fmt.pix.width = dev->width;
819 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300820 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300821
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 /* set new image size */
823 dev->width = f.fmt.pix.width;
824 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
826
827 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300828 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300829
830 mutex_unlock(&dev->lock);
831 return 0;
832}
833
834static const char *iname[] = {
835 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
836 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
837 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
838 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
839 [EM28XX_VMUX_SVIDEO] = "S-Video",
840 [EM28XX_VMUX_TELEVISION] = "Television",
841 [EM28XX_VMUX_CABLE] = "Cable TV",
842 [EM28XX_VMUX_DVB] = "DVB",
843 [EM28XX_VMUX_DEBUG] = "for debug only",
844};
845
846static int vidioc_enum_input(struct file *file, void *priv,
847 struct v4l2_input *i)
848{
849 struct em28xx_fh *fh = priv;
850 struct em28xx *dev = fh->dev;
851 unsigned int n;
852
853 n = i->index;
854 if (n >= MAX_EM28XX_INPUT)
855 return -EINVAL;
856 if (0 == INPUT(n)->type)
857 return -EINVAL;
858
859 i->index = n;
860 i->type = V4L2_INPUT_TYPE_CAMERA;
861
862 strcpy(i->name, iname[INPUT(n)->type]);
863
864 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
865 (EM28XX_VMUX_CABLE == INPUT(n)->type))
866 i->type = V4L2_INPUT_TYPE_TUNER;
867
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300868 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300869
870 return 0;
871}
872
873static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
874{
875 struct em28xx_fh *fh = priv;
876 struct em28xx *dev = fh->dev;
877
878 *i = dev->ctl_input;
879
880 return 0;
881}
882
883static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
884{
885 struct em28xx_fh *fh = priv;
886 struct em28xx *dev = fh->dev;
887 int rc;
888
889 rc = check_dev(dev);
890 if (rc < 0)
891 return rc;
892
893 if (i >= MAX_EM28XX_INPUT)
894 return -EINVAL;
895 if (0 == INPUT(i)->type)
896 return -EINVAL;
897
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300898 dev->ctl_input = i;
899
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300900 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300901 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300902 mutex_unlock(&dev->lock);
903 return 0;
904}
905
906static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
907{
908 struct em28xx_fh *fh = priv;
909 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300911 switch (a->index) {
912 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300913 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300914 break;
915 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300916 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300917 break;
918 case EM28XX_AMUX_VIDEO2:
919 strcpy(a->name, "Television alt");
920 break;
921 case EM28XX_AMUX_PHONE:
922 strcpy(a->name, "Phone");
923 break;
924 case EM28XX_AMUX_MIC:
925 strcpy(a->name, "Mic");
926 break;
927 case EM28XX_AMUX_CD:
928 strcpy(a->name, "CD");
929 break;
930 case EM28XX_AMUX_AUX:
931 strcpy(a->name, "Aux");
932 break;
933 case EM28XX_AMUX_PCM_OUT:
934 strcpy(a->name, "PCM");
935 break;
936 default:
937 return -EINVAL;
938 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300939
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300940 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300942
943 return 0;
944}
945
946static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
947{
948 struct em28xx_fh *fh = priv;
949 struct em28xx *dev = fh->dev;
950
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300951
952 if (a->index >= MAX_EM28XX_INPUT)
953 return -EINVAL;
954 if (0 == INPUT(a->index)->type)
955 return -EINVAL;
956
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300957 mutex_lock(&dev->lock);
958
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300959 dev->ctl_ainput = INPUT(a->index)->amux;
960 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300961
962 if (!dev->ctl_aoutput)
963 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300964
965 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966 return 0;
967}
968
969static int vidioc_queryctrl(struct file *file, void *priv,
970 struct v4l2_queryctrl *qc)
971{
972 struct em28xx_fh *fh = priv;
973 struct em28xx *dev = fh->dev;
974 int id = qc->id;
975 int i;
976 int rc;
977
978 rc = check_dev(dev);
979 if (rc < 0)
980 return rc;
981
982 memset(qc, 0, sizeof(*qc));
983
984 qc->id = id;
985
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300986 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300987 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
988 if (qc->id && qc->id == em28xx_qctrl[i].id) {
989 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
990 return 0;
991 }
992 }
993 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300994
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300995 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300996 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 mutex_unlock(&dev->lock);
998
999 if (qc->type)
1000 return 0;
1001 else
1002 return -EINVAL;
1003}
1004
1005static int vidioc_g_ctrl(struct file *file, void *priv,
1006 struct v4l2_control *ctrl)
1007{
1008 struct em28xx_fh *fh = priv;
1009 struct em28xx *dev = fh->dev;
1010 int rc;
1011
1012 rc = check_dev(dev);
1013 if (rc < 0)
1014 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001015 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001016
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001017 mutex_lock(&dev->lock);
1018
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001019 if (dev->board.has_msp34xx)
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001020 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001021 else {
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001022 rc = em28xx_get_ctrl(dev, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001023 if (rc < 0) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001024 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001025 rc = 0;
1026 }
1027 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028
1029 mutex_unlock(&dev->lock);
1030 return rc;
1031}
1032
1033static int vidioc_s_ctrl(struct file *file, void *priv,
1034 struct v4l2_control *ctrl)
1035{
1036 struct em28xx_fh *fh = priv;
1037 struct em28xx *dev = fh->dev;
1038 u8 i;
1039 int rc;
1040
1041 rc = check_dev(dev);
1042 if (rc < 0)
1043 return rc;
1044
1045 mutex_lock(&dev->lock);
1046
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001047 if (dev->board.has_msp34xx)
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001048 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001049 else {
1050 rc = 1;
1051 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1052 if (ctrl->id == em28xx_qctrl[i].id) {
1053 if (ctrl->value < em28xx_qctrl[i].minimum ||
1054 ctrl->value > em28xx_qctrl[i].maximum) {
1055 rc = -ERANGE;
1056 break;
1057 }
1058
1059 rc = em28xx_set_ctrl(dev, ctrl);
1060 break;
1061 }
1062 }
1063 }
1064
1065 /* Control not found - try to send it to the attached devices */
1066 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001067 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068 rc = 0;
1069 }
1070
1071 mutex_unlock(&dev->lock);
1072 return rc;
1073}
1074
1075static int vidioc_g_tuner(struct file *file, void *priv,
1076 struct v4l2_tuner *t)
1077{
1078 struct em28xx_fh *fh = priv;
1079 struct em28xx *dev = fh->dev;
1080 int rc;
1081
1082 rc = check_dev(dev);
1083 if (rc < 0)
1084 return rc;
1085
1086 if (0 != t->index)
1087 return -EINVAL;
1088
1089 strcpy(t->name, "Tuner");
1090
1091 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001092 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001094
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001095 return 0;
1096}
1097
1098static int vidioc_s_tuner(struct file *file, void *priv,
1099 struct v4l2_tuner *t)
1100{
1101 struct em28xx_fh *fh = priv;
1102 struct em28xx *dev = fh->dev;
1103 int rc;
1104
1105 rc = check_dev(dev);
1106 if (rc < 0)
1107 return rc;
1108
1109 if (0 != t->index)
1110 return -EINVAL;
1111
1112 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001113 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001114 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001115
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001116 return 0;
1117}
1118
1119static int vidioc_g_frequency(struct file *file, void *priv,
1120 struct v4l2_frequency *f)
1121{
1122 struct em28xx_fh *fh = priv;
1123 struct em28xx *dev = fh->dev;
1124
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001125 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001126 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001127 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001128 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129
1130 return 0;
1131}
1132
1133static int vidioc_s_frequency(struct file *file, void *priv,
1134 struct v4l2_frequency *f)
1135{
1136 struct em28xx_fh *fh = priv;
1137 struct em28xx *dev = fh->dev;
1138 int rc;
1139
1140 rc = check_dev(dev);
1141 if (rc < 0)
1142 return rc;
1143
1144 if (0 != f->tuner)
1145 return -EINVAL;
1146
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001147 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1148 return -EINVAL;
1149 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001150 return -EINVAL;
1151
1152 mutex_lock(&dev->lock);
1153
1154 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001155 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001156
1157 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001158
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001159 return 0;
1160}
1161
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001162#ifdef CONFIG_VIDEO_ADV_DEBUG
1163static int em28xx_reg_len(int reg)
1164{
1165 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001166 case EM28XX_R40_AC97LSB:
1167 case EM28XX_R30_HSCALELOW:
1168 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001169 return 2;
1170 default:
1171 return 1;
1172 }
1173}
1174
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001175static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001176 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001177{
1178 struct em28xx_fh *fh = priv;
1179 struct em28xx *dev = fh->dev;
1180
1181 chip->ident = V4L2_IDENT_NONE;
1182 chip->revision = 0;
1183
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001184 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001185
1186 return 0;
1187}
1188
1189
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001190static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001191 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001192{
1193 struct em28xx_fh *fh = priv;
1194 struct em28xx *dev = fh->dev;
1195 int ret;
1196
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001197 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001198 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001199 mutex_lock(&dev->lock);
1200 ret = em28xx_read_ac97(dev, reg->reg);
1201 mutex_unlock(&dev->lock);
1202 if (ret < 0)
1203 return ret;
1204
1205 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001206 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001207 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001208 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001209 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001210 return 0;
1211 case V4L2_CHIP_MATCH_I2C_ADDR:
1212 /* Not supported yet */
1213 return -EINVAL;
1214 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001215 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001216 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001217 }
1218
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001219 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001220 reg->size = em28xx_reg_len(reg->reg);
1221 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001222 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001223 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001224 mutex_unlock(&dev->lock);
1225
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001226 if (ret < 0)
1227 return ret;
1228
1229 reg->val = ret;
1230 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001231 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001232 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001233 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1234 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001235 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001236 if (ret < 0)
1237 return ret;
1238
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001239 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001240 }
1241
1242 return 0;
1243}
1244
1245static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001246 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001247{
1248 struct em28xx_fh *fh = priv;
1249 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001250 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001251 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001252
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001253 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001254 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001255 mutex_lock(&dev->lock);
1256 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1257 mutex_unlock(&dev->lock);
1258
1259 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001260 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001261 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001262 return 0;
1263 case V4L2_CHIP_MATCH_I2C_ADDR:
1264 /* Not supported yet */
1265 return -EINVAL;
1266 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001267 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001268 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001269 }
1270
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001271 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001272 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001273
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001274 mutex_lock(&dev->lock);
1275 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1276 em28xx_reg_len(reg->reg));
1277 mutex_unlock(&dev->lock);
1278
1279 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001280}
1281#endif
1282
1283
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001284static int vidioc_cropcap(struct file *file, void *priv,
1285 struct v4l2_cropcap *cc)
1286{
1287 struct em28xx_fh *fh = priv;
1288 struct em28xx *dev = fh->dev;
1289
1290 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1291 return -EINVAL;
1292
1293 cc->bounds.left = 0;
1294 cc->bounds.top = 0;
1295 cc->bounds.width = dev->width;
1296 cc->bounds.height = dev->height;
1297 cc->defrect = cc->bounds;
1298 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1299 cc->pixelaspect.denominator = 59;
1300
1301 return 0;
1302}
1303
1304static int vidioc_streamon(struct file *file, void *priv,
1305 enum v4l2_buf_type type)
1306{
1307 struct em28xx_fh *fh = priv;
1308 struct em28xx *dev = fh->dev;
1309 int rc;
1310
1311 rc = check_dev(dev);
1312 if (rc < 0)
1313 return rc;
1314
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001315
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001316 mutex_lock(&dev->lock);
1317 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001318
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001319 if (likely(rc >= 0))
1320 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001321
1322 mutex_unlock(&dev->lock);
1323
1324 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001325}
1326
1327static int vidioc_streamoff(struct file *file, void *priv,
1328 enum v4l2_buf_type type)
1329{
1330 struct em28xx_fh *fh = priv;
1331 struct em28xx *dev = fh->dev;
1332 int rc;
1333
1334 rc = check_dev(dev);
1335 if (rc < 0)
1336 return rc;
1337
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001338 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1339 return -EINVAL;
1340 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001341 return -EINVAL;
1342
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001343 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001344
1345 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001346 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001347
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001348 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001349
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001350 return 0;
1351}
1352
1353static int vidioc_querycap(struct file *file, void *priv,
1354 struct v4l2_capability *cap)
1355{
1356 struct em28xx_fh *fh = priv;
1357 struct em28xx *dev = fh->dev;
1358
1359 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1360 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001361 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362
1363 cap->version = EM28XX_VERSION_CODE;
1364
1365 cap->capabilities =
1366 V4L2_CAP_SLICED_VBI_CAPTURE |
1367 V4L2_CAP_VIDEO_CAPTURE |
1368 V4L2_CAP_AUDIO |
1369 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1370
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001371 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372 cap->capabilities |= V4L2_CAP_TUNER;
1373
1374 return 0;
1375}
1376
Hans Verkuil78b526a2008-05-28 12:16:41 -03001377static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001378 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001379{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001380 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381 return -EINVAL;
1382
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001383 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1384 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001385
1386 return 0;
1387}
1388
1389/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001390static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001391 struct v4l2_format *f)
1392{
1393 struct em28xx_fh *fh = priv;
1394 struct em28xx *dev = fh->dev;
1395 int rc;
1396
1397 rc = check_dev(dev);
1398 if (rc < 0)
1399 return rc;
1400
1401 mutex_lock(&dev->lock);
1402
1403 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001404 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001405
1406 if (f->fmt.sliced.service_set == 0)
1407 rc = -EINVAL;
1408
1409 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001410
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411 return rc;
1412}
1413
Hans Verkuil78b526a2008-05-28 12:16:41 -03001414static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415 struct v4l2_format *f)
1416{
1417 struct em28xx_fh *fh = priv;
1418 struct em28xx *dev = fh->dev;
1419 int rc;
1420
1421 rc = check_dev(dev);
1422 if (rc < 0)
1423 return rc;
1424
1425 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001426 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001427 mutex_unlock(&dev->lock);
1428
1429 if (f->fmt.sliced.service_set == 0)
1430 return -EINVAL;
1431
1432 return 0;
1433}
1434
1435
1436static int vidioc_reqbufs(struct file *file, void *priv,
1437 struct v4l2_requestbuffers *rb)
1438{
1439 struct em28xx_fh *fh = priv;
1440 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001441 int rc;
1442
1443 rc = check_dev(dev);
1444 if (rc < 0)
1445 return rc;
1446
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001447 return videobuf_reqbufs(&fh->vb_vidq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001448}
1449
1450static int vidioc_querybuf(struct file *file, void *priv,
1451 struct v4l2_buffer *b)
1452{
1453 struct em28xx_fh *fh = priv;
1454 struct em28xx *dev = fh->dev;
1455 int rc;
1456
1457 rc = check_dev(dev);
1458 if (rc < 0)
1459 return rc;
1460
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001461 return videobuf_querybuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462}
1463
1464static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1465{
1466 struct em28xx_fh *fh = priv;
1467 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001468 int rc;
1469
1470 rc = check_dev(dev);
1471 if (rc < 0)
1472 return rc;
1473
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001474 return videobuf_qbuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001475}
1476
1477static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1478{
1479 struct em28xx_fh *fh = priv;
1480 struct em28xx *dev = fh->dev;
1481 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001482
1483 rc = check_dev(dev);
1484 if (rc < 0)
1485 return rc;
1486
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001487 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488}
1489
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001490#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001491static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001492{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001493 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001494
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001495 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001496}
1497#endif
1498
1499
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001500/* ----------------------------------------------------------- */
1501/* RADIO ESPECIFIC IOCTLS */
1502/* ----------------------------------------------------------- */
1503
1504static int radio_querycap(struct file *file, void *priv,
1505 struct v4l2_capability *cap)
1506{
1507 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1508
1509 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1510 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001511 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001512
1513 cap->version = EM28XX_VERSION_CODE;
1514 cap->capabilities = V4L2_CAP_TUNER;
1515 return 0;
1516}
1517
1518static int radio_g_tuner(struct file *file, void *priv,
1519 struct v4l2_tuner *t)
1520{
1521 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1522
1523 if (unlikely(t->index > 0))
1524 return -EINVAL;
1525
1526 strcpy(t->name, "Radio");
1527 t->type = V4L2_TUNER_RADIO;
1528
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001529 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001530 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001531 mutex_unlock(&dev->lock);
1532
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001533 return 0;
1534}
1535
1536static int radio_enum_input(struct file *file, void *priv,
1537 struct v4l2_input *i)
1538{
1539 if (i->index != 0)
1540 return -EINVAL;
1541 strcpy(i->name, "Radio");
1542 i->type = V4L2_INPUT_TYPE_TUNER;
1543
1544 return 0;
1545}
1546
1547static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1548{
1549 if (unlikely(a->index))
1550 return -EINVAL;
1551
1552 strcpy(a->name, "Radio");
1553 return 0;
1554}
1555
1556static int radio_s_tuner(struct file *file, void *priv,
1557 struct v4l2_tuner *t)
1558{
1559 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1560
1561 if (0 != t->index)
1562 return -EINVAL;
1563
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001564 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001565 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001566 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001567
1568 return 0;
1569}
1570
1571static int radio_s_audio(struct file *file, void *fh,
1572 struct v4l2_audio *a)
1573{
1574 return 0;
1575}
1576
1577static int radio_s_input(struct file *file, void *fh, unsigned int i)
1578{
1579 return 0;
1580}
1581
1582static int radio_queryctrl(struct file *file, void *priv,
1583 struct v4l2_queryctrl *qc)
1584{
1585 int i;
1586
1587 if (qc->id < V4L2_CID_BASE ||
1588 qc->id >= V4L2_CID_LASTP1)
1589 return -EINVAL;
1590
1591 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1592 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1593 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1594 return 0;
1595 }
1596 }
1597
1598 return -EINVAL;
1599}
1600
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001601/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001602 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001603 * inits the device and starts isoc transfer
1604 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001605static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001606{
Hans Verkuilbec43662008-12-30 06:58:20 -03001607 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001608 int errCode = 0, radio;
1609 struct em28xx *dev;
1610 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001611 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001612
Hans Verkuilbec43662008-12-30 06:58:20 -03001613 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001614
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001615 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001616 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001617
1618 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001619
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001620 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001621 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001624 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001625 if (!fh) {
1626 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001627 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001628 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001630 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001631 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001632 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001633 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001634
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001635 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001636 dev->width = norm_maxw(dev);
1637 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001638 dev->hscale = 0;
1639 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001640
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001641 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001642 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001643 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001644
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001645 /* Needed, since GPIO might have disabled power of
1646 some i2c device
1647 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001648 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001649
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001650 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001651 if (fh->radio) {
1652 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001653 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001654 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001655
1656 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001657
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001658 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1659 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1660 sizeof(struct em28xx_buffer), fh);
1661
Ingo Molnar3593cab2006-02-07 06:49:14 -02001662 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001663
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664 return errCode;
1665}
1666
1667/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001668 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 * unregisters the v4l2,i2c and usb devices
1670 * called when the device gets disconected or at module unload
1671*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001672void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001675 /*FIXME: I2C IR should be disconnected */
1676
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001677 if (dev->radio_dev) {
1678 if (-1 != dev->radio_dev->minor)
1679 video_unregister_device(dev->radio_dev);
1680 else
1681 video_device_release(dev->radio_dev);
1682 dev->radio_dev = NULL;
1683 }
1684 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001685 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1686 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001687 if (-1 != dev->vbi_dev->minor)
1688 video_unregister_device(dev->vbi_dev);
1689 else
1690 video_device_release(dev->vbi_dev);
1691 dev->vbi_dev = NULL;
1692 }
1693 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001694 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1695 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001696 if (-1 != dev->vdev->minor)
1697 video_unregister_device(dev->vdev);
1698 else
1699 video_device_release(dev->vdev);
1700 dev->vdev = NULL;
1701 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702}
1703
1704/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001705 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001706 * stops streaming and deallocates all resources allocated by the v4l2
1707 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001709static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 struct em28xx_fh *fh = filp->private_data;
1712 struct em28xx *dev = fh->dev;
1713 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001715 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001716
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001717
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001718 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001719 if (res_check(fh))
1720 res_free(fh);
1721
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001722 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001723 videobuf_stop(&fh->vb_vidq);
1724 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001726 /* the device is already disconnect,
1727 free the remaining resources */
1728 if (dev->state & DEV_DISCONNECTED) {
1729 em28xx_release_resources(dev);
1730 mutex_unlock(&dev->lock);
1731 kfree(dev);
1732 return 0;
1733 }
1734
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001735 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03001736 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001737
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001738 /* do this before setting alternate! */
1739 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001740 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001741
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001742 /* set alternate 0 */
1743 dev->alt = 0;
1744 em28xx_videodbg("setting alternate 0\n");
1745 errCode = usb_set_interface(dev->udev, 0, 0);
1746 if (errCode < 0) {
1747 em28xx_errdev("cannot change alternate number to "
1748 "0 (error=%i)\n", errCode);
1749 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001750 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001751 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001752 dev->users--;
1753 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001754 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755 return 0;
1756}
1757
1758/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001759 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760 * will allocate buffers when called for the first time
1761 */
1762static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001763em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001764 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001766 struct em28xx_fh *fh = filp->private_data;
1767 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001768 int rc;
1769
1770 rc = check_dev(dev);
1771 if (rc < 0)
1772 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001773
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001774 /* FIXME: read() is not prepared to allow changing the video
1775 resolution while streaming. Seems a bug at em28xx_set_fmt
1776 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001777
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001778 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001779 mutex_lock(&dev->lock);
1780 rc = res_get(fh);
1781 mutex_unlock(&dev->lock);
1782
1783 if (unlikely(rc < 0))
1784 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001785
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001786 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1787 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001788 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001789 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790}
1791
1792/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001793 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001794 * will allocate buffers when called for the first time
1795 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001796static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001797{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001798 struct em28xx_fh *fh = filp->private_data;
1799 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001800 int rc;
1801
1802 rc = check_dev(dev);
1803 if (rc < 0)
1804 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001805
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001806 mutex_lock(&dev->lock);
1807 rc = res_get(fh);
1808 mutex_unlock(&dev->lock);
1809
1810 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001811 return POLLERR;
1812
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001813 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1814 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001815
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001816 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001817}
1818
1819/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001820 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001822static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001823{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001824 struct em28xx_fh *fh = filp->private_data;
1825 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001826 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001827
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001828 rc = check_dev(dev);
1829 if (rc < 0)
1830 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001831
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001832 mutex_lock(&dev->lock);
1833 rc = res_get(fh);
1834 mutex_unlock(&dev->lock);
1835
1836 if (unlikely(rc < 0))
1837 return rc;
1838
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001839 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001840
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001841 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1842 (unsigned long)vma->vm_start,
1843 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1844 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001846 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847}
1848
Hans Verkuilbec43662008-12-30 06:58:20 -03001849static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001850 .owner = THIS_MODULE,
1851 .open = em28xx_v4l2_open,
1852 .release = em28xx_v4l2_close,
1853 .read = em28xx_v4l2_read,
1854 .poll = em28xx_v4l2_poll,
1855 .mmap = em28xx_v4l2_mmap,
1856 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001857};
1858
Hans Verkuila3998102008-07-21 02:57:38 -03001859static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001861 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1862 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1863 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1864 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001865 .vidioc_g_audio = vidioc_g_audio,
1866 .vidioc_s_audio = vidioc_s_audio,
1867 .vidioc_cropcap = vidioc_cropcap,
1868
Hans Verkuil78b526a2008-05-28 12:16:41 -03001869 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1870 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1871 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001872
1873 .vidioc_reqbufs = vidioc_reqbufs,
1874 .vidioc_querybuf = vidioc_querybuf,
1875 .vidioc_qbuf = vidioc_qbuf,
1876 .vidioc_dqbuf = vidioc_dqbuf,
1877 .vidioc_s_std = vidioc_s_std,
1878 .vidioc_enum_input = vidioc_enum_input,
1879 .vidioc_g_input = vidioc_g_input,
1880 .vidioc_s_input = vidioc_s_input,
1881 .vidioc_queryctrl = vidioc_queryctrl,
1882 .vidioc_g_ctrl = vidioc_g_ctrl,
1883 .vidioc_s_ctrl = vidioc_s_ctrl,
1884 .vidioc_streamon = vidioc_streamon,
1885 .vidioc_streamoff = vidioc_streamoff,
1886 .vidioc_g_tuner = vidioc_g_tuner,
1887 .vidioc_s_tuner = vidioc_s_tuner,
1888 .vidioc_g_frequency = vidioc_g_frequency,
1889 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001890#ifdef CONFIG_VIDEO_ADV_DEBUG
1891 .vidioc_g_register = vidioc_g_register,
1892 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001893 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001894#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001895#ifdef CONFIG_VIDEO_V4L1_COMPAT
1896 .vidiocgmbuf = vidiocgmbuf,
1897#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001898};
1899
1900static const struct video_device em28xx_video_template = {
1901 .fops = &em28xx_v4l_fops,
1902 .release = video_device_release,
1903 .ioctl_ops = &video_ioctl_ops,
1904
1905 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001906
1907 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001908 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001909};
1910
Hans Verkuilbec43662008-12-30 06:58:20 -03001911static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001912 .owner = THIS_MODULE,
1913 .open = em28xx_v4l2_open,
1914 .release = em28xx_v4l2_close,
1915 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001916};
1917
1918static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001919 .vidioc_querycap = radio_querycap,
1920 .vidioc_g_tuner = radio_g_tuner,
1921 .vidioc_enum_input = radio_enum_input,
1922 .vidioc_g_audio = radio_g_audio,
1923 .vidioc_s_tuner = radio_s_tuner,
1924 .vidioc_s_audio = radio_s_audio,
1925 .vidioc_s_input = radio_s_input,
1926 .vidioc_queryctrl = radio_queryctrl,
1927 .vidioc_g_ctrl = vidioc_g_ctrl,
1928 .vidioc_s_ctrl = vidioc_s_ctrl,
1929 .vidioc_g_frequency = vidioc_g_frequency,
1930 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001931#ifdef CONFIG_VIDEO_ADV_DEBUG
1932 .vidioc_g_register = vidioc_g_register,
1933 .vidioc_s_register = vidioc_s_register,
1934#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001935};
1936
Hans Verkuila3998102008-07-21 02:57:38 -03001937static struct video_device em28xx_radio_template = {
1938 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001939 .fops = &radio_fops,
1940 .ioctl_ops = &radio_ioctl_ops,
1941 .minor = -1,
1942};
1943
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001944/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001945
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001946
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001947
Adrian Bunk532fe652008-01-28 22:10:48 -03001948static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001949 const struct video_device *template,
1950 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001951{
1952 struct video_device *vfd;
1953
1954 vfd = video_device_alloc();
1955 if (NULL == vfd)
1956 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001957
1958 *vfd = *template;
1959 vfd->minor = -1;
1960 vfd->v4l2_dev = &dev->v4l2_dev;
1961 vfd->release = video_device_release;
1962 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001963
1964 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1965 dev->name, type_name);
1966
1967 return vfd;
1968}
1969
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001970int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001971{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03001972 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001973 int ret;
1974
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001975 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1976 dev->name,
1977 (EM28XX_VERSION_CODE >> 16) & 0xff,
1978 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1979
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001980 /* set default norm */
1981 dev->norm = em28xx_video_template.current_norm;
1982 dev->width = norm_maxw(dev);
1983 dev->height = norm_maxh(dev);
1984 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1985 dev->hscale = 0;
1986 dev->vscale = 0;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001987 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001988
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001989 /* Analog specific initialization */
1990 dev->format = &format[0];
1991 video_mux(dev, dev->ctl_input);
1992
1993 /* Audio defaults */
1994 dev->mute = 1;
1995 dev->volume = 0x1f;
1996
1997 /* enable vbi capturing */
1998
1999/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002000 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2001 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2002 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002003 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2004
2005 em28xx_set_outfmt(dev);
2006 em28xx_colorlevels_set_default(dev);
2007 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002008
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002009 /* allocate and fill video video_device struct */
2010 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2011 if (!dev->vdev) {
2012 em28xx_errdev("cannot allocate video_device.\n");
2013 return -ENODEV;
2014 }
2015
2016 /* register v4l2 video video_device */
2017 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2018 video_nr[dev->devno]);
2019 if (ret) {
2020 em28xx_errdev("unable to register video device (error=%i).\n",
2021 ret);
2022 return ret;
2023 }
2024
2025 /* Allocate and fill vbi video_device struct */
2026 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2027
2028 /* register v4l2 vbi video_device */
2029 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2030 vbi_nr[dev->devno]);
2031 if (ret < 0) {
2032 em28xx_errdev("unable to register vbi device\n");
2033 return ret;
2034 }
2035
2036 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002037 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2038 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002039 if (!dev->radio_dev) {
2040 em28xx_errdev("cannot allocate video_device.\n");
2041 return -ENODEV;
2042 }
2043 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2044 radio_nr[dev->devno]);
2045 if (ret < 0) {
2046 em28xx_errdev("can't register radio device\n");
2047 return ret;
2048 }
2049 em28xx_info("Registered radio device as /dev/radio%d\n",
2050 dev->radio_dev->num);
2051 }
2052
2053 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2054 dev->vdev->num, dev->vbi_dev->num);
2055
2056 return 0;
2057}