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