blob: dbae8bcb18d77ed35187870f22783ad8d34e7960 [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 Thorntone0fadfd342008-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 Thorntone0fadfd342008-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);
822 if (!fmt)
823 return -EINVAL;
824
Brandon Philips05612972008-04-13 14:57:01 -0300825 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
826 em28xx_errdev("%s queue busy\n", __func__);
827 rc = -EBUSY;
828 goto out;
829 }
830
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300831 if (dev->stream_on && !fh->stream_on) {
832 em28xx_errdev("%s device in use by another fh\n", __func__);
833 rc = -EBUSY;
834 goto out;
835 }
836
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837 /* set new image size */
838 dev->width = f->fmt.pix.width;
839 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300840 dev->format = fmt;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
842
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300843 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300844 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845
Brandon Philips05612972008-04-13 14:57:01 -0300846 rc = 0;
847
848out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300849 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300850 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300851}
852
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300853static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300854{
855 struct em28xx_fh *fh = priv;
856 struct em28xx *dev = fh->dev;
857 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300858 int rc;
859
860 rc = check_dev(dev);
861 if (rc < 0)
862 return rc;
863
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300864 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300865 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300866
867 /* Adjusts width/height, if needed */
868 f.fmt.pix.width = dev->width;
869 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300870 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300871
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300872 /* set new image size */
873 dev->width = f.fmt.pix.width;
874 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300875 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
876
877 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300878 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300879
880 mutex_unlock(&dev->lock);
881 return 0;
882}
883
884static const char *iname[] = {
885 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
886 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
887 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
888 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
889 [EM28XX_VMUX_SVIDEO] = "S-Video",
890 [EM28XX_VMUX_TELEVISION] = "Television",
891 [EM28XX_VMUX_CABLE] = "Cable TV",
892 [EM28XX_VMUX_DVB] = "DVB",
893 [EM28XX_VMUX_DEBUG] = "for debug only",
894};
895
896static int vidioc_enum_input(struct file *file, void *priv,
897 struct v4l2_input *i)
898{
899 struct em28xx_fh *fh = priv;
900 struct em28xx *dev = fh->dev;
901 unsigned int n;
902
903 n = i->index;
904 if (n >= MAX_EM28XX_INPUT)
905 return -EINVAL;
906 if (0 == INPUT(n)->type)
907 return -EINVAL;
908
909 i->index = n;
910 i->type = V4L2_INPUT_TYPE_CAMERA;
911
912 strcpy(i->name, iname[INPUT(n)->type]);
913
914 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
915 (EM28XX_VMUX_CABLE == INPUT(n)->type))
916 i->type = V4L2_INPUT_TYPE_TUNER;
917
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300918 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300919
920 return 0;
921}
922
923static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
924{
925 struct em28xx_fh *fh = priv;
926 struct em28xx *dev = fh->dev;
927
928 *i = dev->ctl_input;
929
930 return 0;
931}
932
933static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
934{
935 struct em28xx_fh *fh = priv;
936 struct em28xx *dev = fh->dev;
937 int rc;
938
939 rc = check_dev(dev);
940 if (rc < 0)
941 return rc;
942
943 if (i >= MAX_EM28XX_INPUT)
944 return -EINVAL;
945 if (0 == INPUT(i)->type)
946 return -EINVAL;
947
948 mutex_lock(&dev->lock);
949
950 video_mux(dev, i);
951
952 mutex_unlock(&dev->lock);
953 return 0;
954}
955
956static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
957{
958 struct em28xx_fh *fh = priv;
959 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300961 switch (a->index) {
962 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300964 break;
965 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300967 break;
968 case EM28XX_AMUX_VIDEO2:
969 strcpy(a->name, "Television alt");
970 break;
971 case EM28XX_AMUX_PHONE:
972 strcpy(a->name, "Phone");
973 break;
974 case EM28XX_AMUX_MIC:
975 strcpy(a->name, "Mic");
976 break;
977 case EM28XX_AMUX_CD:
978 strcpy(a->name, "CD");
979 break;
980 case EM28XX_AMUX_AUX:
981 strcpy(a->name, "Aux");
982 break;
983 case EM28XX_AMUX_PCM_OUT:
984 strcpy(a->name, "PCM");
985 break;
986 default:
987 return -EINVAL;
988 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300989
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300990 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300991 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300992
993 return 0;
994}
995
996static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
997{
998 struct em28xx_fh *fh = priv;
999 struct em28xx *dev = fh->dev;
1000
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001001 mutex_lock(&dev->lock);
1002
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001003 dev->ctl_ainput = INPUT(a->index)->amux;
1004 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001005
1006 if (!dev->ctl_aoutput)
1007 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001008
1009 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001010 return 0;
1011}
1012
1013static int vidioc_queryctrl(struct file *file, void *priv,
1014 struct v4l2_queryctrl *qc)
1015{
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1018 int id = qc->id;
1019 int i;
1020 int rc;
1021
1022 rc = check_dev(dev);
1023 if (rc < 0)
1024 return rc;
1025
1026 memset(qc, 0, sizeof(*qc));
1027
1028 qc->id = id;
1029
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001030 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001031 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1032 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1033 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1034 return 0;
1035 }
1036 }
1037 }
1038 mutex_lock(&dev->lock);
1039 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1040 mutex_unlock(&dev->lock);
1041
1042 if (qc->type)
1043 return 0;
1044 else
1045 return -EINVAL;
1046}
1047
1048static int vidioc_g_ctrl(struct file *file, void *priv,
1049 struct v4l2_control *ctrl)
1050{
1051 struct em28xx_fh *fh = priv;
1052 struct em28xx *dev = fh->dev;
1053 int rc;
1054
1055 rc = check_dev(dev);
1056 if (rc < 0)
1057 return rc;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001058
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001059 mutex_lock(&dev->lock);
1060
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001061 if (!dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001062 rc = em28xx_get_ctrl(dev, ctrl);
1063 else
1064 rc = -EINVAL;
1065
1066 if (rc == -EINVAL) {
1067 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1068 rc = 0;
1069 }
1070
1071 mutex_unlock(&dev->lock);
1072 return rc;
1073}
1074
1075static int vidioc_s_ctrl(struct file *file, void *priv,
1076 struct v4l2_control *ctrl)
1077{
1078 struct em28xx_fh *fh = priv;
1079 struct em28xx *dev = fh->dev;
1080 u8 i;
1081 int rc;
1082
1083 rc = check_dev(dev);
1084 if (rc < 0)
1085 return rc;
1086
1087 mutex_lock(&dev->lock);
1088
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001089 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001090 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1091 else {
1092 rc = 1;
1093 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1094 if (ctrl->id == em28xx_qctrl[i].id) {
1095 if (ctrl->value < em28xx_qctrl[i].minimum ||
1096 ctrl->value > em28xx_qctrl[i].maximum) {
1097 rc = -ERANGE;
1098 break;
1099 }
1100
1101 rc = em28xx_set_ctrl(dev, ctrl);
1102 break;
1103 }
1104 }
1105 }
1106
1107 /* Control not found - try to send it to the attached devices */
1108 if (rc == 1) {
1109 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1110 rc = 0;
1111 }
1112
1113 mutex_unlock(&dev->lock);
1114 return rc;
1115}
1116
1117static int vidioc_g_tuner(struct file *file, void *priv,
1118 struct v4l2_tuner *t)
1119{
1120 struct em28xx_fh *fh = priv;
1121 struct em28xx *dev = fh->dev;
1122 int rc;
1123
1124 rc = check_dev(dev);
1125 if (rc < 0)
1126 return rc;
1127
1128 if (0 != t->index)
1129 return -EINVAL;
1130
1131 strcpy(t->name, "Tuner");
1132
1133 mutex_lock(&dev->lock);
1134
1135 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1136
1137 mutex_unlock(&dev->lock);
1138 return 0;
1139}
1140
1141static int vidioc_s_tuner(struct file *file, void *priv,
1142 struct v4l2_tuner *t)
1143{
1144 struct em28xx_fh *fh = priv;
1145 struct em28xx *dev = fh->dev;
1146 int rc;
1147
1148 rc = check_dev(dev);
1149 if (rc < 0)
1150 return rc;
1151
1152 if (0 != t->index)
1153 return -EINVAL;
1154
1155 mutex_lock(&dev->lock);
1156
1157 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1158
1159 mutex_unlock(&dev->lock);
1160 return 0;
1161}
1162
1163static int vidioc_g_frequency(struct file *file, void *priv,
1164 struct v4l2_frequency *f)
1165{
1166 struct em28xx_fh *fh = priv;
1167 struct em28xx *dev = fh->dev;
1168
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001169 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001170 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001171 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001172 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001173
1174 return 0;
1175}
1176
1177static int vidioc_s_frequency(struct file *file, void *priv,
1178 struct v4l2_frequency *f)
1179{
1180 struct em28xx_fh *fh = priv;
1181 struct em28xx *dev = fh->dev;
1182 int rc;
1183
1184 rc = check_dev(dev);
1185 if (rc < 0)
1186 return rc;
1187
1188 if (0 != f->tuner)
1189 return -EINVAL;
1190
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001191 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1192 return -EINVAL;
1193 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001194 return -EINVAL;
1195
1196 mutex_lock(&dev->lock);
1197
1198 dev->ctl_freq = f->frequency;
1199 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1200
1201 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001202
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001203 return 0;
1204}
1205
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001206#ifdef CONFIG_VIDEO_ADV_DEBUG
1207static int em28xx_reg_len(int reg)
1208{
1209 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001210 case EM28XX_R40_AC97LSB:
1211 case EM28XX_R30_HSCALELOW:
1212 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001213 return 2;
1214 default:
1215 return 1;
1216 }
1217}
1218
1219static int vidioc_g_register(struct file *file, void *priv,
1220 struct v4l2_register *reg)
1221{
1222 struct em28xx_fh *fh = priv;
1223 struct em28xx *dev = fh->dev;
1224 int ret;
1225
1226 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1227 return -EINVAL;
1228
1229 if (em28xx_reg_len(reg->reg) == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001230 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001231 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001232 mutex_unlock(&dev->lock);
1233
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001234 if (ret < 0)
1235 return ret;
1236
1237 reg->val = ret;
1238 } else {
Al Viroa954b662008-05-21 00:32:51 -03001239 __le64 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001240 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001241 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1242 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001243 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001244 if (ret < 0)
1245 return ret;
1246
Al Viroa954b662008-05-21 00:32:51 -03001247 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001248 }
1249
1250 return 0;
1251}
1252
1253static int vidioc_s_register(struct file *file, void *priv,
1254 struct v4l2_register *reg)
1255{
1256 struct em28xx_fh *fh = priv;
1257 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001258 __le64 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001259 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001260
Al Viroa954b662008-05-21 00:32:51 -03001261 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001262
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001263 mutex_lock(&dev->lock);
1264 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1265 em28xx_reg_len(reg->reg));
1266 mutex_unlock(&dev->lock);
1267
1268 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001269}
1270#endif
1271
1272
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001273static int vidioc_cropcap(struct file *file, void *priv,
1274 struct v4l2_cropcap *cc)
1275{
1276 struct em28xx_fh *fh = priv;
1277 struct em28xx *dev = fh->dev;
1278
1279 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1280 return -EINVAL;
1281
1282 cc->bounds.left = 0;
1283 cc->bounds.top = 0;
1284 cc->bounds.width = dev->width;
1285 cc->bounds.height = dev->height;
1286 cc->defrect = cc->bounds;
1287 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1288 cc->pixelaspect.denominator = 59;
1289
1290 return 0;
1291}
1292
1293static int vidioc_streamon(struct file *file, void *priv,
1294 enum v4l2_buf_type type)
1295{
1296 struct em28xx_fh *fh = priv;
1297 struct em28xx *dev = fh->dev;
1298 int rc;
1299
1300 rc = check_dev(dev);
1301 if (rc < 0)
1302 return rc;
1303
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001304
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001305 mutex_lock(&dev->lock);
1306 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001307
1308 if (unlikely(rc < 0))
1309 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001310
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001311 rc = videobuf_streamon(&fh->vb_vidq);
1312
1313 mutex_unlock(&dev->lock);
1314
1315 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001316}
1317
1318static int vidioc_streamoff(struct file *file, void *priv,
1319 enum v4l2_buf_type type)
1320{
1321 struct em28xx_fh *fh = priv;
1322 struct em28xx *dev = fh->dev;
1323 int rc;
1324
1325 rc = check_dev(dev);
1326 if (rc < 0)
1327 return rc;
1328
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001329 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1330 return -EINVAL;
1331 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001332 return -EINVAL;
1333
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001334 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001335
1336 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001337 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001338
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001339 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001340
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001341 return 0;
1342}
1343
1344static int vidioc_querycap(struct file *file, void *priv,
1345 struct v4l2_capability *cap)
1346{
1347 struct em28xx_fh *fh = priv;
1348 struct em28xx *dev = fh->dev;
1349
1350 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1351 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001352 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001353
1354 cap->version = EM28XX_VERSION_CODE;
1355
1356 cap->capabilities =
1357 V4L2_CAP_SLICED_VBI_CAPTURE |
1358 V4L2_CAP_VIDEO_CAPTURE |
1359 V4L2_CAP_AUDIO |
1360 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1361
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001362 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363 cap->capabilities |= V4L2_CAP_TUNER;
1364
1365 return 0;
1366}
1367
Hans Verkuil78b526a2008-05-28 12:16:41 -03001368static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001369 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001370{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001371 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372 return -EINVAL;
1373
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001374 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1375 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001376
1377 return 0;
1378}
1379
1380/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001381static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382 struct v4l2_format *f)
1383{
1384 struct em28xx_fh *fh = priv;
1385 struct em28xx *dev = fh->dev;
1386 int rc;
1387
1388 rc = check_dev(dev);
1389 if (rc < 0)
1390 return rc;
1391
1392 mutex_lock(&dev->lock);
1393
1394 f->fmt.sliced.service_set = 0;
1395
1396 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1397
1398 if (f->fmt.sliced.service_set == 0)
1399 rc = -EINVAL;
1400
1401 mutex_unlock(&dev->lock);
1402 return rc;
1403}
1404
Hans Verkuil78b526a2008-05-28 12:16:41 -03001405static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001406 struct v4l2_format *f)
1407{
1408 struct em28xx_fh *fh = priv;
1409 struct em28xx *dev = fh->dev;
1410 int rc;
1411
1412 rc = check_dev(dev);
1413 if (rc < 0)
1414 return rc;
1415
1416 mutex_lock(&dev->lock);
1417 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1418 mutex_unlock(&dev->lock);
1419
1420 if (f->fmt.sliced.service_set == 0)
1421 return -EINVAL;
1422
1423 return 0;
1424}
1425
1426
1427static int vidioc_reqbufs(struct file *file, void *priv,
1428 struct v4l2_requestbuffers *rb)
1429{
1430 struct em28xx_fh *fh = priv;
1431 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432 int rc;
1433
1434 rc = check_dev(dev);
1435 if (rc < 0)
1436 return rc;
1437
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001438 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439}
1440
1441static int vidioc_querybuf(struct file *file, void *priv,
1442 struct v4l2_buffer *b)
1443{
1444 struct em28xx_fh *fh = priv;
1445 struct em28xx *dev = fh->dev;
1446 int rc;
1447
1448 rc = check_dev(dev);
1449 if (rc < 0)
1450 return rc;
1451
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001452 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001453}
1454
1455static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1456{
1457 struct em28xx_fh *fh = priv;
1458 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001459 int rc;
1460
1461 rc = check_dev(dev);
1462 if (rc < 0)
1463 return rc;
1464
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001465 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466}
1467
1468static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1469{
1470 struct em28xx_fh *fh = priv;
1471 struct em28xx *dev = fh->dev;
1472 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001473
1474 rc = check_dev(dev);
1475 if (rc < 0)
1476 return rc;
1477
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001478 return (videobuf_dqbuf(&fh->vb_vidq, b,
1479 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001480}
1481
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001482#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001483static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001484{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001485 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001486
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001487 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001488}
1489#endif
1490
1491
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001492/* ----------------------------------------------------------- */
1493/* RADIO ESPECIFIC IOCTLS */
1494/* ----------------------------------------------------------- */
1495
1496static int radio_querycap(struct file *file, void *priv,
1497 struct v4l2_capability *cap)
1498{
1499 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1500
1501 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1502 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001503 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001504
1505 cap->version = EM28XX_VERSION_CODE;
1506 cap->capabilities = V4L2_CAP_TUNER;
1507 return 0;
1508}
1509
1510static int radio_g_tuner(struct file *file, void *priv,
1511 struct v4l2_tuner *t)
1512{
1513 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1514
1515 if (unlikely(t->index > 0))
1516 return -EINVAL;
1517
1518 strcpy(t->name, "Radio");
1519 t->type = V4L2_TUNER_RADIO;
1520
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001521 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001522 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001523 mutex_unlock(&dev->lock);
1524
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525 return 0;
1526}
1527
1528static int radio_enum_input(struct file *file, void *priv,
1529 struct v4l2_input *i)
1530{
1531 if (i->index != 0)
1532 return -EINVAL;
1533 strcpy(i->name, "Radio");
1534 i->type = V4L2_INPUT_TYPE_TUNER;
1535
1536 return 0;
1537}
1538
1539static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1540{
1541 if (unlikely(a->index))
1542 return -EINVAL;
1543
1544 strcpy(a->name, "Radio");
1545 return 0;
1546}
1547
1548static int radio_s_tuner(struct file *file, void *priv,
1549 struct v4l2_tuner *t)
1550{
1551 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1552
1553 if (0 != t->index)
1554 return -EINVAL;
1555
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001556 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001557 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001558 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001559
1560 return 0;
1561}
1562
1563static int radio_s_audio(struct file *file, void *fh,
1564 struct v4l2_audio *a)
1565{
1566 return 0;
1567}
1568
1569static int radio_s_input(struct file *file, void *fh, unsigned int i)
1570{
1571 return 0;
1572}
1573
1574static int radio_queryctrl(struct file *file, void *priv,
1575 struct v4l2_queryctrl *qc)
1576{
1577 int i;
1578
1579 if (qc->id < V4L2_CID_BASE ||
1580 qc->id >= V4L2_CID_LASTP1)
1581 return -EINVAL;
1582
1583 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1584 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1585 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1586 return 0;
1587 }
1588 }
1589
1590 return -EINVAL;
1591}
1592
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001593/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001594 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001595 * inits the device and starts isoc transfer
1596 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001597static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001598{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001600 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001601 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001602 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001603 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001604
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001605 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001606 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001607 if (h->vdev->minor == minor) {
1608 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001609 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001610 }
1611 if (h->vbi_dev->minor == minor) {
1612 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001613 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001614 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001615 if (h->radio_dev &&
1616 h->radio_dev->minor == minor) {
1617 radio = 1;
1618 dev = h;
1619 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001620 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001621 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001622
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001623 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001624 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001625
1626 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001627
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001628 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001629 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001630
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001631
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001632 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001633 if (!fh) {
1634 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001635 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001636 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001637 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001638 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001639 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001640 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001641 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001643 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001644 dev->width = norm_maxw(dev);
1645 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001646 dev->hscale = 0;
1647 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001648
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001649 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001650 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001651 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001653 /* Needed, since GPIO might have disabled power of
1654 some i2c device
1655 */
1656 em28xx_config_i2c(dev);
1657
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001658 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001659 if (fh->radio) {
1660 em28xx_videodbg("video_open: setting radio device\n");
1661 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1662 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001663
1664 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001665
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001666 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1667 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1668 sizeof(struct em28xx_buffer), fh);
1669
Ingo Molnar3593cab2006-02-07 06:49:14 -02001670 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001671
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672 return errCode;
1673}
1674
1675/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677 * unregisters the v4l2,i2c and usb devices
1678 * called when the device gets disconected or at module unload
1679*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001680static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001681{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001683 /*FIXME: I2C IR should be disconnected */
1684
Markus Rechberger9c755412005-11-08 21:37:52 -08001685 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001686 if (dev->sbutton_input_dev)
1687 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001688
1689 if (dev->ir)
1690 em28xx_ir_fini(dev);
1691
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001692 if (dev->radio_dev) {
1693 if (-1 != dev->radio_dev->minor)
1694 video_unregister_device(dev->radio_dev);
1695 else
1696 video_device_release(dev->radio_dev);
1697 dev->radio_dev = NULL;
1698 }
1699 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001700 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1701 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001702 if (-1 != dev->vbi_dev->minor)
1703 video_unregister_device(dev->vbi_dev);
1704 else
1705 video_device_release(dev->vbi_dev);
1706 dev->vbi_dev = NULL;
1707 }
1708 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001709 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1710 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001711 if (-1 != dev->vdev->minor)
1712 video_unregister_device(dev->vdev);
1713 else
1714 video_device_release(dev->vdev);
1715 dev->vdev = NULL;
1716 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001717 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001719
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001720 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001721 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722}
1723
1724/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001726 * stops streaming and deallocates all resources allocated by the v4l2
1727 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001729static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001731 struct em28xx_fh *fh = filp->private_data;
1732 struct em28xx *dev = fh->dev;
1733 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001735 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001737
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001738 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001739 if (res_check(fh))
1740 res_free(fh);
1741
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001742 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001743 videobuf_stop(&fh->vb_vidq);
1744 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001746 /* the device is already disconnect,
1747 free the remaining resources */
1748 if (dev->state & DEV_DISCONNECTED) {
1749 em28xx_release_resources(dev);
1750 mutex_unlock(&dev->lock);
1751 kfree(dev);
1752 return 0;
1753 }
1754
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001755 /* Save some power by putting tuner to sleep */
1756 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1757
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001758 /* do this before setting alternate! */
1759 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001760 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001761
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001762 /* set alternate 0 */
1763 dev->alt = 0;
1764 em28xx_videodbg("setting alternate 0\n");
1765 errCode = usb_set_interface(dev->udev, 0, 0);
1766 if (errCode < 0) {
1767 em28xx_errdev("cannot change alternate number to "
1768 "0 (error=%i)\n", errCode);
1769 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001771 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772 dev->users--;
1773 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001774 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001775 return 0;
1776}
1777
1778/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001779 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001780 * will allocate buffers when called for the first time
1781 */
1782static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001783em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001784 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001786 struct em28xx_fh *fh = filp->private_data;
1787 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001793
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001794 /* FIXME: read() is not prepared to allow changing the video
1795 resolution while streaming. Seems a bug at em28xx_set_fmt
1796 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001797
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001798 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001799 mutex_lock(&dev->lock);
1800 rc = res_get(fh);
1801 mutex_unlock(&dev->lock);
1802
1803 if (unlikely(rc < 0))
1804 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001805
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001806 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1807 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001808 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001809 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810}
1811
1812/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001813 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001814 * will allocate buffers when called for the first time
1815 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001816static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001817{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001818 struct em28xx_fh *fh = filp->private_data;
1819 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001820 int rc;
1821
1822 rc = check_dev(dev);
1823 if (rc < 0)
1824 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001826 mutex_lock(&dev->lock);
1827 rc = res_get(fh);
1828 mutex_unlock(&dev->lock);
1829
1830 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001831 return POLLERR;
1832
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001833 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1834 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001836 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001837}
1838
1839/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001840 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001841 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001842static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001843{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001844 struct em28xx_fh *fh = filp->private_data;
1845 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001846 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001847
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001848 rc = check_dev(dev);
1849 if (rc < 0)
1850 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001851
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001852 mutex_lock(&dev->lock);
1853 rc = res_get(fh);
1854 mutex_unlock(&dev->lock);
1855
1856 if (unlikely(rc < 0))
1857 return rc;
1858
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001859 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001861 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1862 (unsigned long)vma->vm_start,
1863 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1864 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001865
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001866 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001867}
1868
Arjan van de Venfa027c22007-02-12 00:55:33 -08001869static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001870 .owner = THIS_MODULE,
1871 .open = em28xx_v4l2_open,
1872 .release = em28xx_v4l2_close,
1873 .read = em28xx_v4l2_read,
1874 .poll = em28xx_v4l2_poll,
1875 .mmap = em28xx_v4l2_mmap,
1876 .ioctl = video_ioctl2,
1877 .llseek = no_llseek,
1878 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001879};
1880
Hans Verkuila3998102008-07-21 02:57:38 -03001881static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001882 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001883 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1884 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1885 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1886 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001887 .vidioc_g_audio = vidioc_g_audio,
1888 .vidioc_s_audio = vidioc_s_audio,
1889 .vidioc_cropcap = vidioc_cropcap,
1890
Hans Verkuil78b526a2008-05-28 12:16:41 -03001891 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1892 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1893 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001894
1895 .vidioc_reqbufs = vidioc_reqbufs,
1896 .vidioc_querybuf = vidioc_querybuf,
1897 .vidioc_qbuf = vidioc_qbuf,
1898 .vidioc_dqbuf = vidioc_dqbuf,
1899 .vidioc_s_std = vidioc_s_std,
1900 .vidioc_enum_input = vidioc_enum_input,
1901 .vidioc_g_input = vidioc_g_input,
1902 .vidioc_s_input = vidioc_s_input,
1903 .vidioc_queryctrl = vidioc_queryctrl,
1904 .vidioc_g_ctrl = vidioc_g_ctrl,
1905 .vidioc_s_ctrl = vidioc_s_ctrl,
1906 .vidioc_streamon = vidioc_streamon,
1907 .vidioc_streamoff = vidioc_streamoff,
1908 .vidioc_g_tuner = vidioc_g_tuner,
1909 .vidioc_s_tuner = vidioc_s_tuner,
1910 .vidioc_g_frequency = vidioc_g_frequency,
1911 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001912#ifdef CONFIG_VIDEO_ADV_DEBUG
1913 .vidioc_g_register = vidioc_g_register,
1914 .vidioc_s_register = vidioc_s_register,
1915#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001916#ifdef CONFIG_VIDEO_V4L1_COMPAT
1917 .vidiocgmbuf = vidiocgmbuf,
1918#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001919};
1920
1921static const struct video_device em28xx_video_template = {
1922 .fops = &em28xx_v4l_fops,
1923 .release = video_device_release,
1924 .ioctl_ops = &video_ioctl_ops,
1925
1926 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001927
1928 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001929 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001930};
1931
Hans Verkuila3998102008-07-21 02:57:38 -03001932static const struct file_operations radio_fops = {
1933 .owner = THIS_MODULE,
1934 .open = em28xx_v4l2_open,
1935 .release = em28xx_v4l2_close,
1936 .ioctl = video_ioctl2,
1937 .compat_ioctl = v4l_compat_ioctl32,
1938 .llseek = no_llseek,
1939};
1940
1941static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001942 .vidioc_querycap = radio_querycap,
1943 .vidioc_g_tuner = radio_g_tuner,
1944 .vidioc_enum_input = radio_enum_input,
1945 .vidioc_g_audio = radio_g_audio,
1946 .vidioc_s_tuner = radio_s_tuner,
1947 .vidioc_s_audio = radio_s_audio,
1948 .vidioc_s_input = radio_s_input,
1949 .vidioc_queryctrl = radio_queryctrl,
1950 .vidioc_g_ctrl = vidioc_g_ctrl,
1951 .vidioc_s_ctrl = vidioc_s_ctrl,
1952 .vidioc_g_frequency = vidioc_g_frequency,
1953 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001954#ifdef CONFIG_VIDEO_ADV_DEBUG
1955 .vidioc_g_register = vidioc_g_register,
1956 .vidioc_s_register = vidioc_s_register,
1957#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001958};
1959
Hans Verkuila3998102008-07-21 02:57:38 -03001960static struct video_device em28xx_radio_template = {
1961 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001962 .fops = &radio_fops,
1963 .ioctl_ops = &radio_ioctl_ops,
1964 .minor = -1,
1965};
1966
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001967/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001968
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001969
1970static LIST_HEAD(em28xx_extension_devlist);
1971static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1972
1973int em28xx_register_extension(struct em28xx_ops *ops)
1974{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001975 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001976
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001977 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001978 mutex_lock(&em28xx_extension_devlist_lock);
1979 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001980 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1981 if (dev)
1982 ops->init(dev);
1983 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001984 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1985 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001986 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001987 return 0;
1988}
1989EXPORT_SYMBOL(em28xx_register_extension);
1990
1991void em28xx_unregister_extension(struct em28xx_ops *ops)
1992{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001993 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001994
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001995 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001996 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1997 if (dev)
1998 ops->fini(dev);
1999 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002000
2001 mutex_lock(&em28xx_extension_devlist_lock);
2002 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2003 list_del(&ops->next);
2004 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002005 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002006}
2007EXPORT_SYMBOL(em28xx_unregister_extension);
2008
Adrian Bunk532fe652008-01-28 22:10:48 -03002009static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2010 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03002011 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002012{
2013 struct video_device *vfd;
2014
2015 vfd = video_device_alloc();
2016 if (NULL == vfd)
2017 return NULL;
2018 *vfd = *template;
2019 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03002020 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002021 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03002022 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002023
2024 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2025 dev->name, type_name);
2026
2027 return vfd;
2028}
2029
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002030static int register_analog_devices(struct em28xx *dev)
2031{
2032 int ret;
2033
2034 /* allocate and fill video video_device struct */
2035 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2036 if (!dev->vdev) {
2037 em28xx_errdev("cannot allocate video_device.\n");
2038 return -ENODEV;
2039 }
2040
2041 /* register v4l2 video video_device */
2042 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2043 video_nr[dev->devno]);
2044 if (ret) {
2045 em28xx_errdev("unable to register video device (error=%i).\n",
2046 ret);
2047 return ret;
2048 }
2049
2050 /* Allocate and fill vbi video_device struct */
2051 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2052
2053 /* register v4l2 vbi video_device */
2054 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2055 vbi_nr[dev->devno]);
2056 if (ret < 0) {
2057 em28xx_errdev("unable to register vbi device\n");
2058 return ret;
2059 }
2060
2061 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2062 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2063 if (!dev->radio_dev) {
2064 em28xx_errdev("cannot allocate video_device.\n");
2065 return -ENODEV;
2066 }
2067 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2068 radio_nr[dev->devno]);
2069 if (ret < 0) {
2070 em28xx_errdev("can't register radio device\n");
2071 return ret;
2072 }
2073 em28xx_info("Registered radio device as /dev/radio%d\n",
2074 dev->radio_dev->num);
2075 }
2076
2077 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2078 dev->vdev->num, dev->vbi_dev->num);
2079
2080 return 0;
2081}
2082
2083
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002084/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002085 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002086 * allocates and inits the device structs, registers i2c bus and v4l device
2087 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002088static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002089 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002090{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002091 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002092 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002094 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002097 dev->udev = udev;
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002098 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002099 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002100 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002101 init_waitqueue_head(&dev->wait_frame);
2102 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002103
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002104 dev->em28xx_write_regs = em28xx_write_regs;
2105 dev->em28xx_read_reg = em28xx_read_reg;
2106 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2107 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2108 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002109 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002110 dev->format = &format[0];
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002112 em28xx_pre_card_setup(dev);
2113
2114 errCode = em28xx_config(dev);
2115 if (errCode) {
2116 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002117 return -ENOMEM;
2118 }
2119
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002120 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002121 errCode = em28xx_i2c_register(dev);
2122 if (errCode < 0) {
2123 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2124 __func__, errCode);
2125 return errCode;
2126 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002127
2128 /* Do board specific init and eeprom reading */
2129 em28xx_card_setup(dev);
2130
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002131 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002132 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002133 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002134 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002135 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002136 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002137
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002138 /* configure the device */
2139 em28xx_config_i2c(dev);
2140
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002141 /* set default norm */
2142 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002143
2144 maxw = norm_maxw(dev);
2145 maxh = norm_maxh(dev);
2146
2147 /* set default image size */
2148 dev->width = maxw;
2149 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002150 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002151 dev->hscale = 0;
2152 dev->vscale = 0;
2153 dev->ctl_input = 2;
2154
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002155 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002156 if (errCode < 0) {
2157 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2158 __func__, errCode);
2159 return errCode;
2160 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002161
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002162 /* init video dma queues */
2163 INIT_LIST_HEAD(&dev->vidq.active);
2164 INIT_LIST_HEAD(&dev->vidq.queued);
2165
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002166
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002167 if (dev->board.has_msp34xx) {
Sascha Sommer5a804152007-11-03 21:22:38 -03002168 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002169 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002170 if (errCode < 0) {
2171 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2172 __func__, errCode);
2173 return errCode;
2174 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002175 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002176
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002177 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002178 if (errCode < 0) {
2179 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2180 __func__, errCode);
2181 return errCode;
2182 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002183 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002184 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002185
Sascha Sommer5a804152007-11-03 21:22:38 -03002186 video_mux(dev, 0);
2187
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002188 mutex_lock(&em28xx_devlist_mutex);
2189 list_add_tail(&dev->devlist, &em28xx_devlist);
2190 retval = register_analog_devices(dev);
2191 if (retval < 0) {
2192 em28xx_release_resources(dev);
2193 mutex_unlock(&em28xx_devlist_mutex);
2194 goto fail_reg_devices;
2195 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002196
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002197 mutex_lock(&em28xx_extension_devlist_lock);
2198 if (!list_empty(&em28xx_extension_devlist)) {
2199 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2200 if (ops->id)
2201 ops->init(dev);
2202 }
2203 }
2204 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002205 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002206
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002207 /* Save some power by putting tuner to sleep */
2208 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2209
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002210 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002211
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002212fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002213 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214}
2215
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002216#if defined(CONFIG_MODULES) && defined(MODULE)
2217static void request_module_async(struct work_struct *work)
2218{
2219 struct em28xx *dev = container_of(work,
2220 struct em28xx, request_module_wk);
2221
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002222 if (dev->has_audio_class)
2223 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002224 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002225 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002226
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002227 if (dev->board.has_dvb)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002228 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002229}
2230
2231static void request_modules(struct em28xx *dev)
2232{
2233 INIT_WORK(&dev->request_module_wk, request_module_async);
2234 schedule_work(&dev->request_module_wk);
2235}
2236#else
2237#define request_modules(dev)
2238#endif /* CONFIG_MODULES */
2239
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002241 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242 * checks for supported devices
2243 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002244static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002245 const struct usb_device_id *id)
2246{
2247 const struct usb_endpoint_descriptor *endpoint;
2248 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002249 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002250 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002251 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002252 int i, nr, ifnum, isoc_pipe;
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002253 char *speed;
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002254 char descr[255] = "";
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255
2256 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002257 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2258
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002259 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002260 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2261 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002262
2263 /* Don't register audio interfaces */
2264 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002265 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2266 "interface %i, class %i\n",
2267 le16_to_cpu(udev->descriptor.idVendor),
2268 le16_to_cpu(udev->descriptor.idProduct),
2269 ifnum,
2270 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002271
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002272 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002273 return -ENODEV;
2274 }
2275
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002276 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002277
Michael Opdenacker59c51592007-05-09 08:57:56 +02002278 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002279 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2280 USB_ENDPOINT_XFER_ISOC &&
2281 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2282 {
2283 /* It's a newer em2874/em2875 device */
2284 isoc_pipe = 0;
2285 } else {
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002286 int check_interface = 1;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002287 isoc_pipe = 1;
2288 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2289 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002290 USB_ENDPOINT_XFER_ISOC)
2291 check_interface = 0;
2292
2293 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2294 check_interface = 0;
2295
2296 if (!check_interface) {
2297 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2298 "interface %i, class %i found.\n",
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002299 le16_to_cpu(udev->descriptor.idVendor),
2300 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002301 ifnum,
2302 interface->altsetting[0].desc.bInterfaceClass);
2303
2304 em28xx_err(DRIVER_NAME " This is an anciliary "
2305 "interface not used by the driver\n");
2306
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002307 em28xx_devused &= ~(1<<nr);
2308 return -ENODEV;
2309 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310 }
2311
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002312 switch (udev->speed) {
2313 case USB_SPEED_LOW:
2314 speed = "1.5";
2315 break;
2316 case USB_SPEED_UNKNOWN:
2317 case USB_SPEED_FULL:
2318 speed = "12";
2319 break;
2320 case USB_SPEED_HIGH:
2321 speed = "480";
2322 break;
2323 default:
2324 speed = "unknown";
2325 }
2326
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002327 if (udev->manufacturer)
2328 strlcpy(descr, udev->manufacturer, sizeof(descr));
2329
2330 if (udev->product) {
2331 if (*descr)
2332 strlcat(descr, " ", sizeof(descr));
2333 strlcat(descr, udev->product, sizeof(descr));
2334 }
2335 if (*descr)
2336 strlcat(descr, " ", sizeof(descr));
2337
2338 printk(DRIVER_NAME ": New device %s@ %s Mbps "
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002339 "(%04x:%04x, interface %d, class %d)\n",
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002340 descr,
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002341 speed,
2342 le16_to_cpu(udev->descriptor.idVendor),
2343 le16_to_cpu(udev->descriptor.idProduct),
2344 ifnum,
2345 interface->altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002346
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002347 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002348 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2349 EM28XX_MAXBOARDS);
2350 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002351 return -ENOMEM;
2352 }
2353
2354 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002355 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002356 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002357 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002358 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002359 return -ENOMEM;
2360 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002361
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002362 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002363 dev->devno = nr;
2364 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002365 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002366
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002367 /* Checks if audio is provided by some interface */
2368 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2369 uif = udev->config->interface[i];
2370 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2371 dev->has_audio_class = 1;
2372 break;
2373 }
2374 }
2375
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002376 /* compute alternate max packet sizes */
2377 uif = udev->actconfig->interface[0];
2378
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002379 dev->num_alt = uif->num_altsetting;
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002380 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002381/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2382 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2383
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002384 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002385 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002386 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002387 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002388 return -ENOMEM;
2389 }
2390
2391 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002392 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002393 wMaxPacketSize);
2394 dev->alt_max_pkt_size[i] =
2395 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002396 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002397 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002398 }
2399
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002400 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002401 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002402
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002403 /* allocate device struct */
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002404 mutex_init(&dev->lock);
2405 mutex_lock(&dev->lock);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002406 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002407 if (retval) {
2408 em28xx_devused &= ~(1<<dev->devno);
2409 kfree(dev);
2410
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002412 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002414 /* save our data pointer in this interface device */
2415 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002416
2417 request_modules(dev);
2418
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002419 /* Should be the last thing to do, to avoid newer udev's to
2420 open the device before fully initializing it
2421 */
2422 mutex_unlock(&dev->lock);
2423
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002424 return 0;
2425}
2426
2427/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002428 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002429 * called when the device gets diconencted
2430 * video device will be unregistered on v4l2_close in case it is still open
2431 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002432static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002433{
Sascha Sommer5a804152007-11-03 21:22:38 -03002434 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002435 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002436
2437 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002438 usb_set_intfdata(interface, NULL);
2439
2440 if (!dev)
2441 return;
2442
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002443 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002444
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002445 /* wait until all current v4l2 io is finished then deallocate
2446 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002447 mutex_lock(&dev->lock);
2448
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002449 wake_up_interruptible_all(&dev->open);
2450
2451 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002452 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002453 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002454 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002455 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002456
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002457 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002458 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002459 dev->state |= DEV_DISCONNECTED;
2460 wake_up_interruptible(&dev->wait_frame);
2461 wake_up_interruptible(&dev->wait_stream);
2462 } else {
2463 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002464 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002465 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002466 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002467
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002468 mutex_lock(&em28xx_extension_devlist_lock);
2469 if (!list_empty(&em28xx_extension_devlist)) {
2470 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2471 ops->fini(dev);
2472 }
2473 }
2474 mutex_unlock(&em28xx_extension_devlist_lock);
2475
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002476 if (!dev->users) {
2477 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002478 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002479 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002480}
2481
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002482static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002483 .name = "em28xx",
2484 .probe = em28xx_usb_probe,
2485 .disconnect = em28xx_usb_disconnect,
2486 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002487};
2488
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002489static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002490{
2491 int result;
2492
2493 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002494 (EM28XX_VERSION_CODE >> 16) & 0xff,
2495 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002496#ifdef SNAPSHOT
2497 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2498 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2499#endif
2500
2501 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002502 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002503 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002504 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002505 " usb_register failed. Error number %d.\n", result);
2506
2507 return result;
2508}
2509
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002510static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002511{
2512 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002513 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002514}
2515
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002516module_init(em28xx_module_init);
2517module_exit(em28xx_module_exit);
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03002518