blob: f6acce820791fdf9c6d71637a6272771fd6d9a90 [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 Chehab4ac97912005-11-08 21:37:43 -08006 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080029#include <linux/i2c.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080030#include <linux/video_decoder.h>
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080031#include <linux/utsname.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080033#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080034#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080035
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080036#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
37 "Markus Rechberger <mrechberger@gmail.com>, " \
38 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
39 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080040
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080041#define DRIVER_NAME "em28xx"
42#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080043#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080045#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080046 if (video_debug) \
47 printk(KERN_INFO "%s %s :"fmt, \
48 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049
50MODULE_AUTHOR(DRIVER_AUTHOR);
51MODULE_DESCRIPTION(DRIVER_DESC);
52MODULE_LICENSE("GPL");
53
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080055
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080057
58module_param_array(card, int, NULL, 0444);
59MODULE_PARM_DESC(card,"card type");
60
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080061static int tuner = -1;
62module_param(tuner, int, 0444);
63MODULE_PARM_DESC(tuner, "tuner type");
64
65static unsigned int video_debug = 0;
66module_param(video_debug,int,0644);
67MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68
69/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080070static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071 {
72 .name = "PAL",
73 .id = V4L2_STD_PAL,
74 .mode = VIDEO_MODE_PAL,
75 }, {
76 .name = "NTSC",
77 .id = V4L2_STD_NTSC,
78 .mode = VIDEO_MODE_NTSC,
79 }, {
80 .name = "SECAM",
81 .id = V4L2_STD_SECAM,
82 .mode = VIDEO_MODE_SECAM,
83 }, {
84 .name = "PAL-M",
85 .id = V4L2_STD_PAL_M,
86 .mode = VIDEO_MODE_PAL,
87 }
88};
89
Markus Rechbergera38a7d42005-11-08 21:38:06 -080090static const unsigned char saa7114_i2c_init[] = {
91 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
92 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
93 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
94 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
95 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
96 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
97 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
98 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
99 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
100 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
101 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
102 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
103 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
104 0xbe,0x00,0xbf,0x00
105};
106
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800107#define TVNORMS ARRAY_SIZE(tvnorms)
108
109/* supported controls */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800110static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800111 {
112 .id = V4L2_CID_BRIGHTNESS,
113 .type = V4L2_CTRL_TYPE_INTEGER,
114 .name = "Brightness",
115 .minimum = -128,
116 .maximum = 127,
117 .step = 1,
118 .default_value = 0,
119 .flags = 0,
120 },{
121 .id = V4L2_CID_CONTRAST,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 .name = "Contrast",
124 .minimum = 0x0,
125 .maximum = 0x1f,
126 .step = 0x1,
127 .default_value = 0x10,
128 .flags = 0,
129 },{
130 .id = V4L2_CID_SATURATION,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Saturation",
133 .minimum = 0x0,
134 .maximum = 0x1f,
135 .step = 0x1,
136 .default_value = 0x10,
137 .flags = 0,
138 },{
139 .id = V4L2_CID_AUDIO_VOLUME,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "Volume",
142 .minimum = 0x0,
143 .maximum = 0x1f,
144 .step = 0x1,
145 .default_value = 0x1f,
146 .flags = 0,
147 },{
148 .id = V4L2_CID_AUDIO_MUTE,
149 .type = V4L2_CTRL_TYPE_BOOLEAN,
150 .name = "Mute",
151 .minimum = 0,
152 .maximum = 1,
153 .step = 1,
154 .default_value = 1,
155 .flags = 0,
156 },{
157 .id = V4L2_CID_RED_BALANCE,
158 .type = V4L2_CTRL_TYPE_INTEGER,
159 .name = "Red chroma balance",
160 .minimum = -128,
161 .maximum = 127,
162 .step = 1,
163 .default_value = 0,
164 .flags = 0,
165 },{
166 .id = V4L2_CID_BLUE_BALANCE,
167 .type = V4L2_CTRL_TYPE_INTEGER,
168 .name = "Blue chroma balance",
169 .minimum = -128,
170 .maximum = 127,
171 .step = 1,
172 .default_value = 0,
173 .flags = 0,
174 },{
175 .id = V4L2_CID_GAMMA,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Gamma",
178 .minimum = 0x0,
179 .maximum = 0x3f,
180 .step = 0x1,
181 .default_value = 0x20,
182 .flags = 0,
183 }
184};
185
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800186static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800187
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800188static DECLARE_MUTEX(em28xx_sysfs_lock);
189static DECLARE_RWSEM(em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800190
191/********************* v4l2 interface ******************************************/
192
193static inline unsigned long kvirt_to_pa(unsigned long adr)
194{
195 unsigned long kva, ret;
196
197 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
198 kva |= adr & (PAGE_SIZE - 1);
199 ret = __pa(kva);
200 return ret;
201}
202
203/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800204 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800205 * inits registers with sane defaults
206 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800207static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800208{
209
210 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800211 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800212
213 /* enable vbi capturing */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800214 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800215 dev->mute = 1; /* maybe not the right place... */
216 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800217 em28xx_audio_analog_set(dev);
218 em28xx_audio_analog_setup(dev);
219 em28xx_outfmt_set_yuv422(dev);
220 em28xx_colorlevels_set_default(dev);
221 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800222
223 return 0;
224}
225
226/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800227 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800228 * configure i2c attached devices
229 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800230void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800231{
232 struct v4l2_frequency f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800233 struct video_decoder_init em28xx_vdi = {.data = NULL };
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800234
235
236 /* configure decoder */
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800237 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800238 em28xx_vdi.data=saa7114_i2c_init;
239 em28xx_vdi.len=sizeof(saa7114_i2c_init);
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800240 }
241
242
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800243 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
244 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
245/* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
246/* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
247/* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
248/* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800249
250 /* configure tuner */
251 f.tuner = 0;
252 f.type = V4L2_TUNER_ANALOG_TV;
253 f.frequency = 9076; /* FIXME:remove magic number */
254 dev->ctl_freq = f.frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800255 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800256
257 /* configure tda9887 */
258
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800259
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800260/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800261}
262
263/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800264 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800265 * prepare queues for incoming and outgoing frames
266 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800267static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800268{
269 u32 i;
270
271 INIT_LIST_HEAD(&dev->inqueue);
272 INIT_LIST_HEAD(&dev->outqueue);
273
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800274 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800275 dev->frame[i].state = F_UNUSED;
276 dev->frame[i].buf.bytesused = 0;
277 }
278}
279
280/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800281 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800282 * inits the device and starts isoc transfer
283 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800284static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800285{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800286 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800287 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800288 struct em28xx *h,*dev = NULL;
Markus Rechberger9c755412005-11-08 21:37:52 -0800289 struct list_head *list;
290
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800291 list_for_each(list,&em28xx_devlist) {
292 h = list_entry(list, struct em28xx, devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -0800293 if (h->vdev->minor == minor) {
294 dev = h;
295 }
296 }
297
298 filp->private_data=dev;
299
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800300
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800301 em28xx_videodbg("users=%d", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800302
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800303 if (!down_read_trylock(&em28xx_disconnect))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800304 return -ERESTARTSYS;
305
306 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800307 em28xx_warn("this driver can be opened only once\n");
308 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800309 return -EBUSY;
310 }
311
312/* if(dev->vbi_dev->minor == minor){
313 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
314 }*/
315 if (dev->vdev->minor == minor) {
316 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
317 }
318
319 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
320 spin_lock_init(&dev->queue_lock);
321 init_waitqueue_head(&dev->wait_frame);
322 init_waitqueue_head(&dev->wait_stream);
323
324 down(&dev->lock);
325
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800326 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800327
328 dev->width = norm_maxw(dev);
329 dev->height = norm_maxh(dev);
330 dev->frame_size = dev->width * dev->height * 2;
331 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
332 dev->bytesperline = dev->width * 2;
333 dev->hscale = 0;
334 dev->vscale = 0;
335
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800336 em28xx_capture_start(dev, 1);
337 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800338
339 /* start the transfer */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800340 errCode = em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800341 if (errCode)
342 goto err;
343
344 dev->users++;
345 filp->private_data = dev;
346 dev->io = IO_NONE;
347 dev->stream = STREAM_OFF;
348 dev->num_frames = 0;
349
350 /* prepare queues */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800351 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800352
353 dev->state |= DEV_INITIALIZED;
354
355 err:
356 up(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800357 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800358 return errCode;
359}
360
361/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800362 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800363 * unregisters the v4l2,i2c and usb devices
364 * called when the device gets disconected or at module unload
365*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800366static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800367{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800368 down(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800369
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800370 em28xx_info("V4L2 device /dev/video%d deregistered\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800371 dev->vdev->minor);
Markus Rechberger9c755412005-11-08 21:37:52 -0800372 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800373 video_unregister_device(dev->vdev);
374/* video_unregister_device(dev->vbi_dev); */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800375 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800376 usb_put_dev(dev->udev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800377 up(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800378}
379
380/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800381 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800382 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
383 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800384static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800385{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800386 int errCode;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800387 struct em28xx *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800388
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800389 em28xx_videodbg("users=%d", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800390
391 down(&dev->lock);
392
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800393 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800394
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800395 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800396
397 /* the device is already disconnect, free the remaining resources */
398 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800399 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800400 up(&dev->lock);
401 kfree(dev);
402 return 0;
403 }
404
405 /* set alternate 0 */
406 dev->alt = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800407 em28xx_videodbg("setting alternate 0");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800408 errCode = usb_set_interface(dev->udev, 0, 0);
409 if (errCode < 0) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800410 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800411 errCode);
412 }
413
414 dev->users--;
415 wake_up_interruptible_nr(&dev->open, 1);
416 up(&dev->lock);
417 return 0;
418}
419
420/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800421 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800422 * will allocate buffers when called for the first time
423 */
424static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800425em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800426 loff_t * f_pos)
427{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800428 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800429 unsigned long lock_flags;
430 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800431 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800432
433 if (down_interruptible(&dev->fileop_lock))
434 return -ERESTARTSYS;
435
436 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800437 em28xx_videodbg("device not present");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800438 up(&dev->fileop_lock);
439 return -ENODEV;
440 }
441
442 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800443 em28xx_videodbg("device misconfigured; close and open it again");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800444 up(&dev->fileop_lock);
445 return -EIO;
446 }
447
448 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800449 em28xx_videodbg ("IO method is set to mmap; close and open"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800450 " the device again to choose the read method");
451 up(&dev->fileop_lock);
452 return -EINVAL;
453 }
454
455 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800456 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
457 em28xx_errdev("read failed, not enough memory\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800458 up(&dev->fileop_lock);
459 return -ENOMEM;
460 }
461 dev->io = IO_READ;
462 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800463 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800464 }
465
466 if (!count) {
467 up(&dev->fileop_lock);
468 return 0;
469 }
470
471 if (list_empty(&dev->outqueue)) {
472 if (filp->f_flags & O_NONBLOCK) {
473 up(&dev->fileop_lock);
474 return -EAGAIN;
475 }
476 ret = wait_event_interruptible
477 (dev->wait_frame,
478 (!list_empty(&dev->outqueue)) ||
479 (dev->state & DEV_DISCONNECTED));
480 if (ret) {
481 up(&dev->fileop_lock);
482 return ret;
483 }
484 if (dev->state & DEV_DISCONNECTED) {
485 up(&dev->fileop_lock);
486 return -ENODEV;
487 }
488 }
489
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800490 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800491
492 spin_lock_irqsave(&dev->queue_lock, lock_flags);
493 list_for_each_entry(i, &dev->outqueue, frame)
494 i->state = F_UNUSED;
495 INIT_LIST_HEAD(&dev->outqueue);
496 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
497
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800498 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800499
500 if (count > f->buf.length)
501 count = f->buf.length;
502
503 if (copy_to_user(buf, f->bufmem, count)) {
504 up(&dev->fileop_lock);
505 return -EFAULT;
506 }
507 *f_pos += count;
508
509 up(&dev->fileop_lock);
510
511 return count;
512}
513
514/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800515 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516 * will allocate buffers when called for the first time
517 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800518static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800519{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520 unsigned int mask = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800521 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800522
523 if (down_interruptible(&dev->fileop_lock))
524 return POLLERR;
525
526 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800527 em28xx_videodbg("device not present");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800529 em28xx_videodbg("device is misconfigured; close and open it again");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800530 } else {
531 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800532 if (!em28xx_request_buffers
533 (dev, EM28XX_NUM_READ_FRAMES)) {
534 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800535 ("poll() failed, not enough memory\n");
536 } else {
537 dev->io = IO_READ;
538 dev->stream = STREAM_ON;
539 }
540 }
541
542 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800543 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800544 poll_wait(filp, &dev->wait_frame, wait);
545
546 if (!list_empty(&dev->outqueue))
547 mask |= POLLIN | POLLRDNORM;
548
549 up(&dev->fileop_lock);
550
551 return mask;
552 }
553 }
554
555 up(&dev->fileop_lock);
556 return POLLERR;
557}
558
559/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800560 * em28xx_vm_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800561 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800562static void em28xx_vm_open(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800563{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800564 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800565 f->vma_use_count++;
566}
567
568/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800569 * em28xx_vm_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800570 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800571static void em28xx_vm_close(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800572{
573 /* NOTE: buffers are not freed here */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800574 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800575 f->vma_use_count--;
576}
577
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800578static struct vm_operations_struct em28xx_vm_ops = {
579 .open = em28xx_vm_open,
580 .close = em28xx_vm_close,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800581};
582
583/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800584 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800585 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800586static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800587{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800588 unsigned long size = vma->vm_end - vma->vm_start,
589 start = vma->vm_start, pos, page;
590 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800591
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800592 struct em28xx *dev = filp->private_data;
Markus Rechberger9c755412005-11-08 21:37:52 -0800593
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800594 if (down_interruptible(&dev->fileop_lock))
595 return -ERESTARTSYS;
596
597 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800598 em28xx_videodbg("mmap: device not present");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800599 up(&dev->fileop_lock);
600 return -ENODEV;
601 }
602
603 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800604 em28xx_videodbg ("mmap: Device is misconfigured; close and "
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800605 "open it again");
606 up(&dev->fileop_lock);
607 return -EIO;
608 }
609
610 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
611 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
612 up(&dev->fileop_lock);
613 return -EINVAL;
614 }
615
616 for (i = 0; i < dev->num_frames; i++) {
617 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
618 break;
619 }
620 if (i == dev->num_frames) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800621 em28xx_videodbg("mmap: user supplied mapping address is out of range");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800622 up(&dev->fileop_lock);
623 return -EINVAL;
624 }
625
626 /* VM_IO is eventually going to replace PageReserved altogether */
627 vma->vm_flags |= VM_IO;
628 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
629
630 pos = (unsigned long)dev->frame[i].bufmem;
631 while (size > 0) { /* size is page-aligned */
632 page = vmalloc_to_pfn((void *)pos);
633 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
634 vma->vm_page_prot)) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800635 em28xx_videodbg("mmap: rename page map failed");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800636 up(&dev->fileop_lock);
637 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);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800648 up(&dev->fileop_lock);
649 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{
658 s32 tmp;
659 switch (ctrl->id) {
660 case V4L2_CID_AUDIO_MUTE:
661 ctrl->value = dev->mute;
662 return 0;
663 case V4L2_CID_AUDIO_VOLUME:
664 ctrl->value = dev->volume;
665 return 0;
666 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800667 if ((tmp = em28xx_brightness_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800668 return -EIO;
669 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
670 return 0;
671 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800672 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800673 return -EIO;
674 return 0;
675 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800676 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800677 return -EIO;
678 return 0;
679 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800680 if ((tmp = em28xx_v_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800681 return -EIO;
682 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
683 return 0;
684 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800685 if ((tmp = em28xx_u_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800686 return -EIO;
687 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
688 return 0;
689 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800690 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800691 return -EIO;
692 return 0;
693 default:
694 return -EINVAL;
695 }
696}
697
698/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800699 * em28xx_set_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800700 * mute or set new saturation, brightness or contrast
701 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800702static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800703{
704 switch (ctrl->id) {
705 case V4L2_CID_AUDIO_MUTE:
706 if (ctrl->value != dev->mute) {
707 dev->mute = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800708 em28xx_audio_usb_mute(dev, ctrl->value);
709 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800710 }
711 return 0;
712 case V4L2_CID_AUDIO_VOLUME:
713 dev->volume = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800714 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800715 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800716 return em28xx_brightness_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800717 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800718 return em28xx_contrast_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800719 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800720 return em28xx_saturation_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800721 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800722 return em28xx_v_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800723 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800724 return em28xx_u_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800725 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800726 return em28xx_gamma_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800727 default:
728 return -EINVAL;
729 }
730}
731
732/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800733 * em28xx_stream_interrupt()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800734 * stops streaming
735 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800736static int em28xx_stream_interrupt(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800737{
738 int ret = 0;
739
740 /* stop reading from the device */
741
742 dev->stream = STREAM_INTERRUPT;
743 ret = wait_event_timeout(dev->wait_stream,
744 (dev->stream == STREAM_OFF) ||
745 (dev->state & DEV_DISCONNECTED),
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800746 EM28XX_URB_TIMEOUT);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800747 if (dev->state & DEV_DISCONNECTED)
748 return -ENODEV;
749 else if (ret) {
750 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800751 em28xx_videodbg("device is misconfigured; close and "
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800752 "open /dev/video%d again", dev->vdev->minor);
753 return ret;
754 }
755
756 return 0;
757}
758
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800759static int em28xx_set_norm(struct em28xx *dev, int width, int height)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800760{
761 unsigned int hscale, vscale;
762 unsigned int maxh, maxw;
763
764 maxw = norm_maxw(dev);
765 maxh = norm_maxh(dev);
766
767 /* width must even because of the YUYV format */
768 /* height must be even because of interlacing */
769 height &= 0xfffe;
770 width &= 0xfffe;
771
772 if (height < 32)
773 height = 32;
774 if (height > maxh)
775 height = maxh;
776 if (width < 48)
777 width = 48;
778 if (width > maxw)
779 width = maxw;
780
781 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
782 hscale = 0x3fff;
783 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
784
785 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
786 vscale = 0x3fff;
787 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
788
789 /* set new image size */
790 dev->width = width;
791 dev->height = height;
792 dev->frame_size = dev->width * dev->height * 2;
793 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
794 dev->bytesperline = dev->width * 2;
795 dev->hscale = hscale;
796 dev->vscale = vscale;
797
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800798 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800799
800 return 0;
801}
802
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800803static void video_mux(struct em28xx *dev, int index)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800804{
805 int input, ainput;
806
807 input = INPUT(index)->vmux;
808 dev->ctl_input = index;
809
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800810 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800811
812 dev->ctl_ainput = INPUT(index)->amux;
813
814 switch (dev->ctl_ainput) {
815 case 0:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800816 ainput = EM28XX_AUDIO_SRC_TUNER;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800817 break;
818 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800819 ainput = EM28XX_AUDIO_SRC_LINE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800820 }
821
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800822 em28xx_audio_source(dev, ainput);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800823}
824
825/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800826 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800827 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800828 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800829 * copying is done already, arg is a kernel pointer.
830 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800831static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
832 struct em28xx *dev, unsigned int cmd, void *arg,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800833 v4l2_kioctl driver_ioctl)
834{
835 int ret;
836
837 switch (cmd) {
838 /* ---------- tv norms ---------- */
839 case VIDIOC_ENUMSTD:
840 {
841 struct v4l2_standard *e = arg;
842 unsigned int i;
843
844 i = e->index;
845 if (i >= TVNORMS)
846 return -EINVAL;
847 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
848 tvnorms[e->index].name);
849 e->index = i;
850 if (ret < 0)
851 return ret;
852 return 0;
853 }
854 case VIDIOC_G_STD:
855 {
856 v4l2_std_id *id = arg;
857
858 *id = dev->tvnorm->id;
859 return 0;
860 }
861 case VIDIOC_S_STD:
862 {
863 v4l2_std_id *id = arg;
864 unsigned int i;
865
866 for (i = 0; i < TVNORMS; i++)
867 if (*id == tvnorms[i].id)
868 break;
869 if (i == TVNORMS)
870 for (i = 0; i < TVNORMS; i++)
871 if (*id & tvnorms[i].id)
872 break;
873 if (i == TVNORMS)
874 return -EINVAL;
875
876 down(&dev->lock);
877 dev->tvnorm = &tvnorms[i];
878
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800879 em28xx_set_norm(dev, dev->width, dev->height);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800880
881/*
882 dev->width=norm_maxw(dev);
883 dev->height=norm_maxh(dev);
884 dev->frame_size=dev->width*dev->height*2;
885 dev->field_size=dev->frame_size>>1;
886 dev->bytesperline=dev->width*2;
887 dev->hscale=0;
888 dev->vscale=0;
889
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800890 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800891*/
892/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800893 em28xx_uninit_isoc(dev);
894 em28xx_set_alternate(dev);
895 em28xx_capture_start(dev, 1);
896 em28xx_resolution_set(dev);
897 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800898*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800899 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800900 &tvnorms[i].mode);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800901 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800902 &dev->tvnorm->id);
903
904 up(&dev->lock);
905
906 return 0;
907 }
908
909 /* ------ input switching ---------- */
910 case VIDIOC_ENUMINPUT:
911 {
912 struct v4l2_input *i = arg;
913 unsigned int n;
914 static const char *iname[] = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800915 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
916 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
917 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
918 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
919 [EM28XX_VMUX_SVIDEO] = "S-Video",
920 [EM28XX_VMUX_TELEVISION] = "Television",
921 [EM28XX_VMUX_CABLE] = "Cable TV",
922 [EM28XX_VMUX_DVB] = "DVB",
923 [EM28XX_VMUX_DEBUG] = "for debug only",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800924 };
925
926 n = i->index;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800927 if (n >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800928 return -EINVAL;
929 if (0 == INPUT(n)->type)
930 return -EINVAL;
931 memset(i, 0, sizeof(*i));
932 i->index = n;
933 i->type = V4L2_INPUT_TYPE_CAMERA;
934 strcpy(i->name, iname[INPUT(n)->type]);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800935 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
936 (EM28XX_VMUX_CABLE == INPUT(n)->type))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800937 i->type = V4L2_INPUT_TYPE_TUNER;
938 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
939 i->std |= tvnorms[n].id;
940 return 0;
941 }
942
943 case VIDIOC_G_INPUT:
944 {
945 int *i = arg;
946 *i = dev->ctl_input;
947
948 return 0;
949 }
950
951 case VIDIOC_S_INPUT:
952 {
953 int *index = arg;
954
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800955 if (*index >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800956 return -EINVAL;
957 if (0 == INPUT(*index)->type)
958 return -EINVAL;
959
960 down(&dev->lock);
961 video_mux(dev, *index);
962 up(&dev->lock);
963
964 return 0;
965 }
966
967 case VIDIOC_G_AUDIO:
968 {
969 struct v4l2_audio *a = arg;
970 unsigned int index = a->index;
971
972 if (a->index > 1)
973 return -EINVAL;
974 memset(a, 0, sizeof(*a));
975 index = dev->ctl_ainput;
976
977 if (index == 0) {
978 strcpy(a->name, "Television");
979 } else {
980 strcpy(a->name, "Line In");
981 }
982 a->capability = V4L2_AUDCAP_STEREO;
983 a->index = index;
984 return 0;
985 }
986
987 case VIDIOC_S_AUDIO:
988 {
989 struct v4l2_audio *a = arg;
990 if (a->index != dev->ctl_ainput)
991 return -EINVAL;
992
993 return 0;
994 }
995
996 /* --- controls ---------------------------------------------- */
997 case VIDIOC_QUERYCTRL:
998 {
999 struct v4l2_queryctrl *qc = arg;
1000 u8 i, n;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001001 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001002 for (i = 0; i < n; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001003 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1004 memcpy(qc, &(em28xx_qctrl[i]),
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001005 sizeof(*qc));
1006 return 0;
1007 }
1008
1009 return -EINVAL;
1010 }
1011
1012 case VIDIOC_G_CTRL:
1013 {
1014 struct v4l2_control *ctrl = arg;
1015
1016
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001017 return em28xx_get_ctrl(dev, ctrl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001018 }
1019
1020 case VIDIOC_S_CTRL_OLD: /* ??? */
1021 case VIDIOC_S_CTRL:
1022 {
1023 struct v4l2_control *ctrl = arg;
1024 u8 i, n;
1025
1026
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001027 n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001028 for (i = 0; i < n; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001029 if (ctrl->id == em28xx_qctrl[i].id) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001030 if (ctrl->value <
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001031 em28xx_qctrl[i].minimum
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001032 || ctrl->value >
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001033 em28xx_qctrl[i].maximum)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001034 return -ERANGE;
1035
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001036 return em28xx_set_ctrl(dev, ctrl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001037 }
1038 return -EINVAL;
1039 }
1040
1041 /* --- tuner ioctls ------------------------------------------ */
1042 case VIDIOC_G_TUNER:
1043 {
1044 struct v4l2_tuner *t = arg;
1045 int status = 0;
1046
1047 if (0 != t->index)
1048 return -EINVAL;
1049
1050 memset(t, 0, sizeof(*t));
1051 strcpy(t->name, "Tuner");
1052 t->type = V4L2_TUNER_ANALOG_TV;
1053 t->capability = V4L2_TUNER_CAP_NORM;
1054 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1055/* t->signal = 0xffff;*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001056/* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001057 /* No way to get signal strength? */
1058 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001059 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001060 &status);
1061 up(&dev->lock);
1062 t->signal =
1063 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1064
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001065 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001066 t->afc);
1067 return 0;
1068 }
1069 case VIDIOC_S_TUNER:
1070 {
1071 struct v4l2_tuner *t = arg;
1072 int status = 0;
1073
1074 if (0 != t->index)
1075 return -EINVAL;
1076 memset(t, 0, sizeof(*t));
1077 strcpy(t->name, "Tuner");
1078 t->type = V4L2_TUNER_ANALOG_TV;
1079 t->capability = V4L2_TUNER_CAP_NORM;
1080 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1081/* t->signal = 0xffff; */
1082 /* No way to get signal strength? */
1083 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001084 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001085 &status);
1086 up(&dev->lock);
1087 t->signal =
1088 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1089
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001090 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001091 t->signal, t->afc);
1092 return 0;
1093 }
1094 case VIDIOC_G_FREQUENCY:
1095 {
1096 struct v4l2_frequency *f = arg;
1097
1098 memset(f, 0, sizeof(*f));
1099 f->type = V4L2_TUNER_ANALOG_TV;
1100 f->frequency = dev->ctl_freq;
1101
1102 return 0;
1103 }
1104 case VIDIOC_S_FREQUENCY:
1105 {
1106 struct v4l2_frequency *f = arg;
1107
1108 if (0 != f->tuner)
1109 return -EINVAL;
1110
1111 if (V4L2_TUNER_ANALOG_TV != f->type)
1112 return -EINVAL;
1113
1114 down(&dev->lock);
1115 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001116 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001117 up(&dev->lock);
1118 return 0;
1119 }
1120
1121 case VIDIOC_CROPCAP:
1122 {
1123 struct v4l2_cropcap *cc = arg;
1124
1125 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001126 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001127 cc->bounds.left = 0;
1128 cc->bounds.top = 0;
1129 cc->bounds.width = dev->width;
1130 cc->bounds.height = dev->height;
1131 cc->defrect = cc->bounds;
1132 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1133 cc->pixelaspect.denominator = 59;
1134 return 0;
1135 }
1136 case VIDIOC_STREAMON:
1137 {
1138 int *type = arg;
1139
1140 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1141 || dev->io != IO_MMAP)
1142 return -EINVAL;
1143
1144 if (list_empty(&dev->inqueue))
1145 return -EINVAL;
1146
1147 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1148
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001149 em28xx_videodbg("VIDIOC_STREAMON: starting stream");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001150
1151 return 0;
1152 }
1153 case VIDIOC_STREAMOFF:
1154 {
1155 int *type = arg;
1156 int ret;
1157
1158 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1159 || dev->io != IO_MMAP)
1160 return -EINVAL;
1161
1162 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001163 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
1164 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001165 return ret;
1166 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001167 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001168
1169 return 0;
1170 }
1171 default:
1172 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1173 driver_ioctl);
1174 }
1175 return 0;
1176}
1177
1178/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001179 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001180 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001181 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001182 * copying is done already, arg is a kernel pointer.
1183 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001184static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001185 unsigned int cmd, void *arg)
1186{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001187 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001188
1189 if (!dev)
1190 return -ENODEV;
1191
1192 if (video_debug > 1)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001193 em28xx_print_ioctl(dev->name,cmd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001194
1195 switch (cmd) {
1196
1197 /* --- capabilities ------------------------------------------ */
1198 case VIDIOC_QUERYCAP:
1199 {
1200 struct v4l2_capability *cap = arg;
1201
1202 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001203 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1204 strlcpy(cap->card, em28xx_boards[dev->model].name,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001205 sizeof(cap->card));
1206 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1207 sizeof(cap->bus_info));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001208 cap->version = EM28XX_VERSION_CODE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001209 cap->capabilities =
1210 V4L2_CAP_VIDEO_CAPTURE |
1211 V4L2_CAP_AUDIO |
1212 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1213 if (dev->has_tuner)
1214 cap->capabilities |= V4L2_CAP_TUNER;
1215 return 0;
1216 }
1217
1218 /* --- capture ioctls ---------------------------------------- */
1219 case VIDIOC_ENUM_FMT:
1220 {
1221 struct v4l2_fmtdesc *fmtd = arg;
1222
1223 if (fmtd->index != 0)
1224 return -EINVAL;
1225 memset(fmtd, 0, sizeof(*fmtd));
1226 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1227 strcpy(fmtd->description, "Packed YUY2");
1228 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1229 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1230 return 0;
1231 }
1232
1233 case VIDIOC_G_FMT:
1234 {
1235 struct v4l2_format *format = arg;
1236
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001237 em28xx_videodbg("VIDIOC_G_FMT: type=%s",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001238 format->type ==
1239 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1240 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1241 V4L2_BUF_TYPE_VBI_CAPTURE ?
1242 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1243 "not supported");
1244
1245 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1246 return -EINVAL;
1247
1248 format->fmt.pix.width = dev->width;
1249 format->fmt.pix.height = dev->height;
1250 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1251 format->fmt.pix.bytesperline = dev->bytesperline;
1252 format->fmt.pix.sizeimage = dev->frame_size;
1253 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1254 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1255
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001256 em28xx_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001257 dev->height);
1258 return 0;
1259 }
1260
1261 case VIDIOC_TRY_FMT:
1262 case VIDIOC_S_FMT:
1263 {
1264 struct v4l2_format *format = arg;
1265 u32 i;
1266 int ret = 0;
1267 int width = format->fmt.pix.width;
1268 int height = format->fmt.pix.height;
1269 unsigned int hscale, vscale;
1270 unsigned int maxh, maxw;
1271
1272 maxw = norm_maxw(dev);
1273 maxh = norm_maxh(dev);
1274
1275/* int both_fields; */
1276
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001277 em28xx_videodbg("%s: type=%s",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001278 cmd ==
1279 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1280 "VIDIOC_S_FMT",
1281 format->type ==
1282 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1283 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1284 V4L2_BUF_TYPE_VBI_CAPTURE ?
1285 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1286 "not supported");
1287
1288 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1289 return -EINVAL;
1290
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001291 em28xx_videodbg("%s: requested %dx%d",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001292 cmd ==
1293 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1294 "VIDIOC_S_FMT", format->fmt.pix.width,
1295 format->fmt.pix.height);
1296
1297 /* FIXME: Move some code away from here */
1298 /* width must even because of the YUYV format */
1299 /* height must be even because of interlacing */
1300 height &= 0xfffe;
1301 width &= 0xfffe;
1302
1303 if (height < 32)
1304 height = 32;
1305 if (height > maxh)
1306 height = maxh;
1307 if (width < 48)
1308 width = 48;
1309 if (width > maxw)
1310 width = maxw;
1311
Sascha Sommer74458e62005-11-08 21:37:33 -08001312 if(dev->is_em2800){
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001313 /* the em2800 can only scale down to 50% */
Sascha Sommer74458e62005-11-08 21:37:33 -08001314 if(height % (maxh / 2))
1315 height=maxh;
1316 if(width % (maxw / 2))
1317 width=maxw;
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001318 /* according to empiatech support */
1319 /* the MaxPacketSize is to small to support */
1320 /* framesizes larger than 640x480 @ 30 fps */
1321 /* or 640x576 @ 25 fps. As this would cut */
1322 /* of a part of the image we prefer */
1323 /* 360x576 or 360x480 for now */
Sascha Sommer74458e62005-11-08 21:37:33 -08001324 if(width == maxw && height == maxh)
1325 width /= 2;
1326 }
1327
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001328 if ((hscale =
1329 (((unsigned long)maxw) << 12) / width - 4096L) >=
1330 0x4000)
1331 hscale = 0x3fff;
1332 width =
1333 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1334
1335 if ((vscale =
1336 (((unsigned long)maxh) << 12) / height - 4096L) >=
1337 0x4000)
1338 vscale = 0x3fff;
1339 height =
1340 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1341
1342 format->fmt.pix.width = width;
1343 format->fmt.pix.height = height;
1344 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1345 format->fmt.pix.bytesperline = width * 2;
1346 format->fmt.pix.sizeimage = width * 2 * height;
1347 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1348 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1349
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001350 em28xx_videodbg("%s: returned %dx%d (%d, %d)",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001351 cmd ==
1352 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1353 "VIDIOC_S_FMT", format->fmt.pix.width,
1354 format->fmt.pix.height, hscale, vscale);
1355
1356 if (cmd == VIDIOC_TRY_FMT)
1357 return 0;
1358
1359 for (i = 0; i < dev->num_frames; i++)
1360 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001361 em28xx_videodbg("VIDIOC_S_FMT failed. "
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001362 "Unmap the buffers first.");
1363 return -EINVAL;
1364 }
1365
1366 /* stop io in case it is already in progress */
1367 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001368 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream");
1369 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001370 return ret;
1371 }
1372
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001373 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001374 dev->io = IO_NONE;
1375
1376 /* set new image size */
1377 dev->width = width;
1378 dev->height = height;
1379 dev->frame_size = dev->width * dev->height * 2;
1380 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1381 dev->bytesperline = dev->width * 2;
1382 dev->hscale = hscale;
1383 dev->vscale = vscale;
1384/* dev->both_fileds = both_fileds; */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001385 em28xx_uninit_isoc(dev);
1386 em28xx_set_alternate(dev);
1387 em28xx_capture_start(dev, 1);
1388 em28xx_resolution_set(dev);
1389 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001390
1391 return 0;
1392 }
1393
1394 /* --- streaming capture ------------------------------------- */
1395 case VIDIOC_REQBUFS:
1396 {
1397 struct v4l2_requestbuffers *rb = arg;
1398 u32 i;
1399 int ret;
1400
1401 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1402 rb->memory != V4L2_MEMORY_MMAP)
1403 return -EINVAL;
1404
1405 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001406 em28xx_videodbg ("method is set to read;"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001407 " close and open the device again to"
1408 " choose the mmap I/O method");
1409 return -EINVAL;
1410 }
1411
1412 for (i = 0; i < dev->num_frames; i++)
1413 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001414 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001415 return -EINVAL;
1416 }
1417
1418 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001419 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream");
1420 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001421 return ret;
1422 }
1423
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001424 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001425
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001426 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001427 if (rb->count)
1428 rb->count =
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001429 em28xx_request_buffers(dev, rb->count);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001430
1431 dev->frame_current = NULL;
1432
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001433 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001434 rb->count);
1435 dev->io = rb->count ? IO_MMAP : IO_NONE;
1436 return 0;
1437 }
1438
1439 case VIDIOC_QUERYBUF:
1440 {
1441 struct v4l2_buffer *b = arg;
1442
1443 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1444 b->index >= dev->num_frames || dev->io != IO_MMAP)
1445 return -EINVAL;
1446
1447 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1448
1449 if (dev->frame[b->index].vma_use_count) {
1450 b->flags |= V4L2_BUF_FLAG_MAPPED;
1451 }
1452 if (dev->frame[b->index].state == F_DONE)
1453 b->flags |= V4L2_BUF_FLAG_DONE;
1454 else if (dev->frame[b->index].state != F_UNUSED)
1455 b->flags |= V4L2_BUF_FLAG_QUEUED;
1456 return 0;
1457 }
1458 case VIDIOC_QBUF:
1459 {
1460 struct v4l2_buffer *b = arg;
1461 unsigned long lock_flags;
1462
1463 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1464 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1465 return -EINVAL;
1466 }
1467
1468 if (dev->frame[b->index].state != F_UNUSED) {
1469 return -EAGAIN;
1470 }
1471 dev->frame[b->index].state = F_QUEUED;
1472
1473 /* add frame to fifo */
1474 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1475 list_add_tail(&dev->frame[b->index].frame,
1476 &dev->inqueue);
1477 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1478
1479 return 0;
1480 }
1481 case VIDIOC_DQBUF:
1482 {
1483 struct v4l2_buffer *b = arg;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001484 struct em28xx_frame_t *f;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001485 unsigned long lock_flags;
1486 int ret = 0;
1487
1488 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1489 || dev->io != IO_MMAP)
1490 return -EINVAL;
1491
1492 if (list_empty(&dev->outqueue)) {
1493 if (dev->stream == STREAM_OFF)
1494 return -EINVAL;
1495 if (filp->f_flags & O_NONBLOCK)
1496 return -EAGAIN;
1497 ret = wait_event_interruptible
1498 (dev->wait_frame,
1499 (!list_empty(&dev->outqueue)) ||
1500 (dev->state & DEV_DISCONNECTED));
1501 if (ret)
1502 return ret;
1503 if (dev->state & DEV_DISCONNECTED)
1504 return -ENODEV;
1505 }
1506
1507 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1508 f = list_entry(dev->outqueue.next,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001509 struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001510 list_del(dev->outqueue.next);
1511 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1512
1513 f->state = F_UNUSED;
1514 memcpy(b, &f->buf, sizeof(*b));
1515
1516 if (f->vma_use_count)
1517 b->flags |= V4L2_BUF_FLAG_MAPPED;
1518
1519 return 0;
1520 }
1521 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001522 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1523 em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001524 }
1525 return 0;
1526}
1527
1528/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001529 * em28xx_v4l2_ioctl()
1530 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001531 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001532static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001533 unsigned int cmd, unsigned long arg)
1534{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001535 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001536 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001537
1538 if (down_interruptible(&dev->fileop_lock))
1539 return -ERESTARTSYS;
1540
1541 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001542 em28xx_errdev("v4l2 ioctl: device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001543 up(&dev->fileop_lock);
1544 return -ENODEV;
1545 }
1546
1547 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001548 em28xx_errdev
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001549 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1550 up(&dev->fileop_lock);
1551 return -EIO;
1552 }
1553
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001554 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001555
1556 up(&dev->fileop_lock);
1557
1558 return ret;
1559}
1560
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001561static struct file_operations em28xx_v4l_fops = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001562 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001563 .open = em28xx_v4l2_open,
1564 .release = em28xx_v4l2_close,
1565 .ioctl = em28xx_v4l2_ioctl,
1566 .read = em28xx_v4l2_read,
1567 .poll = em28xx_v4l2_poll,
1568 .mmap = em28xx_v4l2_mmap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001569 .llseek = no_llseek,
1570};
1571
1572/******************************** usb interface *****************************************/
1573
1574/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001575 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001576 * allocates and inits the device structs, registers i2c bus and v4l device
1577 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001578static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001579 int minor, int model)
1580{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001581 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582 int retval = -ENOMEM;
1583 int errCode, i;
1584 unsigned int maxh, maxw;
1585 struct usb_interface *uif;
1586
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001587 dev->udev = udev;
1588 dev->model = model;
1589 init_MUTEX(&dev->lock);
1590 init_waitqueue_head(&dev->open);
1591
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001592 dev->em28xx_write_regs = em28xx_write_regs;
1593 dev->em28xx_read_reg = em28xx_read_reg;
1594 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1595 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1596 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1597 dev->is_em2800 = em28xx_boards[model].is_em2800;
1598 dev->has_tuner = em28xx_boards[model].has_tuner;
1599 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1600 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1601 dev->decoder = em28xx_boards[model].decoder;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001602
1603 if (tuner >= 0)
1604 dev->tuner_type = tuner;
1605 else
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001606 dev->tuner_type = em28xx_boards[model].tuner_type;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001607
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001608 dev->video_inputs = em28xx_boards[model].vchannels;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001609
1610 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001611 if (em28xx_boards[model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001612 break;
1613 if (i == TVNORMS)
1614 i = 0;
1615
1616 dev->tvnorm = &tvnorms[i]; /* set default norm */
1617
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001618 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001619
1620 maxw = norm_maxw(dev);
1621 maxh = norm_maxh(dev);
1622
1623 /* set default image size */
1624 dev->width = maxw;
1625 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001626 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627 dev->field_size = dev->width * dev->height;
1628 dev->frame_size =
1629 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1630 dev->bytesperline = dev->width * 2;
1631 dev->hscale = 0;
1632 dev->vscale = 0;
1633 dev->ctl_input = 2;
1634
1635 /* setup video picture settings for saa7113h */
1636 memset(&dev->vpic, 0, sizeof(dev->vpic));
1637 dev->vpic.colour = 128 << 8;
1638 dev->vpic.hue = 128 << 8;
1639 dev->vpic.brightness = 128 << 8;
1640 dev->vpic.contrast = 192 << 8;
1641 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1642 dev->vpic.depth = 16;
1643 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1644
1645 /* compute alternate max packet sizes */
1646 uif = dev->udev->actconfig->interface[0];
1647 dev->alt_max_pkt_size[0] = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001648 for (i = 1; i <= EM28XX_MAX_ALT && i < uif->num_altsetting ; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001649 u16 tmp =
1650 le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1651 wMaxPacketSize);
1652 dev->alt_max_pkt_size[i] =
1653 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1654 }
1655
1656#ifdef CONFIG_MODULES
1657 /* request some modules */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001658 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001659 request_module("saa711x");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001660 if (dev->decoder == EM28XX_TVP5150)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661 request_module("tvp5150");
1662 if (dev->has_tuner)
1663 request_module("tuner");
1664 if (dev->tda9887_conf)
1665 request_module("tda9887");
1666#endif
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001667 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001668 if (errCode) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001669 em28xx_errdev("error configuring device\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670 kfree(dev);
1671 return -ENOMEM;
1672 }
1673
1674 down(&dev->lock);
1675 /* register i2c bus */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676 em28xx_i2c_register(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677
1678 /* Do board specific init and eeprom reading */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001679 em28xx_card_setup(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001680
1681 /* configure the device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001682 em28xx_config_i2c(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001683
1684 up(&dev->lock);
1685
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001686 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001687
1688#ifdef CONFIG_MODULES
1689 if (dev->has_msp34xx)
1690 request_module("msp3400");
1691#endif
1692 /* allocate and fill v4l2 device struct */
1693 dev->vdev = video_device_alloc();
1694 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001695 em28xx_errdev("cannot allocate video_device.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696 kfree(dev);
1697 return -ENOMEM;
1698 }
1699
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001700 dev->vdev->type = VID_TYPE_CAPTURE;
1701 if (dev->has_tuner)
1702 dev->vdev->type |= VID_TYPE_TUNER;
1703 dev->vdev->hardware = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001704 dev->vdev->fops = &em28xx_v4l_fops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001705 dev->vdev->minor = -1;
1706 dev->vdev->dev = &dev->udev->dev;
1707 dev->vdev->release = video_device_release;
1708 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001709 "em28xx video");
1710 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001711
1712 /* register v4l2 device */
1713 down(&dev->lock);
1714 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001715 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001716 retval);
1717 up(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001718 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001719 video_device_release(dev->vdev);
1720 kfree(dev);
1721 return -ENODEV;
1722 }
1723 if (dev->has_msp34xx) {
1724 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001725 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726 udelay(2500);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001727 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728 udelay(2500);
1729
1730 }
1731 video_mux(dev, 0);
1732
1733 up(&dev->lock);
1734
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001735 em28xx_info("V4L2 device registered as /dev/video%d\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736 dev->vdev->minor);
1737
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738 return 0;
1739}
1740
1741/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001742 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743 * checks for supported devices
1744 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001745static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746 const struct usb_device_id *id)
1747{
1748 const struct usb_endpoint_descriptor *endpoint;
1749 struct usb_device *udev;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001750 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001751 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001752 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753
1754 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001755 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1756
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001757
1758 /* Don't register audio interfaces */
1759 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001760 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001761 udev->descriptor.idVendor,udev->descriptor.idProduct,
1762 ifnum,
1763 interface->altsetting[0].desc.bInterfaceClass);
1764 return -ENODEV;
1765 }
1766
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001767 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001768 udev->descriptor.idVendor,udev->descriptor.idProduct,
1769 ifnum,
1770 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001771
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001772 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1773
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001774 /* check if the the device has the iso in endpoint at the correct place */
1775 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1776 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001777 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001778 return -ENODEV;
1779 }
1780 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001781 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001782 return -ENODEV;
1783 }
1784
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001785 model=id->driver_info;
1786 nr=interface->minor;
1787
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001788 if (nr>EM28XX_MAXBOARDS) {
1789 printk ("em28xx: Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001790 return -ENOMEM;
1791 }
1792
1793 /* allocate memory for our device state and initialize it */
1794 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1795 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001796 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001797 return -ENOMEM;
1798 }
1799 memset(dev, 0, sizeof(*dev));
1800
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001801 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001802
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001803 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001804 model=card[nr];
1805
1806 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1807 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1808 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1809 "%s: workaround that. Redirect complaints to the vendor of\n"
1810 "%s: the TV card. Best regards,\n"
1811 "%s: -- tux\n",
1812 dev->name,dev->name,dev->name,dev->name,dev->name);
1813 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1814 dev->name);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001815 for (i = 0; i < em28xx_bcount; i++) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001816 printk("%s: card=%d -> %s\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001817 dev->name, i, em28xx_boards[i].name);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001818 }
1819 }
1820
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821 /* allocate device struct */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001822 retval = em28xx_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001823 if (retval)
1824 return retval;
1825
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001826 em28xx_info("Found %s\n", em28xx_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001827
1828 /* save our data pointer in this interface device */
1829 usb_set_intfdata(interface, dev);
1830 return 0;
1831}
1832
1833/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001834 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835 * called when the device gets diconencted
1836 * video device will be unregistered on v4l2_close in case it is still open
1837 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001838static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001839{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001840 struct em28xx *dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001841 usb_set_intfdata(interface, NULL);
1842
1843 if (!dev)
1844 return;
1845
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001846 down_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847
1848 down(&dev->lock);
1849
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001850 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001851
1852 wake_up_interruptible_all(&dev->open);
1853
1854 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001855 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001856 ("device /dev/video%d is open! Deregistration and memory "
1857 "deallocation are deferred on close.\n", dev->vdev->minor);
1858 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001859 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860 dev->state |= DEV_DISCONNECTED;
1861 wake_up_interruptible(&dev->wait_frame);
1862 wake_up_interruptible(&dev->wait_stream);
1863 } else {
1864 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001865 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001866 }
1867
1868 up(&dev->lock);
1869
1870 if (!dev->users)
1871 kfree(dev);
1872
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001873 up_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001874
1875}
1876
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001877static struct usb_driver em28xx_usb_driver = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001878 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001879 .name = "em28xx",
1880 .probe = em28xx_usb_probe,
1881 .disconnect = em28xx_usb_disconnect,
1882 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001883};
1884
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001885static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886{
1887 int result;
1888
1889 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001890 (EM28XX_VERSION_CODE >> 16) & 0xff,
1891 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001892#ifdef SNAPSHOT
1893 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1894 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1895#endif
1896
1897 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001898 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001899 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001900 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001901 " usb_register failed. Error number %d.\n", result);
1902
1903 return result;
1904}
1905
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001906static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001907{
1908 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001909 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910}
1911
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001912module_init(em28xx_module_init);
1913module_exit(em28xx_module_exit);