blob: 416b691c33c1eafd382420fbc7dbb1af79ff64f3 [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));
189 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300190 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300191 if (lencopy <= 0)
192 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300193 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300194
195 remain -= lencopy;
196
197 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300198 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300199 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300200 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 lencopy = remain;
202 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300205 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 ((char *)startwrite + lencopy) -
208 ((char *)outp + buf->vb.size));
209 lencopy = remain = (char *)outp + buf->vb.size -
210 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300211 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300212 if (lencopy <= 0)
213 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300214
215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218 }
219
220 dma_q->pos += len;
221}
222
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300223static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 int packet, int status)
225{
226 char *errmsg = "Unknown";
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229 case -ENOENT:
230 errmsg = "unlinked synchronuously";
231 break;
232 case -ECONNRESET:
233 errmsg = "unlinked asynchronuously";
234 break;
235 case -ENOSR:
236 errmsg = "Buffer error (overrun)";
237 break;
238 case -EPIPE:
239 errmsg = "Stalled (device not responding)";
240 break;
241 case -EOVERFLOW:
242 errmsg = "Babble (bad cable?)";
243 break;
244 case -EPROTO:
245 errmsg = "Bit-stuff error (bad cable?)";
246 break;
247 case -EILSEQ:
248 errmsg = "CRC/Timeout (could be anything)";
249 break;
250 case -ETIME:
251 errmsg = "Device does not respond";
252 break;
253 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300254 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300255 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
256 } else {
257 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258 packet, status, errmsg);
259 }
260}
261
262/*
263 * video-buf generic routine to get the next available buffer
264 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300265static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266 struct em28xx_buffer **buf)
267{
268 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300269 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300271 if (list_empty(&dma_q->active)) {
272 em28xx_isocdbg("No active queue to serve\n");
273 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300274 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300275 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 }
277
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300278 /* Get the next buffer */
279 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300281 /* Cleans up buffer - Usefull for testing for frame/URB loss */
282 outp = videobuf_to_vmalloc(&(*buf)->vb);
283 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300284
285 dev->isoc_ctl.buf = *buf;
286
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300287 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300288}
289
290/*
291 * Controls the isoc copy of each urb packet
292 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300293static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300294{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300295 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300296 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300297 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300298 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300299 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300
301 if (!dev)
302 return 0;
303
304 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
305 return 0;
306
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300307 if (urb->status < 0) {
308 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300309 if (urb->status == -ENOENT)
310 return 0;
311 }
312
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300313 buf = dev->isoc_ctl.buf;
314 if (buf != NULL)
315 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300316
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300317 for (i = 0; i < urb->number_of_packets; i++) {
318 int status = urb->iso_frame_desc[i].status;
319
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300320 if (status < 0) {
321 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 if (urb->iso_frame_desc[i].status != -EPROTO)
323 continue;
324 }
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327
328 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300329 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330 continue;
331 }
332 if (urb->iso_frame_desc[i].actual_length >
333 dev->max_pkt_size) {
334 em28xx_isocdbg("packet bigger than packet size");
335 continue;
336 }
337
338 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300339
340 /* FIXME: incomplete buffer checks where removed to make
341 logic simpler. Impacts of those changes should be evaluated
342 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300343 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
344 em28xx_isocdbg("VBI HEADER!!!\n");
345 /* FIXME: Should add vbi copy */
346 continue;
347 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300348 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300349 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
350 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300351
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300352 if (!(p[2] & 1)) {
353 if (buf != NULL)
354 buffer_filled(dev, dma_q, buf);
355 get_next_buf(dma_q, &buf);
356 if (buf == NULL)
357 outp = NULL;
358 else
359 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300360 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300361
362 if (buf != NULL) {
363 if (p[2] & 1)
364 buf->top_field = 0;
365 else
366 buf->top_field = 1;
367 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300368
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300371 if (buf != NULL)
372 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300373 }
374 return rc;
375}
376
377/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300378 Videobuf operations
379 ------------------------------------------------------------------*/
380
381static int
382buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383{
384 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300385 struct em28xx *dev = fh->dev;
386 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300387
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300388 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
389
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300390 if (0 == *count)
391 *count = EM28XX_DEF_BUF;
392
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300395
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300396 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300397 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300398 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300399 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300400
401 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
402
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300403 return 0;
404}
405
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300406/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300407static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
408{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300409 struct em28xx_fh *fh = vq->priv_data;
410 struct em28xx *dev = fh->dev;
411 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300412 if (in_interrupt())
413 BUG();
414
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300415 /* We used to wait for the buffer to finish here, but this didn't work
416 because, as we were keeping the state as VIDEOBUF_QUEUED,
417 videobuf_queue_cancel marked it as finished for us.
418 (Also, it could wedge forever if the hardware was misconfigured.)
419
420 This should be safe; by the time we get here, the buffer isn't
421 queued anymore. If we ever start marking the buffers as
422 VIDEOBUF_ACTIVE, it won't be, though.
423 */
424 spin_lock_irqsave(&dev->slock, flags);
425 if (dev->isoc_ctl.buf == buf)
426 dev->isoc_ctl.buf = NULL;
427 spin_unlock_irqrestore(&dev->slock, flags);
428
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300429 videobuf_vmalloc_free(&buf->vb);
430 buf->vb.state = VIDEOBUF_NEEDS_INIT;
431}
432
433static int
434buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
435 enum v4l2_field field)
436{
437 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300438 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439 struct em28xx *dev = fh->dev;
440 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300442 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443
444 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
445 return -EINVAL;
446
Brandon Philips05612972008-04-13 14:57:01 -0300447 buf->vb.width = dev->width;
448 buf->vb.height = dev->height;
449 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450
451 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300452 rc = videobuf_iolock(vq, &buf->vb, NULL);
453 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 }
456
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459
460 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300461 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
462 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300463 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300464 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465 goto fail;
466 }
467
468 buf->vb.state = VIDEOBUF_PREPARED;
469 return 0;
470
471fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300472 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473 return rc;
474}
475
476static void
477buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
478{
479 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
480 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300481 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300482 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300484 buf->vb.state = VIDEOBUF_QUEUED;
485 list_add_tail(&buf->vb.queue, &vidq->active);
486
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300487}
488
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300489static void buffer_release(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300492 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300496 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300498 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300499}
500
501static struct videobuf_queue_ops em28xx_video_qops = {
502 .buf_setup = buffer_setup,
503 .buf_prepare = buffer_prepare,
504 .buf_queue = buffer_queue,
505 .buf_release = buffer_release,
506};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800507
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300508/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800510static void video_mux(struct em28xx *dev, int index)
511{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300512 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800513
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300514 route.input = INPUT(index)->vmux;
515 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800516 dev->ctl_input = index;
517 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300518 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800519
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300520 if (!dev->ctl_aoutput)
521 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
522
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300523 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800524
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300525 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300526 if (dev->i2s_speed) {
527 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
528 &dev->i2s_speed);
529 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300530 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300531 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300532 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300533 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
534 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800535 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300536
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300537 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800538}
539
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300540/* Usage lock check functions */
541static int res_get(struct em28xx_fh *fh)
542{
543 struct em28xx *dev = fh->dev;
544 int rc = 0;
545
546 /* This instance already has stream_on */
547 if (fh->stream_on)
548 return rc;
549
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300550 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300551 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300552
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300553 dev->stream_on = 1;
554 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300555 return rc;
556}
557
558static int res_check(struct em28xx_fh *fh)
559{
560 return (fh->stream_on);
561}
562
563static void res_free(struct em28xx_fh *fh)
564{
565 struct em28xx *dev = fh->dev;
566
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300567 fh->stream_on = 0;
568 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300569}
570
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800571/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300572 * em28xx_get_ctrl()
573 * return the current saturation, brightness or contrast, mute state
574 */
575static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
576{
577 switch (ctrl->id) {
578 case V4L2_CID_AUDIO_MUTE:
579 ctrl->value = dev->mute;
580 return 0;
581 case V4L2_CID_AUDIO_VOLUME:
582 ctrl->value = dev->volume;
583 return 0;
584 default:
585 return -EINVAL;
586 }
587}
588
589/*
590 * em28xx_set_ctrl()
591 * mute or set new saturation, brightness or contrast
592 */
593static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
594{
595 switch (ctrl->id) {
596 case V4L2_CID_AUDIO_MUTE:
597 if (ctrl->value != dev->mute) {
598 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300599 return em28xx_audio_analog_set(dev);
600 }
601 return 0;
602 case V4L2_CID_AUDIO_VOLUME:
603 dev->volume = ctrl->value;
604 return em28xx_audio_analog_set(dev);
605 default:
606 return -EINVAL;
607 }
608}
609
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300610static int check_dev(struct em28xx *dev)
611{
612 if (dev->state & DEV_DISCONNECTED) {
613 em28xx_errdev("v4l2 ioctl: device not present\n");
614 return -ENODEV;
615 }
616
617 if (dev->state & DEV_MISCONFIGURED) {
618 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
619 "close and open it again\n");
620 return -EIO;
621 }
622 return 0;
623}
624
625static void get_scale(struct em28xx *dev,
626 unsigned int width, unsigned int height,
627 unsigned int *hscale, unsigned int *vscale)
628{
629 unsigned int maxw = norm_maxw(dev);
630 unsigned int maxh = norm_maxh(dev);
631
632 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
633 if (*hscale >= 0x4000)
634 *hscale = 0x3fff;
635
636 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
637 if (*vscale >= 0x4000)
638 *vscale = 0x3fff;
639}
640
641/* ------------------------------------------------------------------
642 IOCTL vidioc handling
643 ------------------------------------------------------------------*/
644
Hans Verkuil78b526a2008-05-28 12:16:41 -0300645static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300646 struct v4l2_format *f)
647{
648 struct em28xx_fh *fh = priv;
649 struct em28xx *dev = fh->dev;
650
651 mutex_lock(&dev->lock);
652
653 f->fmt.pix.width = dev->width;
654 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300655 f->fmt.pix.pixelformat = dev->format->fourcc;
656 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300657 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300658 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
659
660 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
661 f->fmt.pix.field = dev->interlaced ?
662 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
663
664 mutex_unlock(&dev->lock);
665 return 0;
666}
667
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300668static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
669{
670 unsigned int i;
671
672 for (i = 0; i < ARRAY_SIZE(format); i++)
673 if (format[i].fourcc == fourcc)
674 return &format[i];
675
676 return NULL;
677}
678
Hans Verkuil78b526a2008-05-28 12:16:41 -0300679static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300680 struct v4l2_format *f)
681{
682 struct em28xx_fh *fh = priv;
683 struct em28xx *dev = fh->dev;
684 int width = f->fmt.pix.width;
685 int height = f->fmt.pix.height;
686 unsigned int maxw = norm_maxw(dev);
687 unsigned int maxh = norm_maxh(dev);
688 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300689 struct em28xx_fmt *fmt;
690
691 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
692 if (!fmt) {
693 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
694 f->fmt.pix.pixelformat);
695 return -EINVAL;
696 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300697
698 /* width must even because of the YUYV format
699 height must be even because of interlacing */
700 height &= 0xfffe;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300701 width &= 0xfffe;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300702
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300703 if (unlikely(height < 32))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300704 height = 32;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300705 if (unlikely(height > maxh))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300706 height = maxh;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300707 if (unlikely(width < 48))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300708 width = 48;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300709 if (unlikely(width > maxw))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710 width = maxw;
711
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300712 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300713 /* the em2800 can only scale down to 50% */
714 if (height % (maxh / 2))
715 height = maxh;
716 if (width % (maxw / 2))
717 width = maxw;
718 /* according to empiatech support */
719 /* the MaxPacketSize is to small to support */
720 /* framesizes larger than 640x480 @ 30 fps */
721 /* or 640x576 @ 25 fps. As this would cut */
722 /* of a part of the image we prefer */
723 /* 360x576 or 360x480 for now */
724 if (width == maxw && height == maxh)
725 width /= 2;
726 }
727
728 get_scale(dev, width, height, &hscale, &vscale);
729
730 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
731 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
732
733 f->fmt.pix.width = width;
734 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300735 f->fmt.pix.pixelformat = fmt->fourcc;
736 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
737 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300738 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
739 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
740
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300741 return 0;
742}
743
Hans Verkuil78b526a2008-05-28 12:16:41 -0300744static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300745 struct v4l2_format *f)
746{
747 struct em28xx_fh *fh = priv;
748 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300749 int rc;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300750 struct em28xx_fmt *fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300751
752 rc = check_dev(dev);
753 if (rc < 0)
754 return rc;
755
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300756 mutex_lock(&dev->lock);
757
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300758 vidioc_try_fmt_vid_cap(file, priv, f);
759
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300760 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -0300761 if (!fmt) {
762 rc = -EINVAL;
763 goto out;
764 }
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300765
Brandon Philips05612972008-04-13 14:57:01 -0300766 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
767 em28xx_errdev("%s queue busy\n", __func__);
768 rc = -EBUSY;
769 goto out;
770 }
771
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300772 if (dev->stream_on && !fh->stream_on) {
773 em28xx_errdev("%s device in use by another fh\n", __func__);
774 rc = -EBUSY;
775 goto out;
776 }
777
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300778 /* set new image size */
779 dev->width = f->fmt.pix.width;
780 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300781 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300782 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
783
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300784 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300785 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300786
Brandon Philips05612972008-04-13 14:57:01 -0300787 rc = 0;
788
789out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300790 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300791 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300792}
793
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300794static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795{
796 struct em28xx_fh *fh = priv;
797 struct em28xx *dev = fh->dev;
798 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799 int rc;
800
801 rc = check_dev(dev);
802 if (rc < 0)
803 return rc;
804
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300806 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300807
808 /* Adjusts width/height, if needed */
809 f.fmt.pix.width = dev->width;
810 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300811 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813 /* set new image size */
814 dev->width = f.fmt.pix.width;
815 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
817
818 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300819 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300820
821 mutex_unlock(&dev->lock);
822 return 0;
823}
824
825static const char *iname[] = {
826 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
827 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
828 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
829 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
830 [EM28XX_VMUX_SVIDEO] = "S-Video",
831 [EM28XX_VMUX_TELEVISION] = "Television",
832 [EM28XX_VMUX_CABLE] = "Cable TV",
833 [EM28XX_VMUX_DVB] = "DVB",
834 [EM28XX_VMUX_DEBUG] = "for debug only",
835};
836
837static int vidioc_enum_input(struct file *file, void *priv,
838 struct v4l2_input *i)
839{
840 struct em28xx_fh *fh = priv;
841 struct em28xx *dev = fh->dev;
842 unsigned int n;
843
844 n = i->index;
845 if (n >= MAX_EM28XX_INPUT)
846 return -EINVAL;
847 if (0 == INPUT(n)->type)
848 return -EINVAL;
849
850 i->index = n;
851 i->type = V4L2_INPUT_TYPE_CAMERA;
852
853 strcpy(i->name, iname[INPUT(n)->type]);
854
855 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
856 (EM28XX_VMUX_CABLE == INPUT(n)->type))
857 i->type = V4L2_INPUT_TYPE_TUNER;
858
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300859 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300860
861 return 0;
862}
863
864static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
865{
866 struct em28xx_fh *fh = priv;
867 struct em28xx *dev = fh->dev;
868
869 *i = dev->ctl_input;
870
871 return 0;
872}
873
874static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
875{
876 struct em28xx_fh *fh = priv;
877 struct em28xx *dev = fh->dev;
878 int rc;
879
880 rc = check_dev(dev);
881 if (rc < 0)
882 return rc;
883
884 if (i >= MAX_EM28XX_INPUT)
885 return -EINVAL;
886 if (0 == INPUT(i)->type)
887 return -EINVAL;
888
889 mutex_lock(&dev->lock);
890
891 video_mux(dev, i);
892
893 mutex_unlock(&dev->lock);
894 return 0;
895}
896
897static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
898{
899 struct em28xx_fh *fh = priv;
900 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300901
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300902 switch (a->index) {
903 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300904 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300905 break;
906 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300907 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300908 break;
909 case EM28XX_AMUX_VIDEO2:
910 strcpy(a->name, "Television alt");
911 break;
912 case EM28XX_AMUX_PHONE:
913 strcpy(a->name, "Phone");
914 break;
915 case EM28XX_AMUX_MIC:
916 strcpy(a->name, "Mic");
917 break;
918 case EM28XX_AMUX_CD:
919 strcpy(a->name, "CD");
920 break;
921 case EM28XX_AMUX_AUX:
922 strcpy(a->name, "Aux");
923 break;
924 case EM28XX_AMUX_PCM_OUT:
925 strcpy(a->name, "PCM");
926 break;
927 default:
928 return -EINVAL;
929 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300930
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300931 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933
934 return 0;
935}
936
937static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
938{
939 struct em28xx_fh *fh = priv;
940 struct em28xx *dev = fh->dev;
941
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300942 mutex_lock(&dev->lock);
943
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300944 dev->ctl_ainput = INPUT(a->index)->amux;
945 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300946
947 if (!dev->ctl_aoutput)
948 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300949
950 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300951 return 0;
952}
953
954static int vidioc_queryctrl(struct file *file, void *priv,
955 struct v4l2_queryctrl *qc)
956{
957 struct em28xx_fh *fh = priv;
958 struct em28xx *dev = fh->dev;
959 int id = qc->id;
960 int i;
961 int rc;
962
963 rc = check_dev(dev);
964 if (rc < 0)
965 return rc;
966
967 memset(qc, 0, sizeof(*qc));
968
969 qc->id = id;
970
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300971 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300972 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
973 if (qc->id && qc->id == em28xx_qctrl[i].id) {
974 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
975 return 0;
976 }
977 }
978 }
979 mutex_lock(&dev->lock);
980 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
981 mutex_unlock(&dev->lock);
982
983 if (qc->type)
984 return 0;
985 else
986 return -EINVAL;
987}
988
989static int vidioc_g_ctrl(struct file *file, void *priv,
990 struct v4l2_control *ctrl)
991{
992 struct em28xx_fh *fh = priv;
993 struct em28xx *dev = fh->dev;
994 int rc;
995
996 rc = check_dev(dev);
997 if (rc < 0)
998 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -0300999 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001000
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001001 mutex_lock(&dev->lock);
1002
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001003 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001005 else
1006 rc = em28xx_get_ctrl(dev, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001007
1008 mutex_unlock(&dev->lock);
1009 return rc;
1010}
1011
1012static int vidioc_s_ctrl(struct file *file, void *priv,
1013 struct v4l2_control *ctrl)
1014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 u8 i;
1018 int rc;
1019
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
1023
1024 mutex_lock(&dev->lock);
1025
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001026 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001027 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1028 else {
1029 rc = 1;
1030 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1031 if (ctrl->id == em28xx_qctrl[i].id) {
1032 if (ctrl->value < em28xx_qctrl[i].minimum ||
1033 ctrl->value > em28xx_qctrl[i].maximum) {
1034 rc = -ERANGE;
1035 break;
1036 }
1037
1038 rc = em28xx_set_ctrl(dev, ctrl);
1039 break;
1040 }
1041 }
1042 }
1043
1044 /* Control not found - try to send it to the attached devices */
1045 if (rc == 1) {
1046 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1047 rc = 0;
1048 }
1049
1050 mutex_unlock(&dev->lock);
1051 return rc;
1052}
1053
1054static int vidioc_g_tuner(struct file *file, void *priv,
1055 struct v4l2_tuner *t)
1056{
1057 struct em28xx_fh *fh = priv;
1058 struct em28xx *dev = fh->dev;
1059 int rc;
1060
1061 rc = check_dev(dev);
1062 if (rc < 0)
1063 return rc;
1064
1065 if (0 != t->index)
1066 return -EINVAL;
1067
1068 strcpy(t->name, "Tuner");
1069
1070 mutex_lock(&dev->lock);
1071
1072 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1073
1074 mutex_unlock(&dev->lock);
1075 return 0;
1076}
1077
1078static int vidioc_s_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 mutex_lock(&dev->lock);
1093
1094 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1095
1096 mutex_unlock(&dev->lock);
1097 return 0;
1098}
1099
1100static int vidioc_g_frequency(struct file *file, void *priv,
1101 struct v4l2_frequency *f)
1102{
1103 struct em28xx_fh *fh = priv;
1104 struct em28xx *dev = fh->dev;
1105
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001106 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001107 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001108 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001109 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001110
1111 return 0;
1112}
1113
1114static int vidioc_s_frequency(struct file *file, void *priv,
1115 struct v4l2_frequency *f)
1116{
1117 struct em28xx_fh *fh = priv;
1118 struct em28xx *dev = fh->dev;
1119 int rc;
1120
1121 rc = check_dev(dev);
1122 if (rc < 0)
1123 return rc;
1124
1125 if (0 != f->tuner)
1126 return -EINVAL;
1127
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001128 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1129 return -EINVAL;
1130 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001131 return -EINVAL;
1132
1133 mutex_lock(&dev->lock);
1134
1135 dev->ctl_freq = f->frequency;
1136 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1137
1138 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001139
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001140 return 0;
1141}
1142
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001143#ifdef CONFIG_VIDEO_ADV_DEBUG
1144static int em28xx_reg_len(int reg)
1145{
1146 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001147 case EM28XX_R40_AC97LSB:
1148 case EM28XX_R30_HSCALELOW:
1149 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001150 return 2;
1151 default:
1152 return 1;
1153 }
1154}
1155
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001156static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001157 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001158{
1159 struct em28xx_fh *fh = priv;
1160 struct em28xx *dev = fh->dev;
1161
1162 chip->ident = V4L2_IDENT_NONE;
1163 chip->revision = 0;
1164
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001165 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001166
1167 return 0;
1168}
1169
1170
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001171static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001172 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001173{
1174 struct em28xx_fh *fh = priv;
1175 struct em28xx *dev = fh->dev;
1176 int ret;
1177
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001178 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001179 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001180 mutex_lock(&dev->lock);
1181 ret = em28xx_read_ac97(dev, reg->reg);
1182 mutex_unlock(&dev->lock);
1183 if (ret < 0)
1184 return ret;
1185
1186 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001187 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001188 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001189 case V4L2_CHIP_MATCH_I2C_DRIVER:
1190 em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
1191 return 0;
1192 case V4L2_CHIP_MATCH_I2C_ADDR:
1193 /* Not supported yet */
1194 return -EINVAL;
1195 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001196 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001197 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001198 }
1199
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001200 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001201 reg->size = em28xx_reg_len(reg->reg);
1202 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001203 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001204 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001205 mutex_unlock(&dev->lock);
1206
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001207 if (ret < 0)
1208 return ret;
1209
1210 reg->val = ret;
1211 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001212 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001213 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001214 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1215 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001216 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001217 if (ret < 0)
1218 return ret;
1219
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001220 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001221 }
1222
1223 return 0;
1224}
1225
1226static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001227 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001228{
1229 struct em28xx_fh *fh = priv;
1230 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001231 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001232 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001233
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001234 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001235 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001236 mutex_lock(&dev->lock);
1237 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1238 mutex_unlock(&dev->lock);
1239
1240 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001241 case V4L2_CHIP_MATCH_I2C_DRIVER:
1242 em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
1243 return 0;
1244 case V4L2_CHIP_MATCH_I2C_ADDR:
1245 /* Not supported yet */
1246 return -EINVAL;
1247 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001248 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001249 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001250 }
1251
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001252 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001253 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001254
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001255 mutex_lock(&dev->lock);
1256 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1257 em28xx_reg_len(reg->reg));
1258 mutex_unlock(&dev->lock);
1259
1260 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001261}
1262#endif
1263
1264
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001265static int vidioc_cropcap(struct file *file, void *priv,
1266 struct v4l2_cropcap *cc)
1267{
1268 struct em28xx_fh *fh = priv;
1269 struct em28xx *dev = fh->dev;
1270
1271 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1272 return -EINVAL;
1273
1274 cc->bounds.left = 0;
1275 cc->bounds.top = 0;
1276 cc->bounds.width = dev->width;
1277 cc->bounds.height = dev->height;
1278 cc->defrect = cc->bounds;
1279 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1280 cc->pixelaspect.denominator = 59;
1281
1282 return 0;
1283}
1284
1285static int vidioc_streamon(struct file *file, void *priv,
1286 enum v4l2_buf_type type)
1287{
1288 struct em28xx_fh *fh = priv;
1289 struct em28xx *dev = fh->dev;
1290 int rc;
1291
1292 rc = check_dev(dev);
1293 if (rc < 0)
1294 return rc;
1295
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001297 mutex_lock(&dev->lock);
1298 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001299
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001300 if (likely(rc >= 0))
1301 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001302
1303 mutex_unlock(&dev->lock);
1304
1305 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306}
1307
1308static int vidioc_streamoff(struct file *file, void *priv,
1309 enum v4l2_buf_type type)
1310{
1311 struct em28xx_fh *fh = priv;
1312 struct em28xx *dev = fh->dev;
1313 int rc;
1314
1315 rc = check_dev(dev);
1316 if (rc < 0)
1317 return rc;
1318
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001319 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1320 return -EINVAL;
1321 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001322 return -EINVAL;
1323
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001324 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001325
1326 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001327 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001328
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001329 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331 return 0;
1332}
1333
1334static int vidioc_querycap(struct file *file, void *priv,
1335 struct v4l2_capability *cap)
1336{
1337 struct em28xx_fh *fh = priv;
1338 struct em28xx *dev = fh->dev;
1339
1340 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1341 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001342 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001343
1344 cap->version = EM28XX_VERSION_CODE;
1345
1346 cap->capabilities =
1347 V4L2_CAP_SLICED_VBI_CAPTURE |
1348 V4L2_CAP_VIDEO_CAPTURE |
1349 V4L2_CAP_AUDIO |
1350 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1351
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001352 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001353 cap->capabilities |= V4L2_CAP_TUNER;
1354
1355 return 0;
1356}
1357
Hans Verkuil78b526a2008-05-28 12:16:41 -03001358static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001359 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001360{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001361 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001362 return -EINVAL;
1363
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001364 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1365 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001366
1367 return 0;
1368}
1369
1370/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001371static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372 struct v4l2_format *f)
1373{
1374 struct em28xx_fh *fh = priv;
1375 struct em28xx *dev = fh->dev;
1376 int rc;
1377
1378 rc = check_dev(dev);
1379 if (rc < 0)
1380 return rc;
1381
1382 mutex_lock(&dev->lock);
1383
1384 f->fmt.sliced.service_set = 0;
1385
1386 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1387
1388 if (f->fmt.sliced.service_set == 0)
1389 rc = -EINVAL;
1390
1391 mutex_unlock(&dev->lock);
1392 return rc;
1393}
1394
Hans Verkuil78b526a2008-05-28 12:16:41 -03001395static int vidioc_try_set_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 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1408 mutex_unlock(&dev->lock);
1409
1410 if (f->fmt.sliced.service_set == 0)
1411 return -EINVAL;
1412
1413 return 0;
1414}
1415
1416
1417static int vidioc_reqbufs(struct file *file, void *priv,
1418 struct v4l2_requestbuffers *rb)
1419{
1420 struct em28xx_fh *fh = priv;
1421 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001422 int rc;
1423
1424 rc = check_dev(dev);
1425 if (rc < 0)
1426 return rc;
1427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001428 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001429}
1430
1431static int vidioc_querybuf(struct file *file, void *priv,
1432 struct v4l2_buffer *b)
1433{
1434 struct em28xx_fh *fh = priv;
1435 struct em28xx *dev = fh->dev;
1436 int rc;
1437
1438 rc = check_dev(dev);
1439 if (rc < 0)
1440 return rc;
1441
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001442 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443}
1444
1445static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1446{
1447 struct em28xx_fh *fh = priv;
1448 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001449 int rc;
1450
1451 rc = check_dev(dev);
1452 if (rc < 0)
1453 return rc;
1454
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001455 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001456}
1457
1458static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1459{
1460 struct em28xx_fh *fh = priv;
1461 struct em28xx *dev = fh->dev;
1462 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001463
1464 rc = check_dev(dev);
1465 if (rc < 0)
1466 return rc;
1467
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001468 return (videobuf_dqbuf(&fh->vb_vidq, b,
1469 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001470}
1471
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001472#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001473static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001474{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001475 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001476
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001477 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001478}
1479#endif
1480
1481
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001482/* ----------------------------------------------------------- */
1483/* RADIO ESPECIFIC IOCTLS */
1484/* ----------------------------------------------------------- */
1485
1486static int radio_querycap(struct file *file, void *priv,
1487 struct v4l2_capability *cap)
1488{
1489 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1490
1491 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1492 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001493 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001494
1495 cap->version = EM28XX_VERSION_CODE;
1496 cap->capabilities = V4L2_CAP_TUNER;
1497 return 0;
1498}
1499
1500static int radio_g_tuner(struct file *file, void *priv,
1501 struct v4l2_tuner *t)
1502{
1503 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1504
1505 if (unlikely(t->index > 0))
1506 return -EINVAL;
1507
1508 strcpy(t->name, "Radio");
1509 t->type = V4L2_TUNER_RADIO;
1510
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001511 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001512 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001513 mutex_unlock(&dev->lock);
1514
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001515 return 0;
1516}
1517
1518static int radio_enum_input(struct file *file, void *priv,
1519 struct v4l2_input *i)
1520{
1521 if (i->index != 0)
1522 return -EINVAL;
1523 strcpy(i->name, "Radio");
1524 i->type = V4L2_INPUT_TYPE_TUNER;
1525
1526 return 0;
1527}
1528
1529static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1530{
1531 if (unlikely(a->index))
1532 return -EINVAL;
1533
1534 strcpy(a->name, "Radio");
1535 return 0;
1536}
1537
1538static int radio_s_tuner(struct file *file, void *priv,
1539 struct v4l2_tuner *t)
1540{
1541 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1542
1543 if (0 != t->index)
1544 return -EINVAL;
1545
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001546 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001547 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001548 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001549
1550 return 0;
1551}
1552
1553static int radio_s_audio(struct file *file, void *fh,
1554 struct v4l2_audio *a)
1555{
1556 return 0;
1557}
1558
1559static int radio_s_input(struct file *file, void *fh, unsigned int i)
1560{
1561 return 0;
1562}
1563
1564static int radio_queryctrl(struct file *file, void *priv,
1565 struct v4l2_queryctrl *qc)
1566{
1567 int i;
1568
1569 if (qc->id < V4L2_CID_BASE ||
1570 qc->id >= V4L2_CID_LASTP1)
1571 return -EINVAL;
1572
1573 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1574 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1575 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1576 return 0;
1577 }
1578 }
1579
1580 return -EINVAL;
1581}
1582
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001583/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001584 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001585 * inits the device and starts isoc transfer
1586 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001587static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001588{
Hans Verkuilbec43662008-12-30 06:58:20 -03001589 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001590 int errCode = 0, radio;
1591 struct em28xx *dev;
1592 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001593 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001594
Hans Verkuilbec43662008-12-30 06:58:20 -03001595 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001596
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001597 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001598 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001599
1600 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001601
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001602 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001603 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001604
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001605
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001606 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001607 if (!fh) {
1608 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001609 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001610 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001611 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001612 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001613 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001614 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001615 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001616
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001617 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001618 dev->width = norm_maxw(dev);
1619 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001620 dev->hscale = 0;
1621 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001623 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001624 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001626
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001627 /* Needed, since GPIO might have disabled power of
1628 some i2c device
1629 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001630 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001631
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001632 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001633 if (fh->radio) {
1634 em28xx_videodbg("video_open: setting radio device\n");
1635 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1636 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001637
1638 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001639
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001640 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1641 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1642 sizeof(struct em28xx_buffer), fh);
1643
Ingo Molnar3593cab2006-02-07 06:49:14 -02001644 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001645
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646 return errCode;
1647}
1648
1649/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001650 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001651 * unregisters the v4l2,i2c and usb devices
1652 * called when the device gets disconected or at module unload
1653*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001654void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001655{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001656
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001657 /*FIXME: I2C IR should be disconnected */
1658
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001659 if (dev->radio_dev) {
1660 if (-1 != dev->radio_dev->minor)
1661 video_unregister_device(dev->radio_dev);
1662 else
1663 video_device_release(dev->radio_dev);
1664 dev->radio_dev = NULL;
1665 }
1666 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001667 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1668 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001669 if (-1 != dev->vbi_dev->minor)
1670 video_unregister_device(dev->vbi_dev);
1671 else
1672 video_device_release(dev->vbi_dev);
1673 dev->vbi_dev = NULL;
1674 }
1675 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001676 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1677 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001678 if (-1 != dev->vdev->minor)
1679 video_unregister_device(dev->vdev);
1680 else
1681 video_device_release(dev->vdev);
1682 dev->vdev = NULL;
1683 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001684}
1685
1686/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001687 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001688 * stops streaming and deallocates all resources allocated by the v4l2
1689 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001691static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001692{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001693 struct em28xx_fh *fh = filp->private_data;
1694 struct em28xx *dev = fh->dev;
1695 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001697 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001698
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001699
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001700 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001701 if (res_check(fh))
1702 res_free(fh);
1703
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001704 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001705 videobuf_stop(&fh->vb_vidq);
1706 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001707
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001708 /* the device is already disconnect,
1709 free the remaining resources */
1710 if (dev->state & DEV_DISCONNECTED) {
1711 em28xx_release_resources(dev);
1712 mutex_unlock(&dev->lock);
1713 kfree(dev);
1714 return 0;
1715 }
1716
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001717 /* Save some power by putting tuner to sleep */
1718 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1719
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001720 /* do this before setting alternate! */
1721 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001722 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001723
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001724 /* set alternate 0 */
1725 dev->alt = 0;
1726 em28xx_videodbg("setting alternate 0\n");
1727 errCode = usb_set_interface(dev->udev, 0, 0);
1728 if (errCode < 0) {
1729 em28xx_errdev("cannot change alternate number to "
1730 "0 (error=%i)\n", errCode);
1731 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001733 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734 dev->users--;
1735 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001736 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737 return 0;
1738}
1739
1740/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001741 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742 * will allocate buffers when called for the first time
1743 */
1744static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001745em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001746 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001748 struct em28xx_fh *fh = filp->private_data;
1749 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001750 int rc;
1751
1752 rc = check_dev(dev);
1753 if (rc < 0)
1754 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001756 /* FIXME: read() is not prepared to allow changing the video
1757 resolution while streaming. Seems a bug at em28xx_set_fmt
1758 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001759
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001760 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001761 mutex_lock(&dev->lock);
1762 rc = res_get(fh);
1763 mutex_unlock(&dev->lock);
1764
1765 if (unlikely(rc < 0))
1766 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001767
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001768 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1769 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001770 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001771 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772}
1773
1774/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001775 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001776 * will allocate buffers when called for the first time
1777 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001778static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001779{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001780 struct em28xx_fh *fh = filp->private_data;
1781 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001782 int rc;
1783
1784 rc = check_dev(dev);
1785 if (rc < 0)
1786 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001787
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001788 mutex_lock(&dev->lock);
1789 rc = res_get(fh);
1790 mutex_unlock(&dev->lock);
1791
1792 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001793 return POLLERR;
1794
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001795 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1796 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001797
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001798 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001799}
1800
1801/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001802 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001804static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001805{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001806 struct em28xx_fh *fh = filp->private_data;
1807 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001808 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001809
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001810 rc = check_dev(dev);
1811 if (rc < 0)
1812 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001813
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001814 mutex_lock(&dev->lock);
1815 rc = res_get(fh);
1816 mutex_unlock(&dev->lock);
1817
1818 if (unlikely(rc < 0))
1819 return rc;
1820
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001821 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001823 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1824 (unsigned long)vma->vm_start,
1825 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1826 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001827
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001828 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001829}
1830
Hans Verkuilbec43662008-12-30 06:58:20 -03001831static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001832 .owner = THIS_MODULE,
1833 .open = em28xx_v4l2_open,
1834 .release = em28xx_v4l2_close,
1835 .read = em28xx_v4l2_read,
1836 .poll = em28xx_v4l2_poll,
1837 .mmap = em28xx_v4l2_mmap,
1838 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001839};
1840
Hans Verkuila3998102008-07-21 02:57:38 -03001841static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001842 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001843 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1844 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1845 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1846 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001847 .vidioc_g_audio = vidioc_g_audio,
1848 .vidioc_s_audio = vidioc_s_audio,
1849 .vidioc_cropcap = vidioc_cropcap,
1850
Hans Verkuil78b526a2008-05-28 12:16:41 -03001851 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1852 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1853 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001854
1855 .vidioc_reqbufs = vidioc_reqbufs,
1856 .vidioc_querybuf = vidioc_querybuf,
1857 .vidioc_qbuf = vidioc_qbuf,
1858 .vidioc_dqbuf = vidioc_dqbuf,
1859 .vidioc_s_std = vidioc_s_std,
1860 .vidioc_enum_input = vidioc_enum_input,
1861 .vidioc_g_input = vidioc_g_input,
1862 .vidioc_s_input = vidioc_s_input,
1863 .vidioc_queryctrl = vidioc_queryctrl,
1864 .vidioc_g_ctrl = vidioc_g_ctrl,
1865 .vidioc_s_ctrl = vidioc_s_ctrl,
1866 .vidioc_streamon = vidioc_streamon,
1867 .vidioc_streamoff = vidioc_streamoff,
1868 .vidioc_g_tuner = vidioc_g_tuner,
1869 .vidioc_s_tuner = vidioc_s_tuner,
1870 .vidioc_g_frequency = vidioc_g_frequency,
1871 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001872#ifdef CONFIG_VIDEO_ADV_DEBUG
1873 .vidioc_g_register = vidioc_g_register,
1874 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001875 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001876#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001877#ifdef CONFIG_VIDEO_V4L1_COMPAT
1878 .vidiocgmbuf = vidiocgmbuf,
1879#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001880};
1881
1882static const struct video_device em28xx_video_template = {
1883 .fops = &em28xx_v4l_fops,
1884 .release = video_device_release,
1885 .ioctl_ops = &video_ioctl_ops,
1886
1887 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001888
1889 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001890 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001891};
1892
Hans Verkuilbec43662008-12-30 06:58:20 -03001893static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03001894 .owner = THIS_MODULE,
1895 .open = em28xx_v4l2_open,
1896 .release = em28xx_v4l2_close,
1897 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03001898};
1899
1900static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001901 .vidioc_querycap = radio_querycap,
1902 .vidioc_g_tuner = radio_g_tuner,
1903 .vidioc_enum_input = radio_enum_input,
1904 .vidioc_g_audio = radio_g_audio,
1905 .vidioc_s_tuner = radio_s_tuner,
1906 .vidioc_s_audio = radio_s_audio,
1907 .vidioc_s_input = radio_s_input,
1908 .vidioc_queryctrl = radio_queryctrl,
1909 .vidioc_g_ctrl = vidioc_g_ctrl,
1910 .vidioc_s_ctrl = vidioc_s_ctrl,
1911 .vidioc_g_frequency = vidioc_g_frequency,
1912 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001913#ifdef CONFIG_VIDEO_ADV_DEBUG
1914 .vidioc_g_register = vidioc_g_register,
1915 .vidioc_s_register = vidioc_s_register,
1916#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001917};
1918
Hans Verkuila3998102008-07-21 02:57:38 -03001919static struct video_device em28xx_radio_template = {
1920 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001921 .fops = &radio_fops,
1922 .ioctl_ops = &radio_ioctl_ops,
1923 .minor = -1,
1924};
1925
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001926/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001928
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001929
Adrian Bunk532fe652008-01-28 22:10:48 -03001930static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1931 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001932 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001933{
1934 struct video_device *vfd;
1935
1936 vfd = video_device_alloc();
1937 if (NULL == vfd)
1938 return NULL;
1939 *vfd = *template;
1940 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001941 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001942 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001943 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001944
1945 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1946 dev->name, type_name);
1947
1948 return vfd;
1949}
1950
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001951int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001952{
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03001953 int ret;
1954
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03001955 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
1956 dev->name,
1957 (EM28XX_VERSION_CODE >> 16) & 0xff,
1958 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
1959
1960 /* Analog specific initialization */
1961 dev->format = &format[0];
1962 video_mux(dev, 0);
1963
1964 /* enable vbi capturing */
1965
1966/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
1967/* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
1968 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
1969
1970 dev->mute = 1; /* maybe not the right place... */
1971 dev->volume = 0x1f;
1972
1973 em28xx_set_outfmt(dev);
1974 em28xx_colorlevels_set_default(dev);
1975 em28xx_compression_disable(dev);
1976
1977 /* 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;
1984
1985 /* FIXME: This is a very bad hack! Not all devices have TV on input 2 */
1986 dev->ctl_input = 2;
1987
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001988 /* allocate and fill video video_device struct */
1989 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1990 if (!dev->vdev) {
1991 em28xx_errdev("cannot allocate video_device.\n");
1992 return -ENODEV;
1993 }
1994
1995 /* register v4l2 video video_device */
1996 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1997 video_nr[dev->devno]);
1998 if (ret) {
1999 em28xx_errdev("unable to register video device (error=%i).\n",
2000 ret);
2001 return ret;
2002 }
2003
2004 /* Allocate and fill vbi video_device struct */
2005 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2006
2007 /* register v4l2 vbi video_device */
2008 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2009 vbi_nr[dev->devno]);
2010 if (ret < 0) {
2011 em28xx_errdev("unable to register vbi device\n");
2012 return ret;
2013 }
2014
2015 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2016 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2017 if (!dev->radio_dev) {
2018 em28xx_errdev("cannot allocate video_device.\n");
2019 return -ENODEV;
2020 }
2021 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2022 radio_nr[dev->devno]);
2023 if (ret < 0) {
2024 em28xx_errdev("can't register radio device\n");
2025 return ret;
2026 }
2027 em28xx_info("Registered radio device as /dev/radio%d\n",
2028 dev->radio_dev->num);
2029 }
2030
2031 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2032 dev->vdev->num, dev->vbi_dev->num);
2033
2034 return 0;
2035}