blob: 575472f1e7029ffcce9797354d8fb1c8ae797e63 [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"
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 1)
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 Chehabbddcf632008-12-20 09:06:37 -030097 },
98};
99
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800100/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200101/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800102static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800103 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200104 .id = V4L2_CID_AUDIO_VOLUME,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Volume",
107 .minimum = 0x0,
108 .maximum = 0x1f,
109 .step = 0x1,
110 .default_value = 0x1f,
111 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300112 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200113 .id = V4L2_CID_AUDIO_MUTE,
114 .type = V4L2_CTRL_TYPE_BOOLEAN,
115 .name = "Mute",
116 .minimum = 0,
117 .maximum = 1,
118 .step = 1,
119 .default_value = 1,
120 .flags = 0,
121 }
122};
123
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300124/* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
127
128/*
129 * Announces that a buffer were filled and request the next
130 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300131static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300132 struct em28xx_dmaqueue *dma_q,
133 struct em28xx_buffer *buf)
134{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300135 /* Advice that buffer was filled */
136 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137 buf->vb.state = VIDEOBUF_DONE;
138 buf->vb.field_count++;
139 do_gettimeofday(&buf->vb.ts);
140
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300141 dev->isoc_ctl.buf = NULL;
142
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
145}
146
147/*
148 * Identify the buffer header type and properly handles
149 */
150static void em28xx_copy_video(struct em28xx *dev,
151 struct em28xx_dmaqueue *dma_q,
152 struct em28xx_buffer *buf,
153 unsigned char *p,
154 unsigned char *outp, unsigned long len)
155{
156 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300158 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159
160 if (dma_q->pos + len > buf->vb.size)
161 len = buf->vb.size - dma_q->pos;
162
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300163 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300164 em28xx_isocdbg("frame is not complete\n");
165 len += 4;
166 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300167 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
169 startread = p;
170 remain = len;
171
172 /* Interlaces frame */
173 if (buf->top_field)
174 fieldstart = outp;
175 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300176 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300177
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300178 linesdone = dma_q->pos / bytesperline;
179 currlinedone = dma_q->pos % bytesperline;
180 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300181 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300182 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300183 lencopy = lencopy > remain ? remain : lencopy;
184
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300185 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300187 ((char *)startwrite + lencopy) -
188 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300189 remain = (char *)outp + buf->vb.size - (char *)startwrite;
190 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300191 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300192 if (lencopy <= 0)
193 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300194 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300195
196 remain -= lencopy;
197
198 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300199 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 lencopy = remain;
203 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300204 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300206 if ((char *)startwrite + lencopy > (char *)outp +
207 buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300208 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300209 ((char *)startwrite + lencopy) -
210 ((char *)outp + buf->vb.size));
211 lencopy = remain = (char *)outp + buf->vb.size -
212 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300213 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300214 if (lencopy <= 0)
215 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300216
217 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300218
219 remain -= lencopy;
220 }
221
222 dma_q->pos += len;
223}
224
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300225static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300226 int packet, int status)
227{
228 char *errmsg = "Unknown";
229
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300230 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300231 case -ENOENT:
232 errmsg = "unlinked synchronuously";
233 break;
234 case -ECONNRESET:
235 errmsg = "unlinked asynchronuously";
236 break;
237 case -ENOSR:
238 errmsg = "Buffer error (overrun)";
239 break;
240 case -EPIPE:
241 errmsg = "Stalled (device not responding)";
242 break;
243 case -EOVERFLOW:
244 errmsg = "Babble (bad cable?)";
245 break;
246 case -EPROTO:
247 errmsg = "Bit-stuff error (bad cable?)";
248 break;
249 case -EILSEQ:
250 errmsg = "CRC/Timeout (could be anything)";
251 break;
252 case -ETIME:
253 errmsg = "Device does not respond";
254 break;
255 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300256 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300257 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
258 } else {
259 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
260 packet, status, errmsg);
261 }
262}
263
264/*
265 * video-buf generic routine to get the next available buffer
266 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300267static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300268 struct em28xx_buffer **buf)
269{
270 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300271 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300272
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300273 if (list_empty(&dma_q->active)) {
274 em28xx_isocdbg("No active queue to serve\n");
275 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300277 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300278 }
279
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300280 /* Get the next buffer */
281 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
282
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300283 /* Cleans up buffer - Usefull for testing for frame/URB loss */
284 outp = videobuf_to_vmalloc(&(*buf)->vb);
285 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300286
287 dev->isoc_ctl.buf = *buf;
288
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300289 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300290}
291
292/*
293 * Controls the isoc copy of each urb packet
294 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300295static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300296{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300297 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300298 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300299 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300301 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300302
303 if (!dev)
304 return 0;
305
306 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
307 return 0;
308
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300309 if (urb->status < 0) {
310 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300311 if (urb->status == -ENOENT)
312 return 0;
313 }
314
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300315 buf = dev->isoc_ctl.buf;
316 if (buf != NULL)
317 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300318
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300319 for (i = 0; i < urb->number_of_packets; i++) {
320 int status = urb->iso_frame_desc[i].status;
321
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322 if (status < 0) {
323 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300324 if (urb->iso_frame_desc[i].status != -EPROTO)
325 continue;
326 }
327
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300328 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329
330 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300331 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300332 continue;
333 }
334 if (urb->iso_frame_desc[i].actual_length >
335 dev->max_pkt_size) {
336 em28xx_isocdbg("packet bigger than packet size");
337 continue;
338 }
339
340 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341
342 /* FIXME: incomplete buffer checks where removed to make
343 logic simpler. Impacts of those changes should be evaluated
344 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300345 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
346 em28xx_isocdbg("VBI HEADER!!!\n");
347 /* FIXME: Should add vbi copy */
348 continue;
349 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300350 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300351 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300352 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300353
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300354 if (!(p[2] & 1)) {
355 if (buf != NULL)
356 buffer_filled(dev, dma_q, buf);
357 get_next_buf(dma_q, &buf);
358 if (buf == NULL)
359 outp = NULL;
360 else
361 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300362 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300363
364 if (buf != NULL) {
365 if (p[2] & 1)
366 buf->top_field = 0;
367 else
368 buf->top_field = 1;
369 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300370
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300372 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300373 if (buf != NULL)
374 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300375 }
376 return rc;
377}
378
379/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300380 Videobuf operations
381 ------------------------------------------------------------------*/
382
383static int
384buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
385{
386 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300387 struct em28xx *dev = fh->dev;
388 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300390 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
391
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300392 if (0 == *count)
393 *count = EM28XX_DEF_BUF;
394
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300395 if (*count < EM28XX_MIN_BUF)
396 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300397
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300398 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300399 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300400 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300401 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300402
403 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
404
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405 return 0;
406}
407
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300408/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300409static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
410{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300411 struct em28xx_fh *fh = vq->priv_data;
412 struct em28xx *dev = fh->dev;
413 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414 if (in_interrupt())
415 BUG();
416
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300417 /* We used to wait for the buffer to finish here, but this didn't work
418 because, as we were keeping the state as VIDEOBUF_QUEUED,
419 videobuf_queue_cancel marked it as finished for us.
420 (Also, it could wedge forever if the hardware was misconfigured.)
421
422 This should be safe; by the time we get here, the buffer isn't
423 queued anymore. If we ever start marking the buffers as
424 VIDEOBUF_ACTIVE, it won't be, though.
425 */
426 spin_lock_irqsave(&dev->slock, flags);
427 if (dev->isoc_ctl.buf == buf)
428 dev->isoc_ctl.buf = NULL;
429 spin_unlock_irqrestore(&dev->slock, flags);
430
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300431 videobuf_vmalloc_free(&buf->vb);
432 buf->vb.state = VIDEOBUF_NEEDS_INIT;
433}
434
435static int
436buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
437 enum v4l2_field field)
438{
439 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300440 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 struct em28xx *dev = fh->dev;
442 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300444 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300445
446 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
447 return -EINVAL;
448
Brandon Philips05612972008-04-13 14:57:01 -0300449 buf->vb.width = dev->width;
450 buf->vb.height = dev->height;
451 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452
453 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454 rc = videobuf_iolock(vq, &buf->vb, NULL);
455 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 }
458
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300460 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461
462 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300463 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
464 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300465 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300466 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300467 goto fail;
468 }
469
470 buf->vb.state = VIDEOBUF_PREPARED;
471 return 0;
472
473fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300474 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300475 return rc;
476}
477
478static void
479buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
480{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300481 struct em28xx_buffer *buf = container_of(vb,
482 struct em28xx_buffer,
483 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300485 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300486 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300487
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300488 buf->vb.state = VIDEOBUF_QUEUED;
489 list_add_tail(&buf->vb.queue, &vidq->active);
490
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491}
492
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300493static void buffer_release(struct videobuf_queue *vq,
494 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300496 struct em28xx_buffer *buf = container_of(vb,
497 struct em28xx_buffer,
498 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300499 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300500 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300501
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300502 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300504 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300505}
506
507static struct videobuf_queue_ops em28xx_video_qops = {
508 .buf_setup = buffer_setup,
509 .buf_prepare = buffer_prepare,
510 .buf_queue = buffer_queue,
511 .buf_release = buffer_release,
512};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800513
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300514/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800515
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800516static void video_mux(struct em28xx *dev, int index)
517{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300518 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800519
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300520 route.input = INPUT(index)->vmux;
521 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800522 dev->ctl_input = index;
523 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300524 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800525
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300526 if (!dev->ctl_aoutput)
527 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
528
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300529 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800530
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300531 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300532 if (dev->i2s_speed) {
533 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
534 &dev->i2s_speed);
535 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300536 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300537 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300538 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300539 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
540 &route);
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) {
544 route.input = dev->ctl_ainput;
545 route.output = dev->ctl_aoutput;
546 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
547 &route);
548 }
549
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300550 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800551}
552
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300553/* Usage lock check functions */
554static int res_get(struct em28xx_fh *fh)
555{
556 struct em28xx *dev = fh->dev;
557 int rc = 0;
558
559 /* This instance already has stream_on */
560 if (fh->stream_on)
561 return rc;
562
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300563 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300564 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300565
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300566 dev->stream_on = 1;
567 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300568 return rc;
569}
570
571static int res_check(struct em28xx_fh *fh)
572{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300573 return fh->stream_on;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300574}
575
576static void res_free(struct em28xx_fh *fh)
577{
578 struct em28xx *dev = fh->dev;
579
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300580 fh->stream_on = 0;
581 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300582}
583
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800584/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300585 * em28xx_get_ctrl()
586 * return the current saturation, brightness or contrast, mute state
587 */
588static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
589{
590 switch (ctrl->id) {
591 case V4L2_CID_AUDIO_MUTE:
592 ctrl->value = dev->mute;
593 return 0;
594 case V4L2_CID_AUDIO_VOLUME:
595 ctrl->value = dev->volume;
596 return 0;
597 default:
598 return -EINVAL;
599 }
600}
601
602/*
603 * em28xx_set_ctrl()
604 * mute or set new saturation, brightness or contrast
605 */
606static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
607{
608 switch (ctrl->id) {
609 case V4L2_CID_AUDIO_MUTE:
610 if (ctrl->value != dev->mute) {
611 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300612 return em28xx_audio_analog_set(dev);
613 }
614 return 0;
615 case V4L2_CID_AUDIO_VOLUME:
616 dev->volume = ctrl->value;
617 return em28xx_audio_analog_set(dev);
618 default:
619 return -EINVAL;
620 }
621}
622
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300623static int check_dev(struct em28xx *dev)
624{
625 if (dev->state & DEV_DISCONNECTED) {
626 em28xx_errdev("v4l2 ioctl: device not present\n");
627 return -ENODEV;
628 }
629
630 if (dev->state & DEV_MISCONFIGURED) {
631 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
632 "close and open it again\n");
633 return -EIO;
634 }
635 return 0;
636}
637
638static void get_scale(struct em28xx *dev,
639 unsigned int width, unsigned int height,
640 unsigned int *hscale, unsigned int *vscale)
641{
642 unsigned int maxw = norm_maxw(dev);
643 unsigned int maxh = norm_maxh(dev);
644
645 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
646 if (*hscale >= 0x4000)
647 *hscale = 0x3fff;
648
649 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
650 if (*vscale >= 0x4000)
651 *vscale = 0x3fff;
652}
653
654/* ------------------------------------------------------------------
655 IOCTL vidioc handling
656 ------------------------------------------------------------------*/
657
Hans Verkuil78b526a2008-05-28 12:16:41 -0300658static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300659 struct v4l2_format *f)
660{
661 struct em28xx_fh *fh = priv;
662 struct em28xx *dev = fh->dev;
663
664 mutex_lock(&dev->lock);
665
666 f->fmt.pix.width = dev->width;
667 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300668 f->fmt.pix.pixelformat = dev->format->fourcc;
669 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300670 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300671 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
672
673 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
674 f->fmt.pix.field = dev->interlaced ?
675 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
676
677 mutex_unlock(&dev->lock);
678 return 0;
679}
680
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300681static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
682{
683 unsigned int i;
684
685 for (i = 0; i < ARRAY_SIZE(format); i++)
686 if (format[i].fourcc == fourcc)
687 return &format[i];
688
689 return NULL;
690}
691
Hans Verkuil78b526a2008-05-28 12:16:41 -0300692static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300693 struct v4l2_format *f)
694{
695 struct em28xx_fh *fh = priv;
696 struct em28xx *dev = fh->dev;
697 int width = f->fmt.pix.width;
698 int height = f->fmt.pix.height;
699 unsigned int maxw = norm_maxw(dev);
700 unsigned int maxh = norm_maxh(dev);
701 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300702 struct em28xx_fmt *fmt;
703
704 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
705 if (!fmt) {
706 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
707 f->fmt.pix.pixelformat);
708 return -EINVAL;
709 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710
711 /* width must even because of the YUYV format
712 height must be even because of interlacing */
713 height &= 0xfffe;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300714 width &= 0xfffe;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300715
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300716 if (unlikely(height < 32))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300717 height = 32;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300718 if (unlikely(height > maxh))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300719 height = maxh;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300720 if (unlikely(width < 48))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300721 width = 48;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300722 if (unlikely(width > maxw))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300723 width = maxw;
724
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300725 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300726 /* the em2800 can only scale down to 50% */
727 if (height % (maxh / 2))
728 height = maxh;
729 if (width % (maxw / 2))
730 width = maxw;
731 /* according to empiatech support */
732 /* the MaxPacketSize is to small to support */
733 /* framesizes larger than 640x480 @ 30 fps */
734 /* or 640x576 @ 25 fps. As this would cut */
735 /* of a part of the image we prefer */
736 /* 360x576 or 360x480 for now */
737 if (width == maxw && height == maxh)
738 width /= 2;
739 }
740
741 get_scale(dev, width, height, &hscale, &vscale);
742
743 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
744 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
745
746 f->fmt.pix.width = width;
747 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300748 f->fmt.pix.pixelformat = fmt->fourcc;
749 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
750 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300751 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
752 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
753
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300754 return 0;
755}
756
Hans Verkuil78b526a2008-05-28 12:16:41 -0300757static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300758 struct v4l2_format *f)
759{
760 struct em28xx_fh *fh = priv;
761 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300762 int rc;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300763 struct em28xx_fmt *fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300764
765 rc = check_dev(dev);
766 if (rc < 0)
767 return rc;
768
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300769 mutex_lock(&dev->lock);
770
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300771 vidioc_try_fmt_vid_cap(file, priv, f);
772
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300773 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -0300774 if (!fmt) {
775 rc = -EINVAL;
776 goto out;
777 }
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300778
Brandon Philips05612972008-04-13 14:57:01 -0300779 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
780 em28xx_errdev("%s queue busy\n", __func__);
781 rc = -EBUSY;
782 goto out;
783 }
784
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300785 if (dev->stream_on && !fh->stream_on) {
786 em28xx_errdev("%s device in use by another fh\n", __func__);
787 rc = -EBUSY;
788 goto out;
789 }
790
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300791 /* set new image size */
792 dev->width = f->fmt.pix.width;
793 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300794 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
796
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300797 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300798 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799
Brandon Philips05612972008-04-13 14:57:01 -0300800 rc = 0;
801
802out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300804 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805}
806
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300807static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808{
809 struct em28xx_fh *fh = priv;
810 struct em28xx *dev = fh->dev;
811 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812 int rc;
813
814 rc = check_dev(dev);
815 if (rc < 0)
816 return rc;
817
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300819 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300820
821 /* Adjusts width/height, if needed */
822 f.fmt.pix.width = dev->width;
823 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300824 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826 /* set new image size */
827 dev->width = f.fmt.pix.width;
828 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300829 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
830
831 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300832 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300833
834 mutex_unlock(&dev->lock);
835 return 0;
836}
837
838static const char *iname[] = {
839 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
840 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
841 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
842 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
843 [EM28XX_VMUX_SVIDEO] = "S-Video",
844 [EM28XX_VMUX_TELEVISION] = "Television",
845 [EM28XX_VMUX_CABLE] = "Cable TV",
846 [EM28XX_VMUX_DVB] = "DVB",
847 [EM28XX_VMUX_DEBUG] = "for debug only",
848};
849
850static int vidioc_enum_input(struct file *file, void *priv,
851 struct v4l2_input *i)
852{
853 struct em28xx_fh *fh = priv;
854 struct em28xx *dev = fh->dev;
855 unsigned int n;
856
857 n = i->index;
858 if (n >= MAX_EM28XX_INPUT)
859 return -EINVAL;
860 if (0 == INPUT(n)->type)
861 return -EINVAL;
862
863 i->index = n;
864 i->type = V4L2_INPUT_TYPE_CAMERA;
865
866 strcpy(i->name, iname[INPUT(n)->type]);
867
868 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
869 (EM28XX_VMUX_CABLE == INPUT(n)->type))
870 i->type = V4L2_INPUT_TYPE_TUNER;
871
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300872 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300873
874 return 0;
875}
876
877static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
878{
879 struct em28xx_fh *fh = priv;
880 struct em28xx *dev = fh->dev;
881
882 *i = dev->ctl_input;
883
884 return 0;
885}
886
887static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
888{
889 struct em28xx_fh *fh = priv;
890 struct em28xx *dev = fh->dev;
891 int rc;
892
893 rc = check_dev(dev);
894 if (rc < 0)
895 return rc;
896
897 if (i >= MAX_EM28XX_INPUT)
898 return -EINVAL;
899 if (0 == INPUT(i)->type)
900 return -EINVAL;
901
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300902 dev->ctl_input = i;
903
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300904 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300905 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300906 mutex_unlock(&dev->lock);
907 return 0;
908}
909
910static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
911{
912 struct em28xx_fh *fh = priv;
913 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300914
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300915 switch (a->index) {
916 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300917 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300918 break;
919 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300920 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300921 break;
922 case EM28XX_AMUX_VIDEO2:
923 strcpy(a->name, "Television alt");
924 break;
925 case EM28XX_AMUX_PHONE:
926 strcpy(a->name, "Phone");
927 break;
928 case EM28XX_AMUX_MIC:
929 strcpy(a->name, "Mic");
930 break;
931 case EM28XX_AMUX_CD:
932 strcpy(a->name, "CD");
933 break;
934 case EM28XX_AMUX_AUX:
935 strcpy(a->name, "Aux");
936 break;
937 case EM28XX_AMUX_PCM_OUT:
938 strcpy(a->name, "PCM");
939 break;
940 default:
941 return -EINVAL;
942 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300943
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300944 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300945 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300946
947 return 0;
948}
949
950static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
951{
952 struct em28xx_fh *fh = priv;
953 struct em28xx *dev = fh->dev;
954
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300955
956 if (a->index >= MAX_EM28XX_INPUT)
957 return -EINVAL;
958 if (0 == INPUT(a->index)->type)
959 return -EINVAL;
960
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300961 mutex_lock(&dev->lock);
962
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300963 dev->ctl_ainput = INPUT(a->index)->amux;
964 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300965
966 if (!dev->ctl_aoutput)
967 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300968
969 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300970 return 0;
971}
972
973static int vidioc_queryctrl(struct file *file, void *priv,
974 struct v4l2_queryctrl *qc)
975{
976 struct em28xx_fh *fh = priv;
977 struct em28xx *dev = fh->dev;
978 int id = qc->id;
979 int i;
980 int rc;
981
982 rc = check_dev(dev);
983 if (rc < 0)
984 return rc;
985
986 memset(qc, 0, sizeof(*qc));
987
988 qc->id = id;
989
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300990 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
992 if (qc->id && qc->id == em28xx_qctrl[i].id) {
993 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
994 return 0;
995 }
996 }
997 }
998 mutex_lock(&dev->lock);
999 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1000 mutex_unlock(&dev->lock);
1001
1002 if (qc->type)
1003 return 0;
1004 else
1005 return -EINVAL;
1006}
1007
1008static int vidioc_g_ctrl(struct file *file, void *priv,
1009 struct v4l2_control *ctrl)
1010{
1011 struct em28xx_fh *fh = priv;
1012 struct em28xx *dev = fh->dev;
1013 int rc;
1014
1015 rc = check_dev(dev);
1016 if (rc < 0)
1017 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001018 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001019
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001020 mutex_lock(&dev->lock);
1021
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001022 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001024 else {
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001025 rc = em28xx_get_ctrl(dev, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001026 if (rc < 0) {
1027 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1028 rc = 0;
1029 }
1030 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001031
1032 mutex_unlock(&dev->lock);
1033 return rc;
1034}
1035
1036static int vidioc_s_ctrl(struct file *file, void *priv,
1037 struct v4l2_control *ctrl)
1038{
1039 struct em28xx_fh *fh = priv;
1040 struct em28xx *dev = fh->dev;
1041 u8 i;
1042 int rc;
1043
1044 rc = check_dev(dev);
1045 if (rc < 0)
1046 return rc;
1047
1048 mutex_lock(&dev->lock);
1049
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001050 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001051 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1052 else {
1053 rc = 1;
1054 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1055 if (ctrl->id == em28xx_qctrl[i].id) {
1056 if (ctrl->value < em28xx_qctrl[i].minimum ||
1057 ctrl->value > em28xx_qctrl[i].maximum) {
1058 rc = -ERANGE;
1059 break;
1060 }
1061
1062 rc = em28xx_set_ctrl(dev, ctrl);
1063 break;
1064 }
1065 }
1066 }
1067
1068 /* Control not found - try to send it to the attached devices */
1069 if (rc == 1) {
1070 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1071 rc = 0;
1072 }
1073
1074 mutex_unlock(&dev->lock);
1075 return rc;
1076}
1077
1078static int vidioc_g_tuner(struct file *file, void *priv,
1079 struct v4l2_tuner *t)
1080{
1081 struct em28xx_fh *fh = priv;
1082 struct em28xx *dev = fh->dev;
1083 int rc;
1084
1085 rc = check_dev(dev);
1086 if (rc < 0)
1087 return rc;
1088
1089 if (0 != t->index)
1090 return -EINVAL;
1091
1092 strcpy(t->name, "Tuner");
1093
1094 mutex_lock(&dev->lock);
1095
1096 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1097
1098 mutex_unlock(&dev->lock);
1099 return 0;
1100}
1101
1102static int vidioc_s_tuner(struct file *file, void *priv,
1103 struct v4l2_tuner *t)
1104{
1105 struct em28xx_fh *fh = priv;
1106 struct em28xx *dev = fh->dev;
1107 int rc;
1108
1109 rc = check_dev(dev);
1110 if (rc < 0)
1111 return rc;
1112
1113 if (0 != t->index)
1114 return -EINVAL;
1115
1116 mutex_lock(&dev->lock);
1117
1118 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1119
1120 mutex_unlock(&dev->lock);
1121 return 0;
1122}
1123
1124static int vidioc_g_frequency(struct file *file, void *priv,
1125 struct v4l2_frequency *f)
1126{
1127 struct em28xx_fh *fh = priv;
1128 struct em28xx *dev = fh->dev;
1129
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001130 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001131 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001132 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001133 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134
1135 return 0;
1136}
1137
1138static int vidioc_s_frequency(struct file *file, void *priv,
1139 struct v4l2_frequency *f)
1140{
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1143 int rc;
1144
1145 rc = check_dev(dev);
1146 if (rc < 0)
1147 return rc;
1148
1149 if (0 != f->tuner)
1150 return -EINVAL;
1151
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001152 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1153 return -EINVAL;
1154 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001155 return -EINVAL;
1156
1157 mutex_lock(&dev->lock);
1158
1159 dev->ctl_freq = f->frequency;
1160 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1161
1162 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001163
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001164 return 0;
1165}
1166
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001167#ifdef CONFIG_VIDEO_ADV_DEBUG
1168static int em28xx_reg_len(int reg)
1169{
1170 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001171 case EM28XX_R40_AC97LSB:
1172 case EM28XX_R30_HSCALELOW:
1173 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001174 return 2;
1175 default:
1176 return 1;
1177 }
1178}
1179
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001180static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001181 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001182{
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
1185
1186 chip->ident = V4L2_IDENT_NONE;
1187 chip->revision = 0;
1188
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001189 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001190
1191 return 0;
1192}
1193
1194
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001195static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001196 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001197{
1198 struct em28xx_fh *fh = priv;
1199 struct em28xx *dev = fh->dev;
1200 int ret;
1201
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001202 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001203 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001204 mutex_lock(&dev->lock);
1205 ret = em28xx_read_ac97(dev, reg->reg);
1206 mutex_unlock(&dev->lock);
1207 if (ret < 0)
1208 return ret;
1209
1210 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001211 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001212 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001213 case V4L2_CHIP_MATCH_I2C_DRIVER:
1214 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1215 return 0;
1216 case V4L2_CHIP_MATCH_I2C_ADDR:
1217 /* Not supported yet */
1218 return -EINVAL;
1219 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001220 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001221 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001222 }
1223
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001224 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001225 reg->size = em28xx_reg_len(reg->reg);
1226 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001227 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001228 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001229 mutex_unlock(&dev->lock);
1230
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001231 if (ret < 0)
1232 return ret;
1233
1234 reg->val = ret;
1235 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001236 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001237 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001238 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1239 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001240 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001241 if (ret < 0)
1242 return ret;
1243
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001244 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001245 }
1246
1247 return 0;
1248}
1249
1250static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001251 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001252{
1253 struct em28xx_fh *fh = priv;
1254 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001255 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001256 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001257
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001258 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001259 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001260 mutex_lock(&dev->lock);
1261 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1262 mutex_unlock(&dev->lock);
1263
1264 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001265 case V4L2_CHIP_MATCH_I2C_DRIVER:
1266 em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1267 return 0;
1268 case V4L2_CHIP_MATCH_I2C_ADDR:
1269 /* Not supported yet */
1270 return -EINVAL;
1271 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001272 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001273 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001274 }
1275
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001276 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001277 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001278
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001279 mutex_lock(&dev->lock);
1280 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1281 em28xx_reg_len(reg->reg));
1282 mutex_unlock(&dev->lock);
1283
1284 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001285}
1286#endif
1287
1288
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001289static int vidioc_cropcap(struct file *file, void *priv,
1290 struct v4l2_cropcap *cc)
1291{
1292 struct em28xx_fh *fh = priv;
1293 struct em28xx *dev = fh->dev;
1294
1295 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1296 return -EINVAL;
1297
1298 cc->bounds.left = 0;
1299 cc->bounds.top = 0;
1300 cc->bounds.width = dev->width;
1301 cc->bounds.height = dev->height;
1302 cc->defrect = cc->bounds;
1303 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1304 cc->pixelaspect.denominator = 59;
1305
1306 return 0;
1307}
1308
1309static int vidioc_streamon(struct file *file, void *priv,
1310 enum v4l2_buf_type type)
1311{
1312 struct em28xx_fh *fh = priv;
1313 struct em28xx *dev = fh->dev;
1314 int rc;
1315
1316 rc = check_dev(dev);
1317 if (rc < 0)
1318 return rc;
1319
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001320
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001321 mutex_lock(&dev->lock);
1322 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001323
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001324 if (likely(rc >= 0))
1325 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001326
1327 mutex_unlock(&dev->lock);
1328
1329 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330}
1331
1332static int vidioc_streamoff(struct file *file, void *priv,
1333 enum v4l2_buf_type type)
1334{
1335 struct em28xx_fh *fh = priv;
1336 struct em28xx *dev = fh->dev;
1337 int rc;
1338
1339 rc = check_dev(dev);
1340 if (rc < 0)
1341 return rc;
1342
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001343 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1344 return -EINVAL;
1345 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001346 return -EINVAL;
1347
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001348 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001349
1350 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001351 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001352
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001353 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355 return 0;
1356}
1357
1358static int vidioc_querycap(struct file *file, void *priv,
1359 struct v4l2_capability *cap)
1360{
1361 struct em28xx_fh *fh = priv;
1362 struct em28xx *dev = fh->dev;
1363
1364 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1365 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001366 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367
1368 cap->version = EM28XX_VERSION_CODE;
1369
1370 cap->capabilities =
1371 V4L2_CAP_SLICED_VBI_CAPTURE |
1372 V4L2_CAP_VIDEO_CAPTURE |
1373 V4L2_CAP_AUDIO |
1374 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1375
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001376 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377 cap->capabilities |= V4L2_CAP_TUNER;
1378
1379 return 0;
1380}
1381
Hans Verkuil78b526a2008-05-28 12:16:41 -03001382static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001383 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001384{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001385 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001386 return -EINVAL;
1387
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001388 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1389 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001390
1391 return 0;
1392}
1393
1394/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001395static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396 struct v4l2_format *f)
1397{
1398 struct em28xx_fh *fh = priv;
1399 struct em28xx *dev = fh->dev;
1400 int rc;
1401
1402 rc = check_dev(dev);
1403 if (rc < 0)
1404 return rc;
1405
1406 mutex_lock(&dev->lock);
1407
1408 f->fmt.sliced.service_set = 0;
1409
1410 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1411
1412 if (f->fmt.sliced.service_set == 0)
1413 rc = -EINVAL;
1414
1415 mutex_unlock(&dev->lock);
1416 return rc;
1417}
1418
Hans Verkuil78b526a2008-05-28 12:16:41 -03001419static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001420 struct v4l2_format *f)
1421{
1422 struct em28xx_fh *fh = priv;
1423 struct em28xx *dev = fh->dev;
1424 int rc;
1425
1426 rc = check_dev(dev);
1427 if (rc < 0)
1428 return rc;
1429
1430 mutex_lock(&dev->lock);
1431 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1432 mutex_unlock(&dev->lock);
1433
1434 if (f->fmt.sliced.service_set == 0)
1435 return -EINVAL;
1436
1437 return 0;
1438}
1439
1440
1441static int vidioc_reqbufs(struct file *file, void *priv,
1442 struct v4l2_requestbuffers *rb)
1443{
1444 struct em28xx_fh *fh = priv;
1445 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001446 int rc;
1447
1448 rc = check_dev(dev);
1449 if (rc < 0)
1450 return rc;
1451
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001452 return videobuf_reqbufs(&fh->vb_vidq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001453}
1454
1455static int vidioc_querybuf(struct file *file, void *priv,
1456 struct v4l2_buffer *b)
1457{
1458 struct em28xx_fh *fh = priv;
1459 struct em28xx *dev = fh->dev;
1460 int rc;
1461
1462 rc = check_dev(dev);
1463 if (rc < 0)
1464 return rc;
1465
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001466 return videobuf_querybuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001467}
1468
1469static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1470{
1471 struct em28xx_fh *fh = priv;
1472 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473 int rc;
1474
1475 rc = check_dev(dev);
1476 if (rc < 0)
1477 return rc;
1478
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001479 return videobuf_qbuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001480}
1481
1482static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1483{
1484 struct em28xx_fh *fh = priv;
1485 struct em28xx *dev = fh->dev;
1486 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001487
1488 rc = check_dev(dev);
1489 if (rc < 0)
1490 return rc;
1491
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001492 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001493}
1494
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001495#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001496static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001497{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001498 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001499
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001500 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001501}
1502#endif
1503
1504
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001505/* ----------------------------------------------------------- */
1506/* RADIO ESPECIFIC IOCTLS */
1507/* ----------------------------------------------------------- */
1508
1509static int radio_querycap(struct file *file, void *priv,
1510 struct v4l2_capability *cap)
1511{
1512 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1513
1514 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1515 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001516 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001517
1518 cap->version = EM28XX_VERSION_CODE;
1519 cap->capabilities = V4L2_CAP_TUNER;
1520 return 0;
1521}
1522
1523static int radio_g_tuner(struct file *file, void *priv,
1524 struct v4l2_tuner *t)
1525{
1526 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1527
1528 if (unlikely(t->index > 0))
1529 return -EINVAL;
1530
1531 strcpy(t->name, "Radio");
1532 t->type = V4L2_TUNER_RADIO;
1533
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001534 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001535 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001536 mutex_unlock(&dev->lock);
1537
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001538 return 0;
1539}
1540
1541static int radio_enum_input(struct file *file, void *priv,
1542 struct v4l2_input *i)
1543{
1544 if (i->index != 0)
1545 return -EINVAL;
1546 strcpy(i->name, "Radio");
1547 i->type = V4L2_INPUT_TYPE_TUNER;
1548
1549 return 0;
1550}
1551
1552static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1553{
1554 if (unlikely(a->index))
1555 return -EINVAL;
1556
1557 strcpy(a->name, "Radio");
1558 return 0;
1559}
1560
1561static int radio_s_tuner(struct file *file, void *priv,
1562 struct v4l2_tuner *t)
1563{
1564 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1565
1566 if (0 != t->index)
1567 return -EINVAL;
1568
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001569 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001570 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001571 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001572
1573 return 0;
1574}
1575
1576static int radio_s_audio(struct file *file, void *fh,
1577 struct v4l2_audio *a)
1578{
1579 return 0;
1580}
1581
1582static int radio_s_input(struct file *file, void *fh, unsigned int i)
1583{
1584 return 0;
1585}
1586
1587static int radio_queryctrl(struct file *file, void *priv,
1588 struct v4l2_queryctrl *qc)
1589{
1590 int i;
1591
1592 if (qc->id < V4L2_CID_BASE ||
1593 qc->id >= V4L2_CID_LASTP1)
1594 return -EINVAL;
1595
1596 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1597 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1598 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1599 return 0;
1600 }
1601 }
1602
1603 return -EINVAL;
1604}
1605
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001606/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001607 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608 * inits the device and starts isoc transfer
1609 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001610static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001611{
Hans Verkuilbec43662008-12-30 06:58:20 -03001612 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001613 int errCode = 0, radio;
1614 struct em28xx *dev;
1615 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001616 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001617
Hans Verkuilbec43662008-12-30 06:58:20 -03001618 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001619
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001620 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001621 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001622
1623 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001624
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001626 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001628
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001629 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001630 if (!fh) {
1631 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001632 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001633 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001634 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001635 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001636 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001637 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001638 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001639
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001640 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001641 dev->width = norm_maxw(dev);
1642 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001643 dev->hscale = 0;
1644 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001646 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001647 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001648 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001649
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001650 /* Needed, since GPIO might have disabled power of
1651 some i2c device
1652 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001653 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001654
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001655 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001656 if (fh->radio) {
1657 em28xx_videodbg("video_open: setting radio device\n");
1658 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1659 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660
1661 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001662
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001663 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1664 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1665 sizeof(struct em28xx_buffer), fh);
1666
Ingo Molnar3593cab2006-02-07 06:49:14 -02001667 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001668
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 return errCode;
1670}
1671
1672/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001673 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674 * unregisters the v4l2,i2c and usb devices
1675 * called when the device gets disconected or at module unload
1676*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001677void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001679
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001680 /*FIXME: I2C IR should be disconnected */
1681
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001682 if (dev->radio_dev) {
1683 if (-1 != dev->radio_dev->minor)
1684 video_unregister_device(dev->radio_dev);
1685 else
1686 video_device_release(dev->radio_dev);
1687 dev->radio_dev = NULL;
1688 }
1689 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001690 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1691 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001692 if (-1 != dev->vbi_dev->minor)
1693 video_unregister_device(dev->vbi_dev);
1694 else
1695 video_device_release(dev->vbi_dev);
1696 dev->vbi_dev = NULL;
1697 }
1698 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001699 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1700 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001701 if (-1 != dev->vdev->minor)
1702 video_unregister_device(dev->vdev);
1703 else
1704 video_device_release(dev->vdev);
1705 dev->vdev = NULL;
1706 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001707}
1708
1709/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001710 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001711 * stops streaming and deallocates all resources allocated by the v4l2
1712 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001714static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001715{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001716 struct em28xx_fh *fh = filp->private_data;
1717 struct em28xx *dev = fh->dev;
1718 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001719
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001720 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001721
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001722
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001723 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001724 if (res_check(fh))
1725 res_free(fh);
1726
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001727 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001728 videobuf_stop(&fh->vb_vidq);
1729 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001731 /* the device is already disconnect,
1732 free the remaining resources */
1733 if (dev->state & DEV_DISCONNECTED) {
1734 em28xx_release_resources(dev);
1735 mutex_unlock(&dev->lock);
1736 kfree(dev);
1737 return 0;
1738 }
1739
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001740 /* Save some power by putting tuner to sleep */
1741 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1742
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001743 /* do this before setting alternate! */
1744 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001745 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001746
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001747 /* set alternate 0 */
1748 dev->alt = 0;
1749 em28xx_videodbg("setting alternate 0\n");
1750 errCode = usb_set_interface(dev->udev, 0, 0);
1751 if (errCode < 0) {
1752 em28xx_errdev("cannot change alternate number to "
1753 "0 (error=%i)\n", errCode);
1754 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001756 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757 dev->users--;
1758 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001759 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760 return 0;
1761}
1762
1763/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001764 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765 * will allocate buffers when called for the first time
1766 */
1767static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001768em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001769 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001771 struct em28xx_fh *fh = filp->private_data;
1772 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001773 int rc;
1774
1775 rc = check_dev(dev);
1776 if (rc < 0)
1777 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001778
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001779 /* FIXME: read() is not prepared to allow changing the video
1780 resolution while streaming. Seems a bug at em28xx_set_fmt
1781 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001782
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001783 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001784 mutex_lock(&dev->lock);
1785 rc = res_get(fh);
1786 mutex_unlock(&dev->lock);
1787
1788 if (unlikely(rc < 0))
1789 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001790
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001791 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1792 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001793 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001794 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795}
1796
1797/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001798 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001799 * will allocate buffers when called for the first time
1800 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001801static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001802{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001803 struct em28xx_fh *fh = filp->private_data;
1804 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001805 int rc;
1806
1807 rc = check_dev(dev);
1808 if (rc < 0)
1809 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001811 mutex_lock(&dev->lock);
1812 rc = res_get(fh);
1813 mutex_unlock(&dev->lock);
1814
1815 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001816 return POLLERR;
1817
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001818 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1819 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001820
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001821 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822}
1823
1824/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001825 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001826 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001827static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001829 struct em28xx_fh *fh = filp->private_data;
1830 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001831 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001832
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001833 rc = check_dev(dev);
1834 if (rc < 0)
1835 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001836
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001837 mutex_lock(&dev->lock);
1838 rc = res_get(fh);
1839 mutex_unlock(&dev->lock);
1840
1841 if (unlikely(rc < 0))
1842 return rc;
1843
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001844 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001846 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1847 (unsigned long)vma->vm_start,
1848 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1849 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001850
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001851 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001852}
1853
Hans Verkuilbec43662008-12-30 06:58:20 -03001854static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001855 .owner = THIS_MODULE,
1856 .open = em28xx_v4l2_open,
1857 .release = em28xx_v4l2_close,
1858 .read = em28xx_v4l2_read,
1859 .poll = em28xx_v4l2_poll,
1860 .mmap = em28xx_v4l2_mmap,
1861 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001862};
1863
Hans Verkuila3998102008-07-21 02:57:38 -03001864static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001865 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001866 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1867 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1868 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1869 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001870 .vidioc_g_audio = vidioc_g_audio,
1871 .vidioc_s_audio = vidioc_s_audio,
1872 .vidioc_cropcap = vidioc_cropcap,
1873
Hans Verkuil78b526a2008-05-28 12:16:41 -03001874 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1875 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1876 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001877
1878 .vidioc_reqbufs = vidioc_reqbufs,
1879 .vidioc_querybuf = vidioc_querybuf,
1880 .vidioc_qbuf = vidioc_qbuf,
1881 .vidioc_dqbuf = vidioc_dqbuf,
1882 .vidioc_s_std = vidioc_s_std,
1883 .vidioc_enum_input = vidioc_enum_input,
1884 .vidioc_g_input = vidioc_g_input,
1885 .vidioc_s_input = vidioc_s_input,
1886 .vidioc_queryctrl = vidioc_queryctrl,
1887 .vidioc_g_ctrl = vidioc_g_ctrl,
1888 .vidioc_s_ctrl = vidioc_s_ctrl,
1889 .vidioc_streamon = vidioc_streamon,
1890 .vidioc_streamoff = vidioc_streamoff,
1891 .vidioc_g_tuner = vidioc_g_tuner,
1892 .vidioc_s_tuner = vidioc_s_tuner,
1893 .vidioc_g_frequency = vidioc_g_frequency,
1894 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001895#ifdef CONFIG_VIDEO_ADV_DEBUG
1896 .vidioc_g_register = vidioc_g_register,
1897 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001898 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001899#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001900#ifdef CONFIG_VIDEO_V4L1_COMPAT
1901 .vidiocgmbuf = vidiocgmbuf,
1902#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001903};
1904
1905static const struct video_device em28xx_video_template = {
1906 .fops = &em28xx_v4l_fops,
1907 .release = video_device_release,
1908 .ioctl_ops = &video_ioctl_ops,
1909
1910 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001911
1912 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001913 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001914};
1915
Hans Verkuilbec43662008-12-30 06:58:20 -03001916static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001917 .owner = THIS_MODULE,
1918 .open = em28xx_v4l2_open,
1919 .release = em28xx_v4l2_close,
1920 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001921};
1922
1923static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001924 .vidioc_querycap = radio_querycap,
1925 .vidioc_g_tuner = radio_g_tuner,
1926 .vidioc_enum_input = radio_enum_input,
1927 .vidioc_g_audio = radio_g_audio,
1928 .vidioc_s_tuner = radio_s_tuner,
1929 .vidioc_s_audio = radio_s_audio,
1930 .vidioc_s_input = radio_s_input,
1931 .vidioc_queryctrl = radio_queryctrl,
1932 .vidioc_g_ctrl = vidioc_g_ctrl,
1933 .vidioc_s_ctrl = vidioc_s_ctrl,
1934 .vidioc_g_frequency = vidioc_g_frequency,
1935 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001936#ifdef CONFIG_VIDEO_ADV_DEBUG
1937 .vidioc_g_register = vidioc_g_register,
1938 .vidioc_s_register = vidioc_s_register,
1939#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001940};
1941
Hans Verkuila3998102008-07-21 02:57:38 -03001942static struct video_device em28xx_radio_template = {
1943 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001944 .fops = &radio_fops,
1945 .ioctl_ops = &radio_ioctl_ops,
1946 .minor = -1,
1947};
1948
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001949/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001950
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001951
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001952
Adrian Bunk532fe652008-01-28 22:10:48 -03001953static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001954 const struct video_device *template,
1955 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001956{
1957 struct video_device *vfd;
1958
1959 vfd = video_device_alloc();
1960 if (NULL == vfd)
1961 return NULL;
1962 *vfd = *template;
1963 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001964 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001965 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001966 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001967
1968 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1969 dev->name, type_name);
1970
1971 return vfd;
1972}
1973
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001974int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001975{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03001976 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001977 int ret;
1978
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001979 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1980 dev->name,
1981 (EM28XX_VERSION_CODE >> 16) & 0xff,
1982 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1983
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001984 /* set default norm */
1985 dev->norm = em28xx_video_template.current_norm;
1986 dev->width = norm_maxw(dev);
1987 dev->height = norm_maxh(dev);
1988 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1989 dev->hscale = 0;
1990 dev->vscale = 0;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001991 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001992
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001993 /* Analog specific initialization */
1994 dev->format = &format[0];
1995 video_mux(dev, dev->ctl_input);
1996
1997 /* Audio defaults */
1998 dev->mute = 1;
1999 dev->volume = 0x1f;
2000
2001 /* enable vbi capturing */
2002
2003/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002004 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2005 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2006 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002007 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2008
2009 em28xx_set_outfmt(dev);
2010 em28xx_colorlevels_set_default(dev);
2011 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002012
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002013 /* allocate and fill video video_device struct */
2014 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2015 if (!dev->vdev) {
2016 em28xx_errdev("cannot allocate video_device.\n");
2017 return -ENODEV;
2018 }
2019
2020 /* register v4l2 video video_device */
2021 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2022 video_nr[dev->devno]);
2023 if (ret) {
2024 em28xx_errdev("unable to register video device (error=%i).\n",
2025 ret);
2026 return ret;
2027 }
2028
2029 /* Allocate and fill vbi video_device struct */
2030 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2031
2032 /* register v4l2 vbi video_device */
2033 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2034 vbi_nr[dev->devno]);
2035 if (ret < 0) {
2036 em28xx_errdev("unable to register vbi device\n");
2037 return ret;
2038 }
2039
2040 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002041 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2042 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002043 if (!dev->radio_dev) {
2044 em28xx_errdev("cannot allocate video_device.\n");
2045 return -ENODEV;
2046 }
2047 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2048 radio_nr[dev->devno]);
2049 if (ret < 0) {
2050 em28xx_errdev("can't register radio device\n");
2051 return ret;
2052 }
2053 em28xx_info("Registered radio device as /dev/radio%d\n",
2054 dev->radio_dev->num);
2055 }
2056
2057 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2058 dev->vdev->num, dev->vbi_dev->num);
2059
2060 return 0;
2061}