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