blob: bea74fd9a5205917d88f0117eb71dc52c2effd1e [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 */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300520 if (!dev->is_em2800) {
521 retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
522 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
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300531/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
532/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
533 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
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
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300571 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800572
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800573 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300574 if (dev->i2s_speed) {
575 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
576 &dev->i2s_speed);
577 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300578 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300579 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300580 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300581 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
582 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800583 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300584
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300585 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800586}
587
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300588/* Usage lock check functions */
589static int res_get(struct em28xx_fh *fh)
590{
591 struct em28xx *dev = fh->dev;
592 int rc = 0;
593
594 /* This instance already has stream_on */
595 if (fh->stream_on)
596 return rc;
597
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300598 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300599 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300600
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300601 mutex_lock(&dev->lock);
602 dev->stream_on = 1;
603 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300604 mutex_unlock(&dev->lock);
605 return rc;
606}
607
608static int res_check(struct em28xx_fh *fh)
609{
610 return (fh->stream_on);
611}
612
613static void res_free(struct em28xx_fh *fh)
614{
615 struct em28xx *dev = fh->dev;
616
617 mutex_lock(&dev->lock);
618 fh->stream_on = 0;
619 dev->stream_on = 0;
620 mutex_unlock(&dev->lock);
621}
622
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800623/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300624 * em28xx_get_ctrl()
625 * return the current saturation, brightness or contrast, mute state
626 */
627static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
628{
629 switch (ctrl->id) {
630 case V4L2_CID_AUDIO_MUTE:
631 ctrl->value = dev->mute;
632 return 0;
633 case V4L2_CID_AUDIO_VOLUME:
634 ctrl->value = dev->volume;
635 return 0;
636 default:
637 return -EINVAL;
638 }
639}
640
641/*
642 * em28xx_set_ctrl()
643 * mute or set new saturation, brightness or contrast
644 */
645static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
646{
647 switch (ctrl->id) {
648 case V4L2_CID_AUDIO_MUTE:
649 if (ctrl->value != dev->mute) {
650 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300651 return em28xx_audio_analog_set(dev);
652 }
653 return 0;
654 case V4L2_CID_AUDIO_VOLUME:
655 dev->volume = ctrl->value;
656 return em28xx_audio_analog_set(dev);
657 default:
658 return -EINVAL;
659 }
660}
661
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300662static int check_dev(struct em28xx *dev)
663{
664 if (dev->state & DEV_DISCONNECTED) {
665 em28xx_errdev("v4l2 ioctl: device not present\n");
666 return -ENODEV;
667 }
668
669 if (dev->state & DEV_MISCONFIGURED) {
670 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
671 "close and open it again\n");
672 return -EIO;
673 }
674 return 0;
675}
676
677static void get_scale(struct em28xx *dev,
678 unsigned int width, unsigned int height,
679 unsigned int *hscale, unsigned int *vscale)
680{
681 unsigned int maxw = norm_maxw(dev);
682 unsigned int maxh = norm_maxh(dev);
683
684 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
685 if (*hscale >= 0x4000)
686 *hscale = 0x3fff;
687
688 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
689 if (*vscale >= 0x4000)
690 *vscale = 0x3fff;
691}
692
693/* ------------------------------------------------------------------
694 IOCTL vidioc handling
695 ------------------------------------------------------------------*/
696
Hans Verkuil78b526a2008-05-28 12:16:41 -0300697static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300698 struct v4l2_format *f)
699{
700 struct em28xx_fh *fh = priv;
701 struct em28xx *dev = fh->dev;
702
703 mutex_lock(&dev->lock);
704
705 f->fmt.pix.width = dev->width;
706 f->fmt.pix.height = dev->height;
707 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300708 f->fmt.pix.bytesperline = dev->width * 2;
709 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
711
712 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
713 f->fmt.pix.field = dev->interlaced ?
714 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
715
716 mutex_unlock(&dev->lock);
717 return 0;
718}
719
Hans Verkuil78b526a2008-05-28 12:16:41 -0300720static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300721 struct v4l2_format *f)
722{
723 struct em28xx_fh *fh = priv;
724 struct em28xx *dev = fh->dev;
725 int width = f->fmt.pix.width;
726 int height = f->fmt.pix.height;
727 unsigned int maxw = norm_maxw(dev);
728 unsigned int maxh = norm_maxh(dev);
729 unsigned int hscale, vscale;
730
731 /* width must even because of the YUYV format
732 height must be even because of interlacing */
733 height &= 0xfffe;
734 width &= 0xfffe;
735
736 if (height < 32)
737 height = 32;
738 if (height > maxh)
739 height = maxh;
740 if (width < 48)
741 width = 48;
742 if (width > maxw)
743 width = maxw;
744
745 mutex_lock(&dev->lock);
746
747 if (dev->is_em2800) {
748 /* the em2800 can only scale down to 50% */
749 if (height % (maxh / 2))
750 height = maxh;
751 if (width % (maxw / 2))
752 width = maxw;
753 /* according to empiatech support */
754 /* the MaxPacketSize is to small to support */
755 /* framesizes larger than 640x480 @ 30 fps */
756 /* or 640x576 @ 25 fps. As this would cut */
757 /* of a part of the image we prefer */
758 /* 360x576 or 360x480 for now */
759 if (width == maxw && height == maxh)
760 width /= 2;
761 }
762
763 get_scale(dev, width, height, &hscale, &vscale);
764
765 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
766 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
767
768 f->fmt.pix.width = width;
769 f->fmt.pix.height = height;
770 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
771 f->fmt.pix.bytesperline = width * 2;
772 f->fmt.pix.sizeimage = width * 2 * height;
773 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
774 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
775
776 mutex_unlock(&dev->lock);
777 return 0;
778}
779
Hans Verkuil78b526a2008-05-28 12:16:41 -0300780static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300781 struct v4l2_format *f)
782{
783 struct em28xx_fh *fh = priv;
784 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300785 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300786
787 rc = check_dev(dev);
788 if (rc < 0)
789 return rc;
790
Hans Verkuil78b526a2008-05-28 12:16:41 -0300791 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300792
793 mutex_lock(&dev->lock);
794
Brandon Philips05612972008-04-13 14:57:01 -0300795 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
796 em28xx_errdev("%s queue busy\n", __func__);
797 rc = -EBUSY;
798 goto out;
799 }
800
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300801 if (dev->stream_on && !fh->stream_on) {
802 em28xx_errdev("%s device in use by another fh\n", __func__);
803 rc = -EBUSY;
804 goto out;
805 }
806
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300807 /* set new image size */
808 dev->width = f->fmt.pix.width;
809 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
811
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814
Brandon Philips05612972008-04-13 14:57:01 -0300815 rc = 0;
816
817out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300819 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300820}
821
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300822static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300823{
824 struct em28xx_fh *fh = priv;
825 struct em28xx *dev = fh->dev;
826 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300827 int rc;
828
829 rc = check_dev(dev);
830 if (rc < 0)
831 return rc;
832
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300833 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300834 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300835 mutex_unlock(&dev->lock);
836
837 /* Adjusts width/height, if needed */
838 f.fmt.pix.width = dev->width;
839 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300840 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841
842 mutex_lock(&dev->lock);
843
844 /* set new image size */
845 dev->width = f.fmt.pix.width;
846 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300847 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
848
849 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300850 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300851
852 mutex_unlock(&dev->lock);
853 return 0;
854}
855
856static const char *iname[] = {
857 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
858 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
859 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
860 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
861 [EM28XX_VMUX_SVIDEO] = "S-Video",
862 [EM28XX_VMUX_TELEVISION] = "Television",
863 [EM28XX_VMUX_CABLE] = "Cable TV",
864 [EM28XX_VMUX_DVB] = "DVB",
865 [EM28XX_VMUX_DEBUG] = "for debug only",
866};
867
868static int vidioc_enum_input(struct file *file, void *priv,
869 struct v4l2_input *i)
870{
871 struct em28xx_fh *fh = priv;
872 struct em28xx *dev = fh->dev;
873 unsigned int n;
874
875 n = i->index;
876 if (n >= MAX_EM28XX_INPUT)
877 return -EINVAL;
878 if (0 == INPUT(n)->type)
879 return -EINVAL;
880
881 i->index = n;
882 i->type = V4L2_INPUT_TYPE_CAMERA;
883
884 strcpy(i->name, iname[INPUT(n)->type]);
885
886 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
887 (EM28XX_VMUX_CABLE == INPUT(n)->type))
888 i->type = V4L2_INPUT_TYPE_TUNER;
889
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300890 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300891
892 return 0;
893}
894
895static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
896{
897 struct em28xx_fh *fh = priv;
898 struct em28xx *dev = fh->dev;
899
900 *i = dev->ctl_input;
901
902 return 0;
903}
904
905static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
906{
907 struct em28xx_fh *fh = priv;
908 struct em28xx *dev = fh->dev;
909 int rc;
910
911 rc = check_dev(dev);
912 if (rc < 0)
913 return rc;
914
915 if (i >= MAX_EM28XX_INPUT)
916 return -EINVAL;
917 if (0 == INPUT(i)->type)
918 return -EINVAL;
919
920 mutex_lock(&dev->lock);
921
922 video_mux(dev, i);
923
924 mutex_unlock(&dev->lock);
925 return 0;
926}
927
928static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
929{
930 struct em28xx_fh *fh = priv;
931 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300933 switch (a->index) {
934 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300936 break;
937 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300939 break;
940 case EM28XX_AMUX_VIDEO2:
941 strcpy(a->name, "Television alt");
942 break;
943 case EM28XX_AMUX_PHONE:
944 strcpy(a->name, "Phone");
945 break;
946 case EM28XX_AMUX_MIC:
947 strcpy(a->name, "Mic");
948 break;
949 case EM28XX_AMUX_CD:
950 strcpy(a->name, "CD");
951 break;
952 case EM28XX_AMUX_AUX:
953 strcpy(a->name, "Aux");
954 break;
955 case EM28XX_AMUX_PCM_OUT:
956 strcpy(a->name, "PCM");
957 break;
958 default:
959 return -EINVAL;
960 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300961
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300962 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300964
965 return 0;
966}
967
968static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
969{
970 struct em28xx_fh *fh = priv;
971 struct em28xx *dev = fh->dev;
972
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300973 dev->ctl_ainput = INPUT(a->index)->amux;
974 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300975 return 0;
976}
977
978static int vidioc_queryctrl(struct file *file, void *priv,
979 struct v4l2_queryctrl *qc)
980{
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
983 int id = qc->id;
984 int i;
985 int rc;
986
987 rc = check_dev(dev);
988 if (rc < 0)
989 return rc;
990
991 memset(qc, 0, sizeof(*qc));
992
993 qc->id = id;
994
995 if (!dev->has_msp34xx) {
996 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
997 if (qc->id && qc->id == em28xx_qctrl[i].id) {
998 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
999 return 0;
1000 }
1001 }
1002 }
1003 mutex_lock(&dev->lock);
1004 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1005 mutex_unlock(&dev->lock);
1006
1007 if (qc->type)
1008 return 0;
1009 else
1010 return -EINVAL;
1011}
1012
1013static int vidioc_g_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1015{
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1018 int rc;
1019
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
1023 mutex_lock(&dev->lock);
1024
1025 if (!dev->has_msp34xx)
1026 rc = em28xx_get_ctrl(dev, ctrl);
1027 else
1028 rc = -EINVAL;
1029
1030 if (rc == -EINVAL) {
1031 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1032 rc = 0;
1033 }
1034
1035 mutex_unlock(&dev->lock);
1036 return rc;
1037}
1038
1039static int vidioc_s_ctrl(struct file *file, void *priv,
1040 struct v4l2_control *ctrl)
1041{
1042 struct em28xx_fh *fh = priv;
1043 struct em28xx *dev = fh->dev;
1044 u8 i;
1045 int rc;
1046
1047 rc = check_dev(dev);
1048 if (rc < 0)
1049 return rc;
1050
1051 mutex_lock(&dev->lock);
1052
1053 if (dev->has_msp34xx)
1054 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1055 else {
1056 rc = 1;
1057 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1058 if (ctrl->id == em28xx_qctrl[i].id) {
1059 if (ctrl->value < em28xx_qctrl[i].minimum ||
1060 ctrl->value > em28xx_qctrl[i].maximum) {
1061 rc = -ERANGE;
1062 break;
1063 }
1064
1065 rc = em28xx_set_ctrl(dev, ctrl);
1066 break;
1067 }
1068 }
1069 }
1070
1071 /* Control not found - try to send it to the attached devices */
1072 if (rc == 1) {
1073 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1074 rc = 0;
1075 }
1076
1077 mutex_unlock(&dev->lock);
1078 return rc;
1079}
1080
1081static int vidioc_g_tuner(struct file *file, void *priv,
1082 struct v4l2_tuner *t)
1083{
1084 struct em28xx_fh *fh = priv;
1085 struct em28xx *dev = fh->dev;
1086 int rc;
1087
1088 rc = check_dev(dev);
1089 if (rc < 0)
1090 return rc;
1091
1092 if (0 != t->index)
1093 return -EINVAL;
1094
1095 strcpy(t->name, "Tuner");
1096
1097 mutex_lock(&dev->lock);
1098
1099 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1100
1101 mutex_unlock(&dev->lock);
1102 return 0;
1103}
1104
1105static int vidioc_s_tuner(struct file *file, void *priv,
1106 struct v4l2_tuner *t)
1107{
1108 struct em28xx_fh *fh = priv;
1109 struct em28xx *dev = fh->dev;
1110 int rc;
1111
1112 rc = check_dev(dev);
1113 if (rc < 0)
1114 return rc;
1115
1116 if (0 != t->index)
1117 return -EINVAL;
1118
1119 mutex_lock(&dev->lock);
1120
1121 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1122
1123 mutex_unlock(&dev->lock);
1124 return 0;
1125}
1126
1127static int vidioc_g_frequency(struct file *file, void *priv,
1128 struct v4l2_frequency *f)
1129{
1130 struct em28xx_fh *fh = priv;
1131 struct em28xx *dev = fh->dev;
1132
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001133 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001134 f->frequency = dev->ctl_freq;
1135
1136 return 0;
1137}
1138
1139static int vidioc_s_frequency(struct file *file, void *priv,
1140 struct v4l2_frequency *f)
1141{
1142 struct em28xx_fh *fh = priv;
1143 struct em28xx *dev = fh->dev;
1144 int rc;
1145
1146 rc = check_dev(dev);
1147 if (rc < 0)
1148 return rc;
1149
1150 if (0 != f->tuner)
1151 return -EINVAL;
1152
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001153 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1154 return -EINVAL;
1155 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001156 return -EINVAL;
1157
1158 mutex_lock(&dev->lock);
1159
1160 dev->ctl_freq = f->frequency;
1161 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1162
1163 mutex_unlock(&dev->lock);
1164 return 0;
1165}
1166
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001167#ifdef CONFIG_VIDEO_ADV_DEBUG
1168static int em28xx_reg_len(int reg)
1169{
1170 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001171 case EM28XX_R40_AC97LSB:
1172 case EM28XX_R30_HSCALELOW:
1173 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001174 return 2;
1175 default:
1176 return 1;
1177 }
1178}
1179
1180static int vidioc_g_register(struct file *file, void *priv,
1181 struct v4l2_register *reg)
1182{
1183 struct em28xx_fh *fh = priv;
1184 struct em28xx *dev = fh->dev;
1185 int ret;
1186
1187 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1188 return -EINVAL;
1189
1190 if (em28xx_reg_len(reg->reg) == 1) {
1191 ret = em28xx_read_reg(dev, reg->reg);
1192 if (ret < 0)
1193 return ret;
1194
1195 reg->val = ret;
1196 } else {
Al Viroa954b662008-05-21 00:32:51 -03001197 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001198 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1199 reg->reg, (char *)&val, 2);
1200 if (ret < 0)
1201 return ret;
1202
Al Viroa954b662008-05-21 00:32:51 -03001203 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001204 }
1205
1206 return 0;
1207}
1208
1209static int vidioc_s_register(struct file *file, void *priv,
1210 struct v4l2_register *reg)
1211{
1212 struct em28xx_fh *fh = priv;
1213 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001214 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001215
Al Viroa954b662008-05-21 00:32:51 -03001216 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001217
1218 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1219 em28xx_reg_len(reg->reg));
1220}
1221#endif
1222
1223
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001224static int vidioc_cropcap(struct file *file, void *priv,
1225 struct v4l2_cropcap *cc)
1226{
1227 struct em28xx_fh *fh = priv;
1228 struct em28xx *dev = fh->dev;
1229
1230 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1231 return -EINVAL;
1232
1233 cc->bounds.left = 0;
1234 cc->bounds.top = 0;
1235 cc->bounds.width = dev->width;
1236 cc->bounds.height = dev->height;
1237 cc->defrect = cc->bounds;
1238 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1239 cc->pixelaspect.denominator = 59;
1240
1241 return 0;
1242}
1243
1244static int vidioc_streamon(struct file *file, void *priv,
1245 enum v4l2_buf_type type)
1246{
1247 struct em28xx_fh *fh = priv;
1248 struct em28xx *dev = fh->dev;
1249 int rc;
1250
1251 rc = check_dev(dev);
1252 if (rc < 0)
1253 return rc;
1254
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001255
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001256 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001257 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001258
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001259 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001260}
1261
1262static int vidioc_streamoff(struct file *file, void *priv,
1263 enum v4l2_buf_type type)
1264{
1265 struct em28xx_fh *fh = priv;
1266 struct em28xx *dev = fh->dev;
1267 int rc;
1268
1269 rc = check_dev(dev);
1270 if (rc < 0)
1271 return rc;
1272
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001273 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1274 return -EINVAL;
1275 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001276 return -EINVAL;
1277
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001278 videobuf_streamoff(&fh->vb_vidq);
1279 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001280
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001281 return 0;
1282}
1283
1284static int vidioc_querycap(struct file *file, void *priv,
1285 struct v4l2_capability *cap)
1286{
1287 struct em28xx_fh *fh = priv;
1288 struct em28xx *dev = fh->dev;
1289
1290 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1291 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001292 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001293
1294 cap->version = EM28XX_VERSION_CODE;
1295
1296 cap->capabilities =
1297 V4L2_CAP_SLICED_VBI_CAPTURE |
1298 V4L2_CAP_VIDEO_CAPTURE |
1299 V4L2_CAP_AUDIO |
1300 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1301
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001302 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001303 cap->capabilities |= V4L2_CAP_TUNER;
1304
1305 return 0;
1306}
1307
Hans Verkuil78b526a2008-05-28 12:16:41 -03001308static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001309 struct v4l2_fmtdesc *fmtd)
1310{
1311 if (fmtd->index != 0)
1312 return -EINVAL;
1313
1314 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1315 strcpy(fmtd->description, "Packed YUY2");
1316 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1317 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1318
1319 return 0;
1320}
1321
1322/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001323static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001324 struct v4l2_format *f)
1325{
1326 struct em28xx_fh *fh = priv;
1327 struct em28xx *dev = fh->dev;
1328 int rc;
1329
1330 rc = check_dev(dev);
1331 if (rc < 0)
1332 return rc;
1333
1334 mutex_lock(&dev->lock);
1335
1336 f->fmt.sliced.service_set = 0;
1337
1338 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1339
1340 if (f->fmt.sliced.service_set == 0)
1341 rc = -EINVAL;
1342
1343 mutex_unlock(&dev->lock);
1344 return rc;
1345}
1346
Hans Verkuil78b526a2008-05-28 12:16:41 -03001347static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 struct v4l2_format *f)
1349{
1350 struct em28xx_fh *fh = priv;
1351 struct em28xx *dev = fh->dev;
1352 int rc;
1353
1354 rc = check_dev(dev);
1355 if (rc < 0)
1356 return rc;
1357
1358 mutex_lock(&dev->lock);
1359 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1360 mutex_unlock(&dev->lock);
1361
1362 if (f->fmt.sliced.service_set == 0)
1363 return -EINVAL;
1364
1365 return 0;
1366}
1367
1368
1369static int vidioc_reqbufs(struct file *file, void *priv,
1370 struct v4l2_requestbuffers *rb)
1371{
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374 int rc;
1375
1376 rc = check_dev(dev);
1377 if (rc < 0)
1378 return rc;
1379
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001380 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381}
1382
1383static int vidioc_querybuf(struct file *file, void *priv,
1384 struct v4l2_buffer *b)
1385{
1386 struct em28xx_fh *fh = priv;
1387 struct em28xx *dev = fh->dev;
1388 int rc;
1389
1390 rc = check_dev(dev);
1391 if (rc < 0)
1392 return rc;
1393
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001394 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395}
1396
1397static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1398{
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001401 int rc;
1402
1403 rc = check_dev(dev);
1404 if (rc < 0)
1405 return rc;
1406
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001407 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001408}
1409
1410static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1411{
1412 struct em28xx_fh *fh = priv;
1413 struct em28xx *dev = fh->dev;
1414 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415
1416 rc = check_dev(dev);
1417 if (rc < 0)
1418 return rc;
1419
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001420 return (videobuf_dqbuf(&fh->vb_vidq, b,
1421 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001422}
1423
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001424#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001425static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001426{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001427 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001428
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001429 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001430}
1431#endif
1432
1433
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001434/* ----------------------------------------------------------- */
1435/* RADIO ESPECIFIC IOCTLS */
1436/* ----------------------------------------------------------- */
1437
1438static int radio_querycap(struct file *file, void *priv,
1439 struct v4l2_capability *cap)
1440{
1441 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1442
1443 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1444 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001445 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001446
1447 cap->version = EM28XX_VERSION_CODE;
1448 cap->capabilities = V4L2_CAP_TUNER;
1449 return 0;
1450}
1451
1452static int radio_g_tuner(struct file *file, void *priv,
1453 struct v4l2_tuner *t)
1454{
1455 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1456
1457 if (unlikely(t->index > 0))
1458 return -EINVAL;
1459
1460 strcpy(t->name, "Radio");
1461 t->type = V4L2_TUNER_RADIO;
1462
1463 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1464 return 0;
1465}
1466
1467static int radio_enum_input(struct file *file, void *priv,
1468 struct v4l2_input *i)
1469{
1470 if (i->index != 0)
1471 return -EINVAL;
1472 strcpy(i->name, "Radio");
1473 i->type = V4L2_INPUT_TYPE_TUNER;
1474
1475 return 0;
1476}
1477
1478static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1479{
1480 if (unlikely(a->index))
1481 return -EINVAL;
1482
1483 strcpy(a->name, "Radio");
1484 return 0;
1485}
1486
1487static int radio_s_tuner(struct file *file, void *priv,
1488 struct v4l2_tuner *t)
1489{
1490 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1491
1492 if (0 != t->index)
1493 return -EINVAL;
1494
1495 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1496
1497 return 0;
1498}
1499
1500static int radio_s_audio(struct file *file, void *fh,
1501 struct v4l2_audio *a)
1502{
1503 return 0;
1504}
1505
1506static int radio_s_input(struct file *file, void *fh, unsigned int i)
1507{
1508 return 0;
1509}
1510
1511static int radio_queryctrl(struct file *file, void *priv,
1512 struct v4l2_queryctrl *qc)
1513{
1514 int i;
1515
1516 if (qc->id < V4L2_CID_BASE ||
1517 qc->id >= V4L2_CID_LASTP1)
1518 return -EINVAL;
1519
1520 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1521 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1522 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1523 return 0;
1524 }
1525 }
1526
1527 return -EINVAL;
1528}
1529
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001530/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001531 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001532 * inits the device and starts isoc transfer
1533 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001534static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001535{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001536 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001537 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001538 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001539 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001540 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001541
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001542 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001543 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001544 if (h->vdev->minor == minor) {
1545 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001546 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001547 }
1548 if (h->vbi_dev->minor == minor) {
1549 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001550 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001551 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001552 if (h->radio_dev &&
1553 h->radio_dev->minor == minor) {
1554 radio = 1;
1555 dev = h;
1556 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001557 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001558 mutex_unlock(&em28xx_devlist_mutex);
1559 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001560 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001561
1562 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001563
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001564 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001565 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001566
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001567
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001568 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001569 if (!fh) {
1570 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001571 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001572 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001573 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001574 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001575 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001576 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001577 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001578
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001579 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001580 dev->width = norm_maxw(dev);
1581 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001582 dev->hscale = 0;
1583 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001584
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001585 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001586 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001587 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001588
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001589 /* Needed, since GPIO might have disabled power of
1590 some i2c device
1591 */
1592 em28xx_config_i2c(dev);
1593
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001594 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001595 if (fh->radio) {
1596 em28xx_videodbg("video_open: setting radio device\n");
1597 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1598 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599
1600 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001601
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001602 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1603 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1604 sizeof(struct em28xx_buffer), fh);
1605
Ingo Molnar3593cab2006-02-07 06:49:14 -02001606 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001607
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608 return errCode;
1609}
1610
1611/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001612 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613 * unregisters the v4l2,i2c and usb devices
1614 * called when the device gets disconected or at module unload
1615*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001616static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001617{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001618
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001619 /*FIXME: I2C IR should be disconnected */
1620
Markus Rechberger9c755412005-11-08 21:37:52 -08001621 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001622 if (dev->sbutton_input_dev)
1623 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001624
1625 if (dev->ir)
1626 em28xx_ir_fini(dev);
1627
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001628 if (dev->radio_dev) {
1629 if (-1 != dev->radio_dev->minor)
1630 video_unregister_device(dev->radio_dev);
1631 else
1632 video_device_release(dev->radio_dev);
1633 dev->radio_dev = NULL;
1634 }
1635 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001636 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1637 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001638 if (-1 != dev->vbi_dev->minor)
1639 video_unregister_device(dev->vbi_dev);
1640 else
1641 video_device_release(dev->vbi_dev);
1642 dev->vbi_dev = NULL;
1643 }
1644 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001645 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1646 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001647 if (-1 != dev->vdev->minor)
1648 video_unregister_device(dev->vdev);
1649 else
1650 video_device_release(dev->vdev);
1651 dev->vdev = NULL;
1652 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001653 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001654 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001655
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001656 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001657 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658}
1659
1660/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001661 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001662 * stops streaming and deallocates all resources allocated by the v4l2
1663 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001665static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001666{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001667 struct em28xx_fh *fh = filp->private_data;
1668 struct em28xx *dev = fh->dev;
1669 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001671 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001673
1674 if (res_check(fh))
1675 res_free(fh);
1676
Ingo Molnar3593cab2006-02-07 06:49:14 -02001677 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001679 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001680 videobuf_stop(&fh->vb_vidq);
1681 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001683 /* the device is already disconnect,
1684 free the remaining resources */
1685 if (dev->state & DEV_DISCONNECTED) {
1686 em28xx_release_resources(dev);
1687 mutex_unlock(&dev->lock);
1688 kfree(dev);
1689 return 0;
1690 }
1691
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001692 /* do this before setting alternate! */
1693 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001694 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001695
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001696 /* set alternate 0 */
1697 dev->alt = 0;
1698 em28xx_videodbg("setting alternate 0\n");
1699 errCode = usb_set_interface(dev->udev, 0, 0);
1700 if (errCode < 0) {
1701 em28xx_errdev("cannot change alternate number to "
1702 "0 (error=%i)\n", errCode);
1703 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001705 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706 dev->users--;
1707 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001708 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001709 return 0;
1710}
1711
1712/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001713 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 * will allocate buffers when called for the first time
1715 */
1716static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001717em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001718 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001719{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001720 struct em28xx_fh *fh = filp->private_data;
1721 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001722 int rc;
1723
1724 rc = check_dev(dev);
1725 if (rc < 0)
1726 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001727
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001728 /* FIXME: read() is not prepared to allow changing the video
1729 resolution while streaming. Seems a bug at em28xx_set_fmt
1730 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001731
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001732 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1733 if (unlikely(res_get(fh)))
1734 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001735
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001736 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1737 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001738 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001739 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001740}
1741
1742/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001743 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001744 * will allocate buffers when called for the first time
1745 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001746static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001748 struct em28xx_fh *fh = filp->private_data;
1749 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001750 int rc;
1751
1752 rc = check_dev(dev);
1753 if (rc < 0)
1754 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001756 if (unlikely(res_get(fh) < 0))
1757 return POLLERR;
1758
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001759 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1760 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001761
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001762 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001763}
1764
1765/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001766 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001767 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001768static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001769{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001770 struct em28xx_fh *fh = filp->private_data;
1771 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001772 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001773
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001774 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001775 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001776
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001777 rc = check_dev(dev);
1778 if (rc < 0)
1779 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001780
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001781 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001782
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001783 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1784 (unsigned long)vma->vm_start,
1785 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1786 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001787
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001788 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001789}
1790
Arjan van de Venfa027c22007-02-12 00:55:33 -08001791static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792 .owner = THIS_MODULE,
1793 .open = em28xx_v4l2_open,
1794 .release = em28xx_v4l2_close,
1795 .read = em28xx_v4l2_read,
1796 .poll = em28xx_v4l2_poll,
1797 .mmap = em28xx_v4l2_mmap,
1798 .ioctl = video_ioctl2,
1799 .llseek = no_llseek,
1800 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001801};
1802
Hans Verkuila3998102008-07-21 02:57:38 -03001803static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001805 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1806 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1807 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1808 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001809 .vidioc_g_audio = vidioc_g_audio,
1810 .vidioc_s_audio = vidioc_s_audio,
1811 .vidioc_cropcap = vidioc_cropcap,
1812
Hans Verkuil78b526a2008-05-28 12:16:41 -03001813 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1814 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1815 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001816
1817 .vidioc_reqbufs = vidioc_reqbufs,
1818 .vidioc_querybuf = vidioc_querybuf,
1819 .vidioc_qbuf = vidioc_qbuf,
1820 .vidioc_dqbuf = vidioc_dqbuf,
1821 .vidioc_s_std = vidioc_s_std,
1822 .vidioc_enum_input = vidioc_enum_input,
1823 .vidioc_g_input = vidioc_g_input,
1824 .vidioc_s_input = vidioc_s_input,
1825 .vidioc_queryctrl = vidioc_queryctrl,
1826 .vidioc_g_ctrl = vidioc_g_ctrl,
1827 .vidioc_s_ctrl = vidioc_s_ctrl,
1828 .vidioc_streamon = vidioc_streamon,
1829 .vidioc_streamoff = vidioc_streamoff,
1830 .vidioc_g_tuner = vidioc_g_tuner,
1831 .vidioc_s_tuner = vidioc_s_tuner,
1832 .vidioc_g_frequency = vidioc_g_frequency,
1833 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001834#ifdef CONFIG_VIDEO_ADV_DEBUG
1835 .vidioc_g_register = vidioc_g_register,
1836 .vidioc_s_register = vidioc_s_register,
1837#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001838#ifdef CONFIG_VIDEO_V4L1_COMPAT
1839 .vidiocgmbuf = vidiocgmbuf,
1840#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001841};
1842
1843static const struct video_device em28xx_video_template = {
1844 .fops = &em28xx_v4l_fops,
1845 .release = video_device_release,
1846 .ioctl_ops = &video_ioctl_ops,
1847
1848 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001849
1850 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001851 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001852};
1853
Hans Verkuila3998102008-07-21 02:57:38 -03001854static const struct file_operations radio_fops = {
1855 .owner = THIS_MODULE,
1856 .open = em28xx_v4l2_open,
1857 .release = em28xx_v4l2_close,
1858 .ioctl = video_ioctl2,
1859 .compat_ioctl = v4l_compat_ioctl32,
1860 .llseek = no_llseek,
1861};
1862
1863static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001864 .vidioc_querycap = radio_querycap,
1865 .vidioc_g_tuner = radio_g_tuner,
1866 .vidioc_enum_input = radio_enum_input,
1867 .vidioc_g_audio = radio_g_audio,
1868 .vidioc_s_tuner = radio_s_tuner,
1869 .vidioc_s_audio = radio_s_audio,
1870 .vidioc_s_input = radio_s_input,
1871 .vidioc_queryctrl = radio_queryctrl,
1872 .vidioc_g_ctrl = vidioc_g_ctrl,
1873 .vidioc_s_ctrl = vidioc_s_ctrl,
1874 .vidioc_g_frequency = vidioc_g_frequency,
1875 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001876#ifdef CONFIG_VIDEO_ADV_DEBUG
1877 .vidioc_g_register = vidioc_g_register,
1878 .vidioc_s_register = vidioc_s_register,
1879#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001880};
1881
Hans Verkuila3998102008-07-21 02:57:38 -03001882static struct video_device em28xx_radio_template = {
1883 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001884 .fops = &radio_fops,
1885 .ioctl_ops = &radio_ioctl_ops,
1886 .minor = -1,
1887};
1888
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001889/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001890
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001891
1892static LIST_HEAD(em28xx_extension_devlist);
1893static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1894
1895int em28xx_register_extension(struct em28xx_ops *ops)
1896{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001897 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001898
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001899 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001900 mutex_lock(&em28xx_extension_devlist_lock);
1901 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001902 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1903 if (dev)
1904 ops->init(dev);
1905 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001906 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1907 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001908 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001909 return 0;
1910}
1911EXPORT_SYMBOL(em28xx_register_extension);
1912
1913void em28xx_unregister_extension(struct em28xx_ops *ops)
1914{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001915 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001916
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001917 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001918 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1919 if (dev)
1920 ops->fini(dev);
1921 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001922
1923 mutex_lock(&em28xx_extension_devlist_lock);
1924 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1925 list_del(&ops->next);
1926 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001927 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001928}
1929EXPORT_SYMBOL(em28xx_unregister_extension);
1930
Adrian Bunk532fe652008-01-28 22:10:48 -03001931static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1932 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001933 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001934{
1935 struct video_device *vfd;
1936
1937 vfd = video_device_alloc();
1938 if (NULL == vfd)
1939 return NULL;
1940 *vfd = *template;
1941 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001942 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001943 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001944 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001945
1946 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1947 dev->name, type_name);
1948
1949 return vfd;
1950}
1951
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001952static int register_analog_devices(struct em28xx *dev)
1953{
1954 int ret;
1955
1956 /* allocate and fill video video_device struct */
1957 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1958 if (!dev->vdev) {
1959 em28xx_errdev("cannot allocate video_device.\n");
1960 return -ENODEV;
1961 }
1962
1963 /* register v4l2 video video_device */
1964 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1965 video_nr[dev->devno]);
1966 if (ret) {
1967 em28xx_errdev("unable to register video device (error=%i).\n",
1968 ret);
1969 return ret;
1970 }
1971
1972 /* Allocate and fill vbi video_device struct */
1973 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1974
1975 /* register v4l2 vbi video_device */
1976 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1977 vbi_nr[dev->devno]);
1978 if (ret < 0) {
1979 em28xx_errdev("unable to register vbi device\n");
1980 return ret;
1981 }
1982
1983 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1984 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1985 if (!dev->radio_dev) {
1986 em28xx_errdev("cannot allocate video_device.\n");
1987 return -ENODEV;
1988 }
1989 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1990 radio_nr[dev->devno]);
1991 if (ret < 0) {
1992 em28xx_errdev("can't register radio device\n");
1993 return ret;
1994 }
1995 em28xx_info("Registered radio device as /dev/radio%d\n",
1996 dev->radio_dev->num);
1997 }
1998
1999 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2000 dev->vdev->num, dev->vbi_dev->num);
2001
2002 return 0;
2003}
2004
2005
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002006/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002007 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002008 * allocates and inits the device structs, registers i2c bus and v4l device
2009 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002010static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002011 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002012{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002013 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002014 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002015 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002016 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002017 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002018
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002019 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002020 mutex_init(&dev->lock);
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002021 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002022 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002023 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002024 init_waitqueue_head(&dev->wait_frame);
2025 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002026
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002027 dev->em28xx_write_regs = em28xx_write_regs;
2028 dev->em28xx_read_reg = em28xx_read_reg;
2029 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2030 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2031 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002032 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002033
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002034 em28xx_pre_card_setup(dev);
2035
2036 errCode = em28xx_config(dev);
2037 if (errCode) {
2038 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002039 return -ENOMEM;
2040 }
2041
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002042 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002043 errCode = em28xx_i2c_register(dev);
2044 if (errCode < 0) {
2045 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2046 __func__, errCode);
2047 return errCode;
2048 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002049
2050 /* Do board specific init and eeprom reading */
2051 em28xx_card_setup(dev);
2052
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002053 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002054 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002055 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002056 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002057 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002058 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002059
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002060 /* configure the device */
2061 em28xx_config_i2c(dev);
2062
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002063 /* set default norm */
2064 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002065
2066 maxw = norm_maxw(dev);
2067 maxh = norm_maxh(dev);
2068
2069 /* set default image size */
2070 dev->width = maxw;
2071 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002072 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073 dev->hscale = 0;
2074 dev->vscale = 0;
2075 dev->ctl_input = 2;
2076
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002077 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002078 if (errCode < 0) {
2079 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2080 __func__, errCode);
2081 return errCode;
2082 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002083
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002084 /* init video dma queues */
2085 INIT_LIST_HEAD(&dev->vidq.active);
2086 INIT_LIST_HEAD(&dev->vidq.queued);
2087
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088
Sascha Sommer5a804152007-11-03 21:22:38 -03002089 if (dev->has_msp34xx) {
2090 /* Send a reset to other chips via gpio */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002091 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2092 if (errCode < 0) {
2093 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2094 __func__, errCode);
2095 return errCode;
2096 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002097 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002098
2099 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2100 if (errCode < 0) {
2101 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2102 __func__, errCode);
2103 return errCode;
2104 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002105 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002106 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002107
Sascha Sommer5a804152007-11-03 21:22:38 -03002108 video_mux(dev, 0);
2109
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002110 mutex_lock(&em28xx_devlist_mutex);
2111 list_add_tail(&dev->devlist, &em28xx_devlist);
2112 retval = register_analog_devices(dev);
2113 if (retval < 0) {
2114 em28xx_release_resources(dev);
2115 mutex_unlock(&em28xx_devlist_mutex);
2116 goto fail_reg_devices;
2117 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002119 mutex_lock(&em28xx_extension_devlist_lock);
2120 if (!list_empty(&em28xx_extension_devlist)) {
2121 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2122 if (ops->id)
2123 ops->init(dev);
2124 }
2125 }
2126 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002127 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002128
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002129 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002130
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002131fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002132 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002133 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134}
2135
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002136#if defined(CONFIG_MODULES) && defined(MODULE)
2137static void request_module_async(struct work_struct *work)
2138{
2139 struct em28xx *dev = container_of(work,
2140 struct em28xx, request_module_wk);
2141
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002142 if (dev->has_audio_class)
2143 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002144 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002145 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002146
2147 if (dev->has_dvb)
2148 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002149}
2150
2151static void request_modules(struct em28xx *dev)
2152{
2153 INIT_WORK(&dev->request_module_wk, request_module_async);
2154 schedule_work(&dev->request_module_wk);
2155}
2156#else
2157#define request_modules(dev)
2158#endif /* CONFIG_MODULES */
2159
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002160/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002161 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162 * checks for supported devices
2163 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002164static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002165 const struct usb_device_id *id)
2166{
2167 const struct usb_endpoint_descriptor *endpoint;
2168 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002169 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002170 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002172 int i, nr, ifnum, isoc_pipe;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173
2174 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002175 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2176
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002177 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002178 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2179 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002180
2181 /* Don't register audio interfaces */
2182 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002183 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002184 udev->descriptor.idVendor,
2185 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002186 ifnum,
2187 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002188
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002189 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002190 return -ENODEV;
2191 }
2192
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002193 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002194 udev->descriptor.idVendor,
2195 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002196 ifnum,
2197 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002199 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002200
Michael Opdenacker59c51592007-05-09 08:57:56 +02002201 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002202 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2203 USB_ENDPOINT_XFER_ISOC &&
2204 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2205 {
2206 /* It's a newer em2874/em2875 device */
2207 isoc_pipe = 0;
2208 } else {
2209 isoc_pipe = 1;
2210 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2211 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2212 USB_ENDPOINT_XFER_ISOC) {
2213 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2214 em28xx_devused &= ~(1<<nr);
2215 return -ENODEV;
2216 }
2217 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2218 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2219 em28xx_devused &= ~(1<<nr);
2220 return -ENODEV;
2221 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222 }
2223
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002224 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002225 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2226 EM28XX_MAXBOARDS);
2227 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002228 return -ENOMEM;
2229 }
2230
2231 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002232 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002233 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002234 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002235 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002236 return -ENOMEM;
2237 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002238
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002239 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002240 dev->devno = nr;
2241 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002242 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002243
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002244 /* Checks if audio is provided by some interface */
2245 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2246 uif = udev->config->interface[i];
2247 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2248 dev->has_audio_class = 1;
2249 break;
2250 }
2251 }
2252
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002253 /* compute alternate max packet sizes */
2254 uif = udev->actconfig->interface[0];
2255
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002256 dev->num_alt = uif->num_altsetting;
2257 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2258/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2259 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2260
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002261 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002262 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002263 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002264 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002265 return -ENOMEM;
2266 }
2267
2268 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002269 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002270 wMaxPacketSize);
2271 dev->alt_max_pkt_size[i] =
2272 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002273 em28xx_info("Alternate setting %i, max size= %i\n", i,
2274 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002275 }
2276
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002277 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002278 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002279
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002281 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002282 if (retval) {
2283 em28xx_devused &= ~(1<<dev->devno);
2284 kfree(dev);
2285
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002286 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002287 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002289 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002290
2291 /* save our data pointer in this interface device */
2292 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002293
2294 request_modules(dev);
2295
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002296 return 0;
2297}
2298
2299/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002300 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002301 * called when the device gets diconencted
2302 * video device will be unregistered on v4l2_close in case it is still open
2303 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002304static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305{
Sascha Sommer5a804152007-11-03 21:22:38 -03002306 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002307 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002308
2309 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310 usb_set_intfdata(interface, NULL);
2311
2312 if (!dev)
2313 return;
2314
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002315 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002316
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002317 /* wait until all current v4l2 io is finished then deallocate
2318 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002319 mutex_lock(&dev->lock);
2320
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002321 wake_up_interruptible_all(&dev->open);
2322
2323 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002324 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002325 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002326 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002327 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002328
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002329 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002330 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002331 dev->state |= DEV_DISCONNECTED;
2332 wake_up_interruptible(&dev->wait_frame);
2333 wake_up_interruptible(&dev->wait_stream);
2334 } else {
2335 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002336 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002337 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002338 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002339
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002340 mutex_lock(&em28xx_extension_devlist_lock);
2341 if (!list_empty(&em28xx_extension_devlist)) {
2342 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2343 ops->fini(dev);
2344 }
2345 }
2346 mutex_unlock(&em28xx_extension_devlist_lock);
2347
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002348 if (!dev->users) {
2349 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002350 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002351 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002352}
2353
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002354static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002355 .name = "em28xx",
2356 .probe = em28xx_usb_probe,
2357 .disconnect = em28xx_usb_disconnect,
2358 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002359};
2360
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002361static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002362{
2363 int result;
2364
2365 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002366 (EM28XX_VERSION_CODE >> 16) & 0xff,
2367 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002368#ifdef SNAPSHOT
2369 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2370 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2371#endif
2372
2373 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002374 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002375 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002376 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002377 " usb_register failed. Error number %d.\n", result);
2378
2379 return result;
2380}
2381
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002382static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002383{
2384 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002385 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386}
2387
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002388module_init(em28xx_module_init);
2389module_exit(em28xx_module_exit);