blob: 4ea4920c927a13b9c2a8c4ed2101106fc6ab0bd4 [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
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030099/* supported video standards */
100static struct em28xx_fmt format[] = {
101 {
102 .name = "16bpp YUY2, 4:2:2, packed",
103 .fourcc = V4L2_PIX_FMT_YUYV,
104 .depth = 16,
105 .reg = 0x14,
106 },
107};
108
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800109/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200110/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800111static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800112 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200113 .id = V4L2_CID_AUDIO_VOLUME,
114 .type = V4L2_CTRL_TYPE_INTEGER,
115 .name = "Volume",
116 .minimum = 0x0,
117 .maximum = 0x1f,
118 .step = 0x1,
119 .default_value = 0x1f,
120 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300121 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200122 .id = V4L2_CID_AUDIO_MUTE,
123 .type = V4L2_CTRL_TYPE_BOOLEAN,
124 .name = "Mute",
125 .minimum = 0,
126 .maximum = 1,
127 .step = 1,
128 .default_value = 1,
129 .flags = 0,
130 }
131};
132
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800133static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800134
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300135/* ------------------------------------------------------------------
136 DMA and thread functions
137 ------------------------------------------------------------------*/
138
139/*
140 * Announces that a buffer were filled and request the next
141 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300142static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300143 struct em28xx_dmaqueue *dma_q,
144 struct em28xx_buffer *buf)
145{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300146 /* Advice that buffer was filled */
147 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
148 buf->vb.state = VIDEOBUF_DONE;
149 buf->vb.field_count++;
150 do_gettimeofday(&buf->vb.ts);
151
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300152 dev->isoc_ctl.buf = NULL;
153
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300154 list_del(&buf->vb.queue);
155 wake_up(&buf->vb.done);
156}
157
158/*
159 * Identify the buffer header type and properly handles
160 */
161static void em28xx_copy_video(struct em28xx *dev,
162 struct em28xx_dmaqueue *dma_q,
163 struct em28xx_buffer *buf,
164 unsigned char *p,
165 unsigned char *outp, unsigned long len)
166{
167 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300168 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300169 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300170
171 if (dma_q->pos + len > buf->vb.size)
172 len = buf->vb.size - dma_q->pos;
173
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300174 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300175 em28xx_isocdbg("frame is not complete\n");
176 len += 4;
177 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300178 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300179
180 startread = p;
181 remain = len;
182
183 /* Interlaces frame */
184 if (buf->top_field)
185 fieldstart = outp;
186 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300187 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300188
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300189 linesdone = dma_q->pos / bytesperline;
190 currlinedone = dma_q->pos % bytesperline;
191 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300192 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300193 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300194 lencopy = lencopy > remain ? remain : lencopy;
195
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300196 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300197 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300198 ((char *)startwrite + lencopy) -
199 ((char *)outp + buf->vb.size));
200 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300201 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300202 if (lencopy <= 0)
203 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300204 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205
206 remain -= lencopy;
207
208 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300209 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300211 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300212 lencopy = remain;
213 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300214 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300216 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300217 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300218 ((char *)startwrite + lencopy) -
219 ((char *)outp + buf->vb.size));
220 lencopy = remain = (char *)outp + buf->vb.size -
221 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300222 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300223 if (lencopy <= 0)
224 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300225
226 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300227
228 remain -= lencopy;
229 }
230
231 dma_q->pos += len;
232}
233
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300235 int packet, int status)
236{
237 char *errmsg = "Unknown";
238
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300239 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300240 case -ENOENT:
241 errmsg = "unlinked synchronuously";
242 break;
243 case -ECONNRESET:
244 errmsg = "unlinked asynchronuously";
245 break;
246 case -ENOSR:
247 errmsg = "Buffer error (overrun)";
248 break;
249 case -EPIPE:
250 errmsg = "Stalled (device not responding)";
251 break;
252 case -EOVERFLOW:
253 errmsg = "Babble (bad cable?)";
254 break;
255 case -EPROTO:
256 errmsg = "Bit-stuff error (bad cable?)";
257 break;
258 case -EILSEQ:
259 errmsg = "CRC/Timeout (could be anything)";
260 break;
261 case -ETIME:
262 errmsg = "Device does not respond";
263 break;
264 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300265 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
267 } else {
268 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
269 packet, status, errmsg);
270 }
271}
272
273/*
274 * video-buf generic routine to get the next available buffer
275 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300276static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300277 struct em28xx_buffer **buf)
278{
279 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300280 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300281
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300282 if (list_empty(&dma_q->active)) {
283 em28xx_isocdbg("No active queue to serve\n");
284 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300285 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300286 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300287 }
288
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300289 /* Get the next buffer */
290 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
291
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300292 /* Cleans up buffer - Usefull for testing for frame/URB loss */
293 outp = videobuf_to_vmalloc(&(*buf)->vb);
294 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300295
296 dev->isoc_ctl.buf = *buf;
297
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300298 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299}
300
301/*
302 * Controls the isoc copy of each urb packet
303 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300304static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300305{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300306 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300307 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300308 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300309 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300310 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300311
312 if (!dev)
313 return 0;
314
315 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
316 return 0;
317
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300318 if (urb->status < 0) {
319 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300320 if (urb->status == -ENOENT)
321 return 0;
322 }
323
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300324 buf = dev->isoc_ctl.buf;
325 if (buf != NULL)
326 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300327
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 for (i = 0; i < urb->number_of_packets; i++) {
329 int status = urb->iso_frame_desc[i].status;
330
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300331 if (status < 0) {
332 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300333 if (urb->iso_frame_desc[i].status != -EPROTO)
334 continue;
335 }
336
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300337 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300338
339 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300340 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300341 continue;
342 }
343 if (urb->iso_frame_desc[i].actual_length >
344 dev->max_pkt_size) {
345 em28xx_isocdbg("packet bigger than packet size");
346 continue;
347 }
348
349 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300350
351 /* FIXME: incomplete buffer checks where removed to make
352 logic simpler. Impacts of those changes should be evaluated
353 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300354 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
355 em28xx_isocdbg("VBI HEADER!!!\n");
356 /* FIXME: Should add vbi copy */
357 continue;
358 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300359 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300360 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
361 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300362
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300363 if (!(p[2] & 1)) {
364 if (buf != NULL)
365 buffer_filled(dev, dma_q, buf);
366 get_next_buf(dma_q, &buf);
367 if (buf == NULL)
368 outp = NULL;
369 else
370 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300371 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300372
373 if (buf != NULL) {
374 if (p[2] & 1)
375 buf->top_field = 0;
376 else
377 buf->top_field = 1;
378 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300379
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300380 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300381 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300382 if (buf != NULL)
383 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300384 }
385 return rc;
386}
387
388/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 Videobuf operations
390 ------------------------------------------------------------------*/
391
392static int
393buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
394{
395 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300396 struct em28xx *dev = fh->dev;
397 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300398
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300399 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
400
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401 if (0 == *count)
402 *count = EM28XX_DEF_BUF;
403
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300404 if (*count < EM28XX_MIN_BUF)
405 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300407 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300408 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300409 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300410 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300411
412 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
413
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300414 return 0;
415}
416
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300417/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300418static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
419{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300420 struct em28xx_fh *fh = vq->priv_data;
421 struct em28xx *dev = fh->dev;
422 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300423 if (in_interrupt())
424 BUG();
425
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300426 /* We used to wait for the buffer to finish here, but this didn't work
427 because, as we were keeping the state as VIDEOBUF_QUEUED,
428 videobuf_queue_cancel marked it as finished for us.
429 (Also, it could wedge forever if the hardware was misconfigured.)
430
431 This should be safe; by the time we get here, the buffer isn't
432 queued anymore. If we ever start marking the buffers as
433 VIDEOBUF_ACTIVE, it won't be, though.
434 */
435 spin_lock_irqsave(&dev->slock, flags);
436 if (dev->isoc_ctl.buf == buf)
437 dev->isoc_ctl.buf = NULL;
438 spin_unlock_irqrestore(&dev->slock, flags);
439
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440 videobuf_vmalloc_free(&buf->vb);
441 buf->vb.state = VIDEOBUF_NEEDS_INIT;
442}
443
444static int
445buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
446 enum v4l2_field field)
447{
448 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300449 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450 struct em28xx *dev = fh->dev;
451 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300453 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454
455 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
456 return -EINVAL;
457
Brandon Philips05612972008-04-13 14:57:01 -0300458 buf->vb.width = dev->width;
459 buf->vb.height = dev->height;
460 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461
462 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300463 rc = videobuf_iolock(vq, &buf->vb, NULL);
464 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 }
467
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300468 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300469 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300470
471 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300472 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
473 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300474 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300475 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300476 goto fail;
477 }
478
479 buf->vb.state = VIDEOBUF_PREPARED;
480 return 0;
481
482fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300483 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484 return rc;
485}
486
487static void
488buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
489{
490 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
491 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300492 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300495 buf->vb.state = VIDEOBUF_QUEUED;
496 list_add_tail(&buf->vb.queue, &vidq->active);
497
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498}
499
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300500static void buffer_release(struct videobuf_queue *vq,
501 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300502{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300503 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300504 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300505 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300506
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300507 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300509 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300510}
511
512static struct videobuf_queue_ops em28xx_video_qops = {
513 .buf_setup = buffer_setup,
514 .buf_prepare = buffer_prepare,
515 .buf_queue = buffer_queue,
516 .buf_release = buffer_release,
517};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800518
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300519/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800521/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800522 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800523 * inits registers with sane defaults
524 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800525static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800526{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300527 int retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528
529 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300530 if (!dev->board.is_em2800) {
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300531 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300532 if (retval < 0) {
533 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
534 __func__, retval);
535 return retval;
536 }
537 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800538
539 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200540
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300541/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
542/* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
543 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200544
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800545 dev->mute = 1; /* maybe not the right place... */
546 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300547
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300548 em28xx_set_outfmt(dev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800549 em28xx_colorlevels_set_default(dev);
550 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800551
552 return 0;
553}
554
555/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800556 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800557 * configure i2c attached devices
558 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800559static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800560{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300561 struct v4l2_routing route;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300562 int zero = 0;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300563
564 route.input = INPUT(dev->ctl_input)->vmux;
565 route.output = 0;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300566 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300567 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300568 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800569}
570
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800571static void video_mux(struct em28xx *dev, int index)
572{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300573 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800574
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300575 route.input = INPUT(index)->vmux;
576 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800577 dev->ctl_input = index;
578 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300579 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800580
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300581 if (!dev->ctl_aoutput)
582 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
583
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300584 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800585
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300586 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300587 if (dev->i2s_speed) {
588 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
589 &dev->i2s_speed);
590 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300591 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300592 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300593 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300594 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
595 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800596 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300597
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300598 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800599}
600
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300601/* Usage lock check functions */
602static int res_get(struct em28xx_fh *fh)
603{
604 struct em28xx *dev = fh->dev;
605 int rc = 0;
606
607 /* This instance already has stream_on */
608 if (fh->stream_on)
609 return rc;
610
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300611 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300612 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300613
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300614 dev->stream_on = 1;
615 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300616 return rc;
617}
618
619static int res_check(struct em28xx_fh *fh)
620{
621 return (fh->stream_on);
622}
623
624static void res_free(struct em28xx_fh *fh)
625{
626 struct em28xx *dev = fh->dev;
627
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300628 fh->stream_on = 0;
629 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300630}
631
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800632/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300633 * em28xx_get_ctrl()
634 * return the current saturation, brightness or contrast, mute state
635 */
636static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
637{
638 switch (ctrl->id) {
639 case V4L2_CID_AUDIO_MUTE:
640 ctrl->value = dev->mute;
641 return 0;
642 case V4L2_CID_AUDIO_VOLUME:
643 ctrl->value = dev->volume;
644 return 0;
645 default:
646 return -EINVAL;
647 }
648}
649
650/*
651 * em28xx_set_ctrl()
652 * mute or set new saturation, brightness or contrast
653 */
654static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
655{
656 switch (ctrl->id) {
657 case V4L2_CID_AUDIO_MUTE:
658 if (ctrl->value != dev->mute) {
659 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300660 return em28xx_audio_analog_set(dev);
661 }
662 return 0;
663 case V4L2_CID_AUDIO_VOLUME:
664 dev->volume = ctrl->value;
665 return em28xx_audio_analog_set(dev);
666 default:
667 return -EINVAL;
668 }
669}
670
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300671static int check_dev(struct em28xx *dev)
672{
673 if (dev->state & DEV_DISCONNECTED) {
674 em28xx_errdev("v4l2 ioctl: device not present\n");
675 return -ENODEV;
676 }
677
678 if (dev->state & DEV_MISCONFIGURED) {
679 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
680 "close and open it again\n");
681 return -EIO;
682 }
683 return 0;
684}
685
686static void get_scale(struct em28xx *dev,
687 unsigned int width, unsigned int height,
688 unsigned int *hscale, unsigned int *vscale)
689{
690 unsigned int maxw = norm_maxw(dev);
691 unsigned int maxh = norm_maxh(dev);
692
693 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
694 if (*hscale >= 0x4000)
695 *hscale = 0x3fff;
696
697 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
698 if (*vscale >= 0x4000)
699 *vscale = 0x3fff;
700}
701
702/* ------------------------------------------------------------------
703 IOCTL vidioc handling
704 ------------------------------------------------------------------*/
705
Hans Verkuil78b526a2008-05-28 12:16:41 -0300706static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300707 struct v4l2_format *f)
708{
709 struct em28xx_fh *fh = priv;
710 struct em28xx *dev = fh->dev;
711
712 mutex_lock(&dev->lock);
713
714 f->fmt.pix.width = dev->width;
715 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300716 f->fmt.pix.pixelformat = dev->format->fourcc;
717 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300718 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300719 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
720
721 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
722 f->fmt.pix.field = dev->interlaced ?
723 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
724
725 mutex_unlock(&dev->lock);
726 return 0;
727}
728
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300729static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
730{
731 unsigned int i;
732
733 for (i = 0; i < ARRAY_SIZE(format); i++)
734 if (format[i].fourcc == fourcc)
735 return &format[i];
736
737 return NULL;
738}
739
Hans Verkuil78b526a2008-05-28 12:16:41 -0300740static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300741 struct v4l2_format *f)
742{
743 struct em28xx_fh *fh = priv;
744 struct em28xx *dev = fh->dev;
745 int width = f->fmt.pix.width;
746 int height = f->fmt.pix.height;
747 unsigned int maxw = norm_maxw(dev);
748 unsigned int maxh = norm_maxh(dev);
749 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300750 struct em28xx_fmt *fmt;
751
752 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
753 if (!fmt) {
754 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
755 f->fmt.pix.pixelformat);
756 return -EINVAL;
757 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300758
759 /* width must even because of the YUYV format
760 height must be even because of interlacing */
761 height &= 0xfffe;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300762 width &= 0xfffe;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300763
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300764 if (unlikely(height < 32))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300765 height = 32;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300766 if (unlikely(height > maxh))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300767 height = maxh;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300768 if (unlikely(width < 48))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300769 width = 48;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300770 if (unlikely(width > maxw))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300771 width = maxw;
772
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300773 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300774 /* the em2800 can only scale down to 50% */
775 if (height % (maxh / 2))
776 height = maxh;
777 if (width % (maxw / 2))
778 width = maxw;
779 /* according to empiatech support */
780 /* the MaxPacketSize is to small to support */
781 /* framesizes larger than 640x480 @ 30 fps */
782 /* or 640x576 @ 25 fps. As this would cut */
783 /* of a part of the image we prefer */
784 /* 360x576 or 360x480 for now */
785 if (width == maxw && height == maxh)
786 width /= 2;
787 }
788
789 get_scale(dev, width, height, &hscale, &vscale);
790
791 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
792 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
793
794 f->fmt.pix.width = width;
795 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300796 f->fmt.pix.pixelformat = fmt->fourcc;
797 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
798 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
800 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
801
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300802 return 0;
803}
804
Hans Verkuil78b526a2008-05-28 12:16:41 -0300805static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300806 struct v4l2_format *f)
807{
808 struct em28xx_fh *fh = priv;
809 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300810 int rc;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300811 struct em28xx_fmt *fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812
813 rc = check_dev(dev);
814 if (rc < 0)
815 return rc;
816
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 mutex_lock(&dev->lock);
818
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300819 vidioc_try_fmt_vid_cap(file, priv, f);
820
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300821 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -0300822 if (!fmt) {
823 rc = -EINVAL;
824 goto out;
825 }
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300826
Brandon Philips05612972008-04-13 14:57:01 -0300827 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
828 em28xx_errdev("%s queue busy\n", __func__);
829 rc = -EBUSY;
830 goto out;
831 }
832
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300833 if (dev->stream_on && !fh->stream_on) {
834 em28xx_errdev("%s device in use by another fh\n", __func__);
835 rc = -EBUSY;
836 goto out;
837 }
838
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300839 /* set new image size */
840 dev->width = f->fmt.pix.width;
841 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300842 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300843 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
844
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300846 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300847
Brandon Philips05612972008-04-13 14:57:01 -0300848 rc = 0;
849
850out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300851 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300852 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300853}
854
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300855static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300856{
857 struct em28xx_fh *fh = priv;
858 struct em28xx *dev = fh->dev;
859 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300860 int rc;
861
862 rc = check_dev(dev);
863 if (rc < 0)
864 return rc;
865
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300867 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300868
869 /* Adjusts width/height, if needed */
870 f.fmt.pix.width = dev->width;
871 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300872 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300873
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300874 /* set new image size */
875 dev->width = f.fmt.pix.width;
876 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300877 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
878
879 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300880 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300881
882 mutex_unlock(&dev->lock);
883 return 0;
884}
885
886static const char *iname[] = {
887 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
888 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
889 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
890 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
891 [EM28XX_VMUX_SVIDEO] = "S-Video",
892 [EM28XX_VMUX_TELEVISION] = "Television",
893 [EM28XX_VMUX_CABLE] = "Cable TV",
894 [EM28XX_VMUX_DVB] = "DVB",
895 [EM28XX_VMUX_DEBUG] = "for debug only",
896};
897
898static int vidioc_enum_input(struct file *file, void *priv,
899 struct v4l2_input *i)
900{
901 struct em28xx_fh *fh = priv;
902 struct em28xx *dev = fh->dev;
903 unsigned int n;
904
905 n = i->index;
906 if (n >= MAX_EM28XX_INPUT)
907 return -EINVAL;
908 if (0 == INPUT(n)->type)
909 return -EINVAL;
910
911 i->index = n;
912 i->type = V4L2_INPUT_TYPE_CAMERA;
913
914 strcpy(i->name, iname[INPUT(n)->type]);
915
916 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
917 (EM28XX_VMUX_CABLE == INPUT(n)->type))
918 i->type = V4L2_INPUT_TYPE_TUNER;
919
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300920 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300921
922 return 0;
923}
924
925static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
926{
927 struct em28xx_fh *fh = priv;
928 struct em28xx *dev = fh->dev;
929
930 *i = dev->ctl_input;
931
932 return 0;
933}
934
935static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
936{
937 struct em28xx_fh *fh = priv;
938 struct em28xx *dev = fh->dev;
939 int rc;
940
941 rc = check_dev(dev);
942 if (rc < 0)
943 return rc;
944
945 if (i >= MAX_EM28XX_INPUT)
946 return -EINVAL;
947 if (0 == INPUT(i)->type)
948 return -EINVAL;
949
950 mutex_lock(&dev->lock);
951
952 video_mux(dev, i);
953
954 mutex_unlock(&dev->lock);
955 return 0;
956}
957
958static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
959{
960 struct em28xx_fh *fh = priv;
961 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300962
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300963 switch (a->index) {
964 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300966 break;
967 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300969 break;
970 case EM28XX_AMUX_VIDEO2:
971 strcpy(a->name, "Television alt");
972 break;
973 case EM28XX_AMUX_PHONE:
974 strcpy(a->name, "Phone");
975 break;
976 case EM28XX_AMUX_MIC:
977 strcpy(a->name, "Mic");
978 break;
979 case EM28XX_AMUX_CD:
980 strcpy(a->name, "CD");
981 break;
982 case EM28XX_AMUX_AUX:
983 strcpy(a->name, "Aux");
984 break;
985 case EM28XX_AMUX_PCM_OUT:
986 strcpy(a->name, "PCM");
987 break;
988 default:
989 return -EINVAL;
990 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300991
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300992 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300993 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300994
995 return 0;
996}
997
998static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
999{
1000 struct em28xx_fh *fh = priv;
1001 struct em28xx *dev = fh->dev;
1002
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001003 mutex_lock(&dev->lock);
1004
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001005 dev->ctl_ainput = INPUT(a->index)->amux;
1006 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001007
1008 if (!dev->ctl_aoutput)
1009 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001010
1011 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012 return 0;
1013}
1014
1015static int vidioc_queryctrl(struct file *file, void *priv,
1016 struct v4l2_queryctrl *qc)
1017{
1018 struct em28xx_fh *fh = priv;
1019 struct em28xx *dev = fh->dev;
1020 int id = qc->id;
1021 int i;
1022 int rc;
1023
1024 rc = check_dev(dev);
1025 if (rc < 0)
1026 return rc;
1027
1028 memset(qc, 0, sizeof(*qc));
1029
1030 qc->id = id;
1031
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001032 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001033 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1034 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1035 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1036 return 0;
1037 }
1038 }
1039 }
1040 mutex_lock(&dev->lock);
1041 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1042 mutex_unlock(&dev->lock);
1043
1044 if (qc->type)
1045 return 0;
1046 else
1047 return -EINVAL;
1048}
1049
1050static int vidioc_g_ctrl(struct file *file, void *priv,
1051 struct v4l2_control *ctrl)
1052{
1053 struct em28xx_fh *fh = priv;
1054 struct em28xx *dev = fh->dev;
1055 int rc;
1056
1057 rc = check_dev(dev);
1058 if (rc < 0)
1059 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001060 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001061
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001062 mutex_lock(&dev->lock);
1063
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001064 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001065 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001066 else
1067 rc = em28xx_get_ctrl(dev, ctrl);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001068
1069 mutex_unlock(&dev->lock);
1070 return rc;
1071}
1072
1073static int vidioc_s_ctrl(struct file *file, void *priv,
1074 struct v4l2_control *ctrl)
1075{
1076 struct em28xx_fh *fh = priv;
1077 struct em28xx *dev = fh->dev;
1078 u8 i;
1079 int rc;
1080
1081 rc = check_dev(dev);
1082 if (rc < 0)
1083 return rc;
1084
1085 mutex_lock(&dev->lock);
1086
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001087 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001088 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1089 else {
1090 rc = 1;
1091 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1092 if (ctrl->id == em28xx_qctrl[i].id) {
1093 if (ctrl->value < em28xx_qctrl[i].minimum ||
1094 ctrl->value > em28xx_qctrl[i].maximum) {
1095 rc = -ERANGE;
1096 break;
1097 }
1098
1099 rc = em28xx_set_ctrl(dev, ctrl);
1100 break;
1101 }
1102 }
1103 }
1104
1105 /* Control not found - try to send it to the attached devices */
1106 if (rc == 1) {
1107 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1108 rc = 0;
1109 }
1110
1111 mutex_unlock(&dev->lock);
1112 return rc;
1113}
1114
1115static int vidioc_g_tuner(struct file *file, void *priv,
1116 struct v4l2_tuner *t)
1117{
1118 struct em28xx_fh *fh = priv;
1119 struct em28xx *dev = fh->dev;
1120 int rc;
1121
1122 rc = check_dev(dev);
1123 if (rc < 0)
1124 return rc;
1125
1126 if (0 != t->index)
1127 return -EINVAL;
1128
1129 strcpy(t->name, "Tuner");
1130
1131 mutex_lock(&dev->lock);
1132
1133 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1134
1135 mutex_unlock(&dev->lock);
1136 return 0;
1137}
1138
1139static int vidioc_s_tuner(struct file *file, void *priv,
1140 struct v4l2_tuner *t)
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 != t->index)
1151 return -EINVAL;
1152
1153 mutex_lock(&dev->lock);
1154
1155 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1156
1157 mutex_unlock(&dev->lock);
1158 return 0;
1159}
1160
1161static int vidioc_g_frequency(struct file *file, void *priv,
1162 struct v4l2_frequency *f)
1163{
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
1166
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001167 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001168 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001170 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001171
1172 return 0;
1173}
1174
1175static int vidioc_s_frequency(struct file *file, void *priv,
1176 struct v4l2_frequency *f)
1177{
1178 struct em28xx_fh *fh = priv;
1179 struct em28xx *dev = fh->dev;
1180 int rc;
1181
1182 rc = check_dev(dev);
1183 if (rc < 0)
1184 return rc;
1185
1186 if (0 != f->tuner)
1187 return -EINVAL;
1188
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001189 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1190 return -EINVAL;
1191 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001192 return -EINVAL;
1193
1194 mutex_lock(&dev->lock);
1195
1196 dev->ctl_freq = f->frequency;
1197 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1198
1199 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001200
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001201 return 0;
1202}
1203
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001204#ifdef CONFIG_VIDEO_ADV_DEBUG
1205static int em28xx_reg_len(int reg)
1206{
1207 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001208 case EM28XX_R40_AC97LSB:
1209 case EM28XX_R30_HSCALELOW:
1210 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001211 return 2;
1212 default:
1213 return 1;
1214 }
1215}
1216
1217static int vidioc_g_register(struct file *file, void *priv,
1218 struct v4l2_register *reg)
1219{
1220 struct em28xx_fh *fh = priv;
1221 struct em28xx *dev = fh->dev;
1222 int ret;
1223
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001224 if (reg->match_type == V4L2_CHIP_MATCH_AC97) {
1225 mutex_lock(&dev->lock);
1226 ret = em28xx_read_ac97(dev, reg->reg);
1227 mutex_unlock(&dev->lock);
1228 if (ret < 0)
1229 return ret;
1230
1231 reg->val = ret;
1232 return 0;
1233 }
1234
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001235 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1236 return -EINVAL;
1237
1238 if (em28xx_reg_len(reg->reg) == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001239 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001240 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001241 mutex_unlock(&dev->lock);
1242
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001243 if (ret < 0)
1244 return ret;
1245
1246 reg->val = ret;
1247 } else {
Al Viroa954b662008-05-21 00:32:51 -03001248 __le64 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001249 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001250 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1251 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001252 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001253 if (ret < 0)
1254 return ret;
1255
Al Viroa954b662008-05-21 00:32:51 -03001256 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001257 }
1258
1259 return 0;
1260}
1261
1262static int vidioc_s_register(struct file *file, void *priv,
1263 struct v4l2_register *reg)
1264{
1265 struct em28xx_fh *fh = priv;
1266 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001267 __le64 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001268 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001269
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001270 if (reg->match_type == V4L2_CHIP_MATCH_AC97) {
1271 mutex_lock(&dev->lock);
1272 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1273 mutex_unlock(&dev->lock);
1274
1275 return rc;
1276 }
1277
Al Viroa954b662008-05-21 00:32:51 -03001278 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001279
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001280 mutex_lock(&dev->lock);
1281 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1282 em28xx_reg_len(reg->reg));
1283 mutex_unlock(&dev->lock);
1284
1285 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001286}
1287#endif
1288
1289
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290static int vidioc_cropcap(struct file *file, void *priv,
1291 struct v4l2_cropcap *cc)
1292{
1293 struct em28xx_fh *fh = priv;
1294 struct em28xx *dev = fh->dev;
1295
1296 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1297 return -EINVAL;
1298
1299 cc->bounds.left = 0;
1300 cc->bounds.top = 0;
1301 cc->bounds.width = dev->width;
1302 cc->bounds.height = dev->height;
1303 cc->defrect = cc->bounds;
1304 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1305 cc->pixelaspect.denominator = 59;
1306
1307 return 0;
1308}
1309
1310static int vidioc_streamon(struct file *file, void *priv,
1311 enum v4l2_buf_type type)
1312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
1315 int rc;
1316
1317 rc = check_dev(dev);
1318 if (rc < 0)
1319 return rc;
1320
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001322 mutex_lock(&dev->lock);
1323 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001324
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001325 if (likely(rc >= 0))
1326 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001327
1328 mutex_unlock(&dev->lock);
1329
1330 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001331}
1332
1333static int vidioc_streamoff(struct file *file, void *priv,
1334 enum v4l2_buf_type type)
1335{
1336 struct em28xx_fh *fh = priv;
1337 struct em28xx *dev = fh->dev;
1338 int rc;
1339
1340 rc = check_dev(dev);
1341 if (rc < 0)
1342 return rc;
1343
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001344 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1345 return -EINVAL;
1346 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347 return -EINVAL;
1348
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001349 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001350
1351 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001352 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001353
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001354 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 return 0;
1357}
1358
1359static int vidioc_querycap(struct file *file, void *priv,
1360 struct v4l2_capability *cap)
1361{
1362 struct em28xx_fh *fh = priv;
1363 struct em28xx *dev = fh->dev;
1364
1365 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1366 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001367 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368
1369 cap->version = EM28XX_VERSION_CODE;
1370
1371 cap->capabilities =
1372 V4L2_CAP_SLICED_VBI_CAPTURE |
1373 V4L2_CAP_VIDEO_CAPTURE |
1374 V4L2_CAP_AUDIO |
1375 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1376
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001377 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001378 cap->capabilities |= V4L2_CAP_TUNER;
1379
1380 return 0;
1381}
1382
Hans Verkuil78b526a2008-05-28 12:16:41 -03001383static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001384 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001385{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001386 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387 return -EINVAL;
1388
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001389 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1390 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001391
1392 return 0;
1393}
1394
1395/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001396static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001397 struct v4l2_format *f)
1398{
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1401 int rc;
1402
1403 rc = check_dev(dev);
1404 if (rc < 0)
1405 return rc;
1406
1407 mutex_lock(&dev->lock);
1408
1409 f->fmt.sliced.service_set = 0;
1410
1411 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1412
1413 if (f->fmt.sliced.service_set == 0)
1414 rc = -EINVAL;
1415
1416 mutex_unlock(&dev->lock);
1417 return rc;
1418}
1419
Hans Verkuil78b526a2008-05-28 12:16:41 -03001420static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421 struct v4l2_format *f)
1422{
1423 struct em28xx_fh *fh = priv;
1424 struct em28xx *dev = fh->dev;
1425 int rc;
1426
1427 rc = check_dev(dev);
1428 if (rc < 0)
1429 return rc;
1430
1431 mutex_lock(&dev->lock);
1432 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1433 mutex_unlock(&dev->lock);
1434
1435 if (f->fmt.sliced.service_set == 0)
1436 return -EINVAL;
1437
1438 return 0;
1439}
1440
1441
1442static int vidioc_reqbufs(struct file *file, void *priv,
1443 struct v4l2_requestbuffers *rb)
1444{
1445 struct em28xx_fh *fh = priv;
1446 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001447 int rc;
1448
1449 rc = check_dev(dev);
1450 if (rc < 0)
1451 return rc;
1452
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001453 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001454}
1455
1456static int vidioc_querybuf(struct file *file, void *priv,
1457 struct v4l2_buffer *b)
1458{
1459 struct em28xx_fh *fh = priv;
1460 struct em28xx *dev = fh->dev;
1461 int rc;
1462
1463 rc = check_dev(dev);
1464 if (rc < 0)
1465 return rc;
1466
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001467 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001468}
1469
1470static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1471{
1472 struct em28xx_fh *fh = priv;
1473 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001474 int rc;
1475
1476 rc = check_dev(dev);
1477 if (rc < 0)
1478 return rc;
1479
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001480 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001481}
1482
1483static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1484{
1485 struct em28xx_fh *fh = priv;
1486 struct em28xx *dev = fh->dev;
1487 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001488
1489 rc = check_dev(dev);
1490 if (rc < 0)
1491 return rc;
1492
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001493 return (videobuf_dqbuf(&fh->vb_vidq, b,
1494 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001495}
1496
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001497#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001498static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001499{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001500 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001501
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001502 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001503}
1504#endif
1505
1506
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001507/* ----------------------------------------------------------- */
1508/* RADIO ESPECIFIC IOCTLS */
1509/* ----------------------------------------------------------- */
1510
1511static int radio_querycap(struct file *file, void *priv,
1512 struct v4l2_capability *cap)
1513{
1514 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1515
1516 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1517 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001518 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001519
1520 cap->version = EM28XX_VERSION_CODE;
1521 cap->capabilities = V4L2_CAP_TUNER;
1522 return 0;
1523}
1524
1525static int radio_g_tuner(struct file *file, void *priv,
1526 struct v4l2_tuner *t)
1527{
1528 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1529
1530 if (unlikely(t->index > 0))
1531 return -EINVAL;
1532
1533 strcpy(t->name, "Radio");
1534 t->type = V4L2_TUNER_RADIO;
1535
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001536 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001537 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001538 mutex_unlock(&dev->lock);
1539
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001540 return 0;
1541}
1542
1543static int radio_enum_input(struct file *file, void *priv,
1544 struct v4l2_input *i)
1545{
1546 if (i->index != 0)
1547 return -EINVAL;
1548 strcpy(i->name, "Radio");
1549 i->type = V4L2_INPUT_TYPE_TUNER;
1550
1551 return 0;
1552}
1553
1554static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1555{
1556 if (unlikely(a->index))
1557 return -EINVAL;
1558
1559 strcpy(a->name, "Radio");
1560 return 0;
1561}
1562
1563static int radio_s_tuner(struct file *file, void *priv,
1564 struct v4l2_tuner *t)
1565{
1566 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1567
1568 if (0 != t->index)
1569 return -EINVAL;
1570
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001571 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001572 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001573 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001574
1575 return 0;
1576}
1577
1578static int radio_s_audio(struct file *file, void *fh,
1579 struct v4l2_audio *a)
1580{
1581 return 0;
1582}
1583
1584static int radio_s_input(struct file *file, void *fh, unsigned int i)
1585{
1586 return 0;
1587}
1588
1589static int radio_queryctrl(struct file *file, void *priv,
1590 struct v4l2_queryctrl *qc)
1591{
1592 int i;
1593
1594 if (qc->id < V4L2_CID_BASE ||
1595 qc->id >= V4L2_CID_LASTP1)
1596 return -EINVAL;
1597
1598 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1599 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1600 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1601 return 0;
1602 }
1603 }
1604
1605 return -EINVAL;
1606}
1607
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001608/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001609 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610 * inits the device and starts isoc transfer
1611 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001612static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001614 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001615 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001616 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001617 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001618 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001619
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001620 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001621 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001622 if (h->vdev->minor == minor) {
1623 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001624 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001625 }
1626 if (h->vbi_dev->minor == minor) {
1627 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001628 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001629 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001630 if (h->radio_dev &&
1631 h->radio_dev->minor == minor) {
1632 radio = 1;
1633 dev = h;
1634 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001635 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001636 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001637
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001638 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001639 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001640
1641 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001642
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001643 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001644 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001647 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001648 if (!fh) {
1649 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001650 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001651 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001653 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001654 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001655 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001656 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001657
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001658 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001659 dev->width = norm_maxw(dev);
1660 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001661 dev->hscale = 0;
1662 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001663
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001664 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001665 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001666 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001667
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001668 /* Needed, since GPIO might have disabled power of
1669 some i2c device
1670 */
1671 em28xx_config_i2c(dev);
1672
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001673 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001674 if (fh->radio) {
1675 em28xx_videodbg("video_open: setting radio device\n");
1676 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1677 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678
1679 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001680
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001681 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1682 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1683 sizeof(struct em28xx_buffer), fh);
1684
Ingo Molnar3593cab2006-02-07 06:49:14 -02001685 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001686
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001687 return errCode;
1688}
1689
1690/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001691 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001692 * unregisters the v4l2,i2c and usb devices
1693 * called when the device gets disconected or at module unload
1694*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001695static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001697
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001698 /*FIXME: I2C IR should be disconnected */
1699
Markus Rechberger9c755412005-11-08 21:37:52 -08001700 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001701 if (dev->sbutton_input_dev)
1702 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001703
1704 if (dev->ir)
1705 em28xx_ir_fini(dev);
1706
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001707 if (dev->radio_dev) {
1708 if (-1 != dev->radio_dev->minor)
1709 video_unregister_device(dev->radio_dev);
1710 else
1711 video_device_release(dev->radio_dev);
1712 dev->radio_dev = NULL;
1713 }
1714 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001715 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1716 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001717 if (-1 != dev->vbi_dev->minor)
1718 video_unregister_device(dev->vbi_dev);
1719 else
1720 video_device_release(dev->vbi_dev);
1721 dev->vbi_dev = NULL;
1722 }
1723 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001724 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1725 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001726 if (-1 != dev->vdev->minor)
1727 video_unregister_device(dev->vdev);
1728 else
1729 video_device_release(dev->vdev);
1730 dev->vdev = NULL;
1731 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001732 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001733 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001734
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001735 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001736 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737}
1738
1739/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001740 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001741 * stops streaming and deallocates all resources allocated by the v4l2
1742 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001744static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001746 struct em28xx_fh *fh = filp->private_data;
1747 struct em28xx *dev = fh->dev;
1748 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001749
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001750 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001751
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001752
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001753 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001754 if (res_check(fh))
1755 res_free(fh);
1756
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001757 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001758 videobuf_stop(&fh->vb_vidq);
1759 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001761 /* the device is already disconnect,
1762 free the remaining resources */
1763 if (dev->state & DEV_DISCONNECTED) {
1764 em28xx_release_resources(dev);
1765 mutex_unlock(&dev->lock);
1766 kfree(dev);
1767 return 0;
1768 }
1769
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001770 /* Save some power by putting tuner to sleep */
1771 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1772
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001773 /* do this before setting alternate! */
1774 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001775 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001776
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001777 /* set alternate 0 */
1778 dev->alt = 0;
1779 em28xx_videodbg("setting alternate 0\n");
1780 errCode = usb_set_interface(dev->udev, 0, 0);
1781 if (errCode < 0) {
1782 em28xx_errdev("cannot change alternate number to "
1783 "0 (error=%i)\n", errCode);
1784 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001786 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001787 dev->users--;
1788 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001789 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001790 return 0;
1791}
1792
1793/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001794 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795 * will allocate buffers when called for the first time
1796 */
1797static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001798em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001799 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001800{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001801 struct em28xx_fh *fh = filp->private_data;
1802 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001803 int rc;
1804
1805 rc = check_dev(dev);
1806 if (rc < 0)
1807 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001808
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001809 /* FIXME: read() is not prepared to allow changing the video
1810 resolution while streaming. Seems a bug at em28xx_set_fmt
1811 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001812
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001813 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001814 mutex_lock(&dev->lock);
1815 rc = res_get(fh);
1816 mutex_unlock(&dev->lock);
1817
1818 if (unlikely(rc < 0))
1819 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001820
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001821 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1822 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001823 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001824 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825}
1826
1827/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001828 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001829 * will allocate buffers when called for the first time
1830 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001831static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001832{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001833 struct em28xx_fh *fh = filp->private_data;
1834 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001835 int rc;
1836
1837 rc = check_dev(dev);
1838 if (rc < 0)
1839 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001840
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001841 mutex_lock(&dev->lock);
1842 rc = res_get(fh);
1843 mutex_unlock(&dev->lock);
1844
1845 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001846 return POLLERR;
1847
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001848 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1849 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001850
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001851 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001852}
1853
1854/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001855 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001856 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001857static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001858{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001859 struct em28xx_fh *fh = filp->private_data;
1860 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001861 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001862
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001863 rc = check_dev(dev);
1864 if (rc < 0)
1865 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001866
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001867 mutex_lock(&dev->lock);
1868 rc = res_get(fh);
1869 mutex_unlock(&dev->lock);
1870
1871 if (unlikely(rc < 0))
1872 return rc;
1873
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001874 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001875
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001876 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1877 (unsigned long)vma->vm_start,
1878 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1879 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001880
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001881 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001882}
1883
Arjan van de Venfa027c22007-02-12 00:55:33 -08001884static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001885 .owner = THIS_MODULE,
1886 .open = em28xx_v4l2_open,
1887 .release = em28xx_v4l2_close,
1888 .read = em28xx_v4l2_read,
1889 .poll = em28xx_v4l2_poll,
1890 .mmap = em28xx_v4l2_mmap,
1891 .ioctl = video_ioctl2,
1892 .llseek = no_llseek,
1893 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894};
1895
Hans Verkuila3998102008-07-21 02:57:38 -03001896static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001897 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001898 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1899 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1900 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1901 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001902 .vidioc_g_audio = vidioc_g_audio,
1903 .vidioc_s_audio = vidioc_s_audio,
1904 .vidioc_cropcap = vidioc_cropcap,
1905
Hans Verkuil78b526a2008-05-28 12:16:41 -03001906 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1907 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1908 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001909
1910 .vidioc_reqbufs = vidioc_reqbufs,
1911 .vidioc_querybuf = vidioc_querybuf,
1912 .vidioc_qbuf = vidioc_qbuf,
1913 .vidioc_dqbuf = vidioc_dqbuf,
1914 .vidioc_s_std = vidioc_s_std,
1915 .vidioc_enum_input = vidioc_enum_input,
1916 .vidioc_g_input = vidioc_g_input,
1917 .vidioc_s_input = vidioc_s_input,
1918 .vidioc_queryctrl = vidioc_queryctrl,
1919 .vidioc_g_ctrl = vidioc_g_ctrl,
1920 .vidioc_s_ctrl = vidioc_s_ctrl,
1921 .vidioc_streamon = vidioc_streamon,
1922 .vidioc_streamoff = vidioc_streamoff,
1923 .vidioc_g_tuner = vidioc_g_tuner,
1924 .vidioc_s_tuner = vidioc_s_tuner,
1925 .vidioc_g_frequency = vidioc_g_frequency,
1926 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001927#ifdef CONFIG_VIDEO_ADV_DEBUG
1928 .vidioc_g_register = vidioc_g_register,
1929 .vidioc_s_register = vidioc_s_register,
1930#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001931#ifdef CONFIG_VIDEO_V4L1_COMPAT
1932 .vidiocgmbuf = vidiocgmbuf,
1933#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001934};
1935
1936static const struct video_device em28xx_video_template = {
1937 .fops = &em28xx_v4l_fops,
1938 .release = video_device_release,
1939 .ioctl_ops = &video_ioctl_ops,
1940
1941 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001942
1943 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001944 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001945};
1946
Hans Verkuila3998102008-07-21 02:57:38 -03001947static const struct file_operations radio_fops = {
1948 .owner = THIS_MODULE,
1949 .open = em28xx_v4l2_open,
1950 .release = em28xx_v4l2_close,
1951 .ioctl = video_ioctl2,
1952 .compat_ioctl = v4l_compat_ioctl32,
1953 .llseek = no_llseek,
1954};
1955
1956static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001957 .vidioc_querycap = radio_querycap,
1958 .vidioc_g_tuner = radio_g_tuner,
1959 .vidioc_enum_input = radio_enum_input,
1960 .vidioc_g_audio = radio_g_audio,
1961 .vidioc_s_tuner = radio_s_tuner,
1962 .vidioc_s_audio = radio_s_audio,
1963 .vidioc_s_input = radio_s_input,
1964 .vidioc_queryctrl = radio_queryctrl,
1965 .vidioc_g_ctrl = vidioc_g_ctrl,
1966 .vidioc_s_ctrl = vidioc_s_ctrl,
1967 .vidioc_g_frequency = vidioc_g_frequency,
1968 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001969#ifdef CONFIG_VIDEO_ADV_DEBUG
1970 .vidioc_g_register = vidioc_g_register,
1971 .vidioc_s_register = vidioc_s_register,
1972#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001973};
1974
Hans Verkuila3998102008-07-21 02:57:38 -03001975static struct video_device em28xx_radio_template = {
1976 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001977 .fops = &radio_fops,
1978 .ioctl_ops = &radio_ioctl_ops,
1979 .minor = -1,
1980};
1981
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001982/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001983
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001984
1985static LIST_HEAD(em28xx_extension_devlist);
1986static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1987
1988int em28xx_register_extension(struct em28xx_ops *ops)
1989{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001990 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001991
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001992 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001993 mutex_lock(&em28xx_extension_devlist_lock);
1994 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001995 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1996 if (dev)
1997 ops->init(dev);
1998 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001999 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2000 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002001 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002002 return 0;
2003}
2004EXPORT_SYMBOL(em28xx_register_extension);
2005
2006void em28xx_unregister_extension(struct em28xx_ops *ops)
2007{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03002008 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002009
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002010 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03002011 list_for_each_entry(dev, &em28xx_devlist, devlist) {
2012 if (dev)
2013 ops->fini(dev);
2014 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002015
2016 mutex_lock(&em28xx_extension_devlist_lock);
2017 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2018 list_del(&ops->next);
2019 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002020 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002021}
2022EXPORT_SYMBOL(em28xx_unregister_extension);
2023
Adrian Bunk532fe652008-01-28 22:10:48 -03002024static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2025 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03002026 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002027{
2028 struct video_device *vfd;
2029
2030 vfd = video_device_alloc();
2031 if (NULL == vfd)
2032 return NULL;
2033 *vfd = *template;
2034 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03002035 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002036 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03002037 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002038
2039 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2040 dev->name, type_name);
2041
2042 return vfd;
2043}
2044
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002045static int register_analog_devices(struct em28xx *dev)
2046{
2047 int ret;
2048
2049 /* allocate and fill video video_device struct */
2050 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2051 if (!dev->vdev) {
2052 em28xx_errdev("cannot allocate video_device.\n");
2053 return -ENODEV;
2054 }
2055
2056 /* register v4l2 video video_device */
2057 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2058 video_nr[dev->devno]);
2059 if (ret) {
2060 em28xx_errdev("unable to register video device (error=%i).\n",
2061 ret);
2062 return ret;
2063 }
2064
2065 /* Allocate and fill vbi video_device struct */
2066 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2067
2068 /* register v4l2 vbi video_device */
2069 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2070 vbi_nr[dev->devno]);
2071 if (ret < 0) {
2072 em28xx_errdev("unable to register vbi device\n");
2073 return ret;
2074 }
2075
2076 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2077 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2078 if (!dev->radio_dev) {
2079 em28xx_errdev("cannot allocate video_device.\n");
2080 return -ENODEV;
2081 }
2082 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2083 radio_nr[dev->devno]);
2084 if (ret < 0) {
2085 em28xx_errdev("can't register radio device\n");
2086 return ret;
2087 }
2088 em28xx_info("Registered radio device as /dev/radio%d\n",
2089 dev->radio_dev->num);
2090 }
2091
2092 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2093 dev->vdev->num, dev->vbi_dev->num);
2094
2095 return 0;
2096}
2097
2098
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002100 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002101 * allocates and inits the device structs, registers i2c bus and v4l device
2102 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002103static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002104 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002106 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002107 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002108 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002109 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002110 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002112 dev->udev = udev;
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002113 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002114 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002115 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002116 init_waitqueue_head(&dev->wait_frame);
2117 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002119 dev->em28xx_write_regs = em28xx_write_regs;
2120 dev->em28xx_read_reg = em28xx_read_reg;
2121 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2122 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2123 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002124 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002125 dev->format = &format[0];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002126
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002127 em28xx_pre_card_setup(dev);
2128
2129 errCode = em28xx_config(dev);
2130 if (errCode) {
2131 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002132 return -ENOMEM;
2133 }
2134
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002135 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002136 errCode = em28xx_i2c_register(dev);
2137 if (errCode < 0) {
2138 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2139 __func__, errCode);
2140 return errCode;
2141 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002142
2143 /* Do board specific init and eeprom reading */
2144 em28xx_card_setup(dev);
2145
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002146 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002147 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002148 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002149 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002150 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002151 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002152
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002153 /* configure the device */
2154 em28xx_config_i2c(dev);
2155
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002156 /* set default norm */
2157 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158
2159 maxw = norm_maxw(dev);
2160 maxh = norm_maxh(dev);
2161
2162 /* set default image size */
2163 dev->width = maxw;
2164 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002165 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002166 dev->hscale = 0;
2167 dev->vscale = 0;
2168 dev->ctl_input = 2;
2169
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002170 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002171 if (errCode < 0) {
2172 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2173 __func__, errCode);
2174 return errCode;
2175 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002176
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002177 /* init video dma queues */
2178 INIT_LIST_HEAD(&dev->vidq.active);
2179 INIT_LIST_HEAD(&dev->vidq.queued);
2180
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002182 if (dev->board.has_msp34xx) {
Sascha Sommer5a804152007-11-03 21:22:38 -03002183 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002184 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002185 if (errCode < 0) {
2186 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2187 __func__, errCode);
2188 return errCode;
2189 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002190 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002191
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002192 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002193 if (errCode < 0) {
2194 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2195 __func__, errCode);
2196 return errCode;
2197 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002198 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002199 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002200
Sascha Sommer5a804152007-11-03 21:22:38 -03002201 video_mux(dev, 0);
2202
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002203 mutex_lock(&em28xx_devlist_mutex);
2204 list_add_tail(&dev->devlist, &em28xx_devlist);
2205 retval = register_analog_devices(dev);
2206 if (retval < 0) {
2207 em28xx_release_resources(dev);
2208 mutex_unlock(&em28xx_devlist_mutex);
2209 goto fail_reg_devices;
2210 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002211
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002212 mutex_lock(&em28xx_extension_devlist_lock);
2213 if (!list_empty(&em28xx_extension_devlist)) {
2214 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2215 if (ops->id)
2216 ops->init(dev);
2217 }
2218 }
2219 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002220 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002221
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002222 /* Save some power by putting tuner to sleep */
2223 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2224
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002225 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002226
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002227fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002228 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002229}
2230
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002231#if defined(CONFIG_MODULES) && defined(MODULE)
2232static void request_module_async(struct work_struct *work)
2233{
2234 struct em28xx *dev = container_of(work,
2235 struct em28xx, request_module_wk);
2236
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002237 if (dev->has_audio_class)
2238 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002239 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002240 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002241
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002242 if (dev->board.has_dvb)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002243 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002244}
2245
2246static void request_modules(struct em28xx *dev)
2247{
2248 INIT_WORK(&dev->request_module_wk, request_module_async);
2249 schedule_work(&dev->request_module_wk);
2250}
2251#else
2252#define request_modules(dev)
2253#endif /* CONFIG_MODULES */
2254
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002256 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002257 * checks for supported devices
2258 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002259static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002260 const struct usb_device_id *id)
2261{
2262 const struct usb_endpoint_descriptor *endpoint;
2263 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002264 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002265 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002266 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002267 int i, nr, ifnum, isoc_pipe;
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002268 char *speed;
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002269 char descr[255] = "";
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270
2271 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002272 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2273
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002274 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002275 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2276 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002277
2278 /* Don't register audio interfaces */
2279 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002280 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2281 "interface %i, class %i\n",
2282 le16_to_cpu(udev->descriptor.idVendor),
2283 le16_to_cpu(udev->descriptor.idProduct),
2284 ifnum,
2285 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002286
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002287 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002288 return -ENODEV;
2289 }
2290
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002291 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002292
Michael Opdenacker59c51592007-05-09 08:57:56 +02002293 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002294 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2295 USB_ENDPOINT_XFER_ISOC &&
2296 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2297 {
2298 /* It's a newer em2874/em2875 device */
2299 isoc_pipe = 0;
2300 } else {
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002301 int check_interface = 1;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002302 isoc_pipe = 1;
2303 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2304 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002305 USB_ENDPOINT_XFER_ISOC)
2306 check_interface = 0;
2307
2308 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2309 check_interface = 0;
2310
2311 if (!check_interface) {
2312 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2313 "interface %i, class %i found.\n",
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002314 le16_to_cpu(udev->descriptor.idVendor),
2315 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002316 ifnum,
2317 interface->altsetting[0].desc.bInterfaceClass);
2318
2319 em28xx_err(DRIVER_NAME " This is an anciliary "
2320 "interface not used by the driver\n");
2321
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002322 em28xx_devused &= ~(1<<nr);
2323 return -ENODEV;
2324 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002325 }
2326
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002327 switch (udev->speed) {
2328 case USB_SPEED_LOW:
2329 speed = "1.5";
2330 break;
2331 case USB_SPEED_UNKNOWN:
2332 case USB_SPEED_FULL:
2333 speed = "12";
2334 break;
2335 case USB_SPEED_HIGH:
2336 speed = "480";
2337 break;
2338 default:
2339 speed = "unknown";
2340 }
2341
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002342 if (udev->manufacturer)
2343 strlcpy(descr, udev->manufacturer, sizeof(descr));
2344
2345 if (udev->product) {
2346 if (*descr)
2347 strlcat(descr, " ", sizeof(descr));
2348 strlcat(descr, udev->product, sizeof(descr));
2349 }
2350 if (*descr)
2351 strlcat(descr, " ", sizeof(descr));
2352
2353 printk(DRIVER_NAME ": New device %s@ %s Mbps "
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002354 "(%04x:%04x, interface %d, class %d)\n",
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002355 descr,
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002356 speed,
2357 le16_to_cpu(udev->descriptor.idVendor),
2358 le16_to_cpu(udev->descriptor.idProduct),
2359 ifnum,
2360 interface->altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002361
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002362 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002363 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2364 EM28XX_MAXBOARDS);
2365 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002366 return -ENOMEM;
2367 }
2368
2369 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002370 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002371 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002372 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002373 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002374 return -ENOMEM;
2375 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002376
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002377 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002378 dev->devno = nr;
2379 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002380 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002381
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002382 /* Checks if audio is provided by some interface */
2383 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2384 uif = udev->config->interface[i];
2385 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2386 dev->has_audio_class = 1;
2387 break;
2388 }
2389 }
2390
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002391 /* compute alternate max packet sizes */
2392 uif = udev->actconfig->interface[0];
2393
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002394 dev->num_alt = uif->num_altsetting;
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002395 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002396/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2397 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2398
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002399 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002400 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002401 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002402 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002403 return -ENOMEM;
2404 }
2405
2406 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002407 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002408 wMaxPacketSize);
2409 dev->alt_max_pkt_size[i] =
2410 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002411 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002412 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002413 }
2414
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002415 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002416 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002417
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002418 /* allocate device struct */
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002419 mutex_init(&dev->lock);
2420 mutex_lock(&dev->lock);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002421 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002422 if (retval) {
2423 em28xx_devused &= ~(1<<dev->devno);
2424 kfree(dev);
2425
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002426 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002427 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002428
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002429 /* save our data pointer in this interface device */
2430 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002431
2432 request_modules(dev);
2433
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002434 /* Should be the last thing to do, to avoid newer udev's to
2435 open the device before fully initializing it
2436 */
2437 mutex_unlock(&dev->lock);
2438
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002439 return 0;
2440}
2441
2442/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002443 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002444 * called when the device gets diconencted
2445 * video device will be unregistered on v4l2_close in case it is still open
2446 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002447static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002448{
Sascha Sommer5a804152007-11-03 21:22:38 -03002449 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002450 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002451
2452 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002453 usb_set_intfdata(interface, NULL);
2454
2455 if (!dev)
2456 return;
2457
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002458 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002459
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002460 /* wait until all current v4l2 io is finished then deallocate
2461 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002462 mutex_lock(&dev->lock);
2463
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002464 wake_up_interruptible_all(&dev->open);
2465
2466 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002467 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002468 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002469 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002470 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002471
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002472 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002473 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002474 dev->state |= DEV_DISCONNECTED;
2475 wake_up_interruptible(&dev->wait_frame);
2476 wake_up_interruptible(&dev->wait_stream);
2477 } else {
2478 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002479 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002480 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002481 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002482
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002483 mutex_lock(&em28xx_extension_devlist_lock);
2484 if (!list_empty(&em28xx_extension_devlist)) {
2485 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2486 ops->fini(dev);
2487 }
2488 }
2489 mutex_unlock(&em28xx_extension_devlist_lock);
2490
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002491 if (!dev->users) {
2492 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002493 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002494 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002495}
2496
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002497static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002498 .name = "em28xx",
2499 .probe = em28xx_usb_probe,
2500 .disconnect = em28xx_usb_disconnect,
2501 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002502};
2503
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002504static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002505{
2506 int result;
2507
2508 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002509 (EM28XX_VERSION_CODE >> 16) & 0xff,
2510 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002511#ifdef SNAPSHOT
2512 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2513 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2514#endif
2515
2516 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002517 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002518 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002519 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002520 " usb_register failed. Error number %d.\n", result);
2521
2522 return result;
2523}
2524
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002525static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002526{
2527 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002528 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002529}
2530
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002531module_init(em28xx_module_init);
2532module_exit(em28xx_module_exit);
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002533