blob: 90aedaeb09b9f4ebc5af9dfb2d3a3ee015542cd2 [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>
Hans Verkuil2474ed42006-03-19 12:35:57 -030042#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030043#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080045#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030047 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050#define DRIVER_NAME "em28xx"
51#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
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 Chehab3acf2802005-11-08 21:38:27 -080075static LIST_HEAD(em28xx_devlist);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -030076static DEFINE_MUTEX(em28xx_devlist_mutex);
Markus Rechberger9c755412005-11-08 21:37:52 -080077
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080078static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030080static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080083module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020084module_param_array(video_nr, int, NULL, 0444);
85module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030086module_param_array(radio_nr, int, NULL, 0444);
87MODULE_PARM_DESC(card, "card type");
88MODULE_PARM_DESC(video_nr, "video device numbers");
89MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080091
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030092static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030093module_param(video_debug, int, 0644);
94MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080095
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020096/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97static unsigned long em28xx_devused;
98
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080099/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200100/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800101static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800102 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Volume",
106 .minimum = 0x0,
107 .maximum = 0x1f,
108 .step = 0x1,
109 .default_value = 0x1f,
110 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300111 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Mute",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 .flags = 0,
120 }
121};
122
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800123static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800124
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300125/* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
128
129/*
130 * Announces that a buffer were filled and request the next
131 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300132static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
135{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300136 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
141
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300142 dev->isoc_ctl.buf = NULL;
143
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
146}
147
148/*
149 * Identify the buffer header type and properly handles
150 */
151static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
154 unsigned char *p,
155 unsigned char *outp, unsigned long len)
156{
157 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300159 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
163
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300164 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300165 em28xx_isocdbg("frame is not complete\n");
166 len += 4;
167 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300168 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169
170 startread = p;
171 remain = len;
172
173 /* Interlaces frame */
174 if (buf->top_field)
175 fieldstart = outp;
176 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300177 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300183 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184 lencopy = lencopy > remain ? remain : lencopy;
185
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
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
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
211 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 if (lencopy <= 0)
214 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215
216 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300217
218 remain -= lencopy;
219 }
220
221 dma_q->pos += len;
222}
223
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300224static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 int packet, int status)
226{
227 char *errmsg = "Unknown";
228
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 case -ENOENT:
231 errmsg = "unlinked synchronuously";
232 break;
233 case -ECONNRESET:
234 errmsg = "unlinked asynchronuously";
235 break;
236 case -ENOSR:
237 errmsg = "Buffer error (overrun)";
238 break;
239 case -EPIPE:
240 errmsg = "Stalled (device not responding)";
241 break;
242 case -EOVERFLOW:
243 errmsg = "Babble (bad cable?)";
244 break;
245 case -EPROTO:
246 errmsg = "Bit-stuff error (bad cable?)";
247 break;
248 case -EILSEQ:
249 errmsg = "CRC/Timeout (could be anything)";
250 break;
251 case -ETIME:
252 errmsg = "Device does not respond";
253 break;
254 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300255 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 } else {
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
260 }
261}
262
263/*
264 * video-buf generic routine to get the next available buffer
265 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300266static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300267 struct em28xx_buffer **buf)
268{
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300270 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300271
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300275 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300276 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300277 }
278
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
281
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300285
286 dev->isoc_ctl.buf = *buf;
287
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300288 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300289}
290
291/*
292 * Controls the isoc copy of each urb packet
293 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300294static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300296 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300298 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300300 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301
302 if (!dev)
303 return 0;
304
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 return 0;
307
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300310 if (urb->status == -ENOENT)
311 return 0;
312 }
313
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300314 buf = dev->isoc_ctl.buf;
315 if (buf != NULL)
316 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300317
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321 if (status < 0) {
322 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
325 }
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300331 continue;
332 }
333 if (urb->iso_frame_desc[i].actual_length >
334 dev->max_pkt_size) {
335 em28xx_isocdbg("packet bigger than packet size");
336 continue;
337 }
338
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
343 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
347 continue;
348 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300349 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300352
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300353 if (!(p[2] & 1)) {
354 if (buf != NULL)
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
357 if (buf == NULL)
358 outp = NULL;
359 else
360 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300361 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300362
363 if (buf != NULL) {
364 if (p[2] & 1)
365 buf->top_field = 0;
366 else
367 buf->top_field = 1;
368 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300369
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300372 if (buf != NULL)
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300374 }
375 return rc;
376}
377
378/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300379 Videobuf operations
380 ------------------------------------------------------------------*/
381
382static int
383buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
384{
385 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300388
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
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 Chehabd2d9fbf2008-04-17 21:38:53 -0300396 /* Ask tuner to go to analog 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;
399
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402 return 0;
403}
404
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300405/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 if (in_interrupt())
412 BUG();
413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
418
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
422 */
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
430}
431
432static int
433buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
435{
436 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
447
Brandon Philips05612972008-04-13 14:57:01 -0300448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 }
457
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300459 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460
461 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300464 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 goto fail;
467 }
468
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
471
472fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300473 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300474 return rc;
475}
476
477static void
478buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479{
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300482 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
487
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488}
489
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300490static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300495 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300497 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300499 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500}
501
502static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
507};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300509/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800512 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800513 * inits registers with sane defaults
514 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800515static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300517 int retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800518
519 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300520 if (!dev->board.is_em2800) {
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300521 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300522 if (retval < 0) {
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
524 __func__, retval);
525 return retval;
526 }
527 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528
529 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200530
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300531/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
532/* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
533 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200534
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800535 dev->mute = 1; /* maybe not the right place... */
536 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300537
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800541
542 return 0;
543}
544
545/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800546 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800547 * configure i2c attached devices
548 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800549static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300551 struct v4l2_routing route;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300552 int zero = 0;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300553
554 route.input = INPUT(dev->ctl_input)->vmux;
555 route.output = 0;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300556 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300558 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800559}
560
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800561static void video_mux(struct em28xx *dev, int index)
562{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300563 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800564
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300565 route.input = INPUT(index)->vmux;
566 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800567 dev->ctl_input = index;
568 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300569 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800570
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300571 if (!dev->ctl_aoutput)
572 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
573
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300574 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800575
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300576 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300577 if (dev->i2s_speed) {
578 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
579 &dev->i2s_speed);
580 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300581 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300582 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300583 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300584 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
585 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800586 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300587
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300588 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800589}
590
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300591/* Usage lock check functions */
592static int res_get(struct em28xx_fh *fh)
593{
594 struct em28xx *dev = fh->dev;
595 int rc = 0;
596
597 /* This instance already has stream_on */
598 if (fh->stream_on)
599 return rc;
600
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300601 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300602 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300603
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300604 mutex_lock(&dev->lock);
605 dev->stream_on = 1;
606 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300607 mutex_unlock(&dev->lock);
608 return rc;
609}
610
611static int res_check(struct em28xx_fh *fh)
612{
613 return (fh->stream_on);
614}
615
616static void res_free(struct em28xx_fh *fh)
617{
618 struct em28xx *dev = fh->dev;
619
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300620 fh->stream_on = 0;
621 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300622}
623
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800624/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300625 * em28xx_get_ctrl()
626 * return the current saturation, brightness or contrast, mute state
627 */
628static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
629{
630 switch (ctrl->id) {
631 case V4L2_CID_AUDIO_MUTE:
632 ctrl->value = dev->mute;
633 return 0;
634 case V4L2_CID_AUDIO_VOLUME:
635 ctrl->value = dev->volume;
636 return 0;
637 default:
638 return -EINVAL;
639 }
640}
641
642/*
643 * em28xx_set_ctrl()
644 * mute or set new saturation, brightness or contrast
645 */
646static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
647{
648 switch (ctrl->id) {
649 case V4L2_CID_AUDIO_MUTE:
650 if (ctrl->value != dev->mute) {
651 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300652 return em28xx_audio_analog_set(dev);
653 }
654 return 0;
655 case V4L2_CID_AUDIO_VOLUME:
656 dev->volume = ctrl->value;
657 return em28xx_audio_analog_set(dev);
658 default:
659 return -EINVAL;
660 }
661}
662
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300663static int check_dev(struct em28xx *dev)
664{
665 if (dev->state & DEV_DISCONNECTED) {
666 em28xx_errdev("v4l2 ioctl: device not present\n");
667 return -ENODEV;
668 }
669
670 if (dev->state & DEV_MISCONFIGURED) {
671 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
672 "close and open it again\n");
673 return -EIO;
674 }
675 return 0;
676}
677
678static void get_scale(struct em28xx *dev,
679 unsigned int width, unsigned int height,
680 unsigned int *hscale, unsigned int *vscale)
681{
682 unsigned int maxw = norm_maxw(dev);
683 unsigned int maxh = norm_maxh(dev);
684
685 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
686 if (*hscale >= 0x4000)
687 *hscale = 0x3fff;
688
689 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
690 if (*vscale >= 0x4000)
691 *vscale = 0x3fff;
692}
693
694/* ------------------------------------------------------------------
695 IOCTL vidioc handling
696 ------------------------------------------------------------------*/
697
Hans Verkuil78b526a2008-05-28 12:16:41 -0300698static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300699 struct v4l2_format *f)
700{
701 struct em28xx_fh *fh = priv;
702 struct em28xx *dev = fh->dev;
703
704 mutex_lock(&dev->lock);
705
706 f->fmt.pix.width = dev->width;
707 f->fmt.pix.height = dev->height;
708 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300709 f->fmt.pix.bytesperline = dev->width * 2;
710 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300711 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
712
713 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
714 f->fmt.pix.field = dev->interlaced ?
715 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
716
717 mutex_unlock(&dev->lock);
718 return 0;
719}
720
Hans Verkuil78b526a2008-05-28 12:16:41 -0300721static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300722 struct v4l2_format *f)
723{
724 struct em28xx_fh *fh = priv;
725 struct em28xx *dev = fh->dev;
726 int width = f->fmt.pix.width;
727 int height = f->fmt.pix.height;
728 unsigned int maxw = norm_maxw(dev);
729 unsigned int maxh = norm_maxh(dev);
730 unsigned int hscale, vscale;
731
732 /* width must even because of the YUYV format
733 height must be even because of interlacing */
734 height &= 0xfffe;
735 width &= 0xfffe;
736
737 if (height < 32)
738 height = 32;
739 if (height > maxh)
740 height = maxh;
741 if (width < 48)
742 width = 48;
743 if (width > maxw)
744 width = maxw;
745
746 mutex_lock(&dev->lock);
747
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300748 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300749 /* the em2800 can only scale down to 50% */
750 if (height % (maxh / 2))
751 height = maxh;
752 if (width % (maxw / 2))
753 width = maxw;
754 /* according to empiatech support */
755 /* the MaxPacketSize is to small to support */
756 /* framesizes larger than 640x480 @ 30 fps */
757 /* or 640x576 @ 25 fps. As this would cut */
758 /* of a part of the image we prefer */
759 /* 360x576 or 360x480 for now */
760 if (width == maxw && height == maxh)
761 width /= 2;
762 }
763
764 get_scale(dev, width, height, &hscale, &vscale);
765
766 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
767 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
768
769 f->fmt.pix.width = width;
770 f->fmt.pix.height = height;
771 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
772 f->fmt.pix.bytesperline = width * 2;
773 f->fmt.pix.sizeimage = width * 2 * height;
774 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
775 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
776
777 mutex_unlock(&dev->lock);
778 return 0;
779}
780
Hans Verkuil78b526a2008-05-28 12:16:41 -0300781static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300782 struct v4l2_format *f)
783{
784 struct em28xx_fh *fh = priv;
785 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300786 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300787
788 rc = check_dev(dev);
789 if (rc < 0)
790 return rc;
791
Hans Verkuil78b526a2008-05-28 12:16:41 -0300792 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793
794 mutex_lock(&dev->lock);
795
Brandon Philips05612972008-04-13 14:57:01 -0300796 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
797 em28xx_errdev("%s queue busy\n", __func__);
798 rc = -EBUSY;
799 goto out;
800 }
801
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300802 if (dev->stream_on && !fh->stream_on) {
803 em28xx_errdev("%s device in use by another fh\n", __func__);
804 rc = -EBUSY;
805 goto out;
806 }
807
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 /* set new image size */
809 dev->width = f->fmt.pix.width;
810 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
812
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300815
Brandon Philips05612972008-04-13 14:57:01 -0300816 rc = 0;
817
818out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300819 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300820 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300821}
822
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300823static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300824{
825 struct em28xx_fh *fh = priv;
826 struct em28xx *dev = fh->dev;
827 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300828 int rc;
829
830 rc = check_dev(dev);
831 if (rc < 0)
832 return rc;
833
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300834 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300835 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300836 mutex_unlock(&dev->lock);
837
838 /* Adjusts width/height, if needed */
839 f.fmt.pix.width = dev->width;
840 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300841 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300842
843 mutex_lock(&dev->lock);
844
845 /* set new image size */
846 dev->width = f.fmt.pix.width;
847 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300848 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
849
850 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300851 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300852
853 mutex_unlock(&dev->lock);
854 return 0;
855}
856
857static const char *iname[] = {
858 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
859 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
860 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
861 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
862 [EM28XX_VMUX_SVIDEO] = "S-Video",
863 [EM28XX_VMUX_TELEVISION] = "Television",
864 [EM28XX_VMUX_CABLE] = "Cable TV",
865 [EM28XX_VMUX_DVB] = "DVB",
866 [EM28XX_VMUX_DEBUG] = "for debug only",
867};
868
869static int vidioc_enum_input(struct file *file, void *priv,
870 struct v4l2_input *i)
871{
872 struct em28xx_fh *fh = priv;
873 struct em28xx *dev = fh->dev;
874 unsigned int n;
875
876 n = i->index;
877 if (n >= MAX_EM28XX_INPUT)
878 return -EINVAL;
879 if (0 == INPUT(n)->type)
880 return -EINVAL;
881
882 i->index = n;
883 i->type = V4L2_INPUT_TYPE_CAMERA;
884
885 strcpy(i->name, iname[INPUT(n)->type]);
886
887 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
888 (EM28XX_VMUX_CABLE == INPUT(n)->type))
889 i->type = V4L2_INPUT_TYPE_TUNER;
890
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300891 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300892
893 return 0;
894}
895
896static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
897{
898 struct em28xx_fh *fh = priv;
899 struct em28xx *dev = fh->dev;
900
901 *i = dev->ctl_input;
902
903 return 0;
904}
905
906static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
907{
908 struct em28xx_fh *fh = priv;
909 struct em28xx *dev = fh->dev;
910 int rc;
911
912 rc = check_dev(dev);
913 if (rc < 0)
914 return rc;
915
916 if (i >= MAX_EM28XX_INPUT)
917 return -EINVAL;
918 if (0 == INPUT(i)->type)
919 return -EINVAL;
920
921 mutex_lock(&dev->lock);
922
923 video_mux(dev, i);
924
925 mutex_unlock(&dev->lock);
926 return 0;
927}
928
929static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
930{
931 struct em28xx_fh *fh = priv;
932 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300934 switch (a->index) {
935 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300937 break;
938 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300940 break;
941 case EM28XX_AMUX_VIDEO2:
942 strcpy(a->name, "Television alt");
943 break;
944 case EM28XX_AMUX_PHONE:
945 strcpy(a->name, "Phone");
946 break;
947 case EM28XX_AMUX_MIC:
948 strcpy(a->name, "Mic");
949 break;
950 case EM28XX_AMUX_CD:
951 strcpy(a->name, "CD");
952 break;
953 case EM28XX_AMUX_AUX:
954 strcpy(a->name, "Aux");
955 break;
956 case EM28XX_AMUX_PCM_OUT:
957 strcpy(a->name, "PCM");
958 break;
959 default:
960 return -EINVAL;
961 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300962
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300963 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965
966 return 0;
967}
968
969static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
970{
971 struct em28xx_fh *fh = priv;
972 struct em28xx *dev = fh->dev;
973
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300974 dev->ctl_ainput = INPUT(a->index)->amux;
975 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300976
977 if (!dev->ctl_aoutput)
978 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300979 return 0;
980}
981
982static int vidioc_queryctrl(struct file *file, void *priv,
983 struct v4l2_queryctrl *qc)
984{
985 struct em28xx_fh *fh = priv;
986 struct em28xx *dev = fh->dev;
987 int id = qc->id;
988 int i;
989 int rc;
990
991 rc = check_dev(dev);
992 if (rc < 0)
993 return rc;
994
995 memset(qc, 0, sizeof(*qc));
996
997 qc->id = id;
998
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300999 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001000 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1001 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1002 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1003 return 0;
1004 }
1005 }
1006 }
1007 mutex_lock(&dev->lock);
1008 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1009 mutex_unlock(&dev->lock);
1010
1011 if (qc->type)
1012 return 0;
1013 else
1014 return -EINVAL;
1015}
1016
1017static int vidioc_g_ctrl(struct file *file, void *priv,
1018 struct v4l2_control *ctrl)
1019{
1020 struct em28xx_fh *fh = priv;
1021 struct em28xx *dev = fh->dev;
1022 int rc;
1023
1024 rc = check_dev(dev);
1025 if (rc < 0)
1026 return rc;
1027 mutex_lock(&dev->lock);
1028
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001029 if (!dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001030 rc = em28xx_get_ctrl(dev, ctrl);
1031 else
1032 rc = -EINVAL;
1033
1034 if (rc == -EINVAL) {
1035 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1036 rc = 0;
1037 }
1038
1039 mutex_unlock(&dev->lock);
1040 return rc;
1041}
1042
1043static int vidioc_s_ctrl(struct file *file, void *priv,
1044 struct v4l2_control *ctrl)
1045{
1046 struct em28xx_fh *fh = priv;
1047 struct em28xx *dev = fh->dev;
1048 u8 i;
1049 int rc;
1050
1051 rc = check_dev(dev);
1052 if (rc < 0)
1053 return rc;
1054
1055 mutex_lock(&dev->lock);
1056
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001057 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001058 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1059 else {
1060 rc = 1;
1061 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1062 if (ctrl->id == em28xx_qctrl[i].id) {
1063 if (ctrl->value < em28xx_qctrl[i].minimum ||
1064 ctrl->value > em28xx_qctrl[i].maximum) {
1065 rc = -ERANGE;
1066 break;
1067 }
1068
1069 rc = em28xx_set_ctrl(dev, ctrl);
1070 break;
1071 }
1072 }
1073 }
1074
1075 /* Control not found - try to send it to the attached devices */
1076 if (rc == 1) {
1077 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1078 rc = 0;
1079 }
1080
1081 mutex_unlock(&dev->lock);
1082 return rc;
1083}
1084
1085static int vidioc_g_tuner(struct file *file, void *priv,
1086 struct v4l2_tuner *t)
1087{
1088 struct em28xx_fh *fh = priv;
1089 struct em28xx *dev = fh->dev;
1090 int rc;
1091
1092 rc = check_dev(dev);
1093 if (rc < 0)
1094 return rc;
1095
1096 if (0 != t->index)
1097 return -EINVAL;
1098
1099 strcpy(t->name, "Tuner");
1100
1101 mutex_lock(&dev->lock);
1102
1103 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1104
1105 mutex_unlock(&dev->lock);
1106 return 0;
1107}
1108
1109static int vidioc_s_tuner(struct file *file, void *priv,
1110 struct v4l2_tuner *t)
1111{
1112 struct em28xx_fh *fh = priv;
1113 struct em28xx *dev = fh->dev;
1114 int rc;
1115
1116 rc = check_dev(dev);
1117 if (rc < 0)
1118 return rc;
1119
1120 if (0 != t->index)
1121 return -EINVAL;
1122
1123 mutex_lock(&dev->lock);
1124
1125 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1126
1127 mutex_unlock(&dev->lock);
1128 return 0;
1129}
1130
1131static int vidioc_g_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
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001137 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001138 f->frequency = dev->ctl_freq;
1139
1140 return 0;
1141}
1142
1143static int vidioc_s_frequency(struct file *file, void *priv,
1144 struct v4l2_frequency *f)
1145{
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
1148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 if (0 != f->tuner)
1155 return -EINVAL;
1156
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001157 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1158 return -EINVAL;
1159 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160 return -EINVAL;
1161
1162 mutex_lock(&dev->lock);
1163
1164 dev->ctl_freq = f->frequency;
1165 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1166
1167 mutex_unlock(&dev->lock);
1168 return 0;
1169}
1170
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001171#ifdef CONFIG_VIDEO_ADV_DEBUG
1172static int em28xx_reg_len(int reg)
1173{
1174 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001175 case EM28XX_R40_AC97LSB:
1176 case EM28XX_R30_HSCALELOW:
1177 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001178 return 2;
1179 default:
1180 return 1;
1181 }
1182}
1183
1184static int vidioc_g_register(struct file *file, void *priv,
1185 struct v4l2_register *reg)
1186{
1187 struct em28xx_fh *fh = priv;
1188 struct em28xx *dev = fh->dev;
1189 int ret;
1190
1191 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1192 return -EINVAL;
1193
1194 if (em28xx_reg_len(reg->reg) == 1) {
1195 ret = em28xx_read_reg(dev, reg->reg);
1196 if (ret < 0)
1197 return ret;
1198
1199 reg->val = ret;
1200 } else {
Al Viroa954b662008-05-21 00:32:51 -03001201 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001202 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1203 reg->reg, (char *)&val, 2);
1204 if (ret < 0)
1205 return ret;
1206
Al Viroa954b662008-05-21 00:32:51 -03001207 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001208 }
1209
1210 return 0;
1211}
1212
1213static int vidioc_s_register(struct file *file, void *priv,
1214 struct v4l2_register *reg)
1215{
1216 struct em28xx_fh *fh = priv;
1217 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001218 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001219
Al Viroa954b662008-05-21 00:32:51 -03001220 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001221
1222 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1223 em28xx_reg_len(reg->reg));
1224}
1225#endif
1226
1227
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001228static int vidioc_cropcap(struct file *file, void *priv,
1229 struct v4l2_cropcap *cc)
1230{
1231 struct em28xx_fh *fh = priv;
1232 struct em28xx *dev = fh->dev;
1233
1234 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1235 return -EINVAL;
1236
1237 cc->bounds.left = 0;
1238 cc->bounds.top = 0;
1239 cc->bounds.width = dev->width;
1240 cc->bounds.height = dev->height;
1241 cc->defrect = cc->bounds;
1242 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1243 cc->pixelaspect.denominator = 59;
1244
1245 return 0;
1246}
1247
1248static int vidioc_streamon(struct file *file, void *priv,
1249 enum v4l2_buf_type type)
1250{
1251 struct em28xx_fh *fh = priv;
1252 struct em28xx *dev = fh->dev;
1253 int rc;
1254
1255 rc = check_dev(dev);
1256 if (rc < 0)
1257 return rc;
1258
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001259
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001260 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001261 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001262
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001263 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001264}
1265
1266static int vidioc_streamoff(struct file *file, void *priv,
1267 enum v4l2_buf_type type)
1268{
1269 struct em28xx_fh *fh = priv;
1270 struct em28xx *dev = fh->dev;
1271 int rc;
1272
1273 rc = check_dev(dev);
1274 if (rc < 0)
1275 return rc;
1276
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001277 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278 return -EINVAL;
1279 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001280 return -EINVAL;
1281
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001282 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001283 mutex_lock(&dev->lock);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001284 res_free(fh);
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001285 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001286
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001287 return 0;
1288}
1289
1290static int vidioc_querycap(struct file *file, void *priv,
1291 struct v4l2_capability *cap)
1292{
1293 struct em28xx_fh *fh = priv;
1294 struct em28xx *dev = fh->dev;
1295
1296 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1297 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001298 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001299
1300 cap->version = EM28XX_VERSION_CODE;
1301
1302 cap->capabilities =
1303 V4L2_CAP_SLICED_VBI_CAPTURE |
1304 V4L2_CAP_VIDEO_CAPTURE |
1305 V4L2_CAP_AUDIO |
1306 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1307
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001308 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309 cap->capabilities |= V4L2_CAP_TUNER;
1310
1311 return 0;
1312}
1313
Hans Verkuil78b526a2008-05-28 12:16:41 -03001314static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001315 struct v4l2_fmtdesc *fmtd)
1316{
1317 if (fmtd->index != 0)
1318 return -EINVAL;
1319
1320 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1321 strcpy(fmtd->description, "Packed YUY2");
1322 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1323 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1324
1325 return 0;
1326}
1327
1328/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001329static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330 struct v4l2_format *f)
1331{
1332 struct em28xx_fh *fh = priv;
1333 struct em28xx *dev = fh->dev;
1334 int rc;
1335
1336 rc = check_dev(dev);
1337 if (rc < 0)
1338 return rc;
1339
1340 mutex_lock(&dev->lock);
1341
1342 f->fmt.sliced.service_set = 0;
1343
1344 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1345
1346 if (f->fmt.sliced.service_set == 0)
1347 rc = -EINVAL;
1348
1349 mutex_unlock(&dev->lock);
1350 return rc;
1351}
1352
Hans Verkuil78b526a2008-05-28 12:16:41 -03001353static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354 struct v4l2_format *f)
1355{
1356 struct em28xx_fh *fh = priv;
1357 struct em28xx *dev = fh->dev;
1358 int rc;
1359
1360 rc = check_dev(dev);
1361 if (rc < 0)
1362 return rc;
1363
1364 mutex_lock(&dev->lock);
1365 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1366 mutex_unlock(&dev->lock);
1367
1368 if (f->fmt.sliced.service_set == 0)
1369 return -EINVAL;
1370
1371 return 0;
1372}
1373
1374
1375static int vidioc_reqbufs(struct file *file, void *priv,
1376 struct v4l2_requestbuffers *rb)
1377{
1378 struct em28xx_fh *fh = priv;
1379 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001380 int rc;
1381
1382 rc = check_dev(dev);
1383 if (rc < 0)
1384 return rc;
1385
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001386 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387}
1388
1389static int vidioc_querybuf(struct file *file, void *priv,
1390 struct v4l2_buffer *b)
1391{
1392 struct em28xx_fh *fh = priv;
1393 struct em28xx *dev = fh->dev;
1394 int rc;
1395
1396 rc = check_dev(dev);
1397 if (rc < 0)
1398 return rc;
1399
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001400 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401}
1402
1403static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1404{
1405 struct em28xx_fh *fh = priv;
1406 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407 int rc;
1408
1409 rc = check_dev(dev);
1410 if (rc < 0)
1411 return rc;
1412
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001413 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001414}
1415
1416static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1417{
1418 struct em28xx_fh *fh = priv;
1419 struct em28xx *dev = fh->dev;
1420 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421
1422 rc = check_dev(dev);
1423 if (rc < 0)
1424 return rc;
1425
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001426 return (videobuf_dqbuf(&fh->vb_vidq, b,
1427 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001428}
1429
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001430#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001431static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001432{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001433 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001434
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001435 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001436}
1437#endif
1438
1439
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001440/* ----------------------------------------------------------- */
1441/* RADIO ESPECIFIC IOCTLS */
1442/* ----------------------------------------------------------- */
1443
1444static int radio_querycap(struct file *file, void *priv,
1445 struct v4l2_capability *cap)
1446{
1447 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1448
1449 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1450 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001451 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001452
1453 cap->version = EM28XX_VERSION_CODE;
1454 cap->capabilities = V4L2_CAP_TUNER;
1455 return 0;
1456}
1457
1458static int radio_g_tuner(struct file *file, void *priv,
1459 struct v4l2_tuner *t)
1460{
1461 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1462
1463 if (unlikely(t->index > 0))
1464 return -EINVAL;
1465
1466 strcpy(t->name, "Radio");
1467 t->type = V4L2_TUNER_RADIO;
1468
1469 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1470 return 0;
1471}
1472
1473static int radio_enum_input(struct file *file, void *priv,
1474 struct v4l2_input *i)
1475{
1476 if (i->index != 0)
1477 return -EINVAL;
1478 strcpy(i->name, "Radio");
1479 i->type = V4L2_INPUT_TYPE_TUNER;
1480
1481 return 0;
1482}
1483
1484static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1485{
1486 if (unlikely(a->index))
1487 return -EINVAL;
1488
1489 strcpy(a->name, "Radio");
1490 return 0;
1491}
1492
1493static int radio_s_tuner(struct file *file, void *priv,
1494 struct v4l2_tuner *t)
1495{
1496 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1497
1498 if (0 != t->index)
1499 return -EINVAL;
1500
1501 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1502
1503 return 0;
1504}
1505
1506static int radio_s_audio(struct file *file, void *fh,
1507 struct v4l2_audio *a)
1508{
1509 return 0;
1510}
1511
1512static int radio_s_input(struct file *file, void *fh, unsigned int i)
1513{
1514 return 0;
1515}
1516
1517static int radio_queryctrl(struct file *file, void *priv,
1518 struct v4l2_queryctrl *qc)
1519{
1520 int i;
1521
1522 if (qc->id < V4L2_CID_BASE ||
1523 qc->id >= V4L2_CID_LASTP1)
1524 return -EINVAL;
1525
1526 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1527 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1528 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1529 return 0;
1530 }
1531 }
1532
1533 return -EINVAL;
1534}
1535
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001536/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001537 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001538 * inits the device and starts isoc transfer
1539 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001540static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001541{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001542 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001543 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001544 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001545 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001546 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001547
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001548 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001549 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001550 if (h->vdev->minor == minor) {
1551 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001552 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001553 }
1554 if (h->vbi_dev->minor == minor) {
1555 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001556 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001557 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001558 if (h->radio_dev &&
1559 h->radio_dev->minor == minor) {
1560 radio = 1;
1561 dev = h;
1562 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001563 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001564 mutex_unlock(&em28xx_devlist_mutex);
1565 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001566 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001567
1568 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001569
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001570 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001571 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001572
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001573
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001574 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001575 if (!fh) {
1576 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001577 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001578 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001579 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001580 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001581 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001582 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001583 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001584
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001585 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001586 dev->width = norm_maxw(dev);
1587 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001588 dev->hscale = 0;
1589 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001591 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001592 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001593 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001595 /* Needed, since GPIO might have disabled power of
1596 some i2c device
1597 */
1598 em28xx_config_i2c(dev);
1599
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001600 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001601 if (fh->radio) {
1602 em28xx_videodbg("video_open: setting radio device\n");
1603 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1604 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001605
1606 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001607
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001608 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1609 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1610 sizeof(struct em28xx_buffer), fh);
1611
Ingo Molnar3593cab2006-02-07 06:49:14 -02001612 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001613
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001614 return errCode;
1615}
1616
1617/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001618 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001619 * unregisters the v4l2,i2c and usb devices
1620 * called when the device gets disconected or at module unload
1621*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001622static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001624
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 /*FIXME: I2C IR should be disconnected */
1626
Markus Rechberger9c755412005-11-08 21:37:52 -08001627 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001628 if (dev->sbutton_input_dev)
1629 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001630
1631 if (dev->ir)
1632 em28xx_ir_fini(dev);
1633
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001634 if (dev->radio_dev) {
1635 if (-1 != dev->radio_dev->minor)
1636 video_unregister_device(dev->radio_dev);
1637 else
1638 video_device_release(dev->radio_dev);
1639 dev->radio_dev = NULL;
1640 }
1641 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001642 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1643 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001644 if (-1 != dev->vbi_dev->minor)
1645 video_unregister_device(dev->vbi_dev);
1646 else
1647 video_device_release(dev->vbi_dev);
1648 dev->vbi_dev = NULL;
1649 }
1650 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001651 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1652 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001653 if (-1 != dev->vdev->minor)
1654 video_unregister_device(dev->vdev);
1655 else
1656 video_device_release(dev->vdev);
1657 dev->vdev = NULL;
1658 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001659 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001661
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001662 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001663 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664}
1665
1666/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001667 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001668 * stops streaming and deallocates all resources allocated by the v4l2
1669 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001671static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001673 struct em28xx_fh *fh = filp->private_data;
1674 struct em28xx *dev = fh->dev;
1675 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001676
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001677 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001679
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001680 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001681 if (res_check(fh))
1682 res_free(fh);
1683
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001684 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001685 videobuf_stop(&fh->vb_vidq);
1686 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001687
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001688 /* the device is already disconnect,
1689 free the remaining resources */
1690 if (dev->state & DEV_DISCONNECTED) {
1691 em28xx_release_resources(dev);
1692 mutex_unlock(&dev->lock);
1693 kfree(dev);
1694 return 0;
1695 }
1696
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001697 /* Save some power by putting tuner to sleep */
1698 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1699
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001700 /* do this before setting alternate! */
1701 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001702 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001703
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001704 /* set alternate 0 */
1705 dev->alt = 0;
1706 em28xx_videodbg("setting alternate 0\n");
1707 errCode = usb_set_interface(dev->udev, 0, 0);
1708 if (errCode < 0) {
1709 em28xx_errdev("cannot change alternate number to "
1710 "0 (error=%i)\n", errCode);
1711 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001712 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001713 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 dev->users--;
1715 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001716 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001717 return 0;
1718}
1719
1720/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001721 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722 * will allocate buffers when called for the first time
1723 */
1724static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001725em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001726 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001727{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001728 struct em28xx_fh *fh = filp->private_data;
1729 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001730 int rc;
1731
1732 rc = check_dev(dev);
1733 if (rc < 0)
1734 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001735
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001736 /* FIXME: read() is not prepared to allow changing the video
1737 resolution while streaming. Seems a bug at em28xx_set_fmt
1738 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001739
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001740 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1741 if (unlikely(res_get(fh)))
1742 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001743
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001744 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1745 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001746 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001747 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748}
1749
1750/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001751 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001752 * will allocate buffers when called for the first time
1753 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001754static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001756 struct em28xx_fh *fh = filp->private_data;
1757 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001758 int rc;
1759
1760 rc = check_dev(dev);
1761 if (rc < 0)
1762 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001763
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001764 if (unlikely(res_get(fh) < 0))
1765 return POLLERR;
1766
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001767 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1768 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001769
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001770 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001771}
1772
1773/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001774 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001775 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001776static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001777{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001778 struct em28xx_fh *fh = filp->private_data;
1779 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001780 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001781
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001782 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001783 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001784
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001785 rc = check_dev(dev);
1786 if (rc < 0)
1787 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001788
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001789 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001791 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1792 (unsigned long)vma->vm_start,
1793 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1794 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001796 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001797}
1798
Arjan van de Venfa027c22007-02-12 00:55:33 -08001799static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001800 .owner = THIS_MODULE,
1801 .open = em28xx_v4l2_open,
1802 .release = em28xx_v4l2_close,
1803 .read = em28xx_v4l2_read,
1804 .poll = em28xx_v4l2_poll,
1805 .mmap = em28xx_v4l2_mmap,
1806 .ioctl = video_ioctl2,
1807 .llseek = no_llseek,
1808 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001809};
1810
Hans Verkuila3998102008-07-21 02:57:38 -03001811static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001812 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001813 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1814 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1815 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1816 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817 .vidioc_g_audio = vidioc_g_audio,
1818 .vidioc_s_audio = vidioc_s_audio,
1819 .vidioc_cropcap = vidioc_cropcap,
1820
Hans Verkuil78b526a2008-05-28 12:16:41 -03001821 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1822 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1823 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824
1825 .vidioc_reqbufs = vidioc_reqbufs,
1826 .vidioc_querybuf = vidioc_querybuf,
1827 .vidioc_qbuf = vidioc_qbuf,
1828 .vidioc_dqbuf = vidioc_dqbuf,
1829 .vidioc_s_std = vidioc_s_std,
1830 .vidioc_enum_input = vidioc_enum_input,
1831 .vidioc_g_input = vidioc_g_input,
1832 .vidioc_s_input = vidioc_s_input,
1833 .vidioc_queryctrl = vidioc_queryctrl,
1834 .vidioc_g_ctrl = vidioc_g_ctrl,
1835 .vidioc_s_ctrl = vidioc_s_ctrl,
1836 .vidioc_streamon = vidioc_streamon,
1837 .vidioc_streamoff = vidioc_streamoff,
1838 .vidioc_g_tuner = vidioc_g_tuner,
1839 .vidioc_s_tuner = vidioc_s_tuner,
1840 .vidioc_g_frequency = vidioc_g_frequency,
1841 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001842#ifdef CONFIG_VIDEO_ADV_DEBUG
1843 .vidioc_g_register = vidioc_g_register,
1844 .vidioc_s_register = vidioc_s_register,
1845#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001846#ifdef CONFIG_VIDEO_V4L1_COMPAT
1847 .vidiocgmbuf = vidiocgmbuf,
1848#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001849};
1850
1851static const struct video_device em28xx_video_template = {
1852 .fops = &em28xx_v4l_fops,
1853 .release = video_device_release,
1854 .ioctl_ops = &video_ioctl_ops,
1855
1856 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001857
1858 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001859 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001860};
1861
Hans Verkuila3998102008-07-21 02:57:38 -03001862static const struct file_operations radio_fops = {
1863 .owner = THIS_MODULE,
1864 .open = em28xx_v4l2_open,
1865 .release = em28xx_v4l2_close,
1866 .ioctl = video_ioctl2,
1867 .compat_ioctl = v4l_compat_ioctl32,
1868 .llseek = no_llseek,
1869};
1870
1871static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001872 .vidioc_querycap = radio_querycap,
1873 .vidioc_g_tuner = radio_g_tuner,
1874 .vidioc_enum_input = radio_enum_input,
1875 .vidioc_g_audio = radio_g_audio,
1876 .vidioc_s_tuner = radio_s_tuner,
1877 .vidioc_s_audio = radio_s_audio,
1878 .vidioc_s_input = radio_s_input,
1879 .vidioc_queryctrl = radio_queryctrl,
1880 .vidioc_g_ctrl = vidioc_g_ctrl,
1881 .vidioc_s_ctrl = vidioc_s_ctrl,
1882 .vidioc_g_frequency = vidioc_g_frequency,
1883 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001884#ifdef CONFIG_VIDEO_ADV_DEBUG
1885 .vidioc_g_register = vidioc_g_register,
1886 .vidioc_s_register = vidioc_s_register,
1887#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001888};
1889
Hans Verkuila3998102008-07-21 02:57:38 -03001890static struct video_device em28xx_radio_template = {
1891 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001892 .fops = &radio_fops,
1893 .ioctl_ops = &radio_ioctl_ops,
1894 .minor = -1,
1895};
1896
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001897/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001898
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001899
1900static LIST_HEAD(em28xx_extension_devlist);
1901static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1902
1903int em28xx_register_extension(struct em28xx_ops *ops)
1904{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001905 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001906
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001907 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001908 mutex_lock(&em28xx_extension_devlist_lock);
1909 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001910 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1911 if (dev)
1912 ops->init(dev);
1913 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001914 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1915 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001916 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001917 return 0;
1918}
1919EXPORT_SYMBOL(em28xx_register_extension);
1920
1921void em28xx_unregister_extension(struct em28xx_ops *ops)
1922{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001923 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001924
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001925 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001926 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1927 if (dev)
1928 ops->fini(dev);
1929 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001930
1931 mutex_lock(&em28xx_extension_devlist_lock);
1932 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1933 list_del(&ops->next);
1934 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001935 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001936}
1937EXPORT_SYMBOL(em28xx_unregister_extension);
1938
Adrian Bunk532fe652008-01-28 22:10:48 -03001939static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1940 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001941 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001942{
1943 struct video_device *vfd;
1944
1945 vfd = video_device_alloc();
1946 if (NULL == vfd)
1947 return NULL;
1948 *vfd = *template;
1949 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001950 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001951 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001952 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001953
1954 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1955 dev->name, type_name);
1956
1957 return vfd;
1958}
1959
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001960static int register_analog_devices(struct em28xx *dev)
1961{
1962 int ret;
1963
1964 /* allocate and fill video video_device struct */
1965 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1966 if (!dev->vdev) {
1967 em28xx_errdev("cannot allocate video_device.\n");
1968 return -ENODEV;
1969 }
1970
1971 /* register v4l2 video video_device */
1972 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1973 video_nr[dev->devno]);
1974 if (ret) {
1975 em28xx_errdev("unable to register video device (error=%i).\n",
1976 ret);
1977 return ret;
1978 }
1979
1980 /* Allocate and fill vbi video_device struct */
1981 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1982
1983 /* register v4l2 vbi video_device */
1984 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1985 vbi_nr[dev->devno]);
1986 if (ret < 0) {
1987 em28xx_errdev("unable to register vbi device\n");
1988 return ret;
1989 }
1990
1991 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1992 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1993 if (!dev->radio_dev) {
1994 em28xx_errdev("cannot allocate video_device.\n");
1995 return -ENODEV;
1996 }
1997 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1998 radio_nr[dev->devno]);
1999 if (ret < 0) {
2000 em28xx_errdev("can't register radio device\n");
2001 return ret;
2002 }
2003 em28xx_info("Registered radio device as /dev/radio%d\n",
2004 dev->radio_dev->num);
2005 }
2006
2007 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2008 dev->vdev->num, dev->vbi_dev->num);
2009
2010 return 0;
2011}
2012
2013
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002014/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002015 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002016 * allocates and inits the device structs, registers i2c bus and v4l device
2017 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002018static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002019 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002020{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002021 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002022 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002023 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002024 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002025 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002026
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002027 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002028 mutex_init(&dev->lock);
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002029 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002030 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002031 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002032 init_waitqueue_head(&dev->wait_frame);
2033 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002034
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002035 dev->em28xx_write_regs = em28xx_write_regs;
2036 dev->em28xx_read_reg = em28xx_read_reg;
2037 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2038 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2039 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002040 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002041
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002042 em28xx_pre_card_setup(dev);
2043
2044 errCode = em28xx_config(dev);
2045 if (errCode) {
2046 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002047 return -ENOMEM;
2048 }
2049
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002050 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002051 errCode = em28xx_i2c_register(dev);
2052 if (errCode < 0) {
2053 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2054 __func__, errCode);
2055 return errCode;
2056 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002057
2058 /* Do board specific init and eeprom reading */
2059 em28xx_card_setup(dev);
2060
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002061 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002062 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002063 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002064 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002065 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002066 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002067
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002068 /* configure the device */
2069 em28xx_config_i2c(dev);
2070
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002071 /* set default norm */
2072 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073
2074 maxw = norm_maxw(dev);
2075 maxh = norm_maxh(dev);
2076
2077 /* set default image size */
2078 dev->width = maxw;
2079 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002080 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002081 dev->hscale = 0;
2082 dev->vscale = 0;
2083 dev->ctl_input = 2;
2084
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002085 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002086 if (errCode < 0) {
2087 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2088 __func__, errCode);
2089 return errCode;
2090 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002092 /* init video dma queues */
2093 INIT_LIST_HEAD(&dev->vidq.active);
2094 INIT_LIST_HEAD(&dev->vidq.queued);
2095
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002097 if (dev->board.has_msp34xx) {
Sascha Sommer5a804152007-11-03 21:22:38 -03002098 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002099 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002100 if (errCode < 0) {
2101 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2102 __func__, errCode);
2103 return errCode;
2104 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002105 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002106
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002107 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002108 if (errCode < 0) {
2109 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2110 __func__, errCode);
2111 return errCode;
2112 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002113 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002114 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002115
Sascha Sommer5a804152007-11-03 21:22:38 -03002116 video_mux(dev, 0);
2117
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002118 mutex_lock(&em28xx_devlist_mutex);
2119 list_add_tail(&dev->devlist, &em28xx_devlist);
2120 retval = register_analog_devices(dev);
2121 if (retval < 0) {
2122 em28xx_release_resources(dev);
2123 mutex_unlock(&em28xx_devlist_mutex);
2124 goto fail_reg_devices;
2125 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002127 mutex_lock(&em28xx_extension_devlist_lock);
2128 if (!list_empty(&em28xx_extension_devlist)) {
2129 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2130 if (ops->id)
2131 ops->init(dev);
2132 }
2133 }
2134 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002135 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002136
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002137 /* Save some power by putting tuner to sleep */
2138 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2139
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002140 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002141
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002142fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002143 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002144 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145}
2146
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002147#if defined(CONFIG_MODULES) && defined(MODULE)
2148static void request_module_async(struct work_struct *work)
2149{
2150 struct em28xx *dev = container_of(work,
2151 struct em28xx, request_module_wk);
2152
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002153 if (dev->has_audio_class)
2154 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002155 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002156 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002157
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002158 if (dev->board.has_dvb)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002159 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002160}
2161
2162static void request_modules(struct em28xx *dev)
2163{
2164 INIT_WORK(&dev->request_module_wk, request_module_async);
2165 schedule_work(&dev->request_module_wk);
2166}
2167#else
2168#define request_modules(dev)
2169#endif /* CONFIG_MODULES */
2170
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002172 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173 * checks for supported devices
2174 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002175static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002176 const struct usb_device_id *id)
2177{
2178 const struct usb_endpoint_descriptor *endpoint;
2179 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002180 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002181 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002182 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002183 int i, nr, ifnum, isoc_pipe;
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002184 char *speed;
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002185 char descr[255] = "";
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
2187 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002188 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2189
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002190 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002191 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2192 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002193
2194 /* Don't register audio interfaces */
2195 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002196 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2197 "interface %i, class %i\n",
2198 le16_to_cpu(udev->descriptor.idVendor),
2199 le16_to_cpu(udev->descriptor.idProduct),
2200 ifnum,
2201 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002202
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002203 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002204 return -ENODEV;
2205 }
2206
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002207 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002208
Michael Opdenacker59c51592007-05-09 08:57:56 +02002209 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002210 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2211 USB_ENDPOINT_XFER_ISOC &&
2212 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2213 {
2214 /* It's a newer em2874/em2875 device */
2215 isoc_pipe = 0;
2216 } else {
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002217 int check_interface = 1;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002218 isoc_pipe = 1;
2219 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2220 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002221 USB_ENDPOINT_XFER_ISOC)
2222 check_interface = 0;
2223
2224 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2225 check_interface = 0;
2226
2227 if (!check_interface) {
2228 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2229 "interface %i, class %i found.\n",
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002230 le16_to_cpu(udev->descriptor.idVendor),
2231 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002232 ifnum,
2233 interface->altsetting[0].desc.bInterfaceClass);
2234
2235 em28xx_err(DRIVER_NAME " This is an anciliary "
2236 "interface not used by the driver\n");
2237
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002238 em28xx_devused &= ~(1<<nr);
2239 return -ENODEV;
2240 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241 }
2242
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002243 switch (udev->speed) {
2244 case USB_SPEED_LOW:
2245 speed = "1.5";
2246 break;
2247 case USB_SPEED_UNKNOWN:
2248 case USB_SPEED_FULL:
2249 speed = "12";
2250 break;
2251 case USB_SPEED_HIGH:
2252 speed = "480";
2253 break;
2254 default:
2255 speed = "unknown";
2256 }
2257
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002258 if (udev->manufacturer)
2259 strlcpy(descr, udev->manufacturer, sizeof(descr));
2260
2261 if (udev->product) {
2262 if (*descr)
2263 strlcat(descr, " ", sizeof(descr));
2264 strlcat(descr, udev->product, sizeof(descr));
2265 }
2266 if (*descr)
2267 strlcat(descr, " ", sizeof(descr));
2268
2269 printk(DRIVER_NAME ": New device %s@ %s Mbps "
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002270 "(%04x:%04x, interface %d, class %d)\n",
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002271 descr,
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002272 speed,
2273 le16_to_cpu(udev->descriptor.idVendor),
2274 le16_to_cpu(udev->descriptor.idProduct),
2275 ifnum,
2276 interface->altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002277
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002278 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002279 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2280 EM28XX_MAXBOARDS);
2281 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002282 return -ENOMEM;
2283 }
2284
2285 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002286 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002287 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002288 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002289 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002290 return -ENOMEM;
2291 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002292
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002293 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002294 dev->devno = nr;
2295 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002296 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002297
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002298 /* Checks if audio is provided by some interface */
2299 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2300 uif = udev->config->interface[i];
2301 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2302 dev->has_audio_class = 1;
2303 break;
2304 }
2305 }
2306
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002307 /* compute alternate max packet sizes */
2308 uif = udev->actconfig->interface[0];
2309
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002310 dev->num_alt = uif->num_altsetting;
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002311 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002312/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2313 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2314
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002315 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002316 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002317 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002318 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002319 return -ENOMEM;
2320 }
2321
2322 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002323 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002324 wMaxPacketSize);
2325 dev->alt_max_pkt_size[i] =
2326 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002327 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002328 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002329 }
2330
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002331 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002332 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002333
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002334 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002335 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002336 if (retval) {
2337 em28xx_devused &= ~(1<<dev->devno);
2338 kfree(dev);
2339
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002340 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002341 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002342
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343 /* save our data pointer in this interface device */
2344 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002345
2346 request_modules(dev);
2347
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002348 return 0;
2349}
2350
2351/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002352 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002353 * called when the device gets diconencted
2354 * video device will be unregistered on v4l2_close in case it is still open
2355 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002356static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002357{
Sascha Sommer5a804152007-11-03 21:22:38 -03002358 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002359 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002360
2361 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002362 usb_set_intfdata(interface, NULL);
2363
2364 if (!dev)
2365 return;
2366
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002367 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002368
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002369 /* wait until all current v4l2 io is finished then deallocate
2370 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002371 mutex_lock(&dev->lock);
2372
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002373 wake_up_interruptible_all(&dev->open);
2374
2375 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002376 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002377 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002378 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002379 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002380
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002381 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002382 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002383 dev->state |= DEV_DISCONNECTED;
2384 wake_up_interruptible(&dev->wait_frame);
2385 wake_up_interruptible(&dev->wait_stream);
2386 } else {
2387 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002388 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002389 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002390 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002391
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002392 mutex_lock(&em28xx_extension_devlist_lock);
2393 if (!list_empty(&em28xx_extension_devlist)) {
2394 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2395 ops->fini(dev);
2396 }
2397 }
2398 mutex_unlock(&em28xx_extension_devlist_lock);
2399
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002400 if (!dev->users) {
2401 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002403 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002404}
2405
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002406static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002407 .name = "em28xx",
2408 .probe = em28xx_usb_probe,
2409 .disconnect = em28xx_usb_disconnect,
2410 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411};
2412
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002413static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002414{
2415 int result;
2416
2417 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002418 (EM28XX_VERSION_CODE >> 16) & 0xff,
2419 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420#ifdef SNAPSHOT
2421 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2422 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2423#endif
2424
2425 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002426 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002427 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002428 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002429 " usb_register failed. Error number %d.\n", result);
2430
2431 return result;
2432}
2433
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002434static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002435{
2436 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002437 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002438}
2439
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002440module_init(em28xx_module_init);
2441module_exit(em28xx_module_exit);