blob: 2a461dde480c89c0dcf339920861764a709c89f9 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035#include <linux/video_decoder.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080039#include <media/tuner.h>
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030041#include <media/msp3400.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020055 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
57MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL");
60
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080061static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080062
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080063static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020064static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080066module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020067module_param_array(video_nr, int, NULL, 0444);
68module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080069MODULE_PARM_DESC(card,"card type");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020070MODULE_PARM_DESC(video_nr,"video device numbers");
71MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073static int tuner = -1;
74module_param(tuner, int, 0444);
75MODULE_PARM_DESC(tuner, "tuner type");
76
77static unsigned int video_debug = 0;
78module_param(video_debug,int,0644);
79MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
80
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020081/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
82static unsigned long em28xx_devused;
83
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080084/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080085static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080086 {
87 .name = "PAL",
88 .id = V4L2_STD_PAL,
89 .mode = VIDEO_MODE_PAL,
90 }, {
91 .name = "NTSC",
92 .id = V4L2_STD_NTSC,
93 .mode = VIDEO_MODE_NTSC,
94 }, {
95 .name = "SECAM",
96 .id = V4L2_STD_SECAM,
97 .mode = VIDEO_MODE_SECAM,
98 }, {
99 .name = "PAL-M",
100 .id = V4L2_STD_PAL_M,
101 .mode = VIDEO_MODE_PAL,
102 }
103};
104
105#define TVNORMS ARRAY_SIZE(tvnorms)
106
107/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200108/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800109static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800110 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200111 .id = V4L2_CID_AUDIO_VOLUME,
112 .type = V4L2_CTRL_TYPE_INTEGER,
113 .name = "Volume",
114 .minimum = 0x0,
115 .maximum = 0x1f,
116 .step = 0x1,
117 .default_value = 0x1f,
118 .flags = 0,
119 },{
120 .id = V4L2_CID_AUDIO_MUTE,
121 .type = V4L2_CTRL_TYPE_BOOLEAN,
122 .name = "Mute",
123 .minimum = 0,
124 .maximum = 1,
125 .step = 1,
126 .default_value = 1,
127 .flags = 0,
128 }
129};
130
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800131static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800132
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200133static DEFINE_MUTEX(em28xx_sysfs_lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800134static DECLARE_RWSEM(em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800135
136/********************* v4l2 interface ******************************************/
137
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800138/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800139 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800140 * inits registers with sane defaults
141 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800142static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800143{
144
145 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800146 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800147
148 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200149
Markus Rechberger9475fb12006-02-27 00:07:34 -0300150/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
151/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200152 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
153
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800154 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800155 dev->mute = 1; /* maybe not the right place... */
156 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800157 em28xx_audio_analog_set(dev);
158 em28xx_audio_analog_setup(dev);
159 em28xx_outfmt_set_yuv422(dev);
160 em28xx_colorlevels_set_default(dev);
161 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800162
163 return 0;
164}
165
166/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800167 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800168 * configure i2c attached devices
169 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800170static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800171{
172 struct v4l2_frequency f;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300173 struct v4l2_routing route;
174
175 route.input = INPUT(dev->ctl_input)->vmux;
176 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100177 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300178 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300179 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800180
181 /* configure tuner */
182 f.tuner = 0;
183 f.type = V4L2_TUNER_ANALOG_TV;
184 f.frequency = 9076; /* FIXME:remove magic number */
185 dev->ctl_freq = f.frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800186 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800187
188 /* configure tda9887 */
189
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800190
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800191/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800192}
193
194/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800195 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800196 * prepare queues for incoming and outgoing frames
197 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800198static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800199{
200 u32 i;
201
202 INIT_LIST_HEAD(&dev->inqueue);
203 INIT_LIST_HEAD(&dev->outqueue);
204
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800205 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800206 dev->frame[i].state = F_UNUSED;
207 dev->frame[i].buf.bytesused = 0;
208 }
209}
210
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800211static void video_mux(struct em28xx *dev, int index)
212{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300213 int ainput;
214 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800215
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300216 route.input = INPUT(index)->vmux;
217 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800218 dev->ctl_input = index;
219 dev->ctl_ainput = INPUT(index)->amux;
220
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300221 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800222
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300223 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800224
225 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200226 if (dev->i2s_speed)
227 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300228 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300229 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300230 /* Note: this is msp3400 specific */
231 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800232 ainput = EM28XX_AUDIO_SRC_TUNER;
233 em28xx_audio_source(dev, ainput);
234 } else {
235 switch (dev->ctl_ainput) {
Markus Rechberger9475fb12006-02-27 00:07:34 -0300236 case 0:
237 ainput = EM28XX_AUDIO_SRC_TUNER;
238 break;
239 default:
240 ainput = EM28XX_AUDIO_SRC_LINE;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800241 }
242 em28xx_audio_source(dev, ainput);
243 }
244}
245
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800246/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800247 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800248 * inits the device and starts isoc transfer
249 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800250static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800251{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800252 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800253 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800254 struct em28xx *h,*dev = NULL;
Markus Rechberger9c755412005-11-08 21:37:52 -0800255 struct list_head *list;
256
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800257 list_for_each(list,&em28xx_devlist) {
258 h = list_entry(list, struct em28xx, devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -0800259 if (h->vdev->minor == minor) {
260 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200261 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
262 }
263 if (h->vbi_dev->minor == minor) {
264 dev = h;
265 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -0800266 }
267 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200268 if (NULL == dev)
269 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -0800270
271 filp->private_data=dev;
272
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200273 em28xx_videodbg("open minor=%d type=%s users=%d\n",
274 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800275
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800276 if (!down_read_trylock(&em28xx_disconnect))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800277 return -ERESTARTSYS;
278
279 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800280 em28xx_warn("this driver can be opened only once\n");
281 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800282 return -EBUSY;
283 }
284
Ingo Molnar3593cab2006-02-07 06:49:14 -0200285 mutex_init(&dev->fileop_lock); /* to 1 == available */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800286 spin_lock_init(&dev->queue_lock);
287 init_waitqueue_head(&dev->wait_frame);
288 init_waitqueue_head(&dev->wait_stream);
289
Ingo Molnar3593cab2006-02-07 06:49:14 -0200290 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800291
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200292 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
293 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800294
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200295 dev->width = norm_maxw(dev);
296 dev->height = norm_maxh(dev);
297 dev->frame_size = dev->width * dev->height * 2;
298 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
299 dev->bytesperline = dev->width * 2;
300 dev->hscale = 0;
301 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800302
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200303 em28xx_capture_start(dev, 1);
304 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800305
Markus Rechbergere5d4a562006-02-07 06:49:13 -0200306 /* device needs to be initialized before isoc transfer */
307 video_mux(dev, 0);
308
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200309 /* start the transfer */
310 errCode = em28xx_init_isoc(dev);
311 if (errCode)
312 goto err;
313
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200314 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800315
316 dev->users++;
317 filp->private_data = dev;
318 dev->io = IO_NONE;
319 dev->stream = STREAM_OFF;
320 dev->num_frames = 0;
321
322 /* prepare queues */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800323 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800324
325 dev->state |= DEV_INITIALIZED;
326
Ingo Molnar3593cab2006-02-07 06:49:14 -0200327err:
328 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800329 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800330 return errCode;
331}
332
333/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800334 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800335 * unregisters the v4l2,i2c and usb devices
336 * called when the device gets disconected or at module unload
337*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800338static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800339{
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200340 mutex_lock(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800341
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200342 /*FIXME: I2C IR should be disconnected */
343
344 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
345 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
346 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -0800347 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800348 video_unregister_device(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200349 video_unregister_device(dev->vbi_dev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800350 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800351 usb_put_dev(dev->udev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200352 mutex_unlock(&em28xx_sysfs_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200353
354
355 /* Mark device as unused */
356 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800357}
358
359/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800360 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800361 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
362 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800363static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800364{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800365 int errCode;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800366 struct em28xx *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800367
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800368 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800369
Ingo Molnar3593cab2006-02-07 06:49:14 -0200370 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800371
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800372 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800373
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800374 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800375
376 /* the device is already disconnect, free the remaining resources */
377 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800378 em28xx_release_resources(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200379 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800380 kfree(dev);
381 return 0;
382 }
383
384 /* set alternate 0 */
385 dev->alt = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800386 em28xx_videodbg("setting alternate 0\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800387 errCode = usb_set_interface(dev->udev, 0, 0);
388 if (errCode < 0) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800389 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800390 errCode);
391 }
392
393 dev->users--;
394 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200395 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800396 return 0;
397}
398
399/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800400 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800401 * will allocate buffers when called for the first time
402 */
403static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800404em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800405 loff_t * f_pos)
406{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800407 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800408 unsigned long lock_flags;
409 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800410 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800411
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200412 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
413 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
414 }
415 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
416 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
417 em28xx_videodbg("not supported yet! ...\n");
418 if (copy_to_user(buf, "", 1)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200419 mutex_unlock(&dev->fileop_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200420 return -EFAULT;
421 }
422 return (1);
423 }
424 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
425 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
426 em28xx_videodbg("not supported yet! ...\n");
427 if (copy_to_user(buf, "", 1)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200428 mutex_unlock(&dev->fileop_lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200429 return -EFAULT;
430 }
431 return (1);
432 }
433
Ingo Molnar3593cab2006-02-07 06:49:14 -0200434 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800435 return -ERESTARTSYS;
436
437 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800438 em28xx_videodbg("device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200439 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800440 return -ENODEV;
441 }
442
443 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800444 em28xx_videodbg("device misconfigured; close and open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200445 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800446 return -EIO;
447 }
448
449 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800450 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800451 " the device again to choose the read method\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200452 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800453 return -EINVAL;
454 }
455
456 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800457 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
458 em28xx_errdev("read failed, not enough memory\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200459 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800460 return -ENOMEM;
461 }
462 dev->io = IO_READ;
463 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800464 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800465 }
466
467 if (!count) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200468 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800469 return 0;
470 }
471
472 if (list_empty(&dev->outqueue)) {
473 if (filp->f_flags & O_NONBLOCK) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200474 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800475 return -EAGAIN;
476 }
477 ret = wait_event_interruptible
478 (dev->wait_frame,
479 (!list_empty(&dev->outqueue)) ||
480 (dev->state & DEV_DISCONNECTED));
481 if (ret) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200482 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800483 return ret;
484 }
485 if (dev->state & DEV_DISCONNECTED) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200486 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800487 return -ENODEV;
488 }
489 }
490
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800491 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800492
493 spin_lock_irqsave(&dev->queue_lock, lock_flags);
494 list_for_each_entry(i, &dev->outqueue, frame)
495 i->state = F_UNUSED;
496 INIT_LIST_HEAD(&dev->outqueue);
497 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
498
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800499 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800500
501 if (count > f->buf.length)
502 count = f->buf.length;
503
504 if (copy_to_user(buf, f->bufmem, count)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200505 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800506 return -EFAULT;
507 }
508 *f_pos += count;
509
Ingo Molnar3593cab2006-02-07 06:49:14 -0200510 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511
512 return count;
513}
514
515/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800516 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800517 * will allocate buffers when called for the first time
518 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800519static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800521 unsigned int mask = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800522 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800523
Ingo Molnar3593cab2006-02-07 06:49:14 -0200524 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800525 return POLLERR;
526
527 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800528 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800529 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800530 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800531 } else {
532 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800533 if (!em28xx_request_buffers
534 (dev, EM28XX_NUM_READ_FRAMES)) {
535 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800536 ("poll() failed, not enough memory\n");
537 } else {
538 dev->io = IO_READ;
539 dev->stream = STREAM_ON;
540 }
541 }
542
543 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800544 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800545 poll_wait(filp, &dev->wait_frame, wait);
546
547 if (!list_empty(&dev->outqueue))
548 mask |= POLLIN | POLLRDNORM;
549
Ingo Molnar3593cab2006-02-07 06:49:14 -0200550 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800551
552 return mask;
553 }
554 }
555
Ingo Molnar3593cab2006-02-07 06:49:14 -0200556 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800557 return POLLERR;
558}
559
560/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800561 * em28xx_vm_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800562 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800563static void em28xx_vm_open(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800564{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800565 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800566 f->vma_use_count++;
567}
568
569/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800570 * em28xx_vm_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800571 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800572static void em28xx_vm_close(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800573{
574 /* NOTE: buffers are not freed here */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800575 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800576 f->vma_use_count--;
577}
578
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800579static struct vm_operations_struct em28xx_vm_ops = {
580 .open = em28xx_vm_open,
581 .close = em28xx_vm_close,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800582};
583
584/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800585 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800586 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800587static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800588{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800589 unsigned long size = vma->vm_end - vma->vm_start,
Sascha Sommer3639c862005-12-12 00:37:30 -0800590 start = vma->vm_start;
591 void *pos;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800592 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800593
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800594 struct em28xx *dev = filp->private_data;
Markus Rechberger9c755412005-11-08 21:37:52 -0800595
Ingo Molnar3593cab2006-02-07 06:49:14 -0200596 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800597 return -ERESTARTSYS;
598
599 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800600 em28xx_videodbg("mmap: device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200601 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800602 return -ENODEV;
603 }
604
605 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800606 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800607 "open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200608 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800609 return -EIO;
610 }
611
612 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
613 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200614 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800615 return -EINVAL;
616 }
617
618 for (i = 0; i < dev->num_frames; i++) {
619 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
620 break;
621 }
622 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800623 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200624 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800625 return -EINVAL;
626 }
627
628 /* VM_IO is eventually going to replace PageReserved altogether */
629 vma->vm_flags |= VM_IO;
630 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
631
Sascha Sommer3639c862005-12-12 00:37:30 -0800632 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800633 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -0800634 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
635 em28xx_videodbg("mmap: vm_insert_page failed\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -0200636 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800637 return -EAGAIN;
638 }
639 start += PAGE_SIZE;
640 pos += PAGE_SIZE;
641 size -= PAGE_SIZE;
642 }
643
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800644 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800645 vma->vm_private_data = &dev->frame[i];
646
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800647 em28xx_vm_open(vma);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200648 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800649 return 0;
650}
651
652/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800653 * em28xx_get_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800654 * return the current saturation, brightness or contrast, mute state
655 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800656static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800657{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800658 switch (ctrl->id) {
659 case V4L2_CID_AUDIO_MUTE:
660 ctrl->value = dev->mute;
661 return 0;
662 case V4L2_CID_AUDIO_VOLUME:
663 ctrl->value = dev->volume;
664 return 0;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200665 default:
666 return -EINVAL;
667 }
668}
669
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800670/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800671 * em28xx_set_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800672 * mute or set new saturation, brightness or contrast
673 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800674static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800675{
676 switch (ctrl->id) {
677 case V4L2_CID_AUDIO_MUTE:
678 if (ctrl->value != dev->mute) {
679 dev->mute = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800680 em28xx_audio_usb_mute(dev, ctrl->value);
681 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800682 }
683 return 0;
684 case V4L2_CID_AUDIO_VOLUME:
685 dev->volume = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800686 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200687 default:
688 return -EINVAL;
689 }
690}
691
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800692/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800693 * em28xx_stream_interrupt()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800694 * stops streaming
695 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800696static int em28xx_stream_interrupt(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800697{
698 int ret = 0;
699
700 /* stop reading from the device */
701
702 dev->stream = STREAM_INTERRUPT;
703 ret = wait_event_timeout(dev->wait_stream,
704 (dev->stream == STREAM_OFF) ||
705 (dev->state & DEV_DISCONNECTED),
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800706 EM28XX_URB_TIMEOUT);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800707 if (dev->state & DEV_DISCONNECTED)
708 return -ENODEV;
709 else if (ret) {
710 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800711 em28xx_videodbg("device is misconfigured; close and "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200712 "open /dev/video%d again\n",
713 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800714 return ret;
715 }
716
717 return 0;
718}
719
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800720static int em28xx_set_norm(struct em28xx *dev, int width, int height)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800721{
722 unsigned int hscale, vscale;
723 unsigned int maxh, maxw;
724
725 maxw = norm_maxw(dev);
726 maxh = norm_maxh(dev);
727
728 /* width must even because of the YUYV format */
729 /* height must be even because of interlacing */
730 height &= 0xfffe;
731 width &= 0xfffe;
732
733 if (height < 32)
734 height = 32;
735 if (height > maxh)
736 height = maxh;
737 if (width < 48)
738 width = 48;
739 if (width > maxw)
740 width = maxw;
741
742 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
743 hscale = 0x3fff;
744 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
745
746 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
747 vscale = 0x3fff;
748 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
749
750 /* set new image size */
751 dev->width = width;
752 dev->height = height;
753 dev->frame_size = dev->width * dev->height * 2;
754 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
755 dev->bytesperline = dev->width * 2;
756 dev->hscale = hscale;
757 dev->vscale = vscale;
758
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800759 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800760
761 return 0;
762}
763
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200764static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
765{
766 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
767 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
768 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
769 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
770 "V4L2_BUF_TYPE_VBI_CAPTURE" :
771 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
772 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
773 "not supported");
774
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200775 switch (format->type) {
776 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
777 {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200778 format->fmt.pix.width = dev->width;
779 format->fmt.pix.height = dev->height;
780 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
781 format->fmt.pix.bytesperline = dev->bytesperline;
782 format->fmt.pix.sizeimage = dev->frame_size;
783 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
784 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
785
786 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
787 dev->height);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200788 break;
789 }
790
791 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
792 {
793 format->fmt.sliced.service_set=0;
794
795 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
796
797 if (format->fmt.sliced.service_set==0)
798 return -EINVAL;
799
800 break;
801 }
802
803 default:
804 return -EINVAL;
805 }
806 return (0);
807}
808
809static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
810{
811 u32 i;
812 int ret = 0;
813 int width = format->fmt.pix.width;
814 int height = format->fmt.pix.height;
815 unsigned int hscale, vscale;
816 unsigned int maxh, maxw;
817
818 maxw = norm_maxw(dev);
819 maxh = norm_maxh(dev);
820
821 em28xx_videodbg("%s: type=%s\n",
822 cmd == VIDIOC_TRY_FMT ?
823 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
824 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
825 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
826 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
827 "V4L2_BUF_TYPE_VBI_CAPTURE " :
828 "not supported");
829
830 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
831 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
832
833 if (format->fmt.sliced.service_set==0)
834 return -EINVAL;
835
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200836 return 0;
837 }
838
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200839
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200840 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
841 return -EINVAL;
842
843 em28xx_videodbg("%s: requested %dx%d\n",
844 cmd == VIDIOC_TRY_FMT ?
845 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
846 format->fmt.pix.width, format->fmt.pix.height);
847
848 /* FIXME: Move some code away from here */
849 /* width must even because of the YUYV format */
850 /* height must be even because of interlacing */
851 height &= 0xfffe;
852 width &= 0xfffe;
853
854 if (height < 32)
855 height = 32;
856 if (height > maxh)
857 height = maxh;
858 if (width < 48)
859 width = 48;
860 if (width > maxw)
861 width = maxw;
862
863 if(dev->is_em2800){
864 /* the em2800 can only scale down to 50% */
865 if(height % (maxh / 2))
866 height=maxh;
867 if(width % (maxw / 2))
868 width=maxw;
869 /* according to empiatech support */
870 /* the MaxPacketSize is to small to support */
871 /* framesizes larger than 640x480 @ 30 fps */
872 /* or 640x576 @ 25 fps. As this would cut */
873 /* of a part of the image we prefer */
874 /* 360x576 or 360x480 for now */
875 if(width == maxw && height == maxh)
876 width /= 2;
877 }
878
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200879 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200880 hscale = 0x3fff;
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200881
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200882 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
883
884 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200885 vscale = 0x3fff;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200886
887 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200888
889 format->fmt.pix.width = width;
890 format->fmt.pix.height = height;
891 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
892 format->fmt.pix.bytesperline = width * 2;
893 format->fmt.pix.sizeimage = width * 2 * height;
894 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
895 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
896
897 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200898 cmd == VIDIOC_TRY_FMT ?
899 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
900 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200901
902 if (cmd == VIDIOC_TRY_FMT)
903 return 0;
904
905 for (i = 0; i < dev->num_frames; i++)
906 if (dev->frame[i].vma_use_count) {
907 em28xx_videodbg("VIDIOC_S_FMT failed. "
908 "Unmap the buffers first.\n");
909 return -EINVAL;
910 }
911
912 /* stop io in case it is already in progress */
913 if (dev->stream == STREAM_ON) {
914 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
915 if ((ret = em28xx_stream_interrupt(dev)))
916 return ret;
917 }
918
919 em28xx_release_buffers(dev);
920 dev->io = IO_NONE;
921
922 /* set new image size */
923 dev->width = width;
924 dev->height = height;
925 dev->frame_size = dev->width * dev->height * 2;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200926 dev->field_size = dev->frame_size >> 1;
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -0200927 dev->bytesperline = dev->width * 2;
928 dev->hscale = hscale;
929 dev->vscale = vscale;
930 em28xx_uninit_isoc(dev);
931 em28xx_set_alternate(dev);
932 em28xx_capture_start(dev, 1);
933 em28xx_resolution_set(dev);
934 em28xx_init_isoc(dev);
935
936 return 0;
937}
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200938
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800939/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800940 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800941 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800942 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800943 * copying is done already, arg is a kernel pointer.
944 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800945static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
946 struct em28xx *dev, unsigned int cmd, void *arg,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800947 v4l2_kioctl driver_ioctl)
948{
949 int ret;
950
951 switch (cmd) {
952 /* ---------- tv norms ---------- */
953 case VIDIOC_ENUMSTD:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200954 {
955 struct v4l2_standard *e = arg;
956 unsigned int i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800957
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200958 i = e->index;
959 if (i >= TVNORMS)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800960 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200961 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
962 tvnorms[e->index].name);
963 e->index = i;
964 if (ret < 0)
965 return ret;
966 return 0;
967 }
968 case VIDIOC_G_STD:
969 {
970 v4l2_std_id *id = arg;
971
972 *id = dev->tvnorm->id;
973 return 0;
974 }
975 case VIDIOC_S_STD:
976 {
977 v4l2_std_id *id = arg;
978 unsigned int i;
979
980 for (i = 0; i < TVNORMS; i++)
981 if (*id == tvnorms[i].id)
982 break;
983 if (i == TVNORMS)
984 for (i = 0; i < TVNORMS; i++)
985 if (*id & tvnorms[i].id)
986 break;
987 if (i == TVNORMS)
988 return -EINVAL;
989
Ingo Molnar3593cab2006-02-07 06:49:14 -0200990 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200991 dev->tvnorm = &tvnorms[i];
992
993 em28xx_set_norm(dev, dev->width, dev->height);
994
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200995 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
996 &dev->tvnorm->id);
997
Ingo Molnar3593cab2006-02-07 06:49:14 -0200998 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -0200999
1000 return 0;
1001 }
1002
1003 /* ------ input switching ---------- */
1004 case VIDIOC_ENUMINPUT:
1005 {
1006 struct v4l2_input *i = arg;
1007 unsigned int n;
1008 static const char *iname[] = {
1009 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1010 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1011 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1012 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1013 [EM28XX_VMUX_SVIDEO] = "S-Video",
1014 [EM28XX_VMUX_TELEVISION] = "Television",
1015 [EM28XX_VMUX_CABLE] = "Cable TV",
1016 [EM28XX_VMUX_DVB] = "DVB",
1017 [EM28XX_VMUX_DEBUG] = "for debug only",
1018 };
1019
1020 n = i->index;
1021 if (n >= MAX_EM28XX_INPUT)
1022 return -EINVAL;
1023 if (0 == INPUT(n)->type)
1024 return -EINVAL;
1025 memset(i, 0, sizeof(*i));
1026 i->index = n;
1027 i->type = V4L2_INPUT_TYPE_CAMERA;
1028 strcpy(i->name, iname[INPUT(n)->type]);
1029 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1030 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1031 i->type = V4L2_INPUT_TYPE_TUNER;
1032 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1033 i->std |= tvnorms[n].id;
1034 return 0;
1035 }
1036 case VIDIOC_G_INPUT:
1037 {
1038 int *i = arg;
1039 *i = dev->ctl_input;
1040
1041 return 0;
1042 }
1043 case VIDIOC_S_INPUT:
1044 {
1045 int *index = arg;
1046
1047 if (*index >= MAX_EM28XX_INPUT)
1048 return -EINVAL;
1049 if (0 == INPUT(*index)->type)
1050 return -EINVAL;
1051
Ingo Molnar3593cab2006-02-07 06:49:14 -02001052 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001053 video_mux(dev, *index);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001054 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001055
1056 return 0;
1057 }
1058 case VIDIOC_G_AUDIO:
1059 {
1060 struct v4l2_audio *a = arg;
1061 unsigned int index = a->index;
1062
1063 if (a->index > 1)
1064 return -EINVAL;
1065 memset(a, 0, sizeof(*a));
1066 index = dev->ctl_ainput;
1067
1068 if (index == 0) {
1069 strcpy(a->name, "Television");
1070 } else {
1071 strcpy(a->name, "Line In");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001072 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001073 a->capability = V4L2_AUDCAP_STEREO;
1074 a->index = index;
1075 return 0;
1076 }
1077 case VIDIOC_S_AUDIO:
1078 {
1079 struct v4l2_audio *a = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001080
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001081 if (a->index != dev->ctl_ainput)
1082 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001083
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001084 return 0;
1085 }
1086
1087 /* --- controls ---------------------------------------------- */
1088 case VIDIOC_QUERYCTRL:
1089 {
1090 struct v4l2_queryctrl *qc = arg;
1091 int i, id=qc->id;
1092
1093 memset(qc,0,sizeof(*qc));
1094 qc->id=id;
1095
1096 if (!dev->has_msp34xx) {
1097 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1098 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1099 memcpy(qc, &(em28xx_qctrl[i]),
1100 sizeof(*qc));
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001101 return 0;
1102 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001103 }
1104 }
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001105 em28xx_i2c_call_clients(dev,cmd,qc);
1106 if (qc->type)
1107 return 0;
1108 else
1109 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001110 }
1111 case VIDIOC_G_CTRL:
1112 {
1113 struct v4l2_control *ctrl = arg;
1114 int retval=-EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001115
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001116 if (!dev->has_msp34xx)
1117 retval=em28xx_get_ctrl(dev, ctrl);
1118 if (retval==-EINVAL) {
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001119 em28xx_i2c_call_clients(dev,cmd,arg);
1120 return 0;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001121 } else return retval;
1122 }
1123 case VIDIOC_S_CTRL:
1124 {
1125 struct v4l2_control *ctrl = arg;
1126 u8 i;
1127
1128 if (!dev->has_msp34xx){
1129 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1130 if (ctrl->id == em28xx_qctrl[i].id) {
1131 if (ctrl->value <
1132 em28xx_qctrl[i].minimum
1133 || ctrl->value >
1134 em28xx_qctrl[i].maximum)
1135 return -ERANGE;
1136 return em28xx_set_ctrl(dev, ctrl);
1137 }
1138 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001139 }
1140
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001141 em28xx_i2c_call_clients(dev,cmd,arg);
1142 return 0;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001143 }
1144 /* --- tuner ioctls ------------------------------------------ */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001145 case VIDIOC_G_TUNER:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001146 {
1147 struct v4l2_tuner *t = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001148
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001149 if (0 != t->index)
1150 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001151
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001152 memset(t, 0, sizeof(*t));
1153 strcpy(t->name, "Tuner");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001154 mutex_lock(&dev->lock);
Hans Verkuilab4cecf2006-04-01 16:40:21 -03001155 /* let clients fill in the remainder of this struct */
1156 em28xx_i2c_call_clients(dev, cmd, t);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001157 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001158 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1159 t->afc);
1160 return 0;
1161 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001162 case VIDIOC_S_TUNER:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001163 {
1164 struct v4l2_tuner *t = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001165
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001166 if (0 != t->index)
1167 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001168 mutex_lock(&dev->lock);
Hans Verkuilab4cecf2006-04-01 16:40:21 -03001169 /* let clients handle this */
1170 em28xx_i2c_call_clients(dev, cmd, t);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001171 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001172 return 0;
1173 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001174 case VIDIOC_G_FREQUENCY:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001175 {
1176 struct v4l2_frequency *f = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001177
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001178 memset(f, 0, sizeof(*f));
1179 f->type = V4L2_TUNER_ANALOG_TV;
1180 f->frequency = dev->ctl_freq;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001181
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001182 return 0;
1183 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001184 case VIDIOC_S_FREQUENCY:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001185 {
1186 struct v4l2_frequency *f = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001187
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001188 if (0 != f->tuner)
1189 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001190
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001191 if (V4L2_TUNER_ANALOG_TV != f->type)
1192 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001193
Ingo Molnar3593cab2006-02-07 06:49:14 -02001194 mutex_lock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001195 dev->ctl_freq = f->frequency;
1196 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001197 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001198 return 0;
1199 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001200 case VIDIOC_CROPCAP:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001201 {
1202 struct v4l2_cropcap *cc = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001203
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001204 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 return -EINVAL;
1206 cc->bounds.left = 0;
1207 cc->bounds.top = 0;
1208 cc->bounds.width = dev->width;
1209 cc->bounds.height = dev->height;
1210 cc->defrect = cc->bounds;
1211 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1212 cc->pixelaspect.denominator = 59;
1213 return 0;
1214 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001215 case VIDIOC_STREAMON:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001216 {
1217 int *type = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001218
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001219 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1220 || dev->io != IO_MMAP)
1221 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001222
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001223 if (list_empty(&dev->inqueue))
1224 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001225
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001226 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001227
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001228 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001229
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001230 return 0;
1231 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001232 case VIDIOC_STREAMOFF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001233 {
1234 int *type = arg;
1235 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001236
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001237 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1238 || dev->io != IO_MMAP)
1239 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001240
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001241 if (dev->stream == STREAM_ON) {
1242 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1243 if ((ret = em28xx_stream_interrupt(dev)))
1244 return ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001245 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001246 em28xx_empty_framequeues(dev);
1247
1248 return 0;
1249 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001250 default:
1251 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1252 driver_ioctl);
1253 }
1254 return 0;
1255}
1256
1257/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001258 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001259 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001260 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001261 * copying is done already, arg is a kernel pointer.
1262 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001263static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001264 unsigned int cmd, void *arg)
1265{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001266 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001267
1268 if (!dev)
1269 return -ENODEV;
1270
1271 if (video_debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -02001272 v4l_print_ioctl(dev->name,cmd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001273
1274 switch (cmd) {
1275
1276 /* --- capabilities ------------------------------------------ */
1277 case VIDIOC_QUERYCAP:
1278 {
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001279 struct v4l2_capability *cap = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001280
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001281 memset(cap, 0, sizeof(*cap));
1282 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1283 strlcpy(cap->card, em28xx_boards[dev->model].name,
1284 sizeof(cap->card));
1285 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1286 sizeof(cap->bus_info));
1287 cap->version = EM28XX_VERSION_CODE;
1288 cap->capabilities =
1289 V4L2_CAP_SLICED_VBI_CAPTURE |
1290 V4L2_CAP_VIDEO_CAPTURE |
1291 V4L2_CAP_AUDIO |
1292 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1293 if (dev->has_tuner)
1294 cap->capabilities |= V4L2_CAP_TUNER;
1295 return 0;
1296 }
1297 /* --- capture ioctls ---------------------------------------- */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001298 case VIDIOC_ENUM_FMT:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001299 {
1300 struct v4l2_fmtdesc *fmtd = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001301
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001302 if (fmtd->index != 0)
1303 return -EINVAL;
1304 memset(fmtd, 0, sizeof(*fmtd));
1305 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1306 strcpy(fmtd->description, "Packed YUY2");
1307 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1308 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1309 return 0;
1310 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001311 case VIDIOC_G_FMT:
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001312 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001313
1314 case VIDIOC_TRY_FMT:
1315 case VIDIOC_S_FMT:
Mauro Carvalho Chehab2d50f842006-01-23 17:11:08 -02001316 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001317
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001318 case VIDIOC_REQBUFS:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001319 {
1320 struct v4l2_requestbuffers *rb = arg;
1321 u32 i;
1322 int ret;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001323
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001324 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1325 rb->memory != V4L2_MEMORY_MMAP)
1326 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001327
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001328 if (dev->io == IO_READ) {
1329 em28xx_videodbg ("method is set to read;"
1330 " close and open the device again to"
1331 " choose the mmap I/O method\n");
1332 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001333 }
1334
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001335 for (i = 0; i < dev->num_frames; i++)
1336 if (dev->frame[i].vma_use_count) {
1337 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1338 return -EINVAL;
1339 }
1340
1341 if (dev->stream == STREAM_ON) {
1342 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1343 if ((ret = em28xx_stream_interrupt(dev)))
1344 return ret;
1345 }
1346
1347 em28xx_empty_framequeues(dev);
1348
1349 em28xx_release_buffers(dev);
1350 if (rb->count)
1351 rb->count =
1352 em28xx_request_buffers(dev, rb->count);
1353
1354 dev->frame_current = NULL;
1355
1356 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
1357 rb->count);
1358 dev->io = rb->count ? IO_MMAP : IO_NONE;
1359 return 0;
1360 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001361 case VIDIOC_QUERYBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001362 {
1363 struct v4l2_buffer *b = arg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001364
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001365 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1366 b->index >= dev->num_frames || dev->io != IO_MMAP)
1367 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001368
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001369 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001370
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001371 if (dev->frame[b->index].vma_use_count) {
1372 b->flags |= V4L2_BUF_FLAG_MAPPED;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001373 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001374 if (dev->frame[b->index].state == F_DONE)
1375 b->flags |= V4L2_BUF_FLAG_DONE;
1376 else if (dev->frame[b->index].state != F_UNUSED)
1377 b->flags |= V4L2_BUF_FLAG_QUEUED;
1378 return 0;
1379 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001380 case VIDIOC_QBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001381 {
1382 struct v4l2_buffer *b = arg;
1383 unsigned long lock_flags;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001384
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001385 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1386 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1387 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001388 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001389
1390 if (dev->frame[b->index].state != F_UNUSED) {
1391 return -EAGAIN;
1392 }
1393 dev->frame[b->index].state = F_QUEUED;
1394
1395 /* add frame to fifo */
1396 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1397 list_add_tail(&dev->frame[b->index].frame,
1398 &dev->inqueue);
1399 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1400
1401 return 0;
1402 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001403 case VIDIOC_DQBUF:
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001404 {
1405 struct v4l2_buffer *b = arg;
1406 struct em28xx_frame_t *f;
1407 unsigned long lock_flags;
1408 int ret = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001409
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001410 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1411 || dev->io != IO_MMAP)
1412 return -EINVAL;
1413
1414 if (list_empty(&dev->outqueue)) {
1415 if (dev->stream == STREAM_OFF)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001416 return -EINVAL;
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001417 if (filp->f_flags & O_NONBLOCK)
1418 return -EAGAIN;
1419 ret = wait_event_interruptible
1420 (dev->wait_frame,
1421 (!list_empty(&dev->outqueue)) ||
1422 (dev->state & DEV_DISCONNECTED));
1423 if (ret)
1424 return ret;
1425 if (dev->state & DEV_DISCONNECTED)
1426 return -ENODEV;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001427 }
Mauro Carvalho Chehab9aeb4b02006-01-23 17:11:09 -02001428
1429 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1430 f = list_entry(dev->outqueue.next,
1431 struct em28xx_frame_t, frame);
1432 list_del(dev->outqueue.next);
1433 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1434
1435 f->state = F_UNUSED;
1436 memcpy(b, &f->buf, sizeof(*b));
1437
1438 if (f->vma_use_count)
1439 b->flags |= V4L2_BUF_FLAG_MAPPED;
1440
1441 return 0;
1442 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001443 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001444 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1445 em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001446 }
1447 return 0;
1448}
1449
1450/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001451 * em28xx_v4l2_ioctl()
1452 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001453 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001454static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001455 unsigned int cmd, unsigned long arg)
1456{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001457 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001458 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001459
Ingo Molnar3593cab2006-02-07 06:49:14 -02001460 if (mutex_lock_interruptible(&dev->fileop_lock))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001461 return -ERESTARTSYS;
1462
1463 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001464 em28xx_errdev("v4l2 ioctl: device not present\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001465 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001466 return -ENODEV;
1467 }
1468
1469 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001470 em28xx_errdev
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001471 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001472 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001473 return -EIO;
1474 }
1475
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001476 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001477
Ingo Molnar3593cab2006-02-07 06:49:14 -02001478 mutex_unlock(&dev->fileop_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001479
1480 return ret;
1481}
1482
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001483static struct file_operations em28xx_v4l_fops = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001484 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001485 .open = em28xx_v4l2_open,
1486 .release = em28xx_v4l2_close,
1487 .ioctl = em28xx_v4l2_ioctl,
1488 .read = em28xx_v4l2_read,
1489 .poll = em28xx_v4l2_poll,
1490 .mmap = em28xx_v4l2_mmap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001491 .llseek = no_llseek,
Mauro Carvalho Chehab17cbe2e2006-01-09 15:24:58 -02001492 .compat_ioctl = v4l_compat_ioctl32,
1493
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001494};
1495
1496/******************************** usb interface *****************************************/
1497
1498/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001499 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001500 * allocates and inits the device structs, registers i2c bus and v4l device
1501 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001502static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001503 int minor, int model)
1504{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001505 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001506 int retval = -ENOMEM;
1507 int errCode, i;
1508 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001509
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001510 dev->udev = udev;
1511 dev->model = model;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001512 mutex_init(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001513 init_waitqueue_head(&dev->open);
1514
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001515 dev->em28xx_write_regs = em28xx_write_regs;
1516 dev->em28xx_read_reg = em28xx_read_reg;
1517 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1518 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1519 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1520 dev->is_em2800 = em28xx_boards[model].is_em2800;
1521 dev->has_tuner = em28xx_boards[model].has_tuner;
1522 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1523 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1524 dev->decoder = em28xx_boards[model].decoder;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001525
1526 if (tuner >= 0)
1527 dev->tuner_type = tuner;
1528 else
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001529 dev->tuner_type = em28xx_boards[model].tuner_type;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001530
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001531 dev->video_inputs = em28xx_boards[model].vchannels;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001532
1533 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001534 if (em28xx_boards[model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001535 break;
1536 if (i == TVNORMS)
1537 i = 0;
1538
1539 dev->tvnorm = &tvnorms[i]; /* set default norm */
1540
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001541 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001542
1543 maxw = norm_maxw(dev);
1544 maxh = norm_maxh(dev);
1545
1546 /* set default image size */
1547 dev->width = maxw;
1548 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001549 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001550 dev->field_size = dev->width * dev->height;
1551 dev->frame_size =
1552 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1553 dev->bytesperline = dev->width * 2;
1554 dev->hscale = 0;
1555 dev->vscale = 0;
1556 dev->ctl_input = 2;
1557
1558 /* setup video picture settings for saa7113h */
1559 memset(&dev->vpic, 0, sizeof(dev->vpic));
1560 dev->vpic.colour = 128 << 8;
1561 dev->vpic.hue = 128 << 8;
1562 dev->vpic.brightness = 128 << 8;
1563 dev->vpic.contrast = 192 << 8;
1564 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1565 dev->vpic.depth = 16;
1566 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1567
Markus Rechbergera94e95b2006-01-23 17:11:10 -02001568 em28xx_pre_card_setup(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001569#ifdef CONFIG_MODULES
1570 /* request some modules */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001571 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -03001572 request_module("saa7115");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001573 if (dev->decoder == EM28XX_TVP5150)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001574 request_module("tvp5150");
1575 if (dev->has_tuner)
1576 request_module("tuner");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001577#endif
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001578 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001579 if (errCode) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001580 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001581 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001582 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001583 return -ENOMEM;
1584 }
1585
Ingo Molnar3593cab2006-02-07 06:49:14 -02001586 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001587 /* register i2c bus */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001588 em28xx_i2c_register(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001589
1590 /* Do board specific init and eeprom reading */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001591 em28xx_card_setup(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001592
1593 /* configure the device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001594 em28xx_config_i2c(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001595
Ingo Molnar3593cab2006-02-07 06:49:14 -02001596 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001597
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001598 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599
1600#ifdef CONFIG_MODULES
1601 if (dev->has_msp34xx)
1602 request_module("msp3400");
1603#endif
1604 /* allocate and fill v4l2 device struct */
1605 dev->vdev = video_device_alloc();
1606 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001607 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001608 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001609 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610 return -ENOMEM;
1611 }
1612
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001613 dev->vbi_dev = video_device_alloc();
1614 if (NULL == dev->vbi_dev) {
1615 em28xx_errdev("cannot allocate video_device.\n");
1616 kfree(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001617 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001618 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001619 return -ENOMEM;
1620 }
1621
1622 /* Fills VBI device info */
1623 dev->vbi_dev->type = VFL_TYPE_VBI;
1624 dev->vbi_dev->hardware = 0;
1625 dev->vbi_dev->fops = &em28xx_v4l_fops;
1626 dev->vbi_dev->minor = -1;
1627 dev->vbi_dev->dev = &dev->udev->dev;
1628 dev->vbi_dev->release = video_device_release;
1629 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1630 "em28xx",dev->devno,"vbi");
1631
1632 /* Fills CAPTURE device info */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633 dev->vdev->type = VID_TYPE_CAPTURE;
1634 if (dev->has_tuner)
1635 dev->vdev->type |= VID_TYPE_TUNER;
1636 dev->vdev->hardware = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001637 dev->vdev->fops = &em28xx_v4l_fops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001638 dev->vdev->minor = -1;
1639 dev->vdev->dev = &dev->udev->dev;
1640 dev->vdev->release = video_device_release;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001641 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1642 "em28xx",dev->devno,"video");
1643
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001644 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
1646 /* register v4l2 device */
Ingo Molnar3593cab2006-02-07 06:49:14 -02001647 mutex_lock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001648 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1649 video_nr[dev->devno]))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001650 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001651 retval);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001652 mutex_unlock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001653 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001654 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001655 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001656 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001657 return -ENODEV;
1658 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001659
1660 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1661 vbi_nr[dev->devno]) < 0) {
1662 printk("unable to register vbi device\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001663 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001664 list_del(&dev->devlist);
1665 video_device_release(dev->vbi_dev);
1666 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001667 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001668 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001669 return -ENODEV;
1670 } else {
1671 printk("registered VBI\n");
1672 }
1673
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674 if (dev->has_msp34xx) {
1675 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677 udelay(2500);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001678 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001679 udelay(2500);
1680
1681 }
1682 video_mux(dev, 0);
1683
Ingo Molnar3593cab2006-02-07 06:49:14 -02001684 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001685
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001686 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1687 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1688 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001689
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690 return 0;
1691}
1692
1693/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001694 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695 * checks for supported devices
1696 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001697static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001698 const struct usb_device_id *id)
1699{
1700 const struct usb_endpoint_descriptor *endpoint;
1701 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001702 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001703 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001705 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706
1707 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001708 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1709
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001710 /* Check to see next free device and mark as used */
1711 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1712 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001713
1714 /* Don't register audio interfaces */
1715 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001716 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001717 udev->descriptor.idVendor,udev->descriptor.idProduct,
1718 ifnum,
1719 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001720
1721 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001722 return -ENODEV;
1723 }
1724
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001726 udev->descriptor.idVendor,udev->descriptor.idProduct,
1727 ifnum,
1728 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001729
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001730 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1731
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732 /* check if the the device has the iso in endpoint at the correct place */
1733 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1734 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001735 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001736 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737 return -ENODEV;
1738 }
1739 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001740 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001741 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742 return -ENODEV;
1743 }
1744
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001745 model=id->driver_info;
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001746
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03001747 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001748 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001749 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001750 return -ENOMEM;
1751 }
1752
1753 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001754 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001755 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001756 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001757 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001758 return -ENOMEM;
1759 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001760
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001761 snprintf(dev->name, 29, "em28xx #%d", nr);
1762 dev->devno=nr;
1763
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001764 /* compute alternate max packet sizes */
1765 uif = udev->actconfig->interface[0];
1766
1767 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001768 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001769// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1770 dev->alt_max_pkt_size = kmalloc(32*
1771 dev->num_alt,GFP_KERNEL);
1772 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001773 em28xx_errdev("out of memory!\n");
1774 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001775 return -ENOMEM;
1776 }
1777
1778 for (i = 0; i < dev->num_alt ; i++) {
1779 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1780 wMaxPacketSize);
1781 dev->alt_max_pkt_size[i] =
1782 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001783 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001784 dev->alt_max_pkt_size[i]);
1785 }
1786
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001787 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001788 model=card[nr];
1789
1790 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001791 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001792 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1793 "%s: workaround that. Redirect complaints to the vendor of\n"
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001794 "%s: the TV card. Generic type will be used."
1795 "%s: Best regards,\n"
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001796 "%s: -- tux\n",
1797 dev->name,dev->name,dev->name,dev->name,dev->name);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001798 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001799 dev->name);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001800 for (i = 0; i < em28xx_bcount; i++) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001801 em28xx_errdev(" card=%d -> %s\n", i,
1802 em28xx_boards[i].name);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001803 }
1804 }
1805
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806 /* allocate device struct */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001807 retval = em28xx_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001808 if (retval)
1809 return retval;
1810
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001811 em28xx_info("Found %s\n", em28xx_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001812
1813 /* save our data pointer in this interface device */
1814 usb_set_intfdata(interface, dev);
1815 return 0;
1816}
1817
1818/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001819 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001820 * called when the device gets diconencted
1821 * video device will be unregistered on v4l2_close in case it is still open
1822 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001823static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001824{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001825 struct em28xx *dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001826 usb_set_intfdata(interface, NULL);
1827
1828 if (!dev)
1829 return;
1830
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001831 down_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001832
Ingo Molnar3593cab2006-02-07 06:49:14 -02001833 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001834
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001835 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001836
1837 wake_up_interruptible_all(&dev->open);
1838
1839 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001840 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001841 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001842 "deallocation are deferred on close.\n",
1843 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1844
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001846 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847 dev->state |= DEV_DISCONNECTED;
1848 wake_up_interruptible(&dev->wait_frame);
1849 wake_up_interruptible(&dev->wait_stream);
1850 } else {
1851 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001852 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001853 }
1854
Ingo Molnar3593cab2006-02-07 06:49:14 -02001855 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001856
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001857 if (!dev->users) {
1858 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001859 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001860 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001861
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001862 up_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001863}
1864
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001865static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001866 .name = "em28xx",
1867 .probe = em28xx_usb_probe,
1868 .disconnect = em28xx_usb_disconnect,
1869 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001870};
1871
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001872static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001873{
1874 int result;
1875
1876 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001877 (EM28XX_VERSION_CODE >> 16) & 0xff,
1878 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001879#ifdef SNAPSHOT
1880 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1881 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1882#endif
1883
1884 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001885 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001887 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001888 " usb_register failed. Error number %d.\n", result);
1889
1890 return result;
1891}
1892
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001893static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894{
1895 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001896 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001897}
1898
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001899module_init(em28xx_module_init);
1900module_exit(em28xx_module_exit);