blob: efd641587e04d6809633f138da2ab5344716335a [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
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300543 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800544}
545
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300546/* Usage lock check functions */
547static int res_get(struct em28xx_fh *fh)
548{
549 struct em28xx *dev = fh->dev;
550 int rc = 0;
551
552 /* This instance already has stream_on */
553 if (fh->stream_on)
554 return rc;
555
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300556 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300557 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300558
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300559 dev->stream_on = 1;
560 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300561 return rc;
562}
563
564static int res_check(struct em28xx_fh *fh)
565{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300566 return fh->stream_on;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300567}
568
569static void res_free(struct em28xx_fh *fh)
570{
571 struct em28xx *dev = fh->dev;
572
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300573 fh->stream_on = 0;
574 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300575}
576
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800577/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300578 * em28xx_get_ctrl()
579 * return the current saturation, brightness or contrast, mute state
580 */
581static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
582{
583 switch (ctrl->id) {
584 case V4L2_CID_AUDIO_MUTE:
585 ctrl->value = dev->mute;
586 return 0;
587 case V4L2_CID_AUDIO_VOLUME:
588 ctrl->value = dev->volume;
589 return 0;
590 default:
591 return -EINVAL;
592 }
593}
594
595/*
596 * em28xx_set_ctrl()
597 * mute or set new saturation, brightness or contrast
598 */
599static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
600{
601 switch (ctrl->id) {
602 case V4L2_CID_AUDIO_MUTE:
603 if (ctrl->value != dev->mute) {
604 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300605 return em28xx_audio_analog_set(dev);
606 }
607 return 0;
608 case V4L2_CID_AUDIO_VOLUME:
609 dev->volume = ctrl->value;
610 return em28xx_audio_analog_set(dev);
611 default:
612 return -EINVAL;
613 }
614}
615
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300616static int check_dev(struct em28xx *dev)
617{
618 if (dev->state & DEV_DISCONNECTED) {
619 em28xx_errdev("v4l2 ioctl: device not present\n");
620 return -ENODEV;
621 }
622
623 if (dev->state & DEV_MISCONFIGURED) {
624 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
625 "close and open it again\n");
626 return -EIO;
627 }
628 return 0;
629}
630
631static void get_scale(struct em28xx *dev,
632 unsigned int width, unsigned int height,
633 unsigned int *hscale, unsigned int *vscale)
634{
635 unsigned int maxw = norm_maxw(dev);
636 unsigned int maxh = norm_maxh(dev);
637
638 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
639 if (*hscale >= 0x4000)
640 *hscale = 0x3fff;
641
642 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
643 if (*vscale >= 0x4000)
644 *vscale = 0x3fff;
645}
646
647/* ------------------------------------------------------------------
648 IOCTL vidioc handling
649 ------------------------------------------------------------------*/
650
Hans Verkuil78b526a2008-05-28 12:16:41 -0300651static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300652 struct v4l2_format *f)
653{
654 struct em28xx_fh *fh = priv;
655 struct em28xx *dev = fh->dev;
656
657 mutex_lock(&dev->lock);
658
659 f->fmt.pix.width = dev->width;
660 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300661 f->fmt.pix.pixelformat = dev->format->fourcc;
662 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300663 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300664 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
665
666 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
667 f->fmt.pix.field = dev->interlaced ?
668 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
669
670 mutex_unlock(&dev->lock);
671 return 0;
672}
673
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300674static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
675{
676 unsigned int i;
677
678 for (i = 0; i < ARRAY_SIZE(format); i++)
679 if (format[i].fourcc == fourcc)
680 return &format[i];
681
682 return NULL;
683}
684
Hans Verkuil78b526a2008-05-28 12:16:41 -0300685static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300686 struct v4l2_format *f)
687{
688 struct em28xx_fh *fh = priv;
689 struct em28xx *dev = fh->dev;
690 int width = f->fmt.pix.width;
691 int height = f->fmt.pix.height;
692 unsigned int maxw = norm_maxw(dev);
693 unsigned int maxh = norm_maxh(dev);
694 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300695 struct em28xx_fmt *fmt;
696
697 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
698 if (!fmt) {
699 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
700 f->fmt.pix.pixelformat);
701 return -EINVAL;
702 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300703
704 /* width must even because of the YUYV format
705 height must be even because of interlacing */
706 height &= 0xfffe;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300707 width &= 0xfffe;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300708
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300709 if (unlikely(height < 32))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710 height = 32;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300711 if (unlikely(height > maxh))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300712 height = maxh;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300713 if (unlikely(width < 48))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300714 width = 48;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300715 if (unlikely(width > maxw))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300716 width = maxw;
717
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300718 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300719 /* the em2800 can only scale down to 50% */
720 if (height % (maxh / 2))
721 height = maxh;
722 if (width % (maxw / 2))
723 width = maxw;
724 /* according to empiatech support */
725 /* the MaxPacketSize is to small to support */
726 /* framesizes larger than 640x480 @ 30 fps */
727 /* or 640x576 @ 25 fps. As this would cut */
728 /* of a part of the image we prefer */
729 /* 360x576 or 360x480 for now */
730 if (width == maxw && height == maxh)
731 width /= 2;
732 }
733
734 get_scale(dev, width, height, &hscale, &vscale);
735
736 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
737 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
738
739 f->fmt.pix.width = width;
740 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300741 f->fmt.pix.pixelformat = fmt->fourcc;
742 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
743 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300744 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
745 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
746
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300747 return 0;
748}
749
Hans Verkuil78b526a2008-05-28 12:16:41 -0300750static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300751 struct v4l2_format *f)
752{
753 struct em28xx_fh *fh = priv;
754 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300755 int rc;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300756 struct em28xx_fmt *fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300757
758 rc = check_dev(dev);
759 if (rc < 0)
760 return rc;
761
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300762 mutex_lock(&dev->lock);
763
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300764 vidioc_try_fmt_vid_cap(file, priv, f);
765
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300766 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -0300767 if (!fmt) {
768 rc = -EINVAL;
769 goto out;
770 }
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300771
Brandon Philips05612972008-04-13 14:57:01 -0300772 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
773 em28xx_errdev("%s queue busy\n", __func__);
774 rc = -EBUSY;
775 goto out;
776 }
777
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300778 if (dev->stream_on && !fh->stream_on) {
779 em28xx_errdev("%s device in use by another fh\n", __func__);
780 rc = -EBUSY;
781 goto out;
782 }
783
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300784 /* set new image size */
785 dev->width = f->fmt.pix.width;
786 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300787 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300788 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
789
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300790 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300791 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300792
Brandon Philips05612972008-04-13 14:57:01 -0300793 rc = 0;
794
795out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300796 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300797 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300798}
799
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300800static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300801{
802 struct em28xx_fh *fh = priv;
803 struct em28xx *dev = fh->dev;
804 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805 int rc;
806
807 rc = check_dev(dev);
808 if (rc < 0)
809 return rc;
810
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300812 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813
814 /* Adjusts width/height, if needed */
815 f.fmt.pix.width = dev->width;
816 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300817 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300819 /* set new image size */
820 dev->width = f.fmt.pix.width;
821 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
823
824 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300825 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826
827 mutex_unlock(&dev->lock);
828 return 0;
829}
830
831static const char *iname[] = {
832 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
833 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
834 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
835 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
836 [EM28XX_VMUX_SVIDEO] = "S-Video",
837 [EM28XX_VMUX_TELEVISION] = "Television",
838 [EM28XX_VMUX_CABLE] = "Cable TV",
839 [EM28XX_VMUX_DVB] = "DVB",
840 [EM28XX_VMUX_DEBUG] = "for debug only",
841};
842
843static int vidioc_enum_input(struct file *file, void *priv,
844 struct v4l2_input *i)
845{
846 struct em28xx_fh *fh = priv;
847 struct em28xx *dev = fh->dev;
848 unsigned int n;
849
850 n = i->index;
851 if (n >= MAX_EM28XX_INPUT)
852 return -EINVAL;
853 if (0 == INPUT(n)->type)
854 return -EINVAL;
855
856 i->index = n;
857 i->type = V4L2_INPUT_TYPE_CAMERA;
858
859 strcpy(i->name, iname[INPUT(n)->type]);
860
861 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
862 (EM28XX_VMUX_CABLE == INPUT(n)->type))
863 i->type = V4L2_INPUT_TYPE_TUNER;
864
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300865 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866
867 return 0;
868}
869
870static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
871{
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
874
875 *i = dev->ctl_input;
876
877 return 0;
878}
879
880static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
881{
882 struct em28xx_fh *fh = priv;
883 struct em28xx *dev = fh->dev;
884 int rc;
885
886 rc = check_dev(dev);
887 if (rc < 0)
888 return rc;
889
890 if (i >= MAX_EM28XX_INPUT)
891 return -EINVAL;
892 if (0 == INPUT(i)->type)
893 return -EINVAL;
894
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300895 dev->ctl_input = i;
896
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300897 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300898 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300899 mutex_unlock(&dev->lock);
900 return 0;
901}
902
903static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
904{
905 struct em28xx_fh *fh = priv;
906 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300907
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300908 switch (a->index) {
909 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300910 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300911 break;
912 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300913 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300914 break;
915 case EM28XX_AMUX_VIDEO2:
916 strcpy(a->name, "Television alt");
917 break;
918 case EM28XX_AMUX_PHONE:
919 strcpy(a->name, "Phone");
920 break;
921 case EM28XX_AMUX_MIC:
922 strcpy(a->name, "Mic");
923 break;
924 case EM28XX_AMUX_CD:
925 strcpy(a->name, "CD");
926 break;
927 case EM28XX_AMUX_AUX:
928 strcpy(a->name, "Aux");
929 break;
930 case EM28XX_AMUX_PCM_OUT:
931 strcpy(a->name, "PCM");
932 break;
933 default:
934 return -EINVAL;
935 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300936
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300937 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939
940 return 0;
941}
942
943static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
944{
945 struct em28xx_fh *fh = priv;
946 struct em28xx *dev = fh->dev;
947
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -0300948
949 if (a->index >= MAX_EM28XX_INPUT)
950 return -EINVAL;
951 if (0 == INPUT(a->index)->type)
952 return -EINVAL;
953
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300954 mutex_lock(&dev->lock);
955
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300956 dev->ctl_ainput = INPUT(a->index)->amux;
957 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300958
959 if (!dev->ctl_aoutput)
960 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300961
962 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963 return 0;
964}
965
966static int vidioc_queryctrl(struct file *file, void *priv,
967 struct v4l2_queryctrl *qc)
968{
969 struct em28xx_fh *fh = priv;
970 struct em28xx *dev = fh->dev;
971 int id = qc->id;
972 int i;
973 int rc;
974
975 rc = check_dev(dev);
976 if (rc < 0)
977 return rc;
978
979 memset(qc, 0, sizeof(*qc));
980
981 qc->id = id;
982
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300983 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
985 if (qc->id && qc->id == em28xx_qctrl[i].id) {
986 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
987 return 0;
988 }
989 }
990 }
991 mutex_lock(&dev->lock);
992 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
993 mutex_unlock(&dev->lock);
994
995 if (qc->type)
996 return 0;
997 else
998 return -EINVAL;
999}
1000
1001static int vidioc_g_ctrl(struct file *file, void *priv,
1002 struct v4l2_control *ctrl)
1003{
1004 struct em28xx_fh *fh = priv;
1005 struct em28xx *dev = fh->dev;
1006 int rc;
1007
1008 rc = check_dev(dev);
1009 if (rc < 0)
1010 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001011 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001012
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 mutex_lock(&dev->lock);
1014
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001015 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001016 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001017 else {
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001018 rc = em28xx_get_ctrl(dev, ctrl);
Hans Verkuil07f7db42009-01-21 17:06:42 -03001019 if (rc < 0) {
1020 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1021 rc = 0;
1022 }
1023 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001024
1025 mutex_unlock(&dev->lock);
1026 return rc;
1027}
1028
1029static int vidioc_s_ctrl(struct file *file, void *priv,
1030 struct v4l2_control *ctrl)
1031{
1032 struct em28xx_fh *fh = priv;
1033 struct em28xx *dev = fh->dev;
1034 u8 i;
1035 int rc;
1036
1037 rc = check_dev(dev);
1038 if (rc < 0)
1039 return rc;
1040
1041 mutex_lock(&dev->lock);
1042
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001043 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1045 else {
1046 rc = 1;
1047 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1048 if (ctrl->id == em28xx_qctrl[i].id) {
1049 if (ctrl->value < em28xx_qctrl[i].minimum ||
1050 ctrl->value > em28xx_qctrl[i].maximum) {
1051 rc = -ERANGE;
1052 break;
1053 }
1054
1055 rc = em28xx_set_ctrl(dev, ctrl);
1056 break;
1057 }
1058 }
1059 }
1060
1061 /* Control not found - try to send it to the attached devices */
1062 if (rc == 1) {
1063 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1064 rc = 0;
1065 }
1066
1067 mutex_unlock(&dev->lock);
1068 return rc;
1069}
1070
1071static int vidioc_g_tuner(struct file *file, void *priv,
1072 struct v4l2_tuner *t)
1073{
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1076 int rc;
1077
1078 rc = check_dev(dev);
1079 if (rc < 0)
1080 return rc;
1081
1082 if (0 != t->index)
1083 return -EINVAL;
1084
1085 strcpy(t->name, "Tuner");
1086
1087 mutex_lock(&dev->lock);
1088
1089 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1090
1091 mutex_unlock(&dev->lock);
1092 return 0;
1093}
1094
1095static int vidioc_s_tuner(struct file *file, void *priv,
1096 struct v4l2_tuner *t)
1097{
1098 struct em28xx_fh *fh = priv;
1099 struct em28xx *dev = fh->dev;
1100 int rc;
1101
1102 rc = check_dev(dev);
1103 if (rc < 0)
1104 return rc;
1105
1106 if (0 != t->index)
1107 return -EINVAL;
1108
1109 mutex_lock(&dev->lock);
1110
1111 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1112
1113 mutex_unlock(&dev->lock);
1114 return 0;
1115}
1116
1117static int vidioc_g_frequency(struct file *file, void *priv,
1118 struct v4l2_frequency *f)
1119{
1120 struct em28xx_fh *fh = priv;
1121 struct em28xx *dev = fh->dev;
1122
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001123 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001124 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001125 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001126 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001127
1128 return 0;
1129}
1130
1131static int vidioc_s_frequency(struct file *file, void *priv,
1132 struct v4l2_frequency *f)
1133{
1134 struct em28xx_fh *fh = priv;
1135 struct em28xx *dev = fh->dev;
1136 int rc;
1137
1138 rc = check_dev(dev);
1139 if (rc < 0)
1140 return rc;
1141
1142 if (0 != f->tuner)
1143 return -EINVAL;
1144
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001145 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1146 return -EINVAL;
1147 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001148 return -EINVAL;
1149
1150 mutex_lock(&dev->lock);
1151
1152 dev->ctl_freq = f->frequency;
1153 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1154
1155 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001156
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001157 return 0;
1158}
1159
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001160#ifdef CONFIG_VIDEO_ADV_DEBUG
1161static int em28xx_reg_len(int reg)
1162{
1163 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001164 case EM28XX_R40_AC97LSB:
1165 case EM28XX_R30_HSCALELOW:
1166 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001167 return 2;
1168 default:
1169 return 1;
1170 }
1171}
1172
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001173static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001174 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001175{
1176 struct em28xx_fh *fh = priv;
1177 struct em28xx *dev = fh->dev;
1178
1179 chip->ident = V4L2_IDENT_NONE;
1180 chip->revision = 0;
1181
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001182 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001183
1184 return 0;
1185}
1186
1187
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001188static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001189 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001190{
1191 struct em28xx_fh *fh = priv;
1192 struct em28xx *dev = fh->dev;
1193 int ret;
1194
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001195 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001196 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001197 mutex_lock(&dev->lock);
1198 ret = em28xx_read_ac97(dev, reg->reg);
1199 mutex_unlock(&dev->lock);
1200 if (ret < 0)
1201 return ret;
1202
1203 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001204 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001205 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001206 case V4L2_CHIP_MATCH_I2C_DRIVER:
1207 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1208 return 0;
1209 case V4L2_CHIP_MATCH_I2C_ADDR:
1210 /* Not supported yet */
1211 return -EINVAL;
1212 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001213 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001214 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001215 }
1216
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001217 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001218 reg->size = em28xx_reg_len(reg->reg);
1219 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001220 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001221 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001222 mutex_unlock(&dev->lock);
1223
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001224 if (ret < 0)
1225 return ret;
1226
1227 reg->val = ret;
1228 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001229 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001230 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001231 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1232 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001233 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001234 if (ret < 0)
1235 return ret;
1236
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001237 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001238 }
1239
1240 return 0;
1241}
1242
1243static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001244 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001245{
1246 struct em28xx_fh *fh = priv;
1247 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001248 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001249 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001250
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001251 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001252 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001253 mutex_lock(&dev->lock);
1254 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1255 mutex_unlock(&dev->lock);
1256
1257 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001258 case V4L2_CHIP_MATCH_I2C_DRIVER:
1259 em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1260 return 0;
1261 case V4L2_CHIP_MATCH_I2C_ADDR:
1262 /* Not supported yet */
1263 return -EINVAL;
1264 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001265 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001266 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001267 }
1268
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001269 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001270 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001271
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001272 mutex_lock(&dev->lock);
1273 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1274 em28xx_reg_len(reg->reg));
1275 mutex_unlock(&dev->lock);
1276
1277 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001278}
1279#endif
1280
1281
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001282static int vidioc_cropcap(struct file *file, void *priv,
1283 struct v4l2_cropcap *cc)
1284{
1285 struct em28xx_fh *fh = priv;
1286 struct em28xx *dev = fh->dev;
1287
1288 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1289 return -EINVAL;
1290
1291 cc->bounds.left = 0;
1292 cc->bounds.top = 0;
1293 cc->bounds.width = dev->width;
1294 cc->bounds.height = dev->height;
1295 cc->defrect = cc->bounds;
1296 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1297 cc->pixelaspect.denominator = 59;
1298
1299 return 0;
1300}
1301
1302static int vidioc_streamon(struct file *file, void *priv,
1303 enum v4l2_buf_type type)
1304{
1305 struct em28xx_fh *fh = priv;
1306 struct em28xx *dev = fh->dev;
1307 int rc;
1308
1309 rc = check_dev(dev);
1310 if (rc < 0)
1311 return rc;
1312
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001313
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001314 mutex_lock(&dev->lock);
1315 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001316
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001317 if (likely(rc >= 0))
1318 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001319
1320 mutex_unlock(&dev->lock);
1321
1322 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001323}
1324
1325static int vidioc_streamoff(struct file *file, void *priv,
1326 enum v4l2_buf_type type)
1327{
1328 struct em28xx_fh *fh = priv;
1329 struct em28xx *dev = fh->dev;
1330 int rc;
1331
1332 rc = check_dev(dev);
1333 if (rc < 0)
1334 return rc;
1335
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001336 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1337 return -EINVAL;
1338 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001339 return -EINVAL;
1340
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001341 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001342
1343 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001344 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001345
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001346 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 return 0;
1349}
1350
1351static int vidioc_querycap(struct file *file, void *priv,
1352 struct v4l2_capability *cap)
1353{
1354 struct em28xx_fh *fh = priv;
1355 struct em28xx *dev = fh->dev;
1356
1357 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1358 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001359 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001360
1361 cap->version = EM28XX_VERSION_CODE;
1362
1363 cap->capabilities =
1364 V4L2_CAP_SLICED_VBI_CAPTURE |
1365 V4L2_CAP_VIDEO_CAPTURE |
1366 V4L2_CAP_AUDIO |
1367 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1368
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001369 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001370 cap->capabilities |= V4L2_CAP_TUNER;
1371
1372 return 0;
1373}
1374
Hans Verkuil78b526a2008-05-28 12:16:41 -03001375static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001376 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001378 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001379 return -EINVAL;
1380
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001381 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1382 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383
1384 return 0;
1385}
1386
1387/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001388static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389 struct v4l2_format *f)
1390{
1391 struct em28xx_fh *fh = priv;
1392 struct em28xx *dev = fh->dev;
1393 int rc;
1394
1395 rc = check_dev(dev);
1396 if (rc < 0)
1397 return rc;
1398
1399 mutex_lock(&dev->lock);
1400
1401 f->fmt.sliced.service_set = 0;
1402
1403 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1404
1405 if (f->fmt.sliced.service_set == 0)
1406 rc = -EINVAL;
1407
1408 mutex_unlock(&dev->lock);
1409 return rc;
1410}
1411
Hans Verkuil78b526a2008-05-28 12:16:41 -03001412static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001413 struct v4l2_format *f)
1414{
1415 struct em28xx_fh *fh = priv;
1416 struct em28xx *dev = fh->dev;
1417 int rc;
1418
1419 rc = check_dev(dev);
1420 if (rc < 0)
1421 return rc;
1422
1423 mutex_lock(&dev->lock);
1424 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1425 mutex_unlock(&dev->lock);
1426
1427 if (f->fmt.sliced.service_set == 0)
1428 return -EINVAL;
1429
1430 return 0;
1431}
1432
1433
1434static int vidioc_reqbufs(struct file *file, void *priv,
1435 struct v4l2_requestbuffers *rb)
1436{
1437 struct em28xx_fh *fh = priv;
1438 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 int rc;
1440
1441 rc = check_dev(dev);
1442 if (rc < 0)
1443 return rc;
1444
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001445 return videobuf_reqbufs(&fh->vb_vidq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001446}
1447
1448static int vidioc_querybuf(struct file *file, void *priv,
1449 struct v4l2_buffer *b)
1450{
1451 struct em28xx_fh *fh = priv;
1452 struct em28xx *dev = fh->dev;
1453 int rc;
1454
1455 rc = check_dev(dev);
1456 if (rc < 0)
1457 return rc;
1458
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001459 return videobuf_querybuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001460}
1461
1462static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
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_qbuf(&fh->vb_vidq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473}
1474
1475static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1476{
1477 struct em28xx_fh *fh = priv;
1478 struct em28xx *dev = fh->dev;
1479 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001480
1481 rc = check_dev(dev);
1482 if (rc < 0)
1483 return rc;
1484
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001485 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001486}
1487
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001488#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001489static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001490{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001491 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001492
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001493 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001494}
1495#endif
1496
1497
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001498/* ----------------------------------------------------------- */
1499/* RADIO ESPECIFIC IOCTLS */
1500/* ----------------------------------------------------------- */
1501
1502static int radio_querycap(struct file *file, void *priv,
1503 struct v4l2_capability *cap)
1504{
1505 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1506
1507 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1508 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001509 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001510
1511 cap->version = EM28XX_VERSION_CODE;
1512 cap->capabilities = V4L2_CAP_TUNER;
1513 return 0;
1514}
1515
1516static int radio_g_tuner(struct file *file, void *priv,
1517 struct v4l2_tuner *t)
1518{
1519 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1520
1521 if (unlikely(t->index > 0))
1522 return -EINVAL;
1523
1524 strcpy(t->name, "Radio");
1525 t->type = V4L2_TUNER_RADIO;
1526
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001527 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001528 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001529 mutex_unlock(&dev->lock);
1530
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001531 return 0;
1532}
1533
1534static int radio_enum_input(struct file *file, void *priv,
1535 struct v4l2_input *i)
1536{
1537 if (i->index != 0)
1538 return -EINVAL;
1539 strcpy(i->name, "Radio");
1540 i->type = V4L2_INPUT_TYPE_TUNER;
1541
1542 return 0;
1543}
1544
1545static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1546{
1547 if (unlikely(a->index))
1548 return -EINVAL;
1549
1550 strcpy(a->name, "Radio");
1551 return 0;
1552}
1553
1554static int radio_s_tuner(struct file *file, void *priv,
1555 struct v4l2_tuner *t)
1556{
1557 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1558
1559 if (0 != t->index)
1560 return -EINVAL;
1561
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001562 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001563 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001564 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001565
1566 return 0;
1567}
1568
1569static int radio_s_audio(struct file *file, void *fh,
1570 struct v4l2_audio *a)
1571{
1572 return 0;
1573}
1574
1575static int radio_s_input(struct file *file, void *fh, unsigned int i)
1576{
1577 return 0;
1578}
1579
1580static int radio_queryctrl(struct file *file, void *priv,
1581 struct v4l2_queryctrl *qc)
1582{
1583 int i;
1584
1585 if (qc->id < V4L2_CID_BASE ||
1586 qc->id >= V4L2_CID_LASTP1)
1587 return -EINVAL;
1588
1589 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1590 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1591 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1592 return 0;
1593 }
1594 }
1595
1596 return -EINVAL;
1597}
1598
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001599/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001600 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001601 * inits the device and starts isoc transfer
1602 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001603static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001604{
Hans Verkuilbec43662008-12-30 06:58:20 -03001605 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001606 int errCode = 0, radio;
1607 struct em28xx *dev;
1608 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001609 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001610
Hans Verkuilbec43662008-12-30 06:58:20 -03001611 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001612
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001613 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001614 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001615
1616 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001617
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001618 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001619 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001620
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001621
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001622 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001623 if (!fh) {
1624 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001625 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001626 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001628 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001629 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001630 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001631 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001632
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001633 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001634 dev->width = norm_maxw(dev);
1635 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001636 dev->hscale = 0;
1637 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001638
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001639 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001640 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001641 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001643 /* Needed, since GPIO might have disabled power of
1644 some i2c device
1645 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001646 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001647
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001648 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001649 if (fh->radio) {
1650 em28xx_videodbg("video_open: setting radio device\n");
1651 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1652 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001653
1654 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001655
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001656 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1657 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1658 sizeof(struct em28xx_buffer), fh);
1659
Ingo Molnar3593cab2006-02-07 06:49:14 -02001660 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001661
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001662 return errCode;
1663}
1664
1665/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001666 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001667 * unregisters the v4l2,i2c and usb devices
1668 * called when the device gets disconected or at module unload
1669*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001670void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001671{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001673 /*FIXME: I2C IR should be disconnected */
1674
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001675 if (dev->radio_dev) {
1676 if (-1 != dev->radio_dev->minor)
1677 video_unregister_device(dev->radio_dev);
1678 else
1679 video_device_release(dev->radio_dev);
1680 dev->radio_dev = NULL;
1681 }
1682 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001683 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1684 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001685 if (-1 != dev->vbi_dev->minor)
1686 video_unregister_device(dev->vbi_dev);
1687 else
1688 video_device_release(dev->vbi_dev);
1689 dev->vbi_dev = NULL;
1690 }
1691 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001692 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1693 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001694 if (-1 != dev->vdev->minor)
1695 video_unregister_device(dev->vdev);
1696 else
1697 video_device_release(dev->vdev);
1698 dev->vdev = NULL;
1699 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001700}
1701
1702/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001703 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001704 * stops streaming and deallocates all resources allocated by the v4l2
1705 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001707static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001709 struct em28xx_fh *fh = filp->private_data;
1710 struct em28xx *dev = fh->dev;
1711 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001712
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001713 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001715
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001716 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001717 if (res_check(fh))
1718 res_free(fh);
1719
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001720 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001721 videobuf_stop(&fh->vb_vidq);
1722 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001723
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001724 /* the device is already disconnect,
1725 free the remaining resources */
1726 if (dev->state & DEV_DISCONNECTED) {
1727 em28xx_release_resources(dev);
1728 mutex_unlock(&dev->lock);
1729 kfree(dev);
1730 return 0;
1731 }
1732
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001733 /* Save some power by putting tuner to sleep */
1734 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1735
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001736 /* do this before setting alternate! */
1737 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001738 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001739
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001740 /* set alternate 0 */
1741 dev->alt = 0;
1742 em28xx_videodbg("setting alternate 0\n");
1743 errCode = usb_set_interface(dev->udev, 0, 0);
1744 if (errCode < 0) {
1745 em28xx_errdev("cannot change alternate number to "
1746 "0 (error=%i)\n", errCode);
1747 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001749 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001750 dev->users--;
1751 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001752 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753 return 0;
1754}
1755
1756/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001757 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001758 * will allocate buffers when called for the first time
1759 */
1760static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001761em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001762 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001763{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001764 struct em28xx_fh *fh = filp->private_data;
1765 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001766 int rc;
1767
1768 rc = check_dev(dev);
1769 if (rc < 0)
1770 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001771
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001772 /* FIXME: read() is not prepared to allow changing the video
1773 resolution while streaming. Seems a bug at em28xx_set_fmt
1774 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001775
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001776 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001777 mutex_lock(&dev->lock);
1778 rc = res_get(fh);
1779 mutex_unlock(&dev->lock);
1780
1781 if (unlikely(rc < 0))
1782 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001783
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001784 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1785 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001786 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001787 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001788}
1789
1790/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001791 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001792 * will allocate buffers when called for the first time
1793 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001794static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001796 struct em28xx_fh *fh = filp->private_data;
1797 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001798 int rc;
1799
1800 rc = check_dev(dev);
1801 if (rc < 0)
1802 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001804 mutex_lock(&dev->lock);
1805 rc = res_get(fh);
1806 mutex_unlock(&dev->lock);
1807
1808 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001809 return POLLERR;
1810
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001811 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1812 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001813
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001814 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001815}
1816
1817/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001818 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001819 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001820static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001822 struct em28xx_fh *fh = filp->private_data;
1823 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001824 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001825
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001826 rc = check_dev(dev);
1827 if (rc < 0)
1828 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001829
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001830 mutex_lock(&dev->lock);
1831 rc = res_get(fh);
1832 mutex_unlock(&dev->lock);
1833
1834 if (unlikely(rc < 0))
1835 return rc;
1836
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001837 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001838
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001839 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1840 (unsigned long)vma->vm_start,
1841 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1842 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001843
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001844 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845}
1846
Hans Verkuilbec43662008-12-30 06:58:20 -03001847static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001848 .owner = THIS_MODULE,
1849 .open = em28xx_v4l2_open,
1850 .release = em28xx_v4l2_close,
1851 .read = em28xx_v4l2_read,
1852 .poll = em28xx_v4l2_poll,
1853 .mmap = em28xx_v4l2_mmap,
1854 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001855};
1856
Hans Verkuila3998102008-07-21 02:57:38 -03001857static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001858 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001859 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1860 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1861 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1862 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001863 .vidioc_g_audio = vidioc_g_audio,
1864 .vidioc_s_audio = vidioc_s_audio,
1865 .vidioc_cropcap = vidioc_cropcap,
1866
Hans Verkuil78b526a2008-05-28 12:16:41 -03001867 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1868 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1869 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001870
1871 .vidioc_reqbufs = vidioc_reqbufs,
1872 .vidioc_querybuf = vidioc_querybuf,
1873 .vidioc_qbuf = vidioc_qbuf,
1874 .vidioc_dqbuf = vidioc_dqbuf,
1875 .vidioc_s_std = vidioc_s_std,
1876 .vidioc_enum_input = vidioc_enum_input,
1877 .vidioc_g_input = vidioc_g_input,
1878 .vidioc_s_input = vidioc_s_input,
1879 .vidioc_queryctrl = vidioc_queryctrl,
1880 .vidioc_g_ctrl = vidioc_g_ctrl,
1881 .vidioc_s_ctrl = vidioc_s_ctrl,
1882 .vidioc_streamon = vidioc_streamon,
1883 .vidioc_streamoff = vidioc_streamoff,
1884 .vidioc_g_tuner = vidioc_g_tuner,
1885 .vidioc_s_tuner = vidioc_s_tuner,
1886 .vidioc_g_frequency = vidioc_g_frequency,
1887 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001888#ifdef CONFIG_VIDEO_ADV_DEBUG
1889 .vidioc_g_register = vidioc_g_register,
1890 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001891 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001892#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001893#ifdef CONFIG_VIDEO_V4L1_COMPAT
1894 .vidiocgmbuf = vidiocgmbuf,
1895#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001896};
1897
1898static const struct video_device em28xx_video_template = {
1899 .fops = &em28xx_v4l_fops,
1900 .release = video_device_release,
1901 .ioctl_ops = &video_ioctl_ops,
1902
1903 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001904
1905 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001906 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001907};
1908
Hans Verkuilbec43662008-12-30 06:58:20 -03001909static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001910 .owner = THIS_MODULE,
1911 .open = em28xx_v4l2_open,
1912 .release = em28xx_v4l2_close,
1913 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001914};
1915
1916static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001917 .vidioc_querycap = radio_querycap,
1918 .vidioc_g_tuner = radio_g_tuner,
1919 .vidioc_enum_input = radio_enum_input,
1920 .vidioc_g_audio = radio_g_audio,
1921 .vidioc_s_tuner = radio_s_tuner,
1922 .vidioc_s_audio = radio_s_audio,
1923 .vidioc_s_input = radio_s_input,
1924 .vidioc_queryctrl = radio_queryctrl,
1925 .vidioc_g_ctrl = vidioc_g_ctrl,
1926 .vidioc_s_ctrl = vidioc_s_ctrl,
1927 .vidioc_g_frequency = vidioc_g_frequency,
1928 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001929#ifdef CONFIG_VIDEO_ADV_DEBUG
1930 .vidioc_g_register = vidioc_g_register,
1931 .vidioc_s_register = vidioc_s_register,
1932#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001933};
1934
Hans Verkuila3998102008-07-21 02:57:38 -03001935static struct video_device em28xx_radio_template = {
1936 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001937 .fops = &radio_fops,
1938 .ioctl_ops = &radio_ioctl_ops,
1939 .minor = -1,
1940};
1941
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001942/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001943
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001944
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001945
Adrian Bunk532fe652008-01-28 22:10:48 -03001946static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001947 const struct video_device *template,
1948 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001949{
1950 struct video_device *vfd;
1951
1952 vfd = video_device_alloc();
1953 if (NULL == vfd)
1954 return NULL;
1955 *vfd = *template;
1956 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001957 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001958 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001959 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001960
1961 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1962 dev->name, type_name);
1963
1964 return vfd;
1965}
1966
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001967int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001968{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03001969 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001970 int ret;
1971
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001972 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1973 dev->name,
1974 (EM28XX_VERSION_CODE >> 16) & 0xff,
1975 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1976
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001977 /* set default norm */
1978 dev->norm = em28xx_video_template.current_norm;
1979 dev->width = norm_maxw(dev);
1980 dev->height = norm_maxh(dev);
1981 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1982 dev->hscale = 0;
1983 dev->vscale = 0;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001984 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001985
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001986 /* Analog specific initialization */
1987 dev->format = &format[0];
1988 video_mux(dev, dev->ctl_input);
1989
1990 /* Audio defaults */
1991 dev->mute = 1;
1992 dev->volume = 0x1f;
1993
1994 /* enable vbi capturing */
1995
1996/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001997 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
1998 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
1999 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002000 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2001
2002 em28xx_set_outfmt(dev);
2003 em28xx_colorlevels_set_default(dev);
2004 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002005
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002006 /* allocate and fill video video_device struct */
2007 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2008 if (!dev->vdev) {
2009 em28xx_errdev("cannot allocate video_device.\n");
2010 return -ENODEV;
2011 }
2012
2013 /* register v4l2 video video_device */
2014 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2015 video_nr[dev->devno]);
2016 if (ret) {
2017 em28xx_errdev("unable to register video device (error=%i).\n",
2018 ret);
2019 return ret;
2020 }
2021
2022 /* Allocate and fill vbi video_device struct */
2023 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2024
2025 /* register v4l2 vbi video_device */
2026 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2027 vbi_nr[dev->devno]);
2028 if (ret < 0) {
2029 em28xx_errdev("unable to register vbi device\n");
2030 return ret;
2031 }
2032
2033 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002034 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2035 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002036 if (!dev->radio_dev) {
2037 em28xx_errdev("cannot allocate video_device.\n");
2038 return -ENODEV;
2039 }
2040 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2041 radio_nr[dev->devno]);
2042 if (ret < 0) {
2043 em28xx_errdev("can't register radio device\n");
2044 return ret;
2045 }
2046 em28xx_info("Registered radio device as /dev/radio%d\n",
2047 dev->radio_dev->num);
2048 }
2049
2050 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2051 dev->vdev->num, dev->vbi_dev->num);
2052
2053 return 0;
2054}