]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (3712): Fix video input setting of em28xx, use _INT_S_VIDEO_ROUTING in tvp5150
[net-next-2.6.git] / drivers / media / video / em28xx / em28xx-video.c
CommitLineData
a6c2ba28 1/*
f7abcd38 2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
a6c2ba28 3
f7abcd38
MCC
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
4ac97914 6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
f7abcd38 7 Sascha Sommer <saschasommer@freenet.de>
a6c2ba28 8
439090d7
MCC
9 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
a6c2ba28
AM
12 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>
e5589bef 31#include <linux/bitmap.h>
a6c2ba28 32#include <linux/usb.h>
a6c2ba28 33#include <linux/i2c.h>
b296fc60 34#include <linux/version.h>
a6c2ba28 35#include <linux/video_decoder.h>
1e4baed3 36#include <linux/mutex.h>
a6c2ba28 37
f7abcd38 38#include "em28xx.h"
d5e52653 39#include <media/tuner.h>
c0477ad9 40#include <media/v4l2-common.h>
2474ed44 41#include <media/msp3400.h>
a6c2ba28 42
f7abcd38
MCC
43#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
45 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
46 "Sascha Sommer <saschasommer@freenet.de>"
a6c2ba28 47
f7abcd38
MCC
48#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
3acf2809 50#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
a6c2ba28 51
3acf2809 52#define em28xx_videodbg(fmt, arg...) do {\
4ac97914
MCC
53 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
f85c657f 55 dev->name, __FUNCTION__ , ##arg); } while (0)
a6c2ba28
AM
56
57MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL");
60
3acf2809 61static LIST_HEAD(em28xx_devlist);
9c75541f 62
9d4d9c05 63static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
e5589bef
MCC
64static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
596d92d5 66module_param_array(card, int, NULL, 0444);
e5589bef
MCC
67module_param_array(video_nr, int, NULL, 0444);
68module_param_array(vbi_nr, int, NULL, 0444);
596d92d5 69MODULE_PARM_DESC(card,"card type");
e5589bef
MCC
70MODULE_PARM_DESC(video_nr,"video device numbers");
71MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
596d92d5 72
a6c2ba28
AM
73static 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
e5589bef
MCC
81/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
82static unsigned long em28xx_devused;
83
a6c2ba28 84/* supported tv norms */
3acf2809 85static struct em28xx_tvnorm tvnorms[] = {
a6c2ba28
AM
86 {
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 */
c0477ad9 108/* Common to all boards */
3acf2809 109static struct v4l2_queryctrl em28xx_qctrl[] = {
c0477ad9
MCC
110 {
111 .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
3acf2809 131static struct usb_driver em28xx_usb_driver;
a6c2ba28 132
1e4baed3 133static DEFINE_MUTEX(em28xx_sysfs_lock);
3acf2809 134static DECLARE_RWSEM(em28xx_disconnect);
a6c2ba28
AM
135
136/********************* v4l2 interface ******************************************/
137
a6c2ba28 138/*
3acf2809 139 * em28xx_config()
a6c2ba28
AM
140 * inits registers with sane defaults
141 */
3acf2809 142static int em28xx_config(struct em28xx *dev)
a6c2ba28
AM
143{
144
145 /* Sets I2C speed to 100 KHz */
3acf2809 146 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
a6c2ba28
AM
147
148 /* enable vbi capturing */
e5589bef 149
9475fb1c
MR
150/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
151/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
e5589bef
MCC
152 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
153
3acf2809 154 em28xx_audio_usb_mute(dev, 1);
a6c2ba28
AM
155 dev->mute = 1; /* maybe not the right place... */
156 dev->volume = 0x1f;
3acf2809
MCC
157 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);
a6c2ba28
AM
162
163 return 0;
164}
165
166/*
3acf2809 167 * em28xx_config_i2c()
a6c2ba28
AM
168 * configure i2c attached devices
169 */
943a4902 170static void em28xx_config_i2c(struct em28xx *dev)
a6c2ba28
AM
171{
172 struct v4l2_frequency f;
c7c0b34c
HV
173 struct v4l2_routing route;
174
175 route.input = INPUT(dev->ctl_input)->vmux;
176 route.output = 0;
f5762e44 177 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
c7c0b34c 178 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
f5762e44 179 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
a6c2ba28
AM
180
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;
3acf2809 186 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
a6c2ba28
AM
187
188 /* configure tda9887 */
189
a6c2ba28 190
3acf2809 191/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
a6c2ba28
AM
192}
193
194/*
3acf2809 195 * em28xx_empty_framequeues()
a6c2ba28
AM
196 * prepare queues for incoming and outgoing frames
197 */
3acf2809 198static void em28xx_empty_framequeues(struct em28xx *dev)
a6c2ba28
AM
199{
200 u32 i;
201
202 INIT_LIST_HEAD(&dev->inqueue);
203 INIT_LIST_HEAD(&dev->outqueue);
204
3acf2809 205 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
a6c2ba28
AM
206 dev->frame[i].state = F_UNUSED;
207 dev->frame[i].buf.bytesused = 0;
208 }
209}
210
eac94356
MCC
211static void video_mux(struct em28xx *dev, int index)
212{
c7c0b34c
HV
213 int ainput;
214 struct v4l2_routing route;
eac94356 215
c7c0b34c
HV
216 route.input = INPUT(index)->vmux;
217 route.output = 0;
eac94356
MCC
218 dev->ctl_input = index;
219 dev->ctl_ainput = INPUT(index)->amux;
220
c7c0b34c 221 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
eac94356 222
c7c0b34c 223 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
eac94356
MCC
224
225 if (dev->has_msp34xx) {
9bb13a6d
MCC
226 if (dev->i2s_speed)
227 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
2474ed44 228 route.input = dev->ctl_ainput;
07151724 229 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
2474ed44
HV
230 /* Note: this is msp3400 specific */
231 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
eac94356
MCC
232 ainput = EM28XX_AUDIO_SRC_TUNER;
233 em28xx_audio_source(dev, ainput);
234 } else {
235 switch (dev->ctl_ainput) {
9475fb1c
MR
236 case 0:
237 ainput = EM28XX_AUDIO_SRC_TUNER;
238 break;
239 default:
240 ainput = EM28XX_AUDIO_SRC_LINE;
eac94356
MCC
241 }
242 em28xx_audio_source(dev, ainput);
243 }
244}
245
a6c2ba28 246/*
3acf2809 247 * em28xx_v4l2_open()
a6c2ba28
AM
248 * inits the device and starts isoc transfer
249 */
3acf2809 250static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
a6c2ba28 251{
a6c2ba28 252 int minor = iminor(inode);
a6c2ba28 253 int errCode = 0;
3acf2809 254 struct em28xx *h,*dev = NULL;
9c75541f
MR
255 struct list_head *list;
256
3acf2809
MCC
257 list_for_each(list,&em28xx_devlist) {
258 h = list_entry(list, struct em28xx, devlist);
9c75541f
MR
259 if (h->vdev->minor == minor) {
260 dev = h;
e5589bef
MCC
261 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;
9c75541f
MR
266 }
267 }
e5589bef
MCC
268 if (NULL == dev)
269 return -ENODEV;
9c75541f
MR
270
271 filp->private_data=dev;
272
e5589bef
MCC
273 em28xx_videodbg("open minor=%d type=%s users=%d\n",
274 minor,v4l2_type_names[dev->type],dev->users);
a6c2ba28 275
3acf2809 276 if (!down_read_trylock(&em28xx_disconnect))
a6c2ba28
AM
277 return -ERESTARTSYS;
278
279 if (dev->users) {
3acf2809
MCC
280 em28xx_warn("this driver can be opened only once\n");
281 up_read(&em28xx_disconnect);
a6c2ba28
AM
282 return -EBUSY;
283 }
284
3593cab5 285 mutex_init(&dev->fileop_lock); /* to 1 == available */
a6c2ba28
AM
286 spin_lock_init(&dev->queue_lock);
287 init_waitqueue_head(&dev->wait_frame);
288 init_waitqueue_head(&dev->wait_stream);
289
3593cab5 290 mutex_lock(&dev->lock);
a6c2ba28 291
e5589bef
MCC
292 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
293 em28xx_set_alternate(dev);
a6c2ba28 294
e5589bef
MCC
295 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;
a6c2ba28 302
e5589bef
MCC
303 em28xx_capture_start(dev, 1);
304 em28xx_resolution_set(dev);
a6c2ba28 305
e5d4a56d
MR
306 /* device needs to be initialized before isoc transfer */
307 video_mux(dev, 0);
308
e5589bef
MCC
309 /* start the transfer */
310 errCode = em28xx_init_isoc(dev);
311 if (errCode)
312 goto err;
313
e5589bef 314 }
a6c2ba28
AM
315
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 */
3acf2809 323 em28xx_empty_framequeues(dev);
a6c2ba28
AM
324
325 dev->state |= DEV_INITIALIZED;
326
3593cab5
IM
327err:
328 mutex_unlock(&dev->lock);
3acf2809 329 up_read(&em28xx_disconnect);
a6c2ba28
AM
330 return errCode;
331}
332
333/*
3acf2809 334 * em28xx_realease_resources()
a6c2ba28
AM
335 * unregisters the v4l2,i2c and usb devices
336 * called when the device gets disconected or at module unload
337*/
3acf2809 338static void em28xx_release_resources(struct em28xx *dev)
a6c2ba28 339{
1e4baed3 340 mutex_lock(&em28xx_sysfs_lock);
a6c2ba28 341
e5589bef
MCC
342 /*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);
9c75541f 347 list_del(&dev->devlist);
a6c2ba28 348 video_unregister_device(dev->vdev);
e5589bef 349 video_unregister_device(dev->vbi_dev);
3acf2809 350 em28xx_i2c_unregister(dev);
a6c2ba28 351 usb_put_dev(dev->udev);
1e4baed3 352 mutex_unlock(&em28xx_sysfs_lock);
e5589bef
MCC
353
354
355 /* Mark device as unused */
356 em28xx_devused&=~(1<<dev->devno);
a6c2ba28
AM
357}
358
359/*
3acf2809 360 * em28xx_v4l2_close()
a6c2ba28
AM
361 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
362 */
3acf2809 363static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
a6c2ba28 364{
a6c2ba28 365 int errCode;
3acf2809 366 struct em28xx *dev=filp->private_data;
a6c2ba28 367
eac94356 368 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 369
3593cab5 370 mutex_lock(&dev->lock);
a6c2ba28 371
3acf2809 372 em28xx_uninit_isoc(dev);
a6c2ba28 373
3acf2809 374 em28xx_release_buffers(dev);
a6c2ba28
AM
375
376 /* the device is already disconnect, free the remaining resources */
377 if (dev->state & DEV_DISCONNECTED) {
3acf2809 378 em28xx_release_resources(dev);
3593cab5 379 mutex_unlock(&dev->lock);
a6c2ba28
AM
380 kfree(dev);
381 return 0;
382 }
383
384 /* set alternate 0 */
385 dev->alt = 0;
eac94356 386 em28xx_videodbg("setting alternate 0\n");
a6c2ba28
AM
387 errCode = usb_set_interface(dev->udev, 0, 0);
388 if (errCode < 0) {
3acf2809 389 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
a6c2ba28
AM
390 errCode);
391 }
392
393 dev->users--;
394 wake_up_interruptible_nr(&dev->open, 1);
3593cab5 395 mutex_unlock(&dev->lock);
a6c2ba28
AM
396 return 0;
397}
398
399/*
3acf2809 400 * em28xx_v4l2_read()
a6c2ba28
AM
401 * will allocate buffers when called for the first time
402 */
403static ssize_t
3acf2809 404em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
a6c2ba28
AM
405 loff_t * f_pos)
406{
3acf2809 407 struct em28xx_frame_t *f, *i;
a6c2ba28
AM
408 unsigned long lock_flags;
409 int ret = 0;
3acf2809 410 struct em28xx *dev = filp->private_data;
a6c2ba28 411
e5589bef
MCC
412 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)) {
3593cab5 419 mutex_unlock(&dev->fileop_lock);
e5589bef
MCC
420 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)) {
3593cab5 428 mutex_unlock(&dev->fileop_lock);
e5589bef
MCC
429 return -EFAULT;
430 }
431 return (1);
432 }
433
3593cab5 434 if (mutex_lock_interruptible(&dev->fileop_lock))
a6c2ba28
AM
435 return -ERESTARTSYS;
436
437 if (dev->state & DEV_DISCONNECTED) {
eac94356 438 em28xx_videodbg("device not present\n");
3593cab5 439 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
440 return -ENODEV;
441 }
442
443 if (dev->state & DEV_MISCONFIGURED) {
eac94356 444 em28xx_videodbg("device misconfigured; close and open it again\n");
3593cab5 445 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
446 return -EIO;
447 }
448
449 if (dev->io == IO_MMAP) {
3acf2809 450 em28xx_videodbg ("IO method is set to mmap; close and open"
eac94356 451 " the device again to choose the read method\n");
3593cab5 452 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
453 return -EINVAL;
454 }
455
456 if (dev->io == IO_NONE) {
3acf2809
MCC
457 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
458 em28xx_errdev("read failed, not enough memory\n");
3593cab5 459 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
460 return -ENOMEM;
461 }
462 dev->io = IO_READ;
463 dev->stream = STREAM_ON;
3acf2809 464 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
465 }
466
467 if (!count) {
3593cab5 468 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
469 return 0;
470 }
471
472 if (list_empty(&dev->outqueue)) {
473 if (filp->f_flags & O_NONBLOCK) {
3593cab5 474 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
475 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) {
3593cab5 482 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
483 return ret;
484 }
485 if (dev->state & DEV_DISCONNECTED) {
3593cab5 486 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
487 return -ENODEV;
488 }
489 }
490
3acf2809 491 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
a6c2ba28
AM
492
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
3acf2809 499 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
500
501 if (count > f->buf.length)
502 count = f->buf.length;
503
504 if (copy_to_user(buf, f->bufmem, count)) {
3593cab5 505 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
506 return -EFAULT;
507 }
508 *f_pos += count;
509
3593cab5 510 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
511
512 return count;
513}
514
515/*
3acf2809 516 * em28xx_v4l2_poll()
a6c2ba28
AM
517 * will allocate buffers when called for the first time
518 */
3acf2809 519static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
a6c2ba28 520{
a6c2ba28 521 unsigned int mask = 0;
3acf2809 522 struct em28xx *dev = filp->private_data;
a6c2ba28 523
3593cab5 524 if (mutex_lock_interruptible(&dev->fileop_lock))
a6c2ba28
AM
525 return POLLERR;
526
527 if (dev->state & DEV_DISCONNECTED) {
eac94356 528 em28xx_videodbg("device not present\n");
a6c2ba28 529 } else if (dev->state & DEV_MISCONFIGURED) {
eac94356 530 em28xx_videodbg("device is misconfigured; close and open it again\n");
a6c2ba28
AM
531 } else {
532 if (dev->io == IO_NONE) {
3acf2809
MCC
533 if (!em28xx_request_buffers
534 (dev, EM28XX_NUM_READ_FRAMES)) {
535 em28xx_warn
a6c2ba28
AM
536 ("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) {
3acf2809 544 em28xx_queue_unusedframes(dev);
a6c2ba28
AM
545 poll_wait(filp, &dev->wait_frame, wait);
546
547 if (!list_empty(&dev->outqueue))
548 mask |= POLLIN | POLLRDNORM;
549
3593cab5 550 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
551
552 return mask;
553 }
554 }
555
3593cab5 556 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
557 return POLLERR;
558}
559
560/*
3acf2809 561 * em28xx_vm_open()
a6c2ba28 562 */
3acf2809 563static void em28xx_vm_open(struct vm_area_struct *vma)
a6c2ba28 564{
3acf2809 565 struct em28xx_frame_t *f = vma->vm_private_data;
a6c2ba28
AM
566 f->vma_use_count++;
567}
568
569/*
3acf2809 570 * em28xx_vm_close()
a6c2ba28 571 */
3acf2809 572static void em28xx_vm_close(struct vm_area_struct *vma)
a6c2ba28
AM
573{
574 /* NOTE: buffers are not freed here */
3acf2809 575 struct em28xx_frame_t *f = vma->vm_private_data;
a6c2ba28
AM
576 f->vma_use_count--;
577}
578
3acf2809
MCC
579static struct vm_operations_struct em28xx_vm_ops = {
580 .open = em28xx_vm_open,
581 .close = em28xx_vm_close,
a6c2ba28
AM
582};
583
584/*
3acf2809 585 * em28xx_v4l2_mmap()
a6c2ba28 586 */
3acf2809 587static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
a6c2ba28 588{
a6c2ba28 589 unsigned long size = vma->vm_end - vma->vm_start,
3639c861
SS
590 start = vma->vm_start;
591 void *pos;
a6c2ba28 592 u32 i;
9c75541f 593
3acf2809 594 struct em28xx *dev = filp->private_data;
9c75541f 595
3593cab5 596 if (mutex_lock_interruptible(&dev->fileop_lock))
a6c2ba28
AM
597 return -ERESTARTSYS;
598
599 if (dev->state & DEV_DISCONNECTED) {
eac94356 600 em28xx_videodbg("mmap: device not present\n");
3593cab5 601 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
602 return -ENODEV;
603 }
604
605 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 606 em28xx_videodbg ("mmap: Device is misconfigured; close and "
eac94356 607 "open it again\n");
3593cab5 608 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
609 return -EIO;
610 }
611
612 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
613 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
3593cab5 614 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
615 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) {
eac94356 623 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
3593cab5 624 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
625 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
3639c861 632 pos = dev->frame[i].bufmem;
a6c2ba28 633 while (size > 0) { /* size is page-aligned */
3639c861
SS
634 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
635 em28xx_videodbg("mmap: vm_insert_page failed\n");
3593cab5 636 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
637 return -EAGAIN;
638 }
639 start += PAGE_SIZE;
640 pos += PAGE_SIZE;
641 size -= PAGE_SIZE;
642 }
643
3acf2809 644 vma->vm_ops = &em28xx_vm_ops;
a6c2ba28
AM
645 vma->vm_private_data = &dev->frame[i];
646
3acf2809 647 em28xx_vm_open(vma);
3593cab5 648 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
649 return 0;
650}
651
652/*
3acf2809 653 * em28xx_get_ctrl()
a6c2ba28
AM
654 * return the current saturation, brightness or contrast, mute state
655 */
3acf2809 656static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
a6c2ba28 657{
a6c2ba28
AM
658 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;
c0477ad9
MCC
665 default:
666 return -EINVAL;
667 }
668}
669
a6c2ba28 670/*
3acf2809 671 * em28xx_set_ctrl()
a6c2ba28
AM
672 * mute or set new saturation, brightness or contrast
673 */
3acf2809 674static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
a6c2ba28
AM
675{
676 switch (ctrl->id) {
677 case V4L2_CID_AUDIO_MUTE:
678 if (ctrl->value != dev->mute) {
679 dev->mute = ctrl->value;
3acf2809
MCC
680 em28xx_audio_usb_mute(dev, ctrl->value);
681 return em28xx_audio_analog_set(dev);
a6c2ba28
AM
682 }
683 return 0;
684 case V4L2_CID_AUDIO_VOLUME:
685 dev->volume = ctrl->value;
3acf2809 686 return em28xx_audio_analog_set(dev);
c0477ad9
MCC
687 default:
688 return -EINVAL;
689 }
690}
691
a6c2ba28 692/*
3acf2809 693 * em28xx_stream_interrupt()
a6c2ba28
AM
694 * stops streaming
695 */
3acf2809 696static int em28xx_stream_interrupt(struct em28xx *dev)
a6c2ba28
AM
697{
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),
3acf2809 706 EM28XX_URB_TIMEOUT);
a6c2ba28
AM
707 if (dev->state & DEV_DISCONNECTED)
708 return -ENODEV;
709 else if (ret) {
710 dev->state |= DEV_MISCONFIGURED;
3acf2809 711 em28xx_videodbg("device is misconfigured; close and "
e5589bef
MCC
712 "open /dev/video%d again\n",
713 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
a6c2ba28
AM
714 return ret;
715 }
716
717 return 0;
718}
719
3acf2809 720static int em28xx_set_norm(struct em28xx *dev, int width, int height)
a6c2ba28
AM
721{
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
3acf2809 759 em28xx_resolution_set(dev);
a6c2ba28
AM
760
761 return 0;
762}
763
e5589bef
MCC
764static 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
2d50f847
MCC
775 switch (format->type) {
776 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
777 {
e5589bef
MCC
778 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);
2d50f847 788 break;
e5589bef
MCC
789 }
790
2d50f847
MCC
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);
e5589bef
MCC
807}
808
2d50f847
MCC
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
836 return 0;
837 }
838
839
840 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
9aeb4b05 879 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
2d50f847 880 hscale = 0x3fff;
2d50f847 881
9aeb4b05
MCC
882 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
883
884 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
2d50f847 885 vscale = 0x3fff;
9aeb4b05
MCC
886
887 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
2d50f847
MCC
888
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",
9aeb4b05
MCC
898 cmd == VIDIOC_TRY_FMT ?
899 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
900 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
2d50f847
MCC
901
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;
9aeb4b05 926 dev->field_size = dev->frame_size >> 1;
2d50f847
MCC
927 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}
e5589bef 938
a6c2ba28 939/*
3acf2809 940 * em28xx_v4l2_do_ioctl()
a6c2ba28 941 * This function is _not_ called directly, but from
3acf2809 942 * em28xx_v4l2_ioctl. Userspace
a6c2ba28
AM
943 * copying is done already, arg is a kernel pointer.
944 */
3acf2809
MCC
945static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
946 struct em28xx *dev, unsigned int cmd, void *arg,
a6c2ba28
AM
947 v4l2_kioctl driver_ioctl)
948{
949 int ret;
950
951 switch (cmd) {
952 /* ---------- tv norms ---------- */
953 case VIDIOC_ENUMSTD:
9aeb4b05
MCC
954 {
955 struct v4l2_standard *e = arg;
956 unsigned int i;
a6c2ba28 957
9aeb4b05
MCC
958 i = e->index;
959 if (i >= TVNORMS)
960 return -EINVAL;
961 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 }
a6c2ba28 968 case VIDIOC_G_STD:
9aeb4b05
MCC
969 {
970 v4l2_std_id *id = arg;
a6c2ba28 971
9aeb4b05
MCC
972 *id = dev->tvnorm->id;
973 return 0;
974 }
a6c2ba28 975 case VIDIOC_S_STD:
9aeb4b05
MCC
976 {
977 v4l2_std_id *id = arg;
978 unsigned int i;
a6c2ba28 979
9aeb4b05
MCC
980 for (i = 0; i < TVNORMS; i++)
981 if (*id == tvnorms[i].id)
982 break;
983 if (i == TVNORMS)
a6c2ba28 984 for (i = 0; i < TVNORMS; i++)
9aeb4b05 985 if (*id & tvnorms[i].id)
a6c2ba28 986 break;
9aeb4b05
MCC
987 if (i == TVNORMS)
988 return -EINVAL;
a6c2ba28 989
3593cab5 990 mutex_lock(&dev->lock);
9aeb4b05 991 dev->tvnorm = &tvnorms[i];
a6c2ba28 992
9aeb4b05 993 em28xx_set_norm(dev, dev->width, dev->height);
a6c2ba28 994
9aeb4b05
MCC
995 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
996 &dev->tvnorm->id);
a6c2ba28 997
3593cab5 998 mutex_unlock(&dev->lock);
a6c2ba28 999
9aeb4b05
MCC
1000 return 0;
1001 }
a6c2ba28 1002
9aeb4b05 1003 /* ------ input switching ---------- */
a6c2ba28 1004 case VIDIOC_ENUMINPUT:
9aeb4b05
MCC
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 }
a6c2ba28 1036 case VIDIOC_G_INPUT:
9aeb4b05
MCC
1037 {
1038 int *i = arg;
1039 *i = dev->ctl_input;
a6c2ba28 1040
9aeb4b05
MCC
1041 return 0;
1042 }
a6c2ba28 1043 case VIDIOC_S_INPUT:
9aeb4b05
MCC
1044 {
1045 int *index = arg;
a6c2ba28 1046
9aeb4b05
MCC
1047 if (*index >= MAX_EM28XX_INPUT)
1048 return -EINVAL;
1049 if (0 == INPUT(*index)->type)
1050 return -EINVAL;
a6c2ba28 1051
3593cab5 1052 mutex_lock(&dev->lock);
9aeb4b05 1053 video_mux(dev, *index);
3593cab5 1054 mutex_unlock(&dev->lock);
a6c2ba28 1055
9aeb4b05
MCC
1056 return 0;
1057 }
a6c2ba28 1058 case VIDIOC_G_AUDIO:
9aeb4b05
MCC
1059 {
1060 struct v4l2_audio *a = arg;
1061 unsigned int index = a->index;
a6c2ba28 1062
9aeb4b05
MCC
1063 if (a->index > 1)
1064 return -EINVAL;
1065 memset(a, 0, sizeof(*a));
1066 index = dev->ctl_ainput;
a6c2ba28 1067
9aeb4b05
MCC
1068 if (index == 0) {
1069 strcpy(a->name, "Television");
1070 } else {
1071 strcpy(a->name, "Line In");
a6c2ba28 1072 }
9aeb4b05
MCC
1073 a->capability = V4L2_AUDCAP_STEREO;
1074 a->index = index;
1075 return 0;
1076 }
a6c2ba28 1077 case VIDIOC_S_AUDIO:
9aeb4b05
MCC
1078 {
1079 struct v4l2_audio *a = arg;
a6c2ba28 1080
9aeb4b05
MCC
1081 if (a->index != dev->ctl_ainput)
1082 return -EINVAL;
1083
1084 return 0;
1085 }
a6c2ba28 1086
9aeb4b05 1087 /* --- controls ---------------------------------------------- */
a6c2ba28 1088 case VIDIOC_QUERYCTRL:
9aeb4b05
MCC
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));
a6c2ba28
AM
1101 return 0;
1102 }
c0477ad9 1103 }
9aeb4b05 1104 }
f5762e44
MCC
1105 em28xx_i2c_call_clients(dev,cmd,qc);
1106 if (qc->type)
1107 return 0;
1108 else
1109 return -EINVAL;
9aeb4b05 1110 }
a6c2ba28 1111 case VIDIOC_G_CTRL:
9aeb4b05
MCC
1112 {
1113 struct v4l2_control *ctrl = arg;
1114 int retval=-EINVAL;
a6c2ba28 1115
9aeb4b05
MCC
1116 if (!dev->has_msp34xx)
1117 retval=em28xx_get_ctrl(dev, ctrl);
1118 if (retval==-EINVAL) {
f5762e44
MCC
1119 em28xx_i2c_call_clients(dev,cmd,arg);
1120 return 0;
9aeb4b05
MCC
1121 } else return retval;
1122 }
a6c2ba28 1123 case VIDIOC_S_CTRL:
9aeb4b05
MCC
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);
c0477ad9
MCC
1137 }
1138 }
9aeb4b05 1139 }
a6c2ba28 1140
f5762e44
MCC
1141 em28xx_i2c_call_clients(dev,cmd,arg);
1142 return 0;
9aeb4b05
MCC
1143 }
1144 /* --- tuner ioctls ------------------------------------------ */
a6c2ba28 1145 case VIDIOC_G_TUNER:
9aeb4b05
MCC
1146 {
1147 struct v4l2_tuner *t = arg;
a6c2ba28 1148
9aeb4b05
MCC
1149 if (0 != t->index)
1150 return -EINVAL;
a6c2ba28 1151
9aeb4b05
MCC
1152 memset(t, 0, sizeof(*t));
1153 strcpy(t->name, "Tuner");
3593cab5 1154 mutex_lock(&dev->lock);
ab4cecf9
HV
1155 /* let clients fill in the remainder of this struct */
1156 em28xx_i2c_call_clients(dev, cmd, t);
3593cab5 1157 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1158 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1159 t->afc);
1160 return 0;
1161 }
a6c2ba28 1162 case VIDIOC_S_TUNER:
9aeb4b05
MCC
1163 {
1164 struct v4l2_tuner *t = arg;
a6c2ba28 1165
9aeb4b05
MCC
1166 if (0 != t->index)
1167 return -EINVAL;
3593cab5 1168 mutex_lock(&dev->lock);
ab4cecf9
HV
1169 /* let clients handle this */
1170 em28xx_i2c_call_clients(dev, cmd, t);
3593cab5 1171 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1172 return 0;
1173 }
a6c2ba28 1174 case VIDIOC_G_FREQUENCY:
9aeb4b05
MCC
1175 {
1176 struct v4l2_frequency *f = arg;
a6c2ba28 1177
9aeb4b05
MCC
1178 memset(f, 0, sizeof(*f));
1179 f->type = V4L2_TUNER_ANALOG_TV;
1180 f->frequency = dev->ctl_freq;
a6c2ba28 1181
9aeb4b05
MCC
1182 return 0;
1183 }
a6c2ba28 1184 case VIDIOC_S_FREQUENCY:
9aeb4b05
MCC
1185 {
1186 struct v4l2_frequency *f = arg;
a6c2ba28 1187
9aeb4b05
MCC
1188 if (0 != f->tuner)
1189 return -EINVAL;
a6c2ba28 1190
9aeb4b05
MCC
1191 if (V4L2_TUNER_ANALOG_TV != f->type)
1192 return -EINVAL;
a6c2ba28 1193
3593cab5 1194 mutex_lock(&dev->lock);
9aeb4b05
MCC
1195 dev->ctl_freq = f->frequency;
1196 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
3593cab5 1197 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1198 return 0;
1199 }
a6c2ba28 1200 case VIDIOC_CROPCAP:
9aeb4b05
MCC
1201 {
1202 struct v4l2_cropcap *cc = arg;
a6c2ba28 1203
9aeb4b05
MCC
1204 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 }
a6c2ba28 1215 case VIDIOC_STREAMON:
9aeb4b05
MCC
1216 {
1217 int *type = arg;
a6c2ba28 1218
9aeb4b05
MCC
1219 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1220 || dev->io != IO_MMAP)
1221 return -EINVAL;
a6c2ba28 1222
9aeb4b05
MCC
1223 if (list_empty(&dev->inqueue))
1224 return -EINVAL;
a6c2ba28 1225
9aeb4b05 1226 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
a6c2ba28 1227
9aeb4b05 1228 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
a6c2ba28 1229
9aeb4b05
MCC
1230 return 0;
1231 }
a6c2ba28 1232 case VIDIOC_STREAMOFF:
9aeb4b05
MCC
1233 {
1234 int *type = arg;
1235 int ret;
a6c2ba28 1236
9aeb4b05
MCC
1237 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1238 || dev->io != IO_MMAP)
1239 return -EINVAL;
a6c2ba28 1240
9aeb4b05
MCC
1241 if (dev->stream == STREAM_ON) {
1242 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
1243 if ((ret = em28xx_stream_interrupt(dev)))
1244 return ret;
a6c2ba28 1245 }
9aeb4b05
MCC
1246 em28xx_empty_framequeues(dev);
1247
1248 return 0;
1249 }
a6c2ba28
AM
1250 default:
1251 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1252 driver_ioctl);
1253 }
1254 return 0;
1255}
1256
1257/*
3acf2809 1258 * em28xx_v4l2_do_ioctl()
a6c2ba28 1259 * This function is _not_ called directly, but from
3acf2809 1260 * em28xx_v4l2_ioctl. Userspace
a6c2ba28
AM
1261 * copying is done already, arg is a kernel pointer.
1262 */
3acf2809 1263static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
a6c2ba28
AM
1264 unsigned int cmd, void *arg)
1265{
3acf2809 1266 struct em28xx *dev = filp->private_data;
a6c2ba28
AM
1267
1268 if (!dev)
1269 return -ENODEV;
1270
1271 if (video_debug > 1)
5e453dc7 1272 v4l_print_ioctl(dev->name,cmd);
a6c2ba28
AM
1273
1274 switch (cmd) {
1275
1276 /* --- capabilities ------------------------------------------ */
1277 case VIDIOC_QUERYCAP:
1278 {
9aeb4b05
MCC
1279 struct v4l2_capability *cap = arg;
1280
1281 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 ---------------------------------------- */
a6c2ba28 1298 case VIDIOC_ENUM_FMT:
9aeb4b05
MCC
1299 {
1300 struct v4l2_fmtdesc *fmtd = arg;
a6c2ba28 1301
9aeb4b05
MCC
1302 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 }
a6c2ba28 1311 case VIDIOC_G_FMT:
e5589bef 1312 return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
a6c2ba28
AM
1313
1314 case VIDIOC_TRY_FMT:
1315 case VIDIOC_S_FMT:
2d50f847 1316 return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
a6c2ba28 1317
a6c2ba28 1318 case VIDIOC_REQBUFS:
9aeb4b05
MCC
1319 {
1320 struct v4l2_requestbuffers *rb = arg;
1321 u32 i;
1322 int ret;
a6c2ba28 1323
9aeb4b05
MCC
1324 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1325 rb->memory != V4L2_MEMORY_MMAP)
1326 return -EINVAL;
a6c2ba28 1327
9aeb4b05
MCC
1328 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;
1333 }
a6c2ba28 1334
9aeb4b05
MCC
1335 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;
a6c2ba28
AM
1339 }
1340
9aeb4b05
MCC
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 }
a6c2ba28 1346
9aeb4b05 1347 em28xx_empty_framequeues(dev);
a6c2ba28 1348
9aeb4b05
MCC
1349 em28xx_release_buffers(dev);
1350 if (rb->count)
1351 rb->count =
1352 em28xx_request_buffers(dev, rb->count);
a6c2ba28 1353
9aeb4b05 1354 dev->frame_current = NULL;
a6c2ba28 1355
9aeb4b05
MCC
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 }
a6c2ba28 1361 case VIDIOC_QUERYBUF:
9aeb4b05
MCC
1362 {
1363 struct v4l2_buffer *b = arg;
a6c2ba28 1364
9aeb4b05
MCC
1365 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1366 b->index >= dev->num_frames || dev->io != IO_MMAP)
1367 return -EINVAL;
a6c2ba28 1368
9aeb4b05 1369 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
a6c2ba28 1370
9aeb4b05
MCC
1371 if (dev->frame[b->index].vma_use_count) {
1372 b->flags |= V4L2_BUF_FLAG_MAPPED;
a6c2ba28 1373 }
9aeb4b05
MCC
1374 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 }
a6c2ba28 1380 case VIDIOC_QBUF:
9aeb4b05
MCC
1381 {
1382 struct v4l2_buffer *b = arg;
1383 unsigned long lock_flags;
a6c2ba28 1384
9aeb4b05
MCC
1385 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1386 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1387 return -EINVAL;
1388 }
a6c2ba28 1389
9aeb4b05
MCC
1390 if (dev->frame[b->index].state != F_UNUSED) {
1391 return -EAGAIN;
1392 }
1393 dev->frame[b->index].state = F_QUEUED;
a6c2ba28 1394
9aeb4b05
MCC
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);
a6c2ba28 1400
9aeb4b05
MCC
1401 return 0;
1402 }
a6c2ba28 1403 case VIDIOC_DQBUF:
9aeb4b05
MCC
1404 {
1405 struct v4l2_buffer *b = arg;
1406 struct em28xx_frame_t *f;
1407 unsigned long lock_flags;
1408 int ret = 0;
a6c2ba28 1409
9aeb4b05
MCC
1410 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1411 || dev->io != IO_MMAP)
1412 return -EINVAL;
a6c2ba28 1413
9aeb4b05
MCC
1414 if (list_empty(&dev->outqueue)) {
1415 if (dev->stream == STREAM_OFF)
1416 return -EINVAL;
1417 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;
1427 }
a6c2ba28 1428
9aeb4b05
MCC
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);
a6c2ba28 1434
9aeb4b05
MCC
1435 f->state = F_UNUSED;
1436 memcpy(b, &f->buf, sizeof(*b));
a6c2ba28 1437
9aeb4b05
MCC
1438 if (f->vma_use_count)
1439 b->flags |= V4L2_BUF_FLAG_MAPPED;
a6c2ba28 1440
9aeb4b05
MCC
1441 return 0;
1442 }
a6c2ba28 1443 default:
3acf2809
MCC
1444 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1445 em28xx_video_do_ioctl);
a6c2ba28
AM
1446 }
1447 return 0;
1448}
1449
1450/*
3acf2809
MCC
1451 * em28xx_v4l2_ioctl()
1452 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
a6c2ba28 1453 */
3acf2809 1454static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
a6c2ba28
AM
1455 unsigned int cmd, unsigned long arg)
1456{
a6c2ba28 1457 int ret = 0;
3acf2809 1458 struct em28xx *dev = filp->private_data;
a6c2ba28 1459
3593cab5 1460 if (mutex_lock_interruptible(&dev->fileop_lock))
a6c2ba28
AM
1461 return -ERESTARTSYS;
1462
1463 if (dev->state & DEV_DISCONNECTED) {
3acf2809 1464 em28xx_errdev("v4l2 ioctl: device not present\n");
3593cab5 1465 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
1466 return -ENODEV;
1467 }
1468
1469 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 1470 em28xx_errdev
a6c2ba28 1471 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
3593cab5 1472 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
1473 return -EIO;
1474 }
1475
3acf2809 1476 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
a6c2ba28 1477
3593cab5 1478 mutex_unlock(&dev->fileop_lock);
a6c2ba28
AM
1479
1480 return ret;
1481}
1482
3acf2809 1483static struct file_operations em28xx_v4l_fops = {
a6c2ba28 1484 .owner = THIS_MODULE,
3acf2809
MCC
1485 .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,
a6c2ba28 1491 .llseek = no_llseek,
17cbe2e5
MCC
1492 .compat_ioctl = v4l_compat_ioctl32,
1493
a6c2ba28
AM
1494};
1495
1496/******************************** usb interface *****************************************/
1497
1498/*
3acf2809 1499 * em28xx_init_dev()
a6c2ba28
AM
1500 * allocates and inits the device structs, registers i2c bus and v4l device
1501 */
3acf2809 1502static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
a6c2ba28
AM
1503 int minor, int model)
1504{
3acf2809 1505 struct em28xx *dev = *devhandle;
a6c2ba28
AM
1506 int retval = -ENOMEM;
1507 int errCode, i;
1508 unsigned int maxh, maxw;
a6c2ba28 1509
a6c2ba28
AM
1510 dev->udev = udev;
1511 dev->model = model;
3593cab5 1512 mutex_init(&dev->lock);
a6c2ba28
AM
1513 init_waitqueue_head(&dev->open);
1514
3acf2809
MCC
1515 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;
a6c2ba28
AM
1525
1526 if (tuner >= 0)
1527 dev->tuner_type = tuner;
1528 else
3acf2809 1529 dev->tuner_type = em28xx_boards[model].tuner_type;
a6c2ba28 1530
3acf2809 1531 dev->video_inputs = em28xx_boards[model].vchannels;
a6c2ba28
AM
1532
1533 for (i = 0; i < TVNORMS; i++)
3acf2809 1534 if (em28xx_boards[model].norm == tvnorms[i].mode)
a6c2ba28
AM
1535 break;
1536 if (i == TVNORMS)
1537 i = 0;
1538
1539 dev->tvnorm = &tvnorms[i]; /* set default norm */
1540
3acf2809 1541 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
a6c2ba28
AM
1542
1543 maxw = norm_maxw(dev);
1544 maxh = norm_maxh(dev);
1545
1546 /* set default image size */
1547 dev->width = maxw;
1548 dev->height = maxh;
3acf2809 1549 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
a6c2ba28
AM
1550 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
a94e95b4 1568 em28xx_pre_card_setup(dev);
a6c2ba28
AM
1569#ifdef CONFIG_MODULES
1570 /* request some modules */
3acf2809 1571 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
f5762e44 1572 request_module("saa7115");
3acf2809 1573 if (dev->decoder == EM28XX_TVP5150)
a6c2ba28
AM
1574 request_module("tvp5150");
1575 if (dev->has_tuner)
1576 request_module("tuner");
1577 if (dev->tda9887_conf)
1578 request_module("tda9887");
1579#endif
3acf2809 1580 errCode = em28xx_config(dev);
a6c2ba28 1581 if (errCode) {
3acf2809 1582 em28xx_errdev("error configuring device\n");
e5589bef 1583 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1584 kfree(dev);
a6c2ba28
AM
1585 return -ENOMEM;
1586 }
1587
3593cab5 1588 mutex_lock(&dev->lock);
a6c2ba28 1589 /* register i2c bus */
3acf2809 1590 em28xx_i2c_register(dev);
a6c2ba28
AM
1591
1592 /* Do board specific init and eeprom reading */
3acf2809 1593 em28xx_card_setup(dev);
a6c2ba28
AM
1594
1595 /* configure the device */
3acf2809 1596 em28xx_config_i2c(dev);
a6c2ba28 1597
3593cab5 1598 mutex_unlock(&dev->lock);
a6c2ba28 1599
3acf2809 1600 errCode = em28xx_config(dev);
a6c2ba28
AM
1601
1602#ifdef CONFIG_MODULES
1603 if (dev->has_msp34xx)
1604 request_module("msp3400");
1605#endif
1606 /* allocate and fill v4l2 device struct */
1607 dev->vdev = video_device_alloc();
1608 if (NULL == dev->vdev) {
3acf2809 1609 em28xx_errdev("cannot allocate video_device.\n");
e5589bef 1610 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1611 kfree(dev);
e5589bef
MCC
1612 return -ENOMEM;
1613 }
1614
1615 dev->vbi_dev = video_device_alloc();
1616 if (NULL == dev->vbi_dev) {
1617 em28xx_errdev("cannot allocate video_device.\n");
1618 kfree(dev->vdev);
e5589bef 1619 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1620 kfree(dev);
a6c2ba28
AM
1621 return -ENOMEM;
1622 }
1623
e5589bef
MCC
1624 /* Fills VBI device info */
1625 dev->vbi_dev->type = VFL_TYPE_VBI;
1626 dev->vbi_dev->hardware = 0;
1627 dev->vbi_dev->fops = &em28xx_v4l_fops;
1628 dev->vbi_dev->minor = -1;
1629 dev->vbi_dev->dev = &dev->udev->dev;
1630 dev->vbi_dev->release = video_device_release;
1631 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1632 "em28xx",dev->devno,"vbi");
1633
1634 /* Fills CAPTURE device info */
a6c2ba28
AM
1635 dev->vdev->type = VID_TYPE_CAPTURE;
1636 if (dev->has_tuner)
1637 dev->vdev->type |= VID_TYPE_TUNER;
1638 dev->vdev->hardware = 0;
3acf2809 1639 dev->vdev->fops = &em28xx_v4l_fops;
a6c2ba28
AM
1640 dev->vdev->minor = -1;
1641 dev->vdev->dev = &dev->udev->dev;
1642 dev->vdev->release = video_device_release;
e5589bef
MCC
1643 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1644 "em28xx",dev->devno,"video");
1645
3acf2809 1646 list_add_tail(&dev->devlist,&em28xx_devlist);
a6c2ba28
AM
1647
1648 /* register v4l2 device */
3593cab5 1649 mutex_lock(&dev->lock);
e5589bef
MCC
1650 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1651 video_nr[dev->devno]))) {
3acf2809 1652 em28xx_errdev("unable to register video device (error=%i).\n",
a6c2ba28 1653 retval);
3593cab5 1654 mutex_unlock(&dev->lock);
9c75541f 1655 list_del(&dev->devlist);
a6c2ba28 1656 video_device_release(dev->vdev);
e5589bef 1657 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1658 kfree(dev);
a6c2ba28
AM
1659 return -ENODEV;
1660 }
e5589bef
MCC
1661
1662 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1663 vbi_nr[dev->devno]) < 0) {
1664 printk("unable to register vbi device\n");
3593cab5 1665 mutex_unlock(&dev->lock);
e5589bef
MCC
1666 list_del(&dev->devlist);
1667 video_device_release(dev->vbi_dev);
1668 video_device_release(dev->vdev);
e5589bef 1669 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1670 kfree(dev);
e5589bef
MCC
1671 return -ENODEV;
1672 } else {
1673 printk("registered VBI\n");
1674 }
1675
a6c2ba28
AM
1676 if (dev->has_msp34xx) {
1677 /* Send a reset to other chips via gpio */
3acf2809 1678 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
a6c2ba28 1679 udelay(2500);
3acf2809 1680 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
a6c2ba28
AM
1681 udelay(2500);
1682
1683 }
1684 video_mux(dev, 0);
1685
3593cab5 1686 mutex_unlock(&dev->lock);
a6c2ba28 1687
e5589bef
MCC
1688 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1689 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1690 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
a6c2ba28 1691
a6c2ba28
AM
1692 return 0;
1693}
1694
1695/*
3acf2809 1696 * em28xx_usb_probe()
a6c2ba28
AM
1697 * checks for supported devices
1698 */
3acf2809 1699static int em28xx_usb_probe(struct usb_interface *interface,
a6c2ba28
AM
1700 const struct usb_device_id *id)
1701{
1702 const struct usb_endpoint_descriptor *endpoint;
1703 struct usb_device *udev;
9d4d9c05 1704 struct usb_interface *uif;
3acf2809 1705 struct em28xx *dev = NULL;
a6c2ba28 1706 int retval = -ENODEV;
d5e52653 1707 int model,i,nr,ifnum;
a6c2ba28
AM
1708
1709 udev = usb_get_dev(interface_to_usbdev(interface));
d5e52653
MCC
1710 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1711
e5589bef
MCC
1712 /* Check to see next free device and mark as used */
1713 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1714 em28xx_devused|=1<<nr;
a6c2ba28 1715
596d92d5 1716 /* Don't register audio interfaces */
91cad0f2 1717 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
3acf2809 1718 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1719 udev->descriptor.idVendor,udev->descriptor.idProduct,
1720 ifnum,
1721 interface->altsetting[0].desc.bInterfaceClass);
e5589bef
MCC
1722
1723 em28xx_devused&=~(1<<nr);
596d92d5 1724 return -ENODEV;
91cad0f2
MCC
1725 }
1726
3acf2809 1727 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1728 udev->descriptor.idVendor,udev->descriptor.idProduct,
1729 ifnum,
1730 interface->altsetting[0].desc.bInterfaceClass);
596d92d5 1731
d5e52653
MCC
1732 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1733
a6c2ba28
AM
1734 /* check if the the device has the iso in endpoint at the correct place */
1735 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1736 USB_ENDPOINT_XFER_ISOC) {
3acf2809 1737 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
e5589bef 1738 em28xx_devused&=~(1<<nr);
a6c2ba28
AM
1739 return -ENODEV;
1740 }
1741 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
3acf2809 1742 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
e5589bef 1743 em28xx_devused&=~(1<<nr);
a6c2ba28
AM
1744 return -ENODEV;
1745 }
1746
596d92d5 1747 model=id->driver_info;
596d92d5 1748
19478843 1749 if (nr >= EM28XX_MAXBOARDS) {
9d4d9c05 1750 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
e5589bef 1751 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1752 return -ENOMEM;
1753 }
1754
1755 /* allocate memory for our device state and initialize it */
7408187d 1756 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596d92d5 1757 if (dev == NULL) {
3acf2809 1758 em28xx_err(DRIVER_NAME ": out of memory!\n");
e5589bef 1759 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1760 return -ENOMEM;
1761 }
596d92d5 1762
e5589bef
MCC
1763 snprintf(dev->name, 29, "em28xx #%d", nr);
1764 dev->devno=nr;
1765
9d4d9c05
MCC
1766 /* compute alternate max packet sizes */
1767 uif = udev->actconfig->interface[0];
1768
1769 dev->num_alt=uif->num_altsetting;
e5589bef 1770 em28xx_info("Alternate settings: %i\n",dev->num_alt);
9d4d9c05
MCC
1771// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1772 dev->alt_max_pkt_size = kmalloc(32*
1773 dev->num_alt,GFP_KERNEL);
1774 if (dev->alt_max_pkt_size == NULL) {
e5589bef
MCC
1775 em28xx_errdev("out of memory!\n");
1776 em28xx_devused&=~(1<<nr);
9d4d9c05
MCC
1777 return -ENOMEM;
1778 }
1779
1780 for (i = 0; i < dev->num_alt ; i++) {
1781 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1782 wMaxPacketSize);
1783 dev->alt_max_pkt_size[i] =
1784 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
e5589bef 1785 em28xx_info("Alternate setting %i, max size= %i\n",i,
9d4d9c05
MCC
1786 dev->alt_max_pkt_size[i]);
1787 }
1788
3acf2809 1789 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
596d92d5
MCC
1790 model=card[nr];
1791
1792 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
e5589bef 1793 em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
596d92d5
MCC
1794 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1795 "%s: workaround that. Redirect complaints to the vendor of\n"
e5589bef
MCC
1796 "%s: the TV card. Generic type will be used."
1797 "%s: Best regards,\n"
596d92d5
MCC
1798 "%s: -- tux\n",
1799 dev->name,dev->name,dev->name,dev->name,dev->name);
e5589bef 1800 em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
596d92d5 1801 dev->name);
3acf2809 1802 for (i = 0; i < em28xx_bcount; i++) {
e5589bef
MCC
1803 em28xx_errdev(" card=%d -> %s\n", i,
1804 em28xx_boards[i].name);
596d92d5
MCC
1805 }
1806 }
1807
a6c2ba28 1808 /* allocate device struct */
3acf2809 1809 retval = em28xx_init_dev(&dev, udev, nr, model);
a6c2ba28
AM
1810 if (retval)
1811 return retval;
1812
3acf2809 1813 em28xx_info("Found %s\n", em28xx_boards[model].name);
a6c2ba28
AM
1814
1815 /* save our data pointer in this interface device */
1816 usb_set_intfdata(interface, dev);
1817 return 0;
1818}
1819
1820/*
3acf2809 1821 * em28xx_usb_disconnect()
a6c2ba28
AM
1822 * called when the device gets diconencted
1823 * video device will be unregistered on v4l2_close in case it is still open
1824 */
3acf2809 1825static void em28xx_usb_disconnect(struct usb_interface *interface)
a6c2ba28 1826{
3acf2809 1827 struct em28xx *dev = usb_get_intfdata(interface);
a6c2ba28
AM
1828 usb_set_intfdata(interface, NULL);
1829
1830 if (!dev)
1831 return;
1832
3acf2809 1833 down_write(&em28xx_disconnect);
a6c2ba28 1834
3593cab5 1835 mutex_lock(&dev->lock);
a6c2ba28 1836
3acf2809 1837 em28xx_info("disconnecting %s\n", dev->vdev->name);
a6c2ba28
AM
1838
1839 wake_up_interruptible_all(&dev->open);
1840
1841 if (dev->users) {
3acf2809 1842 em28xx_warn
a6c2ba28 1843 ("device /dev/video%d is open! Deregistration and memory "
e5589bef
MCC
1844 "deallocation are deferred on close.\n",
1845 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1846
a6c2ba28 1847 dev->state |= DEV_MISCONFIGURED;
3acf2809 1848 em28xx_uninit_isoc(dev);
a6c2ba28
AM
1849 dev->state |= DEV_DISCONNECTED;
1850 wake_up_interruptible(&dev->wait_frame);
1851 wake_up_interruptible(&dev->wait_stream);
1852 } else {
1853 dev->state |= DEV_DISCONNECTED;
3acf2809 1854 em28xx_release_resources(dev);
a6c2ba28
AM
1855 }
1856
3593cab5 1857 mutex_unlock(&dev->lock);
a6c2ba28 1858
9d4d9c05
MCC
1859 if (!dev->users) {
1860 kfree(dev->alt_max_pkt_size);
a6c2ba28 1861 kfree(dev);
9d4d9c05 1862 }
a6c2ba28 1863
3acf2809 1864 up_write(&em28xx_disconnect);
a6c2ba28
AM
1865}
1866
3acf2809 1867static struct usb_driver em28xx_usb_driver = {
3acf2809
MCC
1868 .name = "em28xx",
1869 .probe = em28xx_usb_probe,
1870 .disconnect = em28xx_usb_disconnect,
1871 .id_table = em28xx_id_table,
a6c2ba28
AM
1872};
1873
3acf2809 1874static int __init em28xx_module_init(void)
a6c2ba28
AM
1875{
1876 int result;
1877
1878 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
3acf2809
MCC
1879 (EM28XX_VERSION_CODE >> 16) & 0xff,
1880 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
a6c2ba28
AM
1881#ifdef SNAPSHOT
1882 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1883 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1884#endif
1885
1886 /* register this driver with the USB subsystem */
3acf2809 1887 result = usb_register(&em28xx_usb_driver);
a6c2ba28 1888 if (result)
3acf2809 1889 em28xx_err(DRIVER_NAME
a6c2ba28
AM
1890 " usb_register failed. Error number %d.\n", result);
1891
1892 return result;
1893}
1894
3acf2809 1895static void __exit em28xx_module_exit(void)
a6c2ba28
AM
1896{
1897 /* deregister this driver with the USB subsystem */
3acf2809 1898 usb_deregister(&em28xx_usb_driver);
a6c2ba28
AM
1899}
1900
3acf2809
MCC
1901module_init(em28xx_module_init);
1902module_exit(em28xx_module_exit);