]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/media/video/em28xx/em28xx-video.c
V4L/DVB (6580): Set slave's master before master's attach call.
[mirror_ubuntu-kernels.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>
2e7c6dc3 6 Mauro Carvalho Chehab <mchehab@infradead.org>
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 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>
6d35c8f6 35#include <linux/mm.h>
a6c2ba28 36#include <linux/video_decoder.h>
1e4baed3 37#include <linux/mutex.h>
a6c2ba28 38
f7abcd38 39#include "em28xx.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>, " \
2e7c6dc3 45 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
f7abcd38 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 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 73static unsigned int video_debug = 0;
74module_param(video_debug,int,0644);
75MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
76
e5589bef
MCC
77/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78static unsigned long em28xx_devused;
79
a6c2ba28 80/* supported tv norms */
3acf2809 81static struct em28xx_tvnorm tvnorms[] = {
a6c2ba28 82 {
83 .name = "PAL",
84 .id = V4L2_STD_PAL,
85 .mode = VIDEO_MODE_PAL,
86 }, {
87 .name = "NTSC",
88 .id = V4L2_STD_NTSC,
89 .mode = VIDEO_MODE_NTSC,
90 }, {
91 .name = "SECAM",
92 .id = V4L2_STD_SECAM,
93 .mode = VIDEO_MODE_SECAM,
94 }, {
95 .name = "PAL-M",
96 .id = V4L2_STD_PAL_M,
97 .mode = VIDEO_MODE_PAL,
98 }
99};
100
101#define TVNORMS ARRAY_SIZE(tvnorms)
102
103/* supported controls */
c0477ad9 104/* Common to all boards */
3acf2809 105static struct v4l2_queryctrl em28xx_qctrl[] = {
c0477ad9
MCC
106 {
107 .id = V4L2_CID_AUDIO_VOLUME,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "Volume",
110 .minimum = 0x0,
111 .maximum = 0x1f,
112 .step = 0x1,
113 .default_value = 0x1f,
114 .flags = 0,
115 },{
116 .id = V4L2_CID_AUDIO_MUTE,
117 .type = V4L2_CTRL_TYPE_BOOLEAN,
118 .name = "Mute",
119 .minimum = 0,
120 .maximum = 1,
121 .step = 1,
122 .default_value = 1,
123 .flags = 0,
124 }
125};
126
3acf2809 127static struct usb_driver em28xx_usb_driver;
a6c2ba28 128
a6c2ba28 129
130/********************* v4l2 interface ******************************************/
131
a6c2ba28 132/*
3acf2809 133 * em28xx_config()
a6c2ba28 134 * inits registers with sane defaults
135 */
3acf2809 136static int em28xx_config(struct em28xx *dev)
a6c2ba28 137{
138
139 /* Sets I2C speed to 100 KHz */
2b2c93ac
SS
140 if (!dev->is_em2800)
141 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
a6c2ba28 142
143 /* enable vbi capturing */
e5589bef 144
9475fb1c
MR
145/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
146/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
e5589bef
MCC
147 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
148
3acf2809 149 em28xx_audio_usb_mute(dev, 1);
a6c2ba28 150 dev->mute = 1; /* maybe not the right place... */
151 dev->volume = 0x1f;
3acf2809
MCC
152 em28xx_audio_analog_set(dev);
153 em28xx_audio_analog_setup(dev);
154 em28xx_outfmt_set_yuv422(dev);
155 em28xx_colorlevels_set_default(dev);
156 em28xx_compression_disable(dev);
a6c2ba28 157
158 return 0;
159}
160
161/*
3acf2809 162 * em28xx_config_i2c()
a6c2ba28 163 * configure i2c attached devices
164 */
943a4902 165static void em28xx_config_i2c(struct em28xx *dev)
a6c2ba28 166{
c7c0b34c
HV
167 struct v4l2_routing route;
168
169 route.input = INPUT(dev->ctl_input)->vmux;
170 route.output = 0;
663d1ba2 171 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
c7c0b34c 172 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
f5762e44 173 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
a6c2ba28 174}
175
176/*
3acf2809 177 * em28xx_empty_framequeues()
a6c2ba28 178 * prepare queues for incoming and outgoing frames
179 */
3acf2809 180static void em28xx_empty_framequeues(struct em28xx *dev)
a6c2ba28 181{
182 u32 i;
183
184 INIT_LIST_HEAD(&dev->inqueue);
185 INIT_LIST_HEAD(&dev->outqueue);
186
3acf2809 187 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
a6c2ba28 188 dev->frame[i].state = F_UNUSED;
189 dev->frame[i].buf.bytesused = 0;
190 }
191}
192
eac94356
MCC
193static void video_mux(struct em28xx *dev, int index)
194{
c7c0b34c
HV
195 int ainput;
196 struct v4l2_routing route;
eac94356 197
c7c0b34c
HV
198 route.input = INPUT(index)->vmux;
199 route.output = 0;
eac94356
MCC
200 dev->ctl_input = index;
201 dev->ctl_ainput = INPUT(index)->amux;
202
c7c0b34c 203 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
eac94356 204
c7c0b34c 205 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,route.input,dev->ctl_ainput);
eac94356
MCC
206
207 if (dev->has_msp34xx) {
9bb13a6d
MCC
208 if (dev->i2s_speed)
209 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
2474ed44 210 route.input = dev->ctl_ainput;
07151724 211 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
2474ed44
HV
212 /* Note: this is msp3400 specific */
213 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
eac94356
MCC
214 ainput = EM28XX_AUDIO_SRC_TUNER;
215 em28xx_audio_source(dev, ainput);
216 } else {
217 switch (dev->ctl_ainput) {
9475fb1c
MR
218 case 0:
219 ainput = EM28XX_AUDIO_SRC_TUNER;
220 break;
221 default:
222 ainput = EM28XX_AUDIO_SRC_LINE;
eac94356
MCC
223 }
224 em28xx_audio_source(dev, ainput);
225 }
226}
227
a6c2ba28 228/*
3acf2809 229 * em28xx_v4l2_open()
a6c2ba28 230 * inits the device and starts isoc transfer
231 */
3acf2809 232static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
a6c2ba28 233{
a6c2ba28 234 int minor = iminor(inode);
a6c2ba28 235 int errCode = 0;
3acf2809 236 struct em28xx *h,*dev = NULL;
9c75541f 237
a991f44b 238 list_for_each_entry(h, &em28xx_devlist, devlist) {
9c75541f
MR
239 if (h->vdev->minor == minor) {
240 dev = h;
e5589bef
MCC
241 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
242 }
243 if (h->vbi_dev->minor == minor) {
244 dev = h;
245 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
9c75541f
MR
246 }
247 }
e5589bef
MCC
248 if (NULL == dev)
249 return -ENODEV;
9c75541f 250
e5589bef
MCC
251 em28xx_videodbg("open minor=%d type=%s users=%d\n",
252 minor,v4l2_type_names[dev->type],dev->users);
a6c2ba28 253
5a80415b 254 mutex_lock(&dev->lock);
a6c2ba28 255
256 if (dev->users) {
3acf2809 257 em28xx_warn("this driver can be opened only once\n");
5a80415b 258 mutex_unlock(&dev->lock);
a6c2ba28 259 return -EBUSY;
260 }
261
a6c2ba28 262 spin_lock_init(&dev->queue_lock);
263 init_waitqueue_head(&dev->wait_frame);
264 init_waitqueue_head(&dev->wait_stream);
265
e5589bef
MCC
266 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
267 em28xx_set_alternate(dev);
a6c2ba28 268
e5589bef
MCC
269 dev->width = norm_maxw(dev);
270 dev->height = norm_maxh(dev);
271 dev->frame_size = dev->width * dev->height * 2;
272 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
273 dev->bytesperline = dev->width * 2;
274 dev->hscale = 0;
275 dev->vscale = 0;
a6c2ba28 276
e5589bef
MCC
277 em28xx_capture_start(dev, 1);
278 em28xx_resolution_set(dev);
a6c2ba28 279
e5d4a56d
MR
280 /* device needs to be initialized before isoc transfer */
281 video_mux(dev, 0);
282
e5589bef
MCC
283 /* start the transfer */
284 errCode = em28xx_init_isoc(dev);
285 if (errCode)
286 goto err;
287
e5589bef 288 }
a6c2ba28 289
290 dev->users++;
291 filp->private_data = dev;
292 dev->io = IO_NONE;
293 dev->stream = STREAM_OFF;
294 dev->num_frames = 0;
295
296 /* prepare queues */
3acf2809 297 em28xx_empty_framequeues(dev);
a6c2ba28 298
299 dev->state |= DEV_INITIALIZED;
300
3593cab5
IM
301err:
302 mutex_unlock(&dev->lock);
a6c2ba28 303 return errCode;
304}
305
306/*
3acf2809 307 * em28xx_realease_resources()
a6c2ba28 308 * unregisters the v4l2,i2c and usb devices
309 * called when the device gets disconected or at module unload
310*/
3acf2809 311static void em28xx_release_resources(struct em28xx *dev)
a6c2ba28 312{
a6c2ba28 313
e5589bef
MCC
314 /*FIXME: I2C IR should be disconnected */
315
316 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
317 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
318 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
9c75541f 319 list_del(&dev->devlist);
a6c2ba28 320 video_unregister_device(dev->vdev);
e5589bef 321 video_unregister_device(dev->vbi_dev);
3acf2809 322 em28xx_i2c_unregister(dev);
a6c2ba28 323 usb_put_dev(dev->udev);
e5589bef
MCC
324
325
326 /* Mark device as unused */
327 em28xx_devused&=~(1<<dev->devno);
a6c2ba28 328}
329
330/*
3acf2809 331 * em28xx_v4l2_close()
a6c2ba28 332 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
333 */
3acf2809 334static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
a6c2ba28 335{
a6c2ba28 336 int errCode;
3acf2809 337 struct em28xx *dev=filp->private_data;
a6c2ba28 338
eac94356 339 em28xx_videodbg("users=%d\n", dev->users);
a6c2ba28 340
3593cab5 341 mutex_lock(&dev->lock);
a6c2ba28 342
3acf2809 343 em28xx_uninit_isoc(dev);
a6c2ba28 344
3acf2809 345 em28xx_release_buffers(dev);
a6c2ba28 346
347 /* the device is already disconnect, free the remaining resources */
348 if (dev->state & DEV_DISCONNECTED) {
3acf2809 349 em28xx_release_resources(dev);
3593cab5 350 mutex_unlock(&dev->lock);
a6c2ba28 351 kfree(dev);
352 return 0;
353 }
354
355 /* set alternate 0 */
356 dev->alt = 0;
eac94356 357 em28xx_videodbg("setting alternate 0\n");
a6c2ba28 358 errCode = usb_set_interface(dev->udev, 0, 0);
359 if (errCode < 0) {
3acf2809 360 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
a6c2ba28 361 errCode);
362 }
363
364 dev->users--;
365 wake_up_interruptible_nr(&dev->open, 1);
3593cab5 366 mutex_unlock(&dev->lock);
a6c2ba28 367 return 0;
368}
369
370/*
3acf2809 371 * em28xx_v4l2_read()
a6c2ba28 372 * will allocate buffers when called for the first time
373 */
374static ssize_t
3acf2809 375em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
a6c2ba28 376 loff_t * f_pos)
377{
3acf2809 378 struct em28xx_frame_t *f, *i;
a6c2ba28 379 unsigned long lock_flags;
380 int ret = 0;
3acf2809 381 struct em28xx *dev = filp->private_data;
a6c2ba28 382
5a80415b
SS
383 mutex_lock(&dev->lock);
384
e5589bef
MCC
385 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
386 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
387 }
388 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
389 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
390 em28xx_videodbg("not supported yet! ...\n");
391 if (copy_to_user(buf, "", 1)) {
5a80415b 392 mutex_unlock(&dev->lock);
e5589bef
MCC
393 return -EFAULT;
394 }
5a80415b 395 mutex_unlock(&dev->lock);
e5589bef
MCC
396 return (1);
397 }
398 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
399 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
400 em28xx_videodbg("not supported yet! ...\n");
401 if (copy_to_user(buf, "", 1)) {
5a80415b 402 mutex_unlock(&dev->lock);
e5589bef
MCC
403 return -EFAULT;
404 }
5a80415b 405 mutex_unlock(&dev->lock);
e5589bef
MCC
406 return (1);
407 }
408
a6c2ba28 409 if (dev->state & DEV_DISCONNECTED) {
eac94356 410 em28xx_videodbg("device not present\n");
5a80415b 411 mutex_unlock(&dev->lock);
a6c2ba28 412 return -ENODEV;
413 }
414
415 if (dev->state & DEV_MISCONFIGURED) {
eac94356 416 em28xx_videodbg("device misconfigured; close and open it again\n");
5a80415b 417 mutex_unlock(&dev->lock);
a6c2ba28 418 return -EIO;
419 }
420
421 if (dev->io == IO_MMAP) {
3acf2809 422 em28xx_videodbg ("IO method is set to mmap; close and open"
eac94356 423 " the device again to choose the read method\n");
5a80415b 424 mutex_unlock(&dev->lock);
a6c2ba28 425 return -EINVAL;
426 }
427
428 if (dev->io == IO_NONE) {
3acf2809
MCC
429 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
430 em28xx_errdev("read failed, not enough memory\n");
5a80415b 431 mutex_unlock(&dev->lock);
a6c2ba28 432 return -ENOMEM;
433 }
434 dev->io = IO_READ;
435 dev->stream = STREAM_ON;
3acf2809 436 em28xx_queue_unusedframes(dev);
a6c2ba28 437 }
438
439 if (!count) {
5a80415b 440 mutex_unlock(&dev->lock);
a6c2ba28 441 return 0;
442 }
443
444 if (list_empty(&dev->outqueue)) {
445 if (filp->f_flags & O_NONBLOCK) {
5a80415b 446 mutex_unlock(&dev->lock);
a6c2ba28 447 return -EAGAIN;
448 }
449 ret = wait_event_interruptible
450 (dev->wait_frame,
451 (!list_empty(&dev->outqueue)) ||
452 (dev->state & DEV_DISCONNECTED));
453 if (ret) {
5a80415b 454 mutex_unlock(&dev->lock);
a6c2ba28 455 return ret;
456 }
457 if (dev->state & DEV_DISCONNECTED) {
5a80415b 458 mutex_unlock(&dev->lock);
a6c2ba28 459 return -ENODEV;
460 }
461 }
462
3acf2809 463 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
a6c2ba28 464
465 spin_lock_irqsave(&dev->queue_lock, lock_flags);
466 list_for_each_entry(i, &dev->outqueue, frame)
467 i->state = F_UNUSED;
468 INIT_LIST_HEAD(&dev->outqueue);
469 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
470
3acf2809 471 em28xx_queue_unusedframes(dev);
a6c2ba28 472
473 if (count > f->buf.length)
474 count = f->buf.length;
475
476 if (copy_to_user(buf, f->bufmem, count)) {
5a80415b 477 mutex_unlock(&dev->lock);
a6c2ba28 478 return -EFAULT;
479 }
480 *f_pos += count;
481
5a80415b 482 mutex_unlock(&dev->lock);
a6c2ba28 483
484 return count;
485}
486
487/*
3acf2809 488 * em28xx_v4l2_poll()
a6c2ba28 489 * will allocate buffers when called for the first time
490 */
3acf2809 491static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
a6c2ba28 492{
a6c2ba28 493 unsigned int mask = 0;
3acf2809 494 struct em28xx *dev = filp->private_data;
a6c2ba28 495
5a80415b 496 mutex_lock(&dev->lock);
a6c2ba28 497
498 if (dev->state & DEV_DISCONNECTED) {
eac94356 499 em28xx_videodbg("device not present\n");
a6c2ba28 500 } else if (dev->state & DEV_MISCONFIGURED) {
eac94356 501 em28xx_videodbg("device is misconfigured; close and open it again\n");
a6c2ba28 502 } else {
503 if (dev->io == IO_NONE) {
3acf2809
MCC
504 if (!em28xx_request_buffers
505 (dev, EM28XX_NUM_READ_FRAMES)) {
506 em28xx_warn
a6c2ba28 507 ("poll() failed, not enough memory\n");
508 } else {
509 dev->io = IO_READ;
510 dev->stream = STREAM_ON;
511 }
512 }
513
514 if (dev->io == IO_READ) {
3acf2809 515 em28xx_queue_unusedframes(dev);
a6c2ba28 516 poll_wait(filp, &dev->wait_frame, wait);
517
518 if (!list_empty(&dev->outqueue))
519 mask |= POLLIN | POLLRDNORM;
520
5a80415b 521 mutex_unlock(&dev->lock);
a6c2ba28 522
523 return mask;
524 }
525 }
526
5a80415b 527 mutex_unlock(&dev->lock);
a6c2ba28 528 return POLLERR;
529}
530
531/*
3acf2809 532 * em28xx_vm_open()
a6c2ba28 533 */
3acf2809 534static void em28xx_vm_open(struct vm_area_struct *vma)
a6c2ba28 535{
3acf2809 536 struct em28xx_frame_t *f = vma->vm_private_data;
a6c2ba28 537 f->vma_use_count++;
538}
539
540/*
3acf2809 541 * em28xx_vm_close()
a6c2ba28 542 */
3acf2809 543static void em28xx_vm_close(struct vm_area_struct *vma)
a6c2ba28 544{
545 /* NOTE: buffers are not freed here */
3acf2809 546 struct em28xx_frame_t *f = vma->vm_private_data;
63337dd3
MCC
547
548 if (f->vma_use_count)
549 f->vma_use_count--;
a6c2ba28 550}
551
3acf2809
MCC
552static struct vm_operations_struct em28xx_vm_ops = {
553 .open = em28xx_vm_open,
554 .close = em28xx_vm_close,
a6c2ba28 555};
556
557/*
3acf2809 558 * em28xx_v4l2_mmap()
a6c2ba28 559 */
3acf2809 560static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
a6c2ba28 561{
a6c2ba28 562 unsigned long size = vma->vm_end - vma->vm_start,
3639c861
SS
563 start = vma->vm_start;
564 void *pos;
a6c2ba28 565 u32 i;
9c75541f 566
3acf2809 567 struct em28xx *dev = filp->private_data;
9c75541f 568
5a80415b 569 mutex_lock(&dev->lock);
a6c2ba28 570
571 if (dev->state & DEV_DISCONNECTED) {
eac94356 572 em28xx_videodbg("mmap: device not present\n");
5a80415b 573 mutex_unlock(&dev->lock);
a6c2ba28 574 return -ENODEV;
575 }
576
577 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 578 em28xx_videodbg ("mmap: Device is misconfigured; close and "
eac94356 579 "open it again\n");
5a80415b 580 mutex_unlock(&dev->lock);
a6c2ba28 581 return -EIO;
582 }
583
584 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
585 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
5a80415b 586 mutex_unlock(&dev->lock);
a6c2ba28 587 return -EINVAL;
588 }
589
590 for (i = 0; i < dev->num_frames; i++) {
591 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
592 break;
593 }
594 if (i == dev->num_frames) {
eac94356 595 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
5a80415b 596 mutex_unlock(&dev->lock);
a6c2ba28 597 return -EINVAL;
598 }
599
600 /* VM_IO is eventually going to replace PageReserved altogether */
601 vma->vm_flags |= VM_IO;
602 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
603
3639c861 604 pos = dev->frame[i].bufmem;
a6c2ba28 605 while (size > 0) { /* size is page-aligned */
3639c861
SS
606 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
607 em28xx_videodbg("mmap: vm_insert_page failed\n");
5a80415b 608 mutex_unlock(&dev->lock);
a6c2ba28 609 return -EAGAIN;
610 }
611 start += PAGE_SIZE;
612 pos += PAGE_SIZE;
613 size -= PAGE_SIZE;
614 }
615
3acf2809 616 vma->vm_ops = &em28xx_vm_ops;
a6c2ba28 617 vma->vm_private_data = &dev->frame[i];
618
3acf2809 619 em28xx_vm_open(vma);
5a80415b 620 mutex_unlock(&dev->lock);
a6c2ba28 621 return 0;
622}
623
624/*
3acf2809 625 * em28xx_get_ctrl()
a6c2ba28 626 * return the current saturation, brightness or contrast, mute state
627 */
3acf2809 628static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
a6c2ba28 629{
a6c2ba28 630 switch (ctrl->id) {
631 case V4L2_CID_AUDIO_MUTE:
632 ctrl->value = dev->mute;
633 return 0;
634 case V4L2_CID_AUDIO_VOLUME:
635 ctrl->value = dev->volume;
636 return 0;
c0477ad9
MCC
637 default:
638 return -EINVAL;
639 }
640}
641
a6c2ba28 642/*
3acf2809 643 * em28xx_set_ctrl()
a6c2ba28 644 * mute or set new saturation, brightness or contrast
645 */
3acf2809 646static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
a6c2ba28 647{
648 switch (ctrl->id) {
649 case V4L2_CID_AUDIO_MUTE:
650 if (ctrl->value != dev->mute) {
651 dev->mute = ctrl->value;
3acf2809
MCC
652 em28xx_audio_usb_mute(dev, ctrl->value);
653 return em28xx_audio_analog_set(dev);
a6c2ba28 654 }
655 return 0;
656 case V4L2_CID_AUDIO_VOLUME:
657 dev->volume = ctrl->value;
3acf2809 658 return em28xx_audio_analog_set(dev);
c0477ad9
MCC
659 default:
660 return -EINVAL;
661 }
662}
663
a6c2ba28 664/*
3acf2809 665 * em28xx_stream_interrupt()
a6c2ba28 666 * stops streaming
667 */
3acf2809 668static int em28xx_stream_interrupt(struct em28xx *dev)
a6c2ba28 669{
670 int ret = 0;
671
672 /* stop reading from the device */
673
674 dev->stream = STREAM_INTERRUPT;
675 ret = wait_event_timeout(dev->wait_stream,
676 (dev->stream == STREAM_OFF) ||
677 (dev->state & DEV_DISCONNECTED),
3acf2809 678 EM28XX_URB_TIMEOUT);
a6c2ba28 679 if (dev->state & DEV_DISCONNECTED)
680 return -ENODEV;
681 else if (ret) {
682 dev->state |= DEV_MISCONFIGURED;
3acf2809 683 em28xx_videodbg("device is misconfigured; close and "
e5589bef
MCC
684 "open /dev/video%d again\n",
685 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
a6c2ba28 686 return ret;
687 }
688
689 return 0;
690}
691
3acf2809 692static int em28xx_set_norm(struct em28xx *dev, int width, int height)
a6c2ba28 693{
694 unsigned int hscale, vscale;
695 unsigned int maxh, maxw;
696
697 maxw = norm_maxw(dev);
698 maxh = norm_maxh(dev);
699
700 /* width must even because of the YUYV format */
701 /* height must be even because of interlacing */
702 height &= 0xfffe;
703 width &= 0xfffe;
704
705 if (height < 32)
706 height = 32;
707 if (height > maxh)
708 height = maxh;
709 if (width < 48)
710 width = 48;
711 if (width > maxw)
712 width = maxw;
713
714 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
715 hscale = 0x3fff;
716 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
717
718 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
719 vscale = 0x3fff;
720 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
721
722 /* set new image size */
723 dev->width = width;
724 dev->height = height;
725 dev->frame_size = dev->width * dev->height * 2;
726 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
727 dev->bytesperline = dev->width * 2;
728 dev->hscale = hscale;
729 dev->vscale = vscale;
730
3acf2809 731 em28xx_resolution_set(dev);
a6c2ba28 732
733 return 0;
734}
735
e5589bef
MCC
736static int em28xx_get_fmt(struct em28xx *dev, struct v4l2_format *format)
737{
738 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
739 (format->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
740 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
741 (format->type ==V4L2_BUF_TYPE_VBI_CAPTURE) ?
742 "V4L2_BUF_TYPE_VBI_CAPTURE" :
743 (format->type ==V4L2_CAP_SLICED_VBI_CAPTURE) ?
744 "V4L2_BUF_TYPE_SLICED_VBI_CAPTURE " :
745 "not supported");
746
2d50f847
MCC
747 switch (format->type) {
748 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
749 {
e5589bef
MCC
750 format->fmt.pix.width = dev->width;
751 format->fmt.pix.height = dev->height;
752 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
753 format->fmt.pix.bytesperline = dev->bytesperline;
754 format->fmt.pix.sizeimage = dev->frame_size;
755 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
756 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
757
758 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
759 dev->height);
2d50f847 760 break;
e5589bef
MCC
761 }
762
2d50f847
MCC
763 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
764 {
765 format->fmt.sliced.service_set=0;
766
767 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
768
769 if (format->fmt.sliced.service_set==0)
770 return -EINVAL;
771
772 break;
773 }
774
775 default:
776 return -EINVAL;
777 }
778 return (0);
e5589bef
MCC
779}
780
2d50f847
MCC
781static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
782{
783 u32 i;
784 int ret = 0;
785 int width = format->fmt.pix.width;
786 int height = format->fmt.pix.height;
787 unsigned int hscale, vscale;
788 unsigned int maxh, maxw;
789
790 maxw = norm_maxw(dev);
791 maxh = norm_maxh(dev);
792
793 em28xx_videodbg("%s: type=%s\n",
794 cmd == VIDIOC_TRY_FMT ?
795 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
796 format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
797 "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
798 format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
799 "V4L2_BUF_TYPE_VBI_CAPTURE " :
800 "not supported");
801
802 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
803 em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
804
805 if (format->fmt.sliced.service_set==0)
806 return -EINVAL;
807
808 return 0;
809 }
810
811
812 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
813 return -EINVAL;
814
815 em28xx_videodbg("%s: requested %dx%d\n",
816 cmd == VIDIOC_TRY_FMT ?
817 "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
818 format->fmt.pix.width, format->fmt.pix.height);
819
820 /* FIXME: Move some code away from here */
821 /* width must even because of the YUYV format */
822 /* height must be even because of interlacing */
823 height &= 0xfffe;
824 width &= 0xfffe;
825
826 if (height < 32)
827 height = 32;
828 if (height > maxh)
829 height = maxh;
830 if (width < 48)
831 width = 48;
832 if (width > maxw)
833 width = maxw;
834
835 if(dev->is_em2800){
836 /* the em2800 can only scale down to 50% */
837 if(height % (maxh / 2))
838 height=maxh;
839 if(width % (maxw / 2))
840 width=maxw;
841 /* according to empiatech support */
842 /* the MaxPacketSize is to small to support */
843 /* framesizes larger than 640x480 @ 30 fps */
844 /* or 640x576 @ 25 fps. As this would cut */
845 /* of a part of the image we prefer */
846 /* 360x576 or 360x480 for now */
847 if(width == maxw && height == maxh)
848 width /= 2;
849 }
850
9aeb4b05 851 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
2d50f847 852 hscale = 0x3fff;
2d50f847 853
9aeb4b05
MCC
854 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
855
856 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
2d50f847 857 vscale = 0x3fff;
9aeb4b05
MCC
858
859 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
2d50f847
MCC
860
861 format->fmt.pix.width = width;
862 format->fmt.pix.height = height;
863 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
864 format->fmt.pix.bytesperline = width * 2;
865 format->fmt.pix.sizeimage = width * 2 * height;
866 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
867 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
868
869 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
9aeb4b05
MCC
870 cmd == VIDIOC_TRY_FMT ?
871 "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
872 format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
2d50f847
MCC
873
874 if (cmd == VIDIOC_TRY_FMT)
875 return 0;
876
877 for (i = 0; i < dev->num_frames; i++)
878 if (dev->frame[i].vma_use_count) {
879 em28xx_videodbg("VIDIOC_S_FMT failed. "
880 "Unmap the buffers first.\n");
881 return -EINVAL;
882 }
883
884 /* stop io in case it is already in progress */
885 if (dev->stream == STREAM_ON) {
3a4fa0a2 886 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
2d50f847
MCC
887 if ((ret = em28xx_stream_interrupt(dev)))
888 return ret;
889 }
890
891 em28xx_release_buffers(dev);
892 dev->io = IO_NONE;
893
894 /* set new image size */
895 dev->width = width;
896 dev->height = height;
897 dev->frame_size = dev->width * dev->height * 2;
9aeb4b05 898 dev->field_size = dev->frame_size >> 1;
2d50f847
MCC
899 dev->bytesperline = dev->width * 2;
900 dev->hscale = hscale;
901 dev->vscale = vscale;
902 em28xx_uninit_isoc(dev);
903 em28xx_set_alternate(dev);
904 em28xx_capture_start(dev, 1);
905 em28xx_resolution_set(dev);
906 em28xx_init_isoc(dev);
907
908 return 0;
909}
e5589bef 910
a6c2ba28 911/*
3acf2809 912 * em28xx_v4l2_do_ioctl()
a6c2ba28 913 * This function is _not_ called directly, but from
3acf2809 914 * em28xx_v4l2_ioctl. Userspace
a6c2ba28 915 * copying is done already, arg is a kernel pointer.
916 */
3acf2809
MCC
917static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
918 struct em28xx *dev, unsigned int cmd, void *arg,
a6c2ba28 919 v4l2_kioctl driver_ioctl)
920{
921 int ret;
922
923 switch (cmd) {
924 /* ---------- tv norms ---------- */
925 case VIDIOC_ENUMSTD:
9aeb4b05
MCC
926 {
927 struct v4l2_standard *e = arg;
928 unsigned int i;
a6c2ba28 929
9aeb4b05
MCC
930 i = e->index;
931 if (i >= TVNORMS)
932 return -EINVAL;
933 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
934 tvnorms[e->index].name);
935 e->index = i;
936 if (ret < 0)
937 return ret;
938 return 0;
939 }
a6c2ba28 940 case VIDIOC_G_STD:
9aeb4b05
MCC
941 {
942 v4l2_std_id *id = arg;
a6c2ba28 943
9aeb4b05
MCC
944 *id = dev->tvnorm->id;
945 return 0;
946 }
a6c2ba28 947 case VIDIOC_S_STD:
9aeb4b05
MCC
948 {
949 v4l2_std_id *id = arg;
950 unsigned int i;
a6c2ba28 951
9aeb4b05
MCC
952 for (i = 0; i < TVNORMS; i++)
953 if (*id == tvnorms[i].id)
954 break;
955 if (i == TVNORMS)
a6c2ba28 956 for (i = 0; i < TVNORMS; i++)
9aeb4b05 957 if (*id & tvnorms[i].id)
a6c2ba28 958 break;
9aeb4b05
MCC
959 if (i == TVNORMS)
960 return -EINVAL;
a6c2ba28 961
3593cab5 962 mutex_lock(&dev->lock);
9aeb4b05 963 dev->tvnorm = &tvnorms[i];
a6c2ba28 964
9aeb4b05 965 em28xx_set_norm(dev, dev->width, dev->height);
a6c2ba28 966
9aeb4b05
MCC
967 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
968 &dev->tvnorm->id);
a6c2ba28 969
3593cab5 970 mutex_unlock(&dev->lock);
a6c2ba28 971
9aeb4b05
MCC
972 return 0;
973 }
a6c2ba28 974
9aeb4b05 975 /* ------ input switching ---------- */
a6c2ba28 976 case VIDIOC_ENUMINPUT:
9aeb4b05
MCC
977 {
978 struct v4l2_input *i = arg;
979 unsigned int n;
980 static const char *iname[] = {
981 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
982 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
983 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
984 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
985 [EM28XX_VMUX_SVIDEO] = "S-Video",
986 [EM28XX_VMUX_TELEVISION] = "Television",
987 [EM28XX_VMUX_CABLE] = "Cable TV",
988 [EM28XX_VMUX_DVB] = "DVB",
989 [EM28XX_VMUX_DEBUG] = "for debug only",
990 };
991
992 n = i->index;
993 if (n >= MAX_EM28XX_INPUT)
994 return -EINVAL;
995 if (0 == INPUT(n)->type)
996 return -EINVAL;
997 memset(i, 0, sizeof(*i));
998 i->index = n;
999 i->type = V4L2_INPUT_TYPE_CAMERA;
1000 strcpy(i->name, iname[INPUT(n)->type]);
1001 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1002 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1003 i->type = V4L2_INPUT_TYPE_TUNER;
1004 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1005 i->std |= tvnorms[n].id;
1006 return 0;
1007 }
a6c2ba28 1008 case VIDIOC_G_INPUT:
9aeb4b05
MCC
1009 {
1010 int *i = arg;
1011 *i = dev->ctl_input;
a6c2ba28 1012
9aeb4b05
MCC
1013 return 0;
1014 }
a6c2ba28 1015 case VIDIOC_S_INPUT:
9aeb4b05
MCC
1016 {
1017 int *index = arg;
a6c2ba28 1018
9aeb4b05
MCC
1019 if (*index >= MAX_EM28XX_INPUT)
1020 return -EINVAL;
1021 if (0 == INPUT(*index)->type)
1022 return -EINVAL;
a6c2ba28 1023
3593cab5 1024 mutex_lock(&dev->lock);
9aeb4b05 1025 video_mux(dev, *index);
3593cab5 1026 mutex_unlock(&dev->lock);
a6c2ba28 1027
9aeb4b05
MCC
1028 return 0;
1029 }
a6c2ba28 1030 case VIDIOC_G_AUDIO:
9aeb4b05
MCC
1031 {
1032 struct v4l2_audio *a = arg;
1033 unsigned int index = a->index;
a6c2ba28 1034
9aeb4b05
MCC
1035 if (a->index > 1)
1036 return -EINVAL;
1037 memset(a, 0, sizeof(*a));
1038 index = dev->ctl_ainput;
a6c2ba28 1039
9aeb4b05
MCC
1040 if (index == 0) {
1041 strcpy(a->name, "Television");
1042 } else {
1043 strcpy(a->name, "Line In");
a6c2ba28 1044 }
9aeb4b05
MCC
1045 a->capability = V4L2_AUDCAP_STEREO;
1046 a->index = index;
1047 return 0;
1048 }
a6c2ba28 1049 case VIDIOC_S_AUDIO:
9aeb4b05
MCC
1050 {
1051 struct v4l2_audio *a = arg;
a6c2ba28 1052
9aeb4b05
MCC
1053 if (a->index != dev->ctl_ainput)
1054 return -EINVAL;
1055
1056 return 0;
1057 }
a6c2ba28 1058
9aeb4b05 1059 /* --- controls ---------------------------------------------- */
a6c2ba28 1060 case VIDIOC_QUERYCTRL:
9aeb4b05
MCC
1061 {
1062 struct v4l2_queryctrl *qc = arg;
1063 int i, id=qc->id;
1064
1065 memset(qc,0,sizeof(*qc));
1066 qc->id=id;
1067
1068 if (!dev->has_msp34xx) {
1069 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1070 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1071 memcpy(qc, &(em28xx_qctrl[i]),
1072 sizeof(*qc));
a6c2ba28 1073 return 0;
1074 }
c0477ad9 1075 }
9aeb4b05 1076 }
5a80415b 1077 mutex_lock(&dev->lock);
f5762e44 1078 em28xx_i2c_call_clients(dev,cmd,qc);
5a80415b 1079 mutex_unlock(&dev->lock);
f5762e44
MCC
1080 if (qc->type)
1081 return 0;
1082 else
1083 return -EINVAL;
9aeb4b05 1084 }
a6c2ba28 1085 case VIDIOC_G_CTRL:
9aeb4b05
MCC
1086 {
1087 struct v4l2_control *ctrl = arg;
1088 int retval=-EINVAL;
a6c2ba28 1089
9aeb4b05
MCC
1090 if (!dev->has_msp34xx)
1091 retval=em28xx_get_ctrl(dev, ctrl);
1092 if (retval==-EINVAL) {
5a80415b 1093 mutex_lock(&dev->lock);
f5762e44 1094 em28xx_i2c_call_clients(dev,cmd,arg);
5a80415b 1095 mutex_unlock(&dev->lock);
f5762e44 1096 return 0;
9aeb4b05
MCC
1097 } else return retval;
1098 }
a6c2ba28 1099 case VIDIOC_S_CTRL:
9aeb4b05
MCC
1100 {
1101 struct v4l2_control *ctrl = arg;
1102 u8 i;
5a80415b 1103 mutex_lock(&dev->lock);
9aeb4b05
MCC
1104
1105 if (!dev->has_msp34xx){
1106 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1107 if (ctrl->id == em28xx_qctrl[i].id) {
5a80415b 1108 int retval=-EINVAL;
9aeb4b05
MCC
1109 if (ctrl->value <
1110 em28xx_qctrl[i].minimum
1111 || ctrl->value >
1112 em28xx_qctrl[i].maximum)
1113 return -ERANGE;
5a80415b
SS
1114 retval = em28xx_set_ctrl(dev, ctrl);
1115 mutex_unlock(&dev->lock);
1116 return retval;
c0477ad9
MCC
1117 }
1118 }
9aeb4b05 1119 }
a6c2ba28 1120
f5762e44 1121 em28xx_i2c_call_clients(dev,cmd,arg);
5a80415b 1122 mutex_unlock(&dev->lock);
f5762e44 1123 return 0;
9aeb4b05
MCC
1124 }
1125 /* --- tuner ioctls ------------------------------------------ */
a6c2ba28 1126 case VIDIOC_G_TUNER:
9aeb4b05
MCC
1127 {
1128 struct v4l2_tuner *t = arg;
a6c2ba28 1129
9aeb4b05
MCC
1130 if (0 != t->index)
1131 return -EINVAL;
a6c2ba28 1132
9aeb4b05
MCC
1133 memset(t, 0, sizeof(*t));
1134 strcpy(t->name, "Tuner");
3593cab5 1135 mutex_lock(&dev->lock);
ab4cecf9
HV
1136 /* let clients fill in the remainder of this struct */
1137 em28xx_i2c_call_clients(dev, cmd, t);
3593cab5 1138 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1139 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
1140 t->afc);
1141 return 0;
1142 }
a6c2ba28 1143 case VIDIOC_S_TUNER:
9aeb4b05
MCC
1144 {
1145 struct v4l2_tuner *t = arg;
a6c2ba28 1146
9aeb4b05
MCC
1147 if (0 != t->index)
1148 return -EINVAL;
3593cab5 1149 mutex_lock(&dev->lock);
ab4cecf9
HV
1150 /* let clients handle this */
1151 em28xx_i2c_call_clients(dev, cmd, t);
3593cab5 1152 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1153 return 0;
1154 }
a6c2ba28 1155 case VIDIOC_G_FREQUENCY:
9aeb4b05
MCC
1156 {
1157 struct v4l2_frequency *f = arg;
a6c2ba28 1158
9aeb4b05
MCC
1159 memset(f, 0, sizeof(*f));
1160 f->type = V4L2_TUNER_ANALOG_TV;
1161 f->frequency = dev->ctl_freq;
a6c2ba28 1162
9aeb4b05
MCC
1163 return 0;
1164 }
a6c2ba28 1165 case VIDIOC_S_FREQUENCY:
9aeb4b05
MCC
1166 {
1167 struct v4l2_frequency *f = arg;
a6c2ba28 1168
9aeb4b05
MCC
1169 if (0 != f->tuner)
1170 return -EINVAL;
a6c2ba28 1171
9aeb4b05
MCC
1172 if (V4L2_TUNER_ANALOG_TV != f->type)
1173 return -EINVAL;
a6c2ba28 1174
3593cab5 1175 mutex_lock(&dev->lock);
9aeb4b05
MCC
1176 dev->ctl_freq = f->frequency;
1177 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
3593cab5 1178 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1179 return 0;
1180 }
a6c2ba28 1181 case VIDIOC_CROPCAP:
9aeb4b05
MCC
1182 {
1183 struct v4l2_cropcap *cc = arg;
a6c2ba28 1184
9aeb4b05
MCC
1185 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1186 return -EINVAL;
1187 cc->bounds.left = 0;
1188 cc->bounds.top = 0;
1189 cc->bounds.width = dev->width;
1190 cc->bounds.height = dev->height;
1191 cc->defrect = cc->bounds;
1192 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1193 cc->pixelaspect.denominator = 59;
1194 return 0;
1195 }
a6c2ba28 1196 case VIDIOC_STREAMON:
9aeb4b05
MCC
1197 {
1198 int *type = arg;
a6c2ba28 1199
9aeb4b05
MCC
1200 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1201 || dev->io != IO_MMAP)
1202 return -EINVAL;
a6c2ba28 1203
9aeb4b05
MCC
1204 if (list_empty(&dev->inqueue))
1205 return -EINVAL;
a6c2ba28 1206
9aeb4b05 1207 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
a6c2ba28 1208
9aeb4b05 1209 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
a6c2ba28 1210
9aeb4b05
MCC
1211 return 0;
1212 }
a6c2ba28 1213 case VIDIOC_STREAMOFF:
9aeb4b05
MCC
1214 {
1215 int *type = arg;
1216 int ret;
a6c2ba28 1217
9aeb4b05
MCC
1218 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1219 || dev->io != IO_MMAP)
1220 return -EINVAL;
a6c2ba28 1221
5a80415b 1222 mutex_lock(&dev->lock);
9aeb4b05
MCC
1223 if (dev->stream == STREAM_ON) {
1224 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
5a80415b
SS
1225 if ((ret = em28xx_stream_interrupt(dev))){
1226 mutex_unlock(&dev->lock);
9aeb4b05 1227 return ret;
5a80415b 1228 }
a6c2ba28 1229 }
9aeb4b05 1230 em28xx_empty_framequeues(dev);
5a80415b 1231 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1232
1233 return 0;
1234 }
a6c2ba28 1235 default:
1236 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1237 driver_ioctl);
1238 }
1239 return 0;
1240}
1241
1242/*
3acf2809 1243 * em28xx_v4l2_do_ioctl()
a6c2ba28 1244 * This function is _not_ called directly, but from
3acf2809 1245 * em28xx_v4l2_ioctl. Userspace
a6c2ba28 1246 * copying is done already, arg is a kernel pointer.
1247 */
3acf2809 1248static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
a6c2ba28 1249 unsigned int cmd, void *arg)
1250{
3acf2809 1251 struct em28xx *dev = filp->private_data;
a6c2ba28 1252
1253 if (!dev)
1254 return -ENODEV;
1255
1256 if (video_debug > 1)
5e453dc7 1257 v4l_print_ioctl(dev->name,cmd);
a6c2ba28 1258
1259 switch (cmd) {
1260
1261 /* --- capabilities ------------------------------------------ */
1262 case VIDIOC_QUERYCAP:
1263 {
9aeb4b05
MCC
1264 struct v4l2_capability *cap = arg;
1265
1266 memset(cap, 0, sizeof(*cap));
1267 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1268 strlcpy(cap->card, em28xx_boards[dev->model].name,
1269 sizeof(cap->card));
1270 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1271 sizeof(cap->bus_info));
1272 cap->version = EM28XX_VERSION_CODE;
1273 cap->capabilities =
1274 V4L2_CAP_SLICED_VBI_CAPTURE |
1275 V4L2_CAP_VIDEO_CAPTURE |
1276 V4L2_CAP_AUDIO |
1277 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1278 if (dev->has_tuner)
1279 cap->capabilities |= V4L2_CAP_TUNER;
1280 return 0;
1281 }
1282 /* --- capture ioctls ---------------------------------------- */
a6c2ba28 1283 case VIDIOC_ENUM_FMT:
9aeb4b05
MCC
1284 {
1285 struct v4l2_fmtdesc *fmtd = arg;
a6c2ba28 1286
9aeb4b05
MCC
1287 if (fmtd->index != 0)
1288 return -EINVAL;
1289 memset(fmtd, 0, sizeof(*fmtd));
1290 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1291 strcpy(fmtd->description, "Packed YUY2");
1292 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1293 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1294 return 0;
1295 }
a6c2ba28 1296 case VIDIOC_G_FMT:
5a80415b
SS
1297 {
1298 int retval;
1299 mutex_lock(&dev->lock);
1300 retval = em28xx_get_fmt(dev, (struct v4l2_format *) arg);
1301 mutex_unlock(&dev->lock);
1302 return retval;
a6c2ba28 1303
5a80415b 1304 }
a6c2ba28 1305 case VIDIOC_TRY_FMT:
1306 case VIDIOC_S_FMT:
5a80415b
SS
1307 {
1308 int retval;
1309 mutex_lock(&dev->lock);
1310 retval = em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
1311 mutex_unlock(&dev->lock);
1312 return retval;
1313 }
a6c2ba28 1314
a6c2ba28 1315 case VIDIOC_REQBUFS:
9aeb4b05
MCC
1316 {
1317 struct v4l2_requestbuffers *rb = arg;
1318 u32 i;
1319 int ret;
a6c2ba28 1320
9aeb4b05
MCC
1321 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1322 rb->memory != V4L2_MEMORY_MMAP)
1323 return -EINVAL;
a6c2ba28 1324
9aeb4b05
MCC
1325 if (dev->io == IO_READ) {
1326 em28xx_videodbg ("method is set to read;"
1327 " close and open the device again to"
1328 " choose the mmap I/O method\n");
1329 return -EINVAL;
1330 }
a6c2ba28 1331
9aeb4b05
MCC
1332 for (i = 0; i < dev->num_frames; i++)
1333 if (dev->frame[i].vma_use_count) {
1334 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
1335 return -EINVAL;
a6c2ba28 1336 }
1337
5a80415b 1338 mutex_lock(&dev->lock);
9aeb4b05
MCC
1339 if (dev->stream == STREAM_ON) {
1340 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
5a80415b
SS
1341 if ((ret = em28xx_stream_interrupt(dev))){
1342 mutex_unlock(&dev->lock);
9aeb4b05 1343 return ret;
5a80415b 1344 }
9aeb4b05 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;
5a80415b 1359 mutex_unlock(&dev->lock);
9aeb4b05
MCC
1360 return 0;
1361 }
a6c2ba28 1362 case VIDIOC_QUERYBUF:
9aeb4b05
MCC
1363 {
1364 struct v4l2_buffer *b = arg;
a6c2ba28 1365
9aeb4b05
MCC
1366 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1367 b->index >= dev->num_frames || dev->io != IO_MMAP)
1368 return -EINVAL;
a6c2ba28 1369
9aeb4b05 1370 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
a6c2ba28 1371
9aeb4b05
MCC
1372 if (dev->frame[b->index].vma_use_count) {
1373 b->flags |= V4L2_BUF_FLAG_MAPPED;
a6c2ba28 1374 }
9aeb4b05
MCC
1375 if (dev->frame[b->index].state == F_DONE)
1376 b->flags |= V4L2_BUF_FLAG_DONE;
1377 else if (dev->frame[b->index].state != F_UNUSED)
1378 b->flags |= V4L2_BUF_FLAG_QUEUED;
1379 return 0;
1380 }
a6c2ba28 1381 case VIDIOC_QBUF:
9aeb4b05
MCC
1382 {
1383 struct v4l2_buffer *b = arg;
1384 unsigned long lock_flags;
a6c2ba28 1385
9aeb4b05
MCC
1386 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1387 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1388 return -EINVAL;
1389 }
a6c2ba28 1390
9aeb4b05
MCC
1391 if (dev->frame[b->index].state != F_UNUSED) {
1392 return -EAGAIN;
1393 }
1394 dev->frame[b->index].state = F_QUEUED;
a6c2ba28 1395
9aeb4b05
MCC
1396 /* add frame to fifo */
1397 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1398 list_add_tail(&dev->frame[b->index].frame,
1399 &dev->inqueue);
1400 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
a6c2ba28 1401
9aeb4b05
MCC
1402 return 0;
1403 }
a6c2ba28 1404 case VIDIOC_DQBUF:
9aeb4b05
MCC
1405 {
1406 struct v4l2_buffer *b = arg;
1407 struct em28xx_frame_t *f;
1408 unsigned long lock_flags;
1409 int ret = 0;
a6c2ba28 1410
9aeb4b05
MCC
1411 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1412 || dev->io != IO_MMAP)
1413 return -EINVAL;
a6c2ba28 1414
9aeb4b05
MCC
1415 if (list_empty(&dev->outqueue)) {
1416 if (dev->stream == STREAM_OFF)
1417 return -EINVAL;
1418 if (filp->f_flags & O_NONBLOCK)
1419 return -EAGAIN;
1420 ret = wait_event_interruptible
1421 (dev->wait_frame,
1422 (!list_empty(&dev->outqueue)) ||
1423 (dev->state & DEV_DISCONNECTED));
1424 if (ret)
1425 return ret;
1426 if (dev->state & DEV_DISCONNECTED)
1427 return -ENODEV;
1428 }
a6c2ba28 1429
9aeb4b05
MCC
1430 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1431 f = list_entry(dev->outqueue.next,
1432 struct em28xx_frame_t, frame);
1433 list_del(dev->outqueue.next);
1434 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
a6c2ba28 1435
9aeb4b05
MCC
1436 f->state = F_UNUSED;
1437 memcpy(b, &f->buf, sizeof(*b));
a6c2ba28 1438
9aeb4b05
MCC
1439 if (f->vma_use_count)
1440 b->flags |= V4L2_BUF_FLAG_MAPPED;
a6c2ba28 1441
9aeb4b05
MCC
1442 return 0;
1443 }
a6c2ba28 1444 default:
3acf2809
MCC
1445 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1446 em28xx_video_do_ioctl);
a6c2ba28 1447 }
1448 return 0;
1449}
1450
1451/*
3acf2809
MCC
1452 * em28xx_v4l2_ioctl()
1453 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
a6c2ba28 1454 */
3acf2809 1455static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
a6c2ba28 1456 unsigned int cmd, unsigned long arg)
1457{
a6c2ba28 1458 int ret = 0;
3acf2809 1459 struct em28xx *dev = filp->private_data;
a6c2ba28 1460
a6c2ba28 1461 if (dev->state & DEV_DISCONNECTED) {
3acf2809 1462 em28xx_errdev("v4l2 ioctl: device not present\n");
a6c2ba28 1463 return -ENODEV;
1464 }
1465
1466 if (dev->state & DEV_MISCONFIGURED) {
3acf2809 1467 em28xx_errdev
a6c2ba28 1468 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
a6c2ba28 1469 return -EIO;
1470 }
1471
3acf2809 1472 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
a6c2ba28 1473
a6c2ba28 1474 return ret;
1475}
1476
fa027c2a 1477static const struct file_operations em28xx_v4l_fops = {
a6c2ba28 1478 .owner = THIS_MODULE,
3acf2809
MCC
1479 .open = em28xx_v4l2_open,
1480 .release = em28xx_v4l2_close,
1481 .ioctl = em28xx_v4l2_ioctl,
1482 .read = em28xx_v4l2_read,
1483 .poll = em28xx_v4l2_poll,
1484 .mmap = em28xx_v4l2_mmap,
a6c2ba28 1485 .llseek = no_llseek,
17cbe2e5
MCC
1486 .compat_ioctl = v4l_compat_ioctl32,
1487
a6c2ba28 1488};
1489
1490/******************************** usb interface *****************************************/
1491
1492/*
3acf2809 1493 * em28xx_init_dev()
a6c2ba28 1494 * allocates and inits the device structs, registers i2c bus and v4l device
1495 */
3acf2809 1496static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
03910cc3 1497 int minor)
a6c2ba28 1498{
3acf2809 1499 struct em28xx *dev = *devhandle;
a6c2ba28 1500 int retval = -ENOMEM;
1501 int errCode, i;
1502 unsigned int maxh, maxw;
a6c2ba28 1503
a6c2ba28 1504 dev->udev = udev;
3593cab5 1505 mutex_init(&dev->lock);
a6c2ba28 1506 init_waitqueue_head(&dev->open);
1507
3acf2809
MCC
1508 dev->em28xx_write_regs = em28xx_write_regs;
1509 dev->em28xx_read_reg = em28xx_read_reg;
1510 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1511 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1512 dev->em28xx_read_reg_req = em28xx_read_reg_req;
fad7b958 1513 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
a6c2ba28 1514
1515 /* setup video picture settings for saa7113h */
1516 memset(&dev->vpic, 0, sizeof(dev->vpic));
1517 dev->vpic.colour = 128 << 8;
1518 dev->vpic.hue = 128 << 8;
1519 dev->vpic.brightness = 128 << 8;
1520 dev->vpic.contrast = 192 << 8;
1521 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1522 dev->vpic.depth = 16;
1523 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1524
a94e95b4 1525 em28xx_pre_card_setup(dev);
03910cc3 1526
3acf2809 1527 errCode = em28xx_config(dev);
a6c2ba28 1528 if (errCode) {
3acf2809 1529 em28xx_errdev("error configuring device\n");
03910cc3 1530 em28xx_devused &= ~(1<<dev->devno);
7c908fbb 1531 kfree(dev);
a6c2ba28 1532 return -ENOMEM;
1533 }
1534
a6c2ba28 1535 /* register i2c bus */
3acf2809 1536 em28xx_i2c_register(dev);
a6c2ba28 1537
1538 /* Do board specific init and eeprom reading */
3acf2809 1539 em28xx_card_setup(dev);
a6c2ba28 1540
1541 /* configure the device */
3acf2809 1542 em28xx_config_i2c(dev);
a6c2ba28 1543
03910cc3
MCC
1544 for (i = 0; i < TVNORMS; i++)
1545 if (em28xx_boards[dev->model].norm == tvnorms[i].mode)
1546 break;
1547 if (i == TVNORMS)
1548 i = 0;
1549
1550 dev->tvnorm = &tvnorms[i]; /* set default norm */
1551
1552 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
1553
1554 maxw = norm_maxw(dev);
1555 maxh = norm_maxh(dev);
1556
1557 /* set default image size */
1558 dev->width = maxw;
1559 dev->height = maxh;
1560 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1561 dev->field_size = dev->width * dev->height;
1562 dev->frame_size =
1563 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1564 dev->bytesperline = dev->width * 2;
1565 dev->hscale = 0;
1566 dev->vscale = 0;
1567 dev->ctl_input = 2;
1568
3acf2809 1569 errCode = em28xx_config(dev);
a6c2ba28 1570
a6c2ba28 1571 /* allocate and fill v4l2 device struct */
1572 dev->vdev = video_device_alloc();
1573 if (NULL == dev->vdev) {
3acf2809 1574 em28xx_errdev("cannot allocate video_device.\n");
e5589bef 1575 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1576 kfree(dev);
e5589bef
MCC
1577 return -ENOMEM;
1578 }
1579
1580 dev->vbi_dev = video_device_alloc();
1581 if (NULL == dev->vbi_dev) {
1582 em28xx_errdev("cannot allocate video_device.\n");
1583 kfree(dev->vdev);
e5589bef 1584 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1585 kfree(dev);
a6c2ba28 1586 return -ENOMEM;
1587 }
1588
e5589bef
MCC
1589 /* Fills VBI device info */
1590 dev->vbi_dev->type = VFL_TYPE_VBI;
e5589bef
MCC
1591 dev->vbi_dev->fops = &em28xx_v4l_fops;
1592 dev->vbi_dev->minor = -1;
1593 dev->vbi_dev->dev = &dev->udev->dev;
1594 dev->vbi_dev->release = video_device_release;
1595 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1596 "em28xx",dev->devno,"vbi");
1597
1598 /* Fills CAPTURE device info */
a6c2ba28 1599 dev->vdev->type = VID_TYPE_CAPTURE;
1600 if (dev->has_tuner)
1601 dev->vdev->type |= VID_TYPE_TUNER;
3acf2809 1602 dev->vdev->fops = &em28xx_v4l_fops;
a6c2ba28 1603 dev->vdev->minor = -1;
1604 dev->vdev->dev = &dev->udev->dev;
1605 dev->vdev->release = video_device_release;
e5589bef
MCC
1606 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
1607 "em28xx",dev->devno,"video");
1608
3acf2809 1609 list_add_tail(&dev->devlist,&em28xx_devlist);
a6c2ba28 1610
5a80415b
SS
1611
1612 if (dev->has_msp34xx) {
1613 /* Send a reset to other chips via gpio */
1614 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1615 msleep(3);
1616 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1617 msleep(3);
1618
1619 }
1620 video_mux(dev, 0);
1621
a6c2ba28 1622 /* register v4l2 device */
e5589bef
MCC
1623 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1624 video_nr[dev->devno]))) {
3acf2809 1625 em28xx_errdev("unable to register video device (error=%i).\n",
a6c2ba28 1626 retval);
3593cab5 1627 mutex_unlock(&dev->lock);
9c75541f 1628 list_del(&dev->devlist);
a6c2ba28 1629 video_device_release(dev->vdev);
e5589bef 1630 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1631 kfree(dev);
a6c2ba28 1632 return -ENODEV;
1633 }
e5589bef
MCC
1634
1635 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1636 vbi_nr[dev->devno]) < 0) {
1637 printk("unable to register vbi device\n");
3593cab5 1638 mutex_unlock(&dev->lock);
e5589bef
MCC
1639 list_del(&dev->devlist);
1640 video_device_release(dev->vbi_dev);
1641 video_device_release(dev->vdev);
e5589bef 1642 em28xx_devused&=~(1<<dev->devno);
7c908fbb 1643 kfree(dev);
e5589bef
MCC
1644 return -ENODEV;
1645 } else {
1646 printk("registered VBI\n");
1647 }
1648
e5589bef
MCC
1649 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1650 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1651 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
a6c2ba28 1652
a6c2ba28 1653 return 0;
1654}
1655
1656/*
3acf2809 1657 * em28xx_usb_probe()
a6c2ba28 1658 * checks for supported devices
1659 */
3acf2809 1660static int em28xx_usb_probe(struct usb_interface *interface,
a6c2ba28 1661 const struct usb_device_id *id)
1662{
1663 const struct usb_endpoint_descriptor *endpoint;
1664 struct usb_device *udev;
9d4d9c05 1665 struct usb_interface *uif;
3acf2809 1666 struct em28xx *dev = NULL;
a6c2ba28 1667 int retval = -ENODEV;
03910cc3 1668 int i, nr, ifnum;
a6c2ba28 1669
1670 udev = usb_get_dev(interface_to_usbdev(interface));
d5e52653
MCC
1671 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1672
e5589bef
MCC
1673 /* Check to see next free device and mark as used */
1674 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1675 em28xx_devused|=1<<nr;
a6c2ba28 1676
596d92d5 1677 /* Don't register audio interfaces */
91cad0f2 1678 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
3acf2809 1679 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1680 udev->descriptor.idVendor,udev->descriptor.idProduct,
1681 ifnum,
1682 interface->altsetting[0].desc.bInterfaceClass);
e5589bef
MCC
1683
1684 em28xx_devused&=~(1<<nr);
596d92d5 1685 return -ENODEV;
91cad0f2
MCC
1686 }
1687
3acf2809 1688 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
91cad0f2
MCC
1689 udev->descriptor.idVendor,udev->descriptor.idProduct,
1690 ifnum,
1691 interface->altsetting[0].desc.bInterfaceClass);
596d92d5 1692
d5e52653
MCC
1693 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1694
59c51591 1695 /* check if the device has the iso in endpoint at the correct place */
a6c2ba28 1696 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1697 USB_ENDPOINT_XFER_ISOC) {
3acf2809 1698 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
e5589bef 1699 em28xx_devused&=~(1<<nr);
a6c2ba28 1700 return -ENODEV;
1701 }
1702 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
3acf2809 1703 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
e5589bef 1704 em28xx_devused&=~(1<<nr);
a6c2ba28 1705 return -ENODEV;
1706 }
1707
19478843 1708 if (nr >= EM28XX_MAXBOARDS) {
9d4d9c05 1709 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
e5589bef 1710 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1711 return -ENOMEM;
1712 }
1713
1714 /* allocate memory for our device state and initialize it */
7408187d 1715 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596d92d5 1716 if (dev == NULL) {
3acf2809 1717 em28xx_err(DRIVER_NAME ": out of memory!\n");
e5589bef 1718 em28xx_devused&=~(1<<nr);
596d92d5
MCC
1719 return -ENOMEM;
1720 }
596d92d5 1721
e5589bef 1722 snprintf(dev->name, 29, "em28xx #%d", nr);
03910cc3
MCC
1723 dev->devno = nr;
1724 dev->model = id->driver_info;
e5589bef 1725
9d4d9c05
MCC
1726 /* compute alternate max packet sizes */
1727 uif = udev->actconfig->interface[0];
1728
1729 dev->num_alt=uif->num_altsetting;
e5589bef 1730 em28xx_info("Alternate settings: %i\n",dev->num_alt);
9d4d9c05
MCC
1731// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1732 dev->alt_max_pkt_size = kmalloc(32*
1733 dev->num_alt,GFP_KERNEL);
1734 if (dev->alt_max_pkt_size == NULL) {
e5589bef
MCC
1735 em28xx_errdev("out of memory!\n");
1736 em28xx_devused&=~(1<<nr);
1207cf84 1737 kfree(dev);
9d4d9c05
MCC
1738 return -ENOMEM;
1739 }
1740
1741 for (i = 0; i < dev->num_alt ; i++) {
1742 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1743 wMaxPacketSize);
1744 dev->alt_max_pkt_size[i] =
1745 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
e5589bef 1746 em28xx_info("Alternate setting %i, max size= %i\n",i,
9d4d9c05
MCC
1747 dev->alt_max_pkt_size[i]);
1748 }
1749
3acf2809 1750 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
03910cc3 1751 dev->model = card[nr];
596d92d5 1752
a6c2ba28 1753 /* allocate device struct */
03910cc3 1754 retval = em28xx_init_dev(&dev, udev, nr);
a6c2ba28 1755 if (retval)
1756 return retval;
1757
03910cc3 1758 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
a6c2ba28 1759
1760 /* save our data pointer in this interface device */
1761 usb_set_intfdata(interface, dev);
1762 return 0;
1763}
1764
1765/*
3acf2809 1766 * em28xx_usb_disconnect()
a6c2ba28 1767 * called when the device gets diconencted
1768 * video device will be unregistered on v4l2_close in case it is still open
1769 */
3acf2809 1770static void em28xx_usb_disconnect(struct usb_interface *interface)
a6c2ba28 1771{
5a80415b
SS
1772 struct em28xx *dev;
1773
1774 dev = usb_get_intfdata(interface);
a6c2ba28 1775 usb_set_intfdata(interface, NULL);
1776
1777 if (!dev)
1778 return;
1779
5a80415b 1780 em28xx_info("disconnecting %s\n", dev->vdev->name);
a6c2ba28 1781
5a80415b 1782 /* wait until all current v4l2 io is finished then deallocate resources */
3593cab5 1783 mutex_lock(&dev->lock);
a6c2ba28 1784
a6c2ba28 1785 wake_up_interruptible_all(&dev->open);
1786
1787 if (dev->users) {
3acf2809 1788 em28xx_warn
a6c2ba28 1789 ("device /dev/video%d is open! Deregistration and memory "
e5589bef
MCC
1790 "deallocation are deferred on close.\n",
1791 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1792
a6c2ba28 1793 dev->state |= DEV_MISCONFIGURED;
3acf2809 1794 em28xx_uninit_isoc(dev);
a6c2ba28 1795 dev->state |= DEV_DISCONNECTED;
1796 wake_up_interruptible(&dev->wait_frame);
1797 wake_up_interruptible(&dev->wait_stream);
1798 } else {
1799 dev->state |= DEV_DISCONNECTED;
3acf2809 1800 em28xx_release_resources(dev);
a6c2ba28 1801 }
1802
5a80415b 1803
3593cab5 1804 mutex_unlock(&dev->lock);
a6c2ba28 1805
9d4d9c05
MCC
1806 if (!dev->users) {
1807 kfree(dev->alt_max_pkt_size);
a6c2ba28 1808 kfree(dev);
9d4d9c05 1809 }
a6c2ba28 1810
a6c2ba28 1811}
1812
3acf2809 1813static struct usb_driver em28xx_usb_driver = {
3acf2809
MCC
1814 .name = "em28xx",
1815 .probe = em28xx_usb_probe,
1816 .disconnect = em28xx_usb_disconnect,
1817 .id_table = em28xx_id_table,
a6c2ba28 1818};
1819
3acf2809 1820static int __init em28xx_module_init(void)
a6c2ba28 1821{
1822 int result;
1823
1824 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
3acf2809
MCC
1825 (EM28XX_VERSION_CODE >> 16) & 0xff,
1826 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
a6c2ba28 1827#ifdef SNAPSHOT
1828 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1829 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1830#endif
1831
1832 /* register this driver with the USB subsystem */
3acf2809 1833 result = usb_register(&em28xx_usb_driver);
a6c2ba28 1834 if (result)
3acf2809 1835 em28xx_err(DRIVER_NAME
a6c2ba28 1836 " usb_register failed. Error number %d.\n", result);
1837
1838 return result;
1839}
1840
3acf2809 1841static void __exit em28xx_module_exit(void)
a6c2ba28 1842{
1843 /* deregister this driver with the USB subsystem */
3acf2809 1844 usb_deregister(&em28xx_usb_driver);
a6c2ba28 1845}
1846
3acf2809
MCC
1847module_init(em28xx_module_init);
1848module_exit(em28xx_module_exit);