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