blob: 702fe8d6742e5d5b5d9102053127ca64d0e01d6a [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
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300749static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
750 unsigned width, unsigned height)
751{
752 struct em28xx_fmt *fmt;
753
754 /* FIXME: This is the only supported fmt */
755 if (dev->board.is_27xx) {
756 fourcc = V4L2_PIX_FMT_RGB565;
757 width = 640;
758 height = 480;
759 }
760
761 fmt = format_by_fourcc(fourcc);
762 if (!fmt)
763 return -EINVAL;
764
765 dev->format = fmt;
766 dev->width = width;
767 dev->height = height;
768
769 /* set new image size */
770 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
771
772 em28xx_set_alternate(dev);
773 em28xx_resolution_set(dev);
774
775 return 0;
776}
777
Hans Verkuil78b526a2008-05-28 12:16:41 -0300778static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300779 struct v4l2_format *f)
780{
781 struct em28xx_fh *fh = priv;
782 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300784
785 rc = check_dev(dev);
786 if (rc < 0)
787 return rc;
788
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300789 mutex_lock(&dev->lock);
790
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300791 vidioc_try_fmt_vid_cap(file, priv, f);
792
Brandon Philips05612972008-04-13 14:57:01 -0300793 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
794 em28xx_errdev("%s queue busy\n", __func__);
795 rc = -EBUSY;
796 goto out;
797 }
798
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300799 if (dev->stream_on && !fh->stream_on) {
800 em28xx_errdev("%s device in use by another fh\n", __func__);
801 rc = -EBUSY;
802 goto out;
803 }
804
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -0300805 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
806 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -0300807
808out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300810 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811}
812
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300813static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814{
815 struct em28xx_fh *fh = priv;
816 struct em28xx *dev = fh->dev;
817 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818 int rc;
819
820 rc = check_dev(dev);
821 if (rc < 0)
822 return rc;
823
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300824 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300825 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826
827 /* Adjusts width/height, if needed */
828 f.fmt.pix.width = dev->width;
829 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300830 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300831
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300832 /* set new image size */
833 dev->width = f.fmt.pix.width;
834 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300835 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
836
837 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300838 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300839
840 mutex_unlock(&dev->lock);
841 return 0;
842}
843
844static const char *iname[] = {
845 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
846 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
847 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
848 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
849 [EM28XX_VMUX_SVIDEO] = "S-Video",
850 [EM28XX_VMUX_TELEVISION] = "Television",
851 [EM28XX_VMUX_CABLE] = "Cable TV",
852 [EM28XX_VMUX_DVB] = "DVB",
853 [EM28XX_VMUX_DEBUG] = "for debug only",
854};
855
856static int vidioc_enum_input(struct file *file, void *priv,
857 struct v4l2_input *i)
858{
859 struct em28xx_fh *fh = priv;
860 struct em28xx *dev = fh->dev;
861 unsigned int n;
862
863 n = i->index;
864 if (n >= MAX_EM28XX_INPUT)
865 return -EINVAL;
866 if (0 == INPUT(n)->type)
867 return -EINVAL;
868
869 i->index = n;
870 i->type = V4L2_INPUT_TYPE_CAMERA;
871
872 strcpy(i->name, iname[INPUT(n)->type]);
873
874 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
875 (EM28XX_VMUX_CABLE == INPUT(n)->type))
876 i->type = V4L2_INPUT_TYPE_TUNER;
877
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300878 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300879
880 return 0;
881}
882
883static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
884{
885 struct em28xx_fh *fh = priv;
886 struct em28xx *dev = fh->dev;
887
888 *i = dev->ctl_input;
889
890 return 0;
891}
892
893static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
894{
895 struct em28xx_fh *fh = priv;
896 struct em28xx *dev = fh->dev;
897 int rc;
898
899 rc = check_dev(dev);
900 if (rc < 0)
901 return rc;
902
903 if (i >= MAX_EM28XX_INPUT)
904 return -EINVAL;
905 if (0 == INPUT(i)->type)
906 return -EINVAL;
907
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300908 dev->ctl_input = i;
909
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300911 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300912 mutex_unlock(&dev->lock);
913 return 0;
914}
915
916static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
917{
918 struct em28xx_fh *fh = priv;
919 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300920
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300921 switch (a->index) {
922 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300923 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300924 break;
925 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300926 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300927 break;
928 case EM28XX_AMUX_VIDEO2:
929 strcpy(a->name, "Television alt");
930 break;
931 case EM28XX_AMUX_PHONE:
932 strcpy(a->name, "Phone");
933 break;
934 case EM28XX_AMUX_MIC:
935 strcpy(a->name, "Mic");
936 break;
937 case EM28XX_AMUX_CD:
938 strcpy(a->name, "CD");
939 break;
940 case EM28XX_AMUX_AUX:
941 strcpy(a->name, "Aux");
942 break;
943 case EM28XX_AMUX_PCM_OUT:
944 strcpy(a->name, "PCM");
945 break;
946 default:
947 return -EINVAL;
948 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300949
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300950 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300951 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300952
953 return 0;
954}
955
956static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
957{
958 struct em28xx_fh *fh = priv;
959 struct em28xx *dev = fh->dev;
960
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300961
962 if (a->index >= MAX_EM28XX_INPUT)
963 return -EINVAL;
964 if (0 == INPUT(a->index)->type)
965 return -EINVAL;
966
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300967 mutex_lock(&dev->lock);
968
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300969 dev->ctl_ainput = INPUT(a->index)->amux;
970 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300971
972 if (!dev->ctl_aoutput)
973 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300974
975 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300976 return 0;
977}
978
979static int vidioc_queryctrl(struct file *file, void *priv,
980 struct v4l2_queryctrl *qc)
981{
982 struct em28xx_fh *fh = priv;
983 struct em28xx *dev = fh->dev;
984 int id = qc->id;
985 int i;
986 int rc;
987
988 rc = check_dev(dev);
989 if (rc < 0)
990 return rc;
991
992 memset(qc, 0, sizeof(*qc));
993
994 qc->id = id;
995
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300996 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
998 if (qc->id && qc->id == em28xx_qctrl[i].id) {
999 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1000 return 0;
1001 }
1002 }
1003 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001004
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001006 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007 mutex_unlock(&dev->lock);
1008
1009 if (qc->type)
1010 return 0;
1011 else
1012 return -EINVAL;
1013}
1014
1015static int vidioc_g_ctrl(struct file *file, void *priv,
1016 struct v4l2_control *ctrl)
1017{
1018 struct em28xx_fh *fh = priv;
1019 struct em28xx *dev = fh->dev;
1020 int rc;
1021
1022 rc = check_dev(dev);
1023 if (rc < 0)
1024 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001025 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001026
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001027 mutex_lock(&dev->lock);
1028
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001029 if (dev->board.has_msp34xx)
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001030 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001031 else {
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001032 rc = em28xx_get_ctrl(dev, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001033 if (rc < 0) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001034 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001035 rc = 0;
1036 }
1037 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038
1039 mutex_unlock(&dev->lock);
1040 return rc;
1041}
1042
1043static int vidioc_s_ctrl(struct file *file, void *priv,
1044 struct v4l2_control *ctrl)
1045{
1046 struct em28xx_fh *fh = priv;
1047 struct em28xx *dev = fh->dev;
1048 u8 i;
1049 int rc;
1050
1051 rc = check_dev(dev);
1052 if (rc < 0)
1053 return rc;
1054
1055 mutex_lock(&dev->lock);
1056
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001057 if (dev->board.has_msp34xx)
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001058 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059 else {
1060 rc = 1;
1061 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1062 if (ctrl->id == em28xx_qctrl[i].id) {
1063 if (ctrl->value < em28xx_qctrl[i].minimum ||
1064 ctrl->value > em28xx_qctrl[i].maximum) {
1065 rc = -ERANGE;
1066 break;
1067 }
1068
1069 rc = em28xx_set_ctrl(dev, ctrl);
1070 break;
1071 }
1072 }
1073 }
1074
1075 /* Control not found - try to send it to the attached devices */
1076 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001077 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001078 rc = 0;
1079 }
1080
1081 mutex_unlock(&dev->lock);
1082 return rc;
1083}
1084
1085static int vidioc_g_tuner(struct file *file, void *priv,
1086 struct v4l2_tuner *t)
1087{
1088 struct em28xx_fh *fh = priv;
1089 struct em28xx *dev = fh->dev;
1090 int rc;
1091
1092 rc = check_dev(dev);
1093 if (rc < 0)
1094 return rc;
1095
1096 if (0 != t->index)
1097 return -EINVAL;
1098
1099 strcpy(t->name, "Tuner");
1100
1101 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001102 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001103 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001104
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001105 return 0;
1106}
1107
1108static int vidioc_s_tuner(struct file *file, void *priv,
1109 struct v4l2_tuner *t)
1110{
1111 struct em28xx_fh *fh = priv;
1112 struct em28xx *dev = fh->dev;
1113 int rc;
1114
1115 rc = check_dev(dev);
1116 if (rc < 0)
1117 return rc;
1118
1119 if (0 != t->index)
1120 return -EINVAL;
1121
1122 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001123 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001124 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001125
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001126 return 0;
1127}
1128
1129static int vidioc_g_frequency(struct file *file, void *priv,
1130 struct v4l2_frequency *f)
1131{
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
1134
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001135 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001136 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001137 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001138 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001139
1140 return 0;
1141}
1142
1143static int vidioc_s_frequency(struct file *file, void *priv,
1144 struct v4l2_frequency *f)
1145{
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
1148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 if (0 != f->tuner)
1155 return -EINVAL;
1156
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001157 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1158 return -EINVAL;
1159 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160 return -EINVAL;
1161
1162 mutex_lock(&dev->lock);
1163
1164 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001165 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001166
1167 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001168
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169 return 0;
1170}
1171
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001172#ifdef CONFIG_VIDEO_ADV_DEBUG
1173static int em28xx_reg_len(int reg)
1174{
1175 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001176 case EM28XX_R40_AC97LSB:
1177 case EM28XX_R30_HSCALELOW:
1178 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001179 return 2;
1180 default:
1181 return 1;
1182 }
1183}
1184
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001185static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001186 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190
1191 chip->ident = V4L2_IDENT_NONE;
1192 chip->revision = 0;
1193
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001194 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001195
1196 return 0;
1197}
1198
1199
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001200static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001201 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001202{
1203 struct em28xx_fh *fh = priv;
1204 struct em28xx *dev = fh->dev;
1205 int ret;
1206
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001207 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001208 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001209 mutex_lock(&dev->lock);
1210 ret = em28xx_read_ac97(dev, reg->reg);
1211 mutex_unlock(&dev->lock);
1212 if (ret < 0)
1213 return ret;
1214
1215 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001216 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001217 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001218 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001219 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001220 return 0;
1221 case V4L2_CHIP_MATCH_I2C_ADDR:
1222 /* Not supported yet */
1223 return -EINVAL;
1224 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001225 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001226 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001227 }
1228
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001229 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001230 reg->size = em28xx_reg_len(reg->reg);
1231 if (reg->size == 1) {
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(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001234 mutex_unlock(&dev->lock);
1235
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001236 if (ret < 0)
1237 return ret;
1238
1239 reg->val = ret;
1240 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001241 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001242 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001243 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1244 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001245 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001246 if (ret < 0)
1247 return ret;
1248
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001249 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001250 }
1251
1252 return 0;
1253}
1254
1255static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001256 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001257{
1258 struct em28xx_fh *fh = priv;
1259 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001260 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001261 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001262
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001263 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001264 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001265 mutex_lock(&dev->lock);
1266 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1267 mutex_unlock(&dev->lock);
1268
1269 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001270 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001271 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001272 return 0;
1273 case V4L2_CHIP_MATCH_I2C_ADDR:
1274 /* Not supported yet */
1275 return -EINVAL;
1276 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001277 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001278 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001279 }
1280
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001281 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001282 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001283
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001284 mutex_lock(&dev->lock);
1285 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1286 em28xx_reg_len(reg->reg));
1287 mutex_unlock(&dev->lock);
1288
1289 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001290}
1291#endif
1292
1293
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001294static int vidioc_cropcap(struct file *file, void *priv,
1295 struct v4l2_cropcap *cc)
1296{
1297 struct em28xx_fh *fh = priv;
1298 struct em28xx *dev = fh->dev;
1299
1300 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1301 return -EINVAL;
1302
1303 cc->bounds.left = 0;
1304 cc->bounds.top = 0;
1305 cc->bounds.width = dev->width;
1306 cc->bounds.height = dev->height;
1307 cc->defrect = cc->bounds;
1308 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1309 cc->pixelaspect.denominator = 59;
1310
1311 return 0;
1312}
1313
1314static int vidioc_streamon(struct file *file, void *priv,
1315 enum v4l2_buf_type type)
1316{
1317 struct em28xx_fh *fh = priv;
1318 struct em28xx *dev = fh->dev;
1319 int rc;
1320
1321 rc = check_dev(dev);
1322 if (rc < 0)
1323 return rc;
1324
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001325
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001326 mutex_lock(&dev->lock);
1327 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001328
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001329 if (likely(rc >= 0))
1330 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001331
1332 mutex_unlock(&dev->lock);
1333
1334 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001335}
1336
1337static int vidioc_streamoff(struct file *file, void *priv,
1338 enum v4l2_buf_type type)
1339{
1340 struct em28xx_fh *fh = priv;
1341 struct em28xx *dev = fh->dev;
1342 int rc;
1343
1344 rc = check_dev(dev);
1345 if (rc < 0)
1346 return rc;
1347
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001348 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1349 return -EINVAL;
1350 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001351 return -EINVAL;
1352
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001353 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001354
1355 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001356 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001357
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001358 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001359
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001360 return 0;
1361}
1362
1363static int vidioc_querycap(struct file *file, void *priv,
1364 struct v4l2_capability *cap)
1365{
1366 struct em28xx_fh *fh = priv;
1367 struct em28xx *dev = fh->dev;
1368
1369 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1370 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001371 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372
1373 cap->version = EM28XX_VERSION_CODE;
1374
1375 cap->capabilities =
1376 V4L2_CAP_SLICED_VBI_CAPTURE |
1377 V4L2_CAP_VIDEO_CAPTURE |
1378 V4L2_CAP_AUDIO |
1379 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1380
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001381 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 cap->capabilities |= V4L2_CAP_TUNER;
1383
1384 return 0;
1385}
1386
Hans Verkuil78b526a2008-05-28 12:16:41 -03001387static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001388 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389{
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001390 struct em28xx_fh *fh = priv;
1391 struct em28xx *dev = fh->dev;
1392
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001393 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001394 return -EINVAL;
1395
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001396 if (dev->board.is_27xx) {
1397 struct em28xx_fmt *fmt;
1398 if (f->index)
1399 return -EINVAL;
1400
1401 f->pixelformat = V4L2_PIX_FMT_RGB565;
1402 fmt = format_by_fourcc(f->pixelformat);
1403 strlcpy(f->description, fmt->name, sizeof(f->description));
1404
1405 return 0;
1406 }
1407
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001408 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1409 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001410
1411 return 0;
1412}
1413
1414/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001415static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001416 struct v4l2_format *f)
1417{
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1420 int rc;
1421
1422 rc = check_dev(dev);
1423 if (rc < 0)
1424 return rc;
1425
1426 mutex_lock(&dev->lock);
1427
1428 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001429 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001430
1431 if (f->fmt.sliced.service_set == 0)
1432 rc = -EINVAL;
1433
1434 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001435
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436 return rc;
1437}
1438
Hans Verkuil78b526a2008-05-28 12:16:41 -03001439static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001440 struct v4l2_format *f)
1441{
1442 struct em28xx_fh *fh = priv;
1443 struct em28xx *dev = fh->dev;
1444 int rc;
1445
1446 rc = check_dev(dev);
1447 if (rc < 0)
1448 return rc;
1449
1450 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001451 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001452 mutex_unlock(&dev->lock);
1453
1454 if (f->fmt.sliced.service_set == 0)
1455 return -EINVAL;
1456
1457 return 0;
1458}
1459
1460
1461static int vidioc_reqbufs(struct file *file, void *priv,
1462 struct v4l2_requestbuffers *rb)
1463{
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466 int rc;
1467
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
1471
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001472 return videobuf_reqbufs(&fh->vb_vidq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473}
1474
1475static int vidioc_querybuf(struct file *file, void *priv,
1476 struct v4l2_buffer *b)
1477{
1478 struct em28xx_fh *fh = priv;
1479 struct em28xx *dev = fh->dev;
1480 int rc;
1481
1482 rc = check_dev(dev);
1483 if (rc < 0)
1484 return rc;
1485
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001486 return videobuf_querybuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001487}
1488
1489static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1490{
1491 struct em28xx_fh *fh = priv;
1492 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001493 int rc;
1494
1495 rc = check_dev(dev);
1496 if (rc < 0)
1497 return rc;
1498
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001499 return videobuf_qbuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001500}
1501
1502static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1503{
1504 struct em28xx_fh *fh = priv;
1505 struct em28xx *dev = fh->dev;
1506 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001507
1508 rc = check_dev(dev);
1509 if (rc < 0)
1510 return rc;
1511
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001512 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001513}
1514
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001515#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001516static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001517{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001518 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001519
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001520 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001521}
1522#endif
1523
1524
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525/* ----------------------------------------------------------- */
1526/* RADIO ESPECIFIC IOCTLS */
1527/* ----------------------------------------------------------- */
1528
1529static int radio_querycap(struct file *file, void *priv,
1530 struct v4l2_capability *cap)
1531{
1532 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1533
1534 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1535 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001536 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001537
1538 cap->version = EM28XX_VERSION_CODE;
1539 cap->capabilities = V4L2_CAP_TUNER;
1540 return 0;
1541}
1542
1543static int radio_g_tuner(struct file *file, void *priv,
1544 struct v4l2_tuner *t)
1545{
1546 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1547
1548 if (unlikely(t->index > 0))
1549 return -EINVAL;
1550
1551 strcpy(t->name, "Radio");
1552 t->type = V4L2_TUNER_RADIO;
1553
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001554 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001555 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001556 mutex_unlock(&dev->lock);
1557
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001558 return 0;
1559}
1560
1561static int radio_enum_input(struct file *file, void *priv,
1562 struct v4l2_input *i)
1563{
1564 if (i->index != 0)
1565 return -EINVAL;
1566 strcpy(i->name, "Radio");
1567 i->type = V4L2_INPUT_TYPE_TUNER;
1568
1569 return 0;
1570}
1571
1572static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1573{
1574 if (unlikely(a->index))
1575 return -EINVAL;
1576
1577 strcpy(a->name, "Radio");
1578 return 0;
1579}
1580
1581static int radio_s_tuner(struct file *file, void *priv,
1582 struct v4l2_tuner *t)
1583{
1584 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1585
1586 if (0 != t->index)
1587 return -EINVAL;
1588
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001589 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001590 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001591 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001592
1593 return 0;
1594}
1595
1596static int radio_s_audio(struct file *file, void *fh,
1597 struct v4l2_audio *a)
1598{
1599 return 0;
1600}
1601
1602static int radio_s_input(struct file *file, void *fh, unsigned int i)
1603{
1604 return 0;
1605}
1606
1607static int radio_queryctrl(struct file *file, void *priv,
1608 struct v4l2_queryctrl *qc)
1609{
1610 int i;
1611
1612 if (qc->id < V4L2_CID_BASE ||
1613 qc->id >= V4L2_CID_LASTP1)
1614 return -EINVAL;
1615
1616 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1617 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1618 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1619 return 0;
1620 }
1621 }
1622
1623 return -EINVAL;
1624}
1625
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001626/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001627 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001628 * inits the device and starts isoc transfer
1629 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001630static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001631{
Hans Verkuilbec43662008-12-30 06:58:20 -03001632 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001633 int errCode = 0, radio;
1634 struct em28xx *dev;
1635 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001636 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001637
Hans Verkuilbec43662008-12-30 06:58:20 -03001638 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001639
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001640 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001641 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001642
1643 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001644
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001645 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001646 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001647
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001648
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001649 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001650 if (!fh) {
1651 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001652 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001653 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001654 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001655 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001656 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001657 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001658 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001659
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001660 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001661 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001662 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001663 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001665 /* Needed, since GPIO might have disabled power of
1666 some i2c device
1667 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001668 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001669
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001670 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001671 if (fh->radio) {
1672 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001673 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001674 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001675
1676 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001678 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1679 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1680 sizeof(struct em28xx_buffer), fh);
1681
Ingo Molnar3593cab2006-02-07 06:49:14 -02001682 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001683
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001684 return errCode;
1685}
1686
1687/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001688 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001689 * unregisters the v4l2,i2c and usb devices
1690 * called when the device gets disconected or at module unload
1691*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001692void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001694
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001695 /*FIXME: I2C IR should be disconnected */
1696
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001697 if (dev->radio_dev) {
1698 if (-1 != dev->radio_dev->minor)
1699 video_unregister_device(dev->radio_dev);
1700 else
1701 video_device_release(dev->radio_dev);
1702 dev->radio_dev = NULL;
1703 }
1704 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001705 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1706 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001707 if (-1 != dev->vbi_dev->minor)
1708 video_unregister_device(dev->vbi_dev);
1709 else
1710 video_device_release(dev->vbi_dev);
1711 dev->vbi_dev = NULL;
1712 }
1713 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001714 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1715 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001716 if (-1 != dev->vdev->minor)
1717 video_unregister_device(dev->vdev);
1718 else
1719 video_device_release(dev->vdev);
1720 dev->vdev = NULL;
1721 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722}
1723
1724/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001726 * stops streaming and deallocates all resources allocated by the v4l2
1727 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001729static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001731 struct em28xx_fh *fh = filp->private_data;
1732 struct em28xx *dev = fh->dev;
1733 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001735 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001737
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001738 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001739 if (res_check(fh))
1740 res_free(fh);
1741
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001742 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001743 videobuf_stop(&fh->vb_vidq);
1744 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001746 /* the device is already disconnect,
1747 free the remaining resources */
1748 if (dev->state & DEV_DISCONNECTED) {
1749 em28xx_release_resources(dev);
1750 mutex_unlock(&dev->lock);
1751 kfree(dev);
1752 return 0;
1753 }
1754
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001755 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03001756 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001757
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001758 /* do this before setting alternate! */
1759 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001760 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001761
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001762 /* set alternate 0 */
1763 dev->alt = 0;
1764 em28xx_videodbg("setting alternate 0\n");
1765 errCode = usb_set_interface(dev->udev, 0, 0);
1766 if (errCode < 0) {
1767 em28xx_errdev("cannot change alternate number to "
1768 "0 (error=%i)\n", errCode);
1769 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001771 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772 dev->users--;
1773 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001774 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001775 return 0;
1776}
1777
1778/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001779 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001780 * will allocate buffers when called for the first time
1781 */
1782static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001783em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001784 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001786 struct em28xx_fh *fh = filp->private_data;
1787 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001793
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001794 /* FIXME: read() is not prepared to allow changing the video
1795 resolution while streaming. Seems a bug at em28xx_set_fmt
1796 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001797
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001798 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001799 mutex_lock(&dev->lock);
1800 rc = res_get(fh);
1801 mutex_unlock(&dev->lock);
1802
1803 if (unlikely(rc < 0))
1804 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001805
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001806 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1807 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001808 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001809 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810}
1811
1812/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001813 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001814 * will allocate buffers when called for the first time
1815 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001816static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001817{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001818 struct em28xx_fh *fh = filp->private_data;
1819 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001820 int rc;
1821
1822 rc = check_dev(dev);
1823 if (rc < 0)
1824 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001826 mutex_lock(&dev->lock);
1827 rc = res_get(fh);
1828 mutex_unlock(&dev->lock);
1829
1830 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001831 return POLLERR;
1832
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001833 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1834 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001836 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001837}
1838
1839/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001840 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001841 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001842static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001843{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001844 struct em28xx_fh *fh = filp->private_data;
1845 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001846 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001847
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001848 rc = check_dev(dev);
1849 if (rc < 0)
1850 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001851
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001852 mutex_lock(&dev->lock);
1853 rc = res_get(fh);
1854 mutex_unlock(&dev->lock);
1855
1856 if (unlikely(rc < 0))
1857 return rc;
1858
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001859 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001861 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1862 (unsigned long)vma->vm_start,
1863 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1864 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001865
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001866 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001867}
1868
Hans Verkuilbec43662008-12-30 06:58:20 -03001869static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001870 .owner = THIS_MODULE,
1871 .open = em28xx_v4l2_open,
1872 .release = em28xx_v4l2_close,
1873 .read = em28xx_v4l2_read,
1874 .poll = em28xx_v4l2_poll,
1875 .mmap = em28xx_v4l2_mmap,
1876 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001877};
1878
Hans Verkuila3998102008-07-21 02:57:38 -03001879static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001880 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001881 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1882 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1883 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1884 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001885 .vidioc_g_audio = vidioc_g_audio,
1886 .vidioc_s_audio = vidioc_s_audio,
1887 .vidioc_cropcap = vidioc_cropcap,
1888
Hans Verkuil78b526a2008-05-28 12:16:41 -03001889 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1890 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1891 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001892
1893 .vidioc_reqbufs = vidioc_reqbufs,
1894 .vidioc_querybuf = vidioc_querybuf,
1895 .vidioc_qbuf = vidioc_qbuf,
1896 .vidioc_dqbuf = vidioc_dqbuf,
1897 .vidioc_s_std = vidioc_s_std,
1898 .vidioc_enum_input = vidioc_enum_input,
1899 .vidioc_g_input = vidioc_g_input,
1900 .vidioc_s_input = vidioc_s_input,
1901 .vidioc_queryctrl = vidioc_queryctrl,
1902 .vidioc_g_ctrl = vidioc_g_ctrl,
1903 .vidioc_s_ctrl = vidioc_s_ctrl,
1904 .vidioc_streamon = vidioc_streamon,
1905 .vidioc_streamoff = vidioc_streamoff,
1906 .vidioc_g_tuner = vidioc_g_tuner,
1907 .vidioc_s_tuner = vidioc_s_tuner,
1908 .vidioc_g_frequency = vidioc_g_frequency,
1909 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001910#ifdef CONFIG_VIDEO_ADV_DEBUG
1911 .vidioc_g_register = vidioc_g_register,
1912 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001913 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001914#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001915#ifdef CONFIG_VIDEO_V4L1_COMPAT
1916 .vidiocgmbuf = vidiocgmbuf,
1917#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001918};
1919
1920static const struct video_device em28xx_video_template = {
1921 .fops = &em28xx_v4l_fops,
1922 .release = video_device_release,
1923 .ioctl_ops = &video_ioctl_ops,
1924
1925 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001926
1927 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001928 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001929};
1930
Hans Verkuilbec43662008-12-30 06:58:20 -03001931static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001932 .owner = THIS_MODULE,
1933 .open = em28xx_v4l2_open,
1934 .release = em28xx_v4l2_close,
1935 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001936};
1937
1938static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001939 .vidioc_querycap = radio_querycap,
1940 .vidioc_g_tuner = radio_g_tuner,
1941 .vidioc_enum_input = radio_enum_input,
1942 .vidioc_g_audio = radio_g_audio,
1943 .vidioc_s_tuner = radio_s_tuner,
1944 .vidioc_s_audio = radio_s_audio,
1945 .vidioc_s_input = radio_s_input,
1946 .vidioc_queryctrl = radio_queryctrl,
1947 .vidioc_g_ctrl = vidioc_g_ctrl,
1948 .vidioc_s_ctrl = vidioc_s_ctrl,
1949 .vidioc_g_frequency = vidioc_g_frequency,
1950 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001951#ifdef CONFIG_VIDEO_ADV_DEBUG
1952 .vidioc_g_register = vidioc_g_register,
1953 .vidioc_s_register = vidioc_s_register,
1954#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001955};
1956
Hans Verkuila3998102008-07-21 02:57:38 -03001957static struct video_device em28xx_radio_template = {
1958 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001959 .fops = &radio_fops,
1960 .ioctl_ops = &radio_ioctl_ops,
1961 .minor = -1,
1962};
1963
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001964/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001965
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001966
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001967
Adrian Bunk532fe652008-01-28 22:10:48 -03001968static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001969 const struct video_device *template,
1970 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001971{
1972 struct video_device *vfd;
1973
1974 vfd = video_device_alloc();
1975 if (NULL == vfd)
1976 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001977
1978 *vfd = *template;
1979 vfd->minor = -1;
1980 vfd->v4l2_dev = &dev->v4l2_dev;
1981 vfd->release = video_device_release;
1982 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001983
1984 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1985 dev->name, type_name);
1986
1987 return vfd;
1988}
1989
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001990int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001991{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03001992 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001993 int ret;
1994
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001995 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1996 dev->name,
1997 (EM28XX_VERSION_CODE >> 16) & 0xff,
1998 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1999
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002000 /* set default norm */
2001 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002002 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002003 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002004
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002005 /* Analog specific initialization */
2006 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002007 em28xx_set_video_format(dev, format[0].fourcc,
2008 norm_maxw(dev), norm_maxh(dev));
2009
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002010 video_mux(dev, dev->ctl_input);
2011
2012 /* Audio defaults */
2013 dev->mute = 1;
2014 dev->volume = 0x1f;
2015
2016 /* enable vbi capturing */
2017
2018/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002019 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2020 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2021 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002022 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2023
2024 em28xx_set_outfmt(dev);
2025 em28xx_colorlevels_set_default(dev);
2026 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002027
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002028 /* allocate and fill video video_device struct */
2029 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2030 if (!dev->vdev) {
2031 em28xx_errdev("cannot allocate video_device.\n");
2032 return -ENODEV;
2033 }
2034
2035 /* register v4l2 video video_device */
2036 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2037 video_nr[dev->devno]);
2038 if (ret) {
2039 em28xx_errdev("unable to register video device (error=%i).\n",
2040 ret);
2041 return ret;
2042 }
2043
2044 /* Allocate and fill vbi video_device struct */
2045 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2046
2047 /* register v4l2 vbi video_device */
2048 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2049 vbi_nr[dev->devno]);
2050 if (ret < 0) {
2051 em28xx_errdev("unable to register vbi device\n");
2052 return ret;
2053 }
2054
2055 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002056 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2057 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002058 if (!dev->radio_dev) {
2059 em28xx_errdev("cannot allocate video_device.\n");
2060 return -ENODEV;
2061 }
2062 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2063 radio_nr[dev->devno]);
2064 if (ret < 0) {
2065 em28xx_errdev("can't register radio device\n");
2066 return ret;
2067 }
2068 em28xx_info("Registered radio device as /dev/radio%d\n",
2069 dev->radio_dev->num);
2070 }
2071
2072 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2073 dev->vdev->num, dev->vbi_dev->num);
2074
2075 return 0;
2076}