blob: 4701b6589b10f914e01b1bf4dc3a577b853d3f22 [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
1224 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1225 return -EINVAL;
1226
1227 if (em28xx_reg_len(reg->reg) == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001228 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001229 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001230 mutex_unlock(&dev->lock);
1231
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001232 if (ret < 0)
1233 return ret;
1234
1235 reg->val = ret;
1236 } else {
Al Viroa954b662008-05-21 00:32:51 -03001237 __le64 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001238 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001239 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1240 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001241 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001242 if (ret < 0)
1243 return ret;
1244
Al Viroa954b662008-05-21 00:32:51 -03001245 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001246 }
1247
1248 return 0;
1249}
1250
1251static int vidioc_s_register(struct file *file, void *priv,
1252 struct v4l2_register *reg)
1253{
1254 struct em28xx_fh *fh = priv;
1255 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001256 __le64 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001257 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001258
Al Viroa954b662008-05-21 00:32:51 -03001259 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001260
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001261 mutex_lock(&dev->lock);
1262 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1263 em28xx_reg_len(reg->reg));
1264 mutex_unlock(&dev->lock);
1265
1266 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001267}
1268#endif
1269
1270
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001271static int vidioc_cropcap(struct file *file, void *priv,
1272 struct v4l2_cropcap *cc)
1273{
1274 struct em28xx_fh *fh = priv;
1275 struct em28xx *dev = fh->dev;
1276
1277 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1278 return -EINVAL;
1279
1280 cc->bounds.left = 0;
1281 cc->bounds.top = 0;
1282 cc->bounds.width = dev->width;
1283 cc->bounds.height = dev->height;
1284 cc->defrect = cc->bounds;
1285 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1286 cc->pixelaspect.denominator = 59;
1287
1288 return 0;
1289}
1290
1291static int vidioc_streamon(struct file *file, void *priv,
1292 enum v4l2_buf_type type)
1293{
1294 struct em28xx_fh *fh = priv;
1295 struct em28xx *dev = fh->dev;
1296 int rc;
1297
1298 rc = check_dev(dev);
1299 if (rc < 0)
1300 return rc;
1301
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001302
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001303 mutex_lock(&dev->lock);
1304 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001305
Mauro Carvalho Chehab5db0b5e2008-12-22 06:14:31 -03001306 if (likely(rc >= 0))
1307 rc = videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001308
1309 mutex_unlock(&dev->lock);
1310
1311 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001312}
1313
1314static int vidioc_streamoff(struct file *file, void *priv,
1315 enum v4l2_buf_type type)
1316{
1317 struct em28xx_fh *fh = priv;
1318 struct em28xx *dev = fh->dev;
1319 int rc;
1320
1321 rc = check_dev(dev);
1322 if (rc < 0)
1323 return rc;
1324
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001325 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326 return -EINVAL;
1327 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328 return -EINVAL;
1329
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001330 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001331
1332 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001333 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001334
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001335 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001336
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001337 return 0;
1338}
1339
1340static int vidioc_querycap(struct file *file, void *priv,
1341 struct v4l2_capability *cap)
1342{
1343 struct em28xx_fh *fh = priv;
1344 struct em28xx *dev = fh->dev;
1345
1346 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1347 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001348 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001349
1350 cap->version = EM28XX_VERSION_CODE;
1351
1352 cap->capabilities =
1353 V4L2_CAP_SLICED_VBI_CAPTURE |
1354 V4L2_CAP_VIDEO_CAPTURE |
1355 V4L2_CAP_AUDIO |
1356 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1357
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001358 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001359 cap->capabilities |= V4L2_CAP_TUNER;
1360
1361 return 0;
1362}
1363
Hans Verkuil78b526a2008-05-28 12:16:41 -03001364static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001365 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001366{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001367 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368 return -EINVAL;
1369
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001370 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1371 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372
1373 return 0;
1374}
1375
1376/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001377static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001378 struct v4l2_format *f)
1379{
1380 struct em28xx_fh *fh = priv;
1381 struct em28xx *dev = fh->dev;
1382 int rc;
1383
1384 rc = check_dev(dev);
1385 if (rc < 0)
1386 return rc;
1387
1388 mutex_lock(&dev->lock);
1389
1390 f->fmt.sliced.service_set = 0;
1391
1392 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1393
1394 if (f->fmt.sliced.service_set == 0)
1395 rc = -EINVAL;
1396
1397 mutex_unlock(&dev->lock);
1398 return rc;
1399}
1400
Hans Verkuil78b526a2008-05-28 12:16:41 -03001401static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001402 struct v4l2_format *f)
1403{
1404 struct em28xx_fh *fh = priv;
1405 struct em28xx *dev = fh->dev;
1406 int rc;
1407
1408 rc = check_dev(dev);
1409 if (rc < 0)
1410 return rc;
1411
1412 mutex_lock(&dev->lock);
1413 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1414 mutex_unlock(&dev->lock);
1415
1416 if (f->fmt.sliced.service_set == 0)
1417 return -EINVAL;
1418
1419 return 0;
1420}
1421
1422
1423static int vidioc_reqbufs(struct file *file, void *priv,
1424 struct v4l2_requestbuffers *rb)
1425{
1426 struct em28xx_fh *fh = priv;
1427 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001428 int rc;
1429
1430 rc = check_dev(dev);
1431 if (rc < 0)
1432 return rc;
1433
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001434 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001435}
1436
1437static int vidioc_querybuf(struct file *file, void *priv,
1438 struct v4l2_buffer *b)
1439{
1440 struct em28xx_fh *fh = priv;
1441 struct em28xx *dev = fh->dev;
1442 int rc;
1443
1444 rc = check_dev(dev);
1445 if (rc < 0)
1446 return rc;
1447
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001448 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001449}
1450
1451static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1452{
1453 struct em28xx_fh *fh = priv;
1454 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001455 int rc;
1456
1457 rc = check_dev(dev);
1458 if (rc < 0)
1459 return rc;
1460
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001461 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001462}
1463
1464static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1465{
1466 struct em28xx_fh *fh = priv;
1467 struct em28xx *dev = fh->dev;
1468 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001469
1470 rc = check_dev(dev);
1471 if (rc < 0)
1472 return rc;
1473
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001474 return (videobuf_dqbuf(&fh->vb_vidq, b,
1475 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001476}
1477
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001478#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001479static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001480{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001481 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001482
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001483 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001484}
1485#endif
1486
1487
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001488/* ----------------------------------------------------------- */
1489/* RADIO ESPECIFIC IOCTLS */
1490/* ----------------------------------------------------------- */
1491
1492static int radio_querycap(struct file *file, void *priv,
1493 struct v4l2_capability *cap)
1494{
1495 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1496
1497 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1498 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001499 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001500
1501 cap->version = EM28XX_VERSION_CODE;
1502 cap->capabilities = V4L2_CAP_TUNER;
1503 return 0;
1504}
1505
1506static int radio_g_tuner(struct file *file, void *priv,
1507 struct v4l2_tuner *t)
1508{
1509 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1510
1511 if (unlikely(t->index > 0))
1512 return -EINVAL;
1513
1514 strcpy(t->name, "Radio");
1515 t->type = V4L2_TUNER_RADIO;
1516
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001517 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001518 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001519 mutex_unlock(&dev->lock);
1520
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001521 return 0;
1522}
1523
1524static int radio_enum_input(struct file *file, void *priv,
1525 struct v4l2_input *i)
1526{
1527 if (i->index != 0)
1528 return -EINVAL;
1529 strcpy(i->name, "Radio");
1530 i->type = V4L2_INPUT_TYPE_TUNER;
1531
1532 return 0;
1533}
1534
1535static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1536{
1537 if (unlikely(a->index))
1538 return -EINVAL;
1539
1540 strcpy(a->name, "Radio");
1541 return 0;
1542}
1543
1544static int radio_s_tuner(struct file *file, void *priv,
1545 struct v4l2_tuner *t)
1546{
1547 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1548
1549 if (0 != t->index)
1550 return -EINVAL;
1551
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001552 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001553 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001554 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001555
1556 return 0;
1557}
1558
1559static int radio_s_audio(struct file *file, void *fh,
1560 struct v4l2_audio *a)
1561{
1562 return 0;
1563}
1564
1565static int radio_s_input(struct file *file, void *fh, unsigned int i)
1566{
1567 return 0;
1568}
1569
1570static int radio_queryctrl(struct file *file, void *priv,
1571 struct v4l2_queryctrl *qc)
1572{
1573 int i;
1574
1575 if (qc->id < V4L2_CID_BASE ||
1576 qc->id >= V4L2_CID_LASTP1)
1577 return -EINVAL;
1578
1579 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1580 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1581 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1582 return 0;
1583 }
1584 }
1585
1586 return -EINVAL;
1587}
1588
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001589/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001590 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001591 * inits the device and starts isoc transfer
1592 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001593static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001595 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001596 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001597 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001598 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001599 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001600
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001601 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001602 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001603 if (h->vdev->minor == minor) {
1604 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001605 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001606 }
1607 if (h->vbi_dev->minor == minor) {
1608 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001609 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001610 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001611 if (h->radio_dev &&
1612 h->radio_dev->minor == minor) {
1613 radio = 1;
1614 dev = h;
1615 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001616 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001617 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001618
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001619 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001620 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001621
1622 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001623
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001624 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001625 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001626
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001628 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001629 if (!fh) {
1630 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001631 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001632 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001634 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001635 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001636 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001637 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001638
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001639 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001640 dev->width = norm_maxw(dev);
1641 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001642 dev->hscale = 0;
1643 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001644
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001645 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001646 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001647 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001648
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001649 /* Needed, since GPIO might have disabled power of
1650 some i2c device
1651 */
1652 em28xx_config_i2c(dev);
1653
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001654 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001655 if (fh->radio) {
1656 em28xx_videodbg("video_open: setting radio device\n");
1657 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1658 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001659
1660 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001662 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1663 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1664 sizeof(struct em28xx_buffer), fh);
1665
Ingo Molnar3593cab2006-02-07 06:49:14 -02001666 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001667
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001668 return errCode;
1669}
1670
1671/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001672 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673 * unregisters the v4l2,i2c and usb devices
1674 * called when the device gets disconected or at module unload
1675*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001679 /*FIXME: I2C IR should be disconnected */
1680
Markus Rechberger9c755412005-11-08 21:37:52 -08001681 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001682 if (dev->sbutton_input_dev)
1683 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001684
1685 if (dev->ir)
1686 em28xx_ir_fini(dev);
1687
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001688 if (dev->radio_dev) {
1689 if (-1 != dev->radio_dev->minor)
1690 video_unregister_device(dev->radio_dev);
1691 else
1692 video_device_release(dev->radio_dev);
1693 dev->radio_dev = NULL;
1694 }
1695 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001696 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1697 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001698 if (-1 != dev->vbi_dev->minor)
1699 video_unregister_device(dev->vbi_dev);
1700 else
1701 video_device_release(dev->vbi_dev);
1702 dev->vbi_dev = NULL;
1703 }
1704 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001705 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1706 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001707 if (-1 != dev->vdev->minor)
1708 video_unregister_device(dev->vdev);
1709 else
1710 video_device_release(dev->vdev);
1711 dev->vdev = NULL;
1712 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001713 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001715
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001716 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001717 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718}
1719
1720/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001721 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001722 * stops streaming and deallocates all resources allocated by the v4l2
1723 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001727 struct em28xx_fh *fh = filp->private_data;
1728 struct em28xx *dev = fh->dev;
1729 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001731 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001733
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001734 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001735 if (res_check(fh))
1736 res_free(fh);
1737
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001738 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001739 videobuf_stop(&fh->vb_vidq);
1740 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001741
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001742 /* the device is already disconnect,
1743 free the remaining resources */
1744 if (dev->state & DEV_DISCONNECTED) {
1745 em28xx_release_resources(dev);
1746 mutex_unlock(&dev->lock);
1747 kfree(dev);
1748 return 0;
1749 }
1750
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001751 /* Save some power by putting tuner to sleep */
1752 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1753
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001754 /* do this before setting alternate! */
1755 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001756 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001757
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001758 /* set alternate 0 */
1759 dev->alt = 0;
1760 em28xx_videodbg("setting alternate 0\n");
1761 errCode = usb_set_interface(dev->udev, 0, 0);
1762 if (errCode < 0) {
1763 em28xx_errdev("cannot change alternate number to "
1764 "0 (error=%i)\n", errCode);
1765 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001767 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001768 dev->users--;
1769 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001770 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001771 return 0;
1772}
1773
1774/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001775 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001776 * will allocate buffers when called for the first time
1777 */
1778static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001779em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001780 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001782 struct em28xx_fh *fh = filp->private_data;
1783 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001784 int rc;
1785
1786 rc = check_dev(dev);
1787 if (rc < 0)
1788 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001789
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001790 /* FIXME: read() is not prepared to allow changing the video
1791 resolution while streaming. Seems a bug at em28xx_set_fmt
1792 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001793
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001794 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001795 mutex_lock(&dev->lock);
1796 rc = res_get(fh);
1797 mutex_unlock(&dev->lock);
1798
1799 if (unlikely(rc < 0))
1800 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001801
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001802 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1803 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001804 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001805 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806}
1807
1808/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001809 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810 * will allocate buffers when called for the first time
1811 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001812static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001813{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001814 struct em28xx_fh *fh = filp->private_data;
1815 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001816 int rc;
1817
1818 rc = check_dev(dev);
1819 if (rc < 0)
1820 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001822 mutex_lock(&dev->lock);
1823 rc = res_get(fh);
1824 mutex_unlock(&dev->lock);
1825
1826 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001827 return POLLERR;
1828
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001829 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1830 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001831
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001832 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001833}
1834
1835/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001836 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001837 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001838static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001839{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001840 struct em28xx_fh *fh = filp->private_data;
1841 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001842 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001843
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001844 rc = check_dev(dev);
1845 if (rc < 0)
1846 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001847
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001848 mutex_lock(&dev->lock);
1849 rc = res_get(fh);
1850 mutex_unlock(&dev->lock);
1851
1852 if (unlikely(rc < 0))
1853 return rc;
1854
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001855 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001856
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001857 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1858 (unsigned long)vma->vm_start,
1859 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1860 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001861
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001862 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001863}
1864
Arjan van de Venfa027c22007-02-12 00:55:33 -08001865static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001866 .owner = THIS_MODULE,
1867 .open = em28xx_v4l2_open,
1868 .release = em28xx_v4l2_close,
1869 .read = em28xx_v4l2_read,
1870 .poll = em28xx_v4l2_poll,
1871 .mmap = em28xx_v4l2_mmap,
1872 .ioctl = video_ioctl2,
1873 .llseek = no_llseek,
1874 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001875};
1876
Hans Verkuila3998102008-07-21 02:57:38 -03001877static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001878 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001879 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1880 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1881 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1882 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001883 .vidioc_g_audio = vidioc_g_audio,
1884 .vidioc_s_audio = vidioc_s_audio,
1885 .vidioc_cropcap = vidioc_cropcap,
1886
Hans Verkuil78b526a2008-05-28 12:16:41 -03001887 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1888 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1889 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001890
1891 .vidioc_reqbufs = vidioc_reqbufs,
1892 .vidioc_querybuf = vidioc_querybuf,
1893 .vidioc_qbuf = vidioc_qbuf,
1894 .vidioc_dqbuf = vidioc_dqbuf,
1895 .vidioc_s_std = vidioc_s_std,
1896 .vidioc_enum_input = vidioc_enum_input,
1897 .vidioc_g_input = vidioc_g_input,
1898 .vidioc_s_input = vidioc_s_input,
1899 .vidioc_queryctrl = vidioc_queryctrl,
1900 .vidioc_g_ctrl = vidioc_g_ctrl,
1901 .vidioc_s_ctrl = vidioc_s_ctrl,
1902 .vidioc_streamon = vidioc_streamon,
1903 .vidioc_streamoff = vidioc_streamoff,
1904 .vidioc_g_tuner = vidioc_g_tuner,
1905 .vidioc_s_tuner = vidioc_s_tuner,
1906 .vidioc_g_frequency = vidioc_g_frequency,
1907 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001908#ifdef CONFIG_VIDEO_ADV_DEBUG
1909 .vidioc_g_register = vidioc_g_register,
1910 .vidioc_s_register = vidioc_s_register,
1911#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001912#ifdef CONFIG_VIDEO_V4L1_COMPAT
1913 .vidiocgmbuf = vidiocgmbuf,
1914#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001915};
1916
1917static const struct video_device em28xx_video_template = {
1918 .fops = &em28xx_v4l_fops,
1919 .release = video_device_release,
1920 .ioctl_ops = &video_ioctl_ops,
1921
1922 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001923
1924 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001925 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001926};
1927
Hans Verkuila3998102008-07-21 02:57:38 -03001928static const struct file_operations radio_fops = {
1929 .owner = THIS_MODULE,
1930 .open = em28xx_v4l2_open,
1931 .release = em28xx_v4l2_close,
1932 .ioctl = video_ioctl2,
1933 .compat_ioctl = v4l_compat_ioctl32,
1934 .llseek = no_llseek,
1935};
1936
1937static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001938 .vidioc_querycap = radio_querycap,
1939 .vidioc_g_tuner = radio_g_tuner,
1940 .vidioc_enum_input = radio_enum_input,
1941 .vidioc_g_audio = radio_g_audio,
1942 .vidioc_s_tuner = radio_s_tuner,
1943 .vidioc_s_audio = radio_s_audio,
1944 .vidioc_s_input = radio_s_input,
1945 .vidioc_queryctrl = radio_queryctrl,
1946 .vidioc_g_ctrl = vidioc_g_ctrl,
1947 .vidioc_s_ctrl = vidioc_s_ctrl,
1948 .vidioc_g_frequency = vidioc_g_frequency,
1949 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001950#ifdef CONFIG_VIDEO_ADV_DEBUG
1951 .vidioc_g_register = vidioc_g_register,
1952 .vidioc_s_register = vidioc_s_register,
1953#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001954};
1955
Hans Verkuila3998102008-07-21 02:57:38 -03001956static struct video_device em28xx_radio_template = {
1957 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001958 .fops = &radio_fops,
1959 .ioctl_ops = &radio_ioctl_ops,
1960 .minor = -1,
1961};
1962
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001963/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001964
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001965
1966static LIST_HEAD(em28xx_extension_devlist);
1967static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1968
1969int em28xx_register_extension(struct em28xx_ops *ops)
1970{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001971 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001972
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001973 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001974 mutex_lock(&em28xx_extension_devlist_lock);
1975 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001976 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1977 if (dev)
1978 ops->init(dev);
1979 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001980 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1981 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001982 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001983 return 0;
1984}
1985EXPORT_SYMBOL(em28xx_register_extension);
1986
1987void em28xx_unregister_extension(struct em28xx_ops *ops)
1988{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001989 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001990
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001991 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001992 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1993 if (dev)
1994 ops->fini(dev);
1995 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001996
1997 mutex_lock(&em28xx_extension_devlist_lock);
1998 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1999 list_del(&ops->next);
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}
2003EXPORT_SYMBOL(em28xx_unregister_extension);
2004
Adrian Bunk532fe652008-01-28 22:10:48 -03002005static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2006 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03002007 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002008{
2009 struct video_device *vfd;
2010
2011 vfd = video_device_alloc();
2012 if (NULL == vfd)
2013 return NULL;
2014 *vfd = *template;
2015 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03002016 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002017 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03002018 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002019
2020 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2021 dev->name, type_name);
2022
2023 return vfd;
2024}
2025
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002026static int register_analog_devices(struct em28xx *dev)
2027{
2028 int ret;
2029
2030 /* allocate and fill video video_device struct */
2031 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2032 if (!dev->vdev) {
2033 em28xx_errdev("cannot allocate video_device.\n");
2034 return -ENODEV;
2035 }
2036
2037 /* register v4l2 video video_device */
2038 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2039 video_nr[dev->devno]);
2040 if (ret) {
2041 em28xx_errdev("unable to register video device (error=%i).\n",
2042 ret);
2043 return ret;
2044 }
2045
2046 /* Allocate and fill vbi video_device struct */
2047 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2048
2049 /* register v4l2 vbi video_device */
2050 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2051 vbi_nr[dev->devno]);
2052 if (ret < 0) {
2053 em28xx_errdev("unable to register vbi device\n");
2054 return ret;
2055 }
2056
2057 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2058 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2059 if (!dev->radio_dev) {
2060 em28xx_errdev("cannot allocate video_device.\n");
2061 return -ENODEV;
2062 }
2063 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2064 radio_nr[dev->devno]);
2065 if (ret < 0) {
2066 em28xx_errdev("can't register radio device\n");
2067 return ret;
2068 }
2069 em28xx_info("Registered radio device as /dev/radio%d\n",
2070 dev->radio_dev->num);
2071 }
2072
2073 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2074 dev->vdev->num, dev->vbi_dev->num);
2075
2076 return 0;
2077}
2078
2079
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002080/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002081 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002082 * allocates and inits the device structs, registers i2c bus and v4l device
2083 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002084static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002085 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002086{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002087 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002088 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002089 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002090 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002092
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093 dev->udev = udev;
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002094 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002095 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002097 init_waitqueue_head(&dev->wait_frame);
2098 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002100 dev->em28xx_write_regs = em28xx_write_regs;
2101 dev->em28xx_read_reg = em28xx_read_reg;
2102 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2103 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2104 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002105 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002106 dev->format = &format[0];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002107
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002108 em28xx_pre_card_setup(dev);
2109
2110 errCode = em28xx_config(dev);
2111 if (errCode) {
2112 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002113 return -ENOMEM;
2114 }
2115
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002116 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002117 errCode = em28xx_i2c_register(dev);
2118 if (errCode < 0) {
2119 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2120 __func__, errCode);
2121 return errCode;
2122 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002123
2124 /* Do board specific init and eeprom reading */
2125 em28xx_card_setup(dev);
2126
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002127 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002128 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002129 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002130 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002131 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002132 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002133
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002134 /* configure the device */
2135 em28xx_config_i2c(dev);
2136
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002137 /* set default norm */
2138 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002139
2140 maxw = norm_maxw(dev);
2141 maxh = norm_maxh(dev);
2142
2143 /* set default image size */
2144 dev->width = maxw;
2145 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002146 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002147 dev->hscale = 0;
2148 dev->vscale = 0;
2149 dev->ctl_input = 2;
2150
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002151 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002152 if (errCode < 0) {
2153 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2154 __func__, errCode);
2155 return errCode;
2156 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002158 /* init video dma queues */
2159 INIT_LIST_HEAD(&dev->vidq.active);
2160 INIT_LIST_HEAD(&dev->vidq.queued);
2161
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002163 if (dev->board.has_msp34xx) {
Sascha Sommer5a804152007-11-03 21:22:38 -03002164 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002165 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002166 if (errCode < 0) {
2167 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2168 __func__, errCode);
2169 return errCode;
2170 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002171 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002172
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002173 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002174 if (errCode < 0) {
2175 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2176 __func__, errCode);
2177 return errCode;
2178 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002179 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002180 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002181
Sascha Sommer5a804152007-11-03 21:22:38 -03002182 video_mux(dev, 0);
2183
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002184 mutex_lock(&em28xx_devlist_mutex);
2185 list_add_tail(&dev->devlist, &em28xx_devlist);
2186 retval = register_analog_devices(dev);
2187 if (retval < 0) {
2188 em28xx_release_resources(dev);
2189 mutex_unlock(&em28xx_devlist_mutex);
2190 goto fail_reg_devices;
2191 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002192
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002193 mutex_lock(&em28xx_extension_devlist_lock);
2194 if (!list_empty(&em28xx_extension_devlist)) {
2195 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2196 if (ops->id)
2197 ops->init(dev);
2198 }
2199 }
2200 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002201 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002202
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002203 /* Save some power by putting tuner to sleep */
2204 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2205
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002206 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002207
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002208fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002209 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002210}
2211
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002212#if defined(CONFIG_MODULES) && defined(MODULE)
2213static void request_module_async(struct work_struct *work)
2214{
2215 struct em28xx *dev = container_of(work,
2216 struct em28xx, request_module_wk);
2217
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002218 if (dev->has_audio_class)
2219 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002220 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002221 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002222
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002223 if (dev->board.has_dvb)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002224 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002225}
2226
2227static void request_modules(struct em28xx *dev)
2228{
2229 INIT_WORK(&dev->request_module_wk, request_module_async);
2230 schedule_work(&dev->request_module_wk);
2231}
2232#else
2233#define request_modules(dev)
2234#endif /* CONFIG_MODULES */
2235
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002236/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002237 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002238 * checks for supported devices
2239 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002240static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241 const struct usb_device_id *id)
2242{
2243 const struct usb_endpoint_descriptor *endpoint;
2244 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002245 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002246 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002248 int i, nr, ifnum, isoc_pipe;
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002249 char *speed;
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002250 char descr[255] = "";
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002251
2252 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002253 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2254
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002255 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002256 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2257 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002258
2259 /* Don't register audio interfaces */
2260 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002261 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2262 "interface %i, class %i\n",
2263 le16_to_cpu(udev->descriptor.idVendor),
2264 le16_to_cpu(udev->descriptor.idProduct),
2265 ifnum,
2266 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002267
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002268 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002269 return -ENODEV;
2270 }
2271
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002272 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002273
Michael Opdenacker59c51592007-05-09 08:57:56 +02002274 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002275 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2276 USB_ENDPOINT_XFER_ISOC &&
2277 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2278 {
2279 /* It's a newer em2874/em2875 device */
2280 isoc_pipe = 0;
2281 } else {
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002282 int check_interface = 1;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002283 isoc_pipe = 1;
2284 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2285 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002286 USB_ENDPOINT_XFER_ISOC)
2287 check_interface = 0;
2288
2289 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2290 check_interface = 0;
2291
2292 if (!check_interface) {
2293 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2294 "interface %i, class %i found.\n",
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002295 le16_to_cpu(udev->descriptor.idVendor),
2296 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002297 ifnum,
2298 interface->altsetting[0].desc.bInterfaceClass);
2299
2300 em28xx_err(DRIVER_NAME " This is an anciliary "
2301 "interface not used by the driver\n");
2302
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002303 em28xx_devused &= ~(1<<nr);
2304 return -ENODEV;
2305 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002306 }
2307
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002308 switch (udev->speed) {
2309 case USB_SPEED_LOW:
2310 speed = "1.5";
2311 break;
2312 case USB_SPEED_UNKNOWN:
2313 case USB_SPEED_FULL:
2314 speed = "12";
2315 break;
2316 case USB_SPEED_HIGH:
2317 speed = "480";
2318 break;
2319 default:
2320 speed = "unknown";
2321 }
2322
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002323 if (udev->manufacturer)
2324 strlcpy(descr, udev->manufacturer, sizeof(descr));
2325
2326 if (udev->product) {
2327 if (*descr)
2328 strlcat(descr, " ", sizeof(descr));
2329 strlcat(descr, udev->product, sizeof(descr));
2330 }
2331 if (*descr)
2332 strlcat(descr, " ", sizeof(descr));
2333
2334 printk(DRIVER_NAME ": New device %s@ %s Mbps "
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002335 "(%04x:%04x, interface %d, class %d)\n",
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002336 descr,
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002337 speed,
2338 le16_to_cpu(udev->descriptor.idVendor),
2339 le16_to_cpu(udev->descriptor.idProduct),
2340 ifnum,
2341 interface->altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002342
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002343 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002344 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2345 EM28XX_MAXBOARDS);
2346 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002347 return -ENOMEM;
2348 }
2349
2350 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002351 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002352 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002353 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002354 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002355 return -ENOMEM;
2356 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002357
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002358 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002359 dev->devno = nr;
2360 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002361 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002362
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002363 /* Checks if audio is provided by some interface */
2364 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2365 uif = udev->config->interface[i];
2366 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2367 dev->has_audio_class = 1;
2368 break;
2369 }
2370 }
2371
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002372 /* compute alternate max packet sizes */
2373 uif = udev->actconfig->interface[0];
2374
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002375 dev->num_alt = uif->num_altsetting;
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002376 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002377/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2378 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2379
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002380 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002381 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002382 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002383 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002384 return -ENOMEM;
2385 }
2386
2387 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002388 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002389 wMaxPacketSize);
2390 dev->alt_max_pkt_size[i] =
2391 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002392 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002393 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002394 }
2395
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002396 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002397 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002398
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002399 /* allocate device struct */
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002400 mutex_init(&dev->lock);
2401 mutex_lock(&dev->lock);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002402 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002403 if (retval) {
2404 em28xx_devused &= ~(1<<dev->devno);
2405 kfree(dev);
2406
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002407 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002408 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002409
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002410 /* save our data pointer in this interface device */
2411 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002412
2413 request_modules(dev);
2414
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002415 /* Should be the last thing to do, to avoid newer udev's to
2416 open the device before fully initializing it
2417 */
2418 mutex_unlock(&dev->lock);
2419
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420 return 0;
2421}
2422
2423/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002424 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002425 * called when the device gets diconencted
2426 * video device will be unregistered on v4l2_close in case it is still open
2427 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002428static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002429{
Sascha Sommer5a804152007-11-03 21:22:38 -03002430 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002431 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002432
2433 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002434 usb_set_intfdata(interface, NULL);
2435
2436 if (!dev)
2437 return;
2438
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002439 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002440
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002441 /* wait until all current v4l2 io is finished then deallocate
2442 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002443 mutex_lock(&dev->lock);
2444
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002445 wake_up_interruptible_all(&dev->open);
2446
2447 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002448 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002449 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002450 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002451 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002452
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002453 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002454 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002455 dev->state |= DEV_DISCONNECTED;
2456 wake_up_interruptible(&dev->wait_frame);
2457 wake_up_interruptible(&dev->wait_stream);
2458 } else {
2459 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002460 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002461 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002462 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002463
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002464 mutex_lock(&em28xx_extension_devlist_lock);
2465 if (!list_empty(&em28xx_extension_devlist)) {
2466 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2467 ops->fini(dev);
2468 }
2469 }
2470 mutex_unlock(&em28xx_extension_devlist_lock);
2471
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002472 if (!dev->users) {
2473 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002474 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002475 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002476}
2477
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002478static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002479 .name = "em28xx",
2480 .probe = em28xx_usb_probe,
2481 .disconnect = em28xx_usb_disconnect,
2482 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002483};
2484
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002485static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002486{
2487 int result;
2488
2489 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002490 (EM28XX_VERSION_CODE >> 16) & 0xff,
2491 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002492#ifdef SNAPSHOT
2493 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2494 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2495#endif
2496
2497 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002498 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002499 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002500 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002501 " usb_register failed. Error number %d.\n", result);
2502
2503 return result;
2504}
2505
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002506static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002507{
2508 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002509 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002510}
2511
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002512module_init(em28xx_module_init);
2513module_exit(em28xx_module_exit);
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002514