pvrusb2-v4l2.c 29.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *
 *  $Id$
 *
 *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
 *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/kernel.h>
24
#include <linux/version.h>
25 26 27 28 29 30 31
#include "pvrusb2-context.h"
#include "pvrusb2-hdw.h"
#include "pvrusb2.h"
#include "pvrusb2-debug.h"
#include "pvrusb2-v4l2.h"
#include "pvrusb2-ioread.h"
#include <linux/videodev2.h>
32
#include <media/v4l2-dev.h>
33 34 35 36 37 38 39
#include <media/v4l2-common.h>

struct pvr2_v4l2_dev;
struct pvr2_v4l2_fh;
struct pvr2_v4l2;

struct pvr2_v4l2_dev {
40
	struct video_device devbase; /* MUST be first! */
41 42
	struct pvr2_v4l2 *v4lp;
	struct pvr2_context_stream *stream;
43 44 45 46
	/* Information about this device: */
	enum pvr2_config config; /* Expected stream format */
	int v4l_type; /* V4L defined type for this device node */
	enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
47 48 49 50 51 52 53 54 55 56 57 58 59
};

struct pvr2_v4l2_fh {
	struct pvr2_channel channel;
	struct pvr2_v4l2_dev *dev_info;
	enum v4l2_priority prio;
	struct pvr2_ioread *rhp;
	struct file *file;
	struct pvr2_v4l2 *vhead;
	struct pvr2_v4l2_fh *vnext;
	struct pvr2_v4l2_fh *vprev;
	wait_queue_head_t wait_data;
	int fw_mode_flag;
60
	int prev_input_val;
61 62 63 64 65 66 67 68 69
};

struct pvr2_v4l2 {
	struct pvr2_channel channel;
	struct pvr2_v4l2_fh *vfirst;
	struct pvr2_v4l2_fh *vlast;

	struct v4l2_prio_state prio;

70 71 72 73 74
	/* streams - Note that these must be separately, individually,
	 * allocated pointers.  This is because the v4l core is going to
	 * manage their deletion - separately, individually...  */
	struct pvr2_v4l2_dev *dev_video;
	struct pvr2_v4l2_dev *dev_radio;
75 76 77 78
};

static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
module_param_array(video_nr, int, NULL, 0444);
79 80 81 82 83 84 85
MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
module_param_array(vbi_nr, int, NULL, 0444);
MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
86

87
static struct v4l2_capability pvr_capability ={
88 89 90 91 92
	.driver         = "pvrusb2",
	.card           = "Hauppauge WinTV pvr-usb2",
	.bus_info       = "usb",
	.version        = KERNEL_VERSION(0,8,0),
	.capabilities   = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
93
			   V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
94 95 96 97
			   V4L2_CAP_READWRITE),
	.reserved       = {0,0,0,0}
};

98
static struct v4l2_fmtdesc pvr_fmtdesc [] = {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	{
		.index          = 0,
		.type           = V4L2_BUF_TYPE_VIDEO_CAPTURE,
		.flags          = V4L2_FMT_FLAG_COMPRESSED,
		.description    = "MPEG1/2",
		// This should really be V4L2_PIX_FMT_MPEG, but xawtv
		// breaks when I do that.
		.pixelformat    = 0, // V4L2_PIX_FMT_MPEG,
		.reserved       = { 0, 0, 0, 0 }
	}
};

#define PVR_FORMAT_PIX  0
#define PVR_FORMAT_VBI  1

114
static struct v4l2_format pvr_format [] = {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	[PVR_FORMAT_PIX] = {
		.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
		.fmt    = {
			.pix        = {
				.width          = 720,
				.height             = 576,
				// This should really be V4L2_PIX_FMT_MPEG,
				// but xawtv breaks when I do that.
				.pixelformat    = 0, // V4L2_PIX_FMT_MPEG,
				.field          = V4L2_FIELD_INTERLACED,
				.bytesperline   = 0,  // doesn't make sense
						      // here
				//FIXME : Don't know what to put here...
				.sizeimage          = (32*1024),
				.colorspace     = 0, // doesn't make sense here
				.priv           = 0
			}
		}
	},
	[PVR_FORMAT_VBI] = {
		.type   = V4L2_BUF_TYPE_VBI_CAPTURE,
		.fmt    = {
			.vbi        = {
				.sampling_rate = 27000000,
				.offset = 248,
				.samples_per_line = 1443,
				.sample_format = V4L2_PIX_FMT_GREY,
				.start = { 0, 0 },
				.count = { 0, 0 },
				.flags = 0,
				.reserved = { 0, 0 }
			}
		}
	}
};

151 152 153 154 155 156 157 158 159 160 161 162

static const char *get_v4l_name(int v4l_type)
{
	switch (v4l_type) {
	case VFL_TYPE_GRABBER: return "video";
	case VFL_TYPE_RADIO: return "radio";
	case VFL_TYPE_VBI: return "vbi";
	default: return "?";
	}
}


163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/*
 * pvr_ioctl()
 *
 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
 *
 */
static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
			      unsigned int cmd, void *arg)
{
	struct pvr2_v4l2_fh *fh = file->private_data;
	struct pvr2_v4l2 *vp = fh->vhead;
	struct pvr2_v4l2_dev *dev_info = fh->dev_info;
	struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
	int ret = -EINVAL;

	if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
		v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
	}

	if (!pvr2_hdw_dev_ok(hdw)) {
		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
			   "ioctl failed - bad or no context");
		return -EFAULT;
	}

	/* check priority */
	switch (cmd) {
	case VIDIOC_S_CTRL:
	case VIDIOC_S_STD:
	case VIDIOC_S_INPUT:
	case VIDIOC_S_TUNER:
	case VIDIOC_S_FREQUENCY:
		ret = v4l2_prio_check(&vp->prio, &fh->prio);
		if (ret)
			return ret;
	}

	switch (cmd) {
	case VIDIOC_QUERYCAP:
	{
		struct v4l2_capability *cap = arg;

		memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
206 207
		strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
			sizeof(cap->bus_info));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

		ret = 0;
		break;
	}

	case VIDIOC_G_PRIORITY:
	{
		enum v4l2_priority *p = arg;

		*p = v4l2_prio_max(&vp->prio);
		ret = 0;
		break;
	}

	case VIDIOC_S_PRIORITY:
	{
		enum v4l2_priority *prio = arg;

		ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
		break;
	}

	case VIDIOC_ENUMSTD:
	{
		struct v4l2_standard *vs = (struct v4l2_standard *)arg;
		int idx = vs->index;
		ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
		break;
	}

	case VIDIOC_G_STD:
	{
		int val = 0;
		ret = pvr2_ctrl_get_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
		*(v4l2_std_id *)arg = val;
		break;
	}

	case VIDIOC_S_STD:
	{
		ret = pvr2_ctrl_set_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
			*(v4l2_std_id *)arg);
		break;
	}

	case VIDIOC_ENUMINPUT:
	{
		struct pvr2_ctrl *cptr;
		struct v4l2_input *vi = (struct v4l2_input *)arg;
		struct v4l2_input tmp;
		unsigned int cnt;

		cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);

		memset(&tmp,0,sizeof(tmp));
		tmp.index = vi->index;
		ret = 0;
		switch (vi->index) {
		case PVR2_CVAL_INPUT_TV:
		case PVR2_CVAL_INPUT_RADIO:
			tmp.type = V4L2_INPUT_TYPE_TUNER;
			break;
		case PVR2_CVAL_INPUT_SVIDEO:
		case PVR2_CVAL_INPUT_COMPOSITE:
			tmp.type = V4L2_INPUT_TYPE_CAMERA;
			break;
		default:
			ret = -EINVAL;
			break;
		}
		if (ret < 0) break;

		cnt = 0;
		pvr2_ctrl_get_valname(cptr,vi->index,
				      tmp.name,sizeof(tmp.name)-1,&cnt);
		tmp.name[cnt] = 0;

		/* Don't bother with audioset, since this driver currently
		   always switches the audio whenever the video is
		   switched. */

		/* Handling std is a tougher problem.  It doesn't make
		   sense in cases where a device might be multi-standard.
		   We could just copy out the current value for the
		   standard, but it can change over time.  For now just
		   leave it zero. */

		memcpy(vi, &tmp, sizeof(tmp));

		ret = 0;
		break;
	}

	case VIDIOC_G_INPUT:
	{
		struct pvr2_ctrl *cptr;
		struct v4l2_input *vi = (struct v4l2_input *)arg;
		int val;
		cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
		val = 0;
		ret = pvr2_ctrl_get_value(cptr,&val);
		vi->index = val;
		break;
	}

	case VIDIOC_S_INPUT:
	{
		struct v4l2_input *vi = (struct v4l2_input *)arg;
		ret = pvr2_ctrl_set_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
			vi->index);
		break;
	}

	case VIDIOC_ENUMAUDIO:
	{
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		/* pkt: FIXME: We are returning one "fake" input here
		   which could very well be called "whatever_we_like".
		   This is for apps that want to see an audio input
		   just to feel comfortable, as well as to test if
		   it can do stereo or sth. There is actually no guarantee
		   that the actual audio input cannot change behind the app's
		   back, but most applications should not mind that either.

		   Hopefully, mplayer people will work with us on this (this
		   whole mess is to support mplayer pvr://), or Hans will come
		   up with a more standard way to say "we have inputs but we
		   don 't want you to change them independent of video" which
		   will sort this mess.
		 */
		struct v4l2_audio *vin = arg;
341
		ret = -EINVAL;
342 343 344 345 346
		if (vin->index > 0) break;
		strncpy(vin->name, "PVRUSB2 Audio",14);
		vin->capability = V4L2_AUDCAP_STEREO;
		ret = 0;
		break;
347 348 349 350 351
		break;
	}

	case VIDIOC_G_AUDIO:
	{
352 353 354 355 356 357 358
		/* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
		struct v4l2_audio *vin = arg;
		memset(vin,0,sizeof(*vin));
		vin->index = 0;
		strncpy(vin->name, "PVRUSB2 Audio",14);
		vin->capability = V4L2_AUDCAP_STEREO;
		ret = 0;
359 360 361 362 363 364 365 366 367 368 369
		break;
	}

	case VIDIOC_S_AUDIO:
	{
		ret = -EINVAL;
		break;
	}
	case VIDIOC_G_TUNER:
	{
		struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
370

371
		if (vt->index != 0) break; /* Only answer for the 1st tuner */
372

373 374
		pvr2_hdw_execute_tuner_poll(hdw);
		ret = pvr2_hdw_get_tuner_status(hdw,vt);
375 376 377 378 379 380 381 382 383 384 385 386 387
		break;
	}

	case VIDIOC_S_TUNER:
	{
		struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;

		if (vt->index != 0)
			break;

		ret = pvr2_ctrl_set_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
			vt->audmode);
388
		break;
389 390 391 392 393
	}

	case VIDIOC_S_FREQUENCY:
	{
		const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
394
		unsigned long fv;
395 396 397 398 399 400 401 402
		struct v4l2_tuner vt;
		int cur_input;
		struct pvr2_ctrl *ctrlp;
		ret = pvr2_hdw_get_tuner_status(hdw,&vt);
		if (ret != 0) break;
		ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
		ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
		if (ret != 0) break;
403
		if (vf->type == V4L2_TUNER_RADIO) {
404 405 406 407 408 409 410 411 412 413 414 415
			if (cur_input != PVR2_CVAL_INPUT_RADIO) {
				pvr2_ctrl_set_value(ctrlp,
						    PVR2_CVAL_INPUT_RADIO);
			}
		} else {
			if (cur_input == PVR2_CVAL_INPUT_RADIO) {
				pvr2_ctrl_set_value(ctrlp,
						    PVR2_CVAL_INPUT_TV);
			}
		}
		fv = vf->frequency;
		if (vt.capability & V4L2_TUNER_CAP_LOW) {
416 417 418 419
			fv = (fv * 125) / 2;
		} else {
			fv = fv * 62500;
		}
420
		ret = pvr2_ctrl_set_value(
421
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
422 423 424 425 426 427 428
		break;
	}

	case VIDIOC_G_FREQUENCY:
	{
		struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
		int val = 0;
429 430 431 432
		int cur_input;
		struct v4l2_tuner vt;
		ret = pvr2_hdw_get_tuner_status(hdw,&vt);
		if (ret != 0) break;
433 434 435
		ret = pvr2_ctrl_get_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
			&val);
436 437 438 439 440 441 442 443 444
		if (ret != 0) break;
		pvr2_ctrl_get_value(
			pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
			&cur_input);
		if (cur_input == PVR2_CVAL_INPUT_RADIO) {
			vf->type = V4L2_TUNER_RADIO;
		} else {
			vf->type = V4L2_TUNER_ANALOG_TV;
		}
445 446 447 448 449 450
		if (vt.capability & V4L2_TUNER_CAP_LOW) {
			val = (val * 2) / 125;
		} else {
			val /= 62500;
		}
		vf->frequency = val;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
		break;
	}

	case VIDIOC_ENUM_FMT:
	{
		struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;

		/* Only one format is supported : mpeg.*/
		if (fd->index != 0)
			break;

		memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
		ret = 0;
		break;
	}

	case VIDIOC_G_FMT:
	{
		struct v4l2_format *vf = (struct v4l2_format *)arg;
		int val;
		switch(vf->type) {
		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
			memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
			       sizeof(struct v4l2_format));
			val = 0;
			pvr2_ctrl_get_value(
				pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
				&val);
			vf->fmt.pix.width = val;
			val = 0;
			pvr2_ctrl_get_value(
				pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
				&val);
			vf->fmt.pix.height = val;
			ret = 0;
			break;
		case V4L2_BUF_TYPE_VBI_CAPTURE:
			// ????? Still need to figure out to do VBI correctly
			ret = -EINVAL;
			break;
		default:
			ret = -EINVAL;
			break;
		}
		break;
	}

	case VIDIOC_TRY_FMT:
	case VIDIOC_S_FMT:
	{
		struct v4l2_format *vf = (struct v4l2_format *)arg;

		ret = 0;
		switch(vf->type) {
		case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
506
			int lmin,lmax,ldef;
507
			struct pvr2_ctrl *hcp,*vcp;
508 509
			int h = vf->fmt.pix.height;
			int w = vf->fmt.pix.width;
510 511 512 513 514
			hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
			vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);

			lmin = pvr2_ctrl_get_min(hcp);
			lmax = pvr2_ctrl_get_max(hcp);
515 516 517 518
			ldef = pvr2_ctrl_get_def(hcp);
			if (w == -1) {
				w = ldef;
			} else if (w < lmin) {
519 520 521
				w = lmin;
			} else if (w > lmax) {
				w = lmax;
522
			}
523 524
			lmin = pvr2_ctrl_get_min(vcp);
			lmax = pvr2_ctrl_get_max(vcp);
525 526 527 528
			ldef = pvr2_ctrl_get_def(vcp);
			if (h == -1) {
				h = ldef;
			} else if (h < lmin) {
529 530 531 532
				h = lmin;
			} else if (h > lmax) {
				h = lmax;
			}
533 534 535

			memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
			       sizeof(struct v4l2_format));
536 537
			vf->fmt.pix.width = w;
			vf->fmt.pix.height = h;
538 539

			if (cmd == VIDIOC_S_FMT) {
540 541
				pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
				pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
			}
		} break;
		case V4L2_BUF_TYPE_VBI_CAPTURE:
			// ????? Still need to figure out to do VBI correctly
			ret = -EINVAL;
			break;
		default:
			ret = -EINVAL;
			break;
		}
		break;
	}

	case VIDIOC_STREAMON:
	{
557 558 559 560 561 562 563
		if (!fh->dev_info->stream) {
			/* No stream defined for this node.  This means
			   that we're not currently allowed to stream from
			   this node. */
			ret = -EPERM;
			break;
		}
564 565 566 567 568 569 570 571
		ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
		if (ret < 0) return ret;
		ret = pvr2_hdw_set_streaming(hdw,!0);
		break;
	}

	case VIDIOC_STREAMOFF:
	{
572 573 574 575 576 577 578
		if (!fh->dev_info->stream) {
			/* No stream defined for this node.  This means
			   that we're not currently allowed to stream from
			   this node. */
			ret = -EPERM;
			break;
		}
579 580 581 582 583 584 585 586 587
		ret = pvr2_hdw_set_streaming(hdw,0);
		break;
	}

	case VIDIOC_QUERYCTRL:
	{
		struct pvr2_ctrl *cptr;
		struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
		ret = 0;
588 589 590 591 592 593 594
		if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
			cptr = pvr2_hdw_get_ctrl_nextv4l(
				hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
			if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
		} else {
			cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
		}
595
		if (!cptr) {
596
			pvr2_trace(PVR2_TRACE_V4LIOCTL,
597 598
				   "QUERYCTRL id=0x%x not implemented here",
				   vc->id);
599 600 601 602
			ret = -EINVAL;
			break;
		}

603 604 605 606 607 608
		pvr2_trace(PVR2_TRACE_V4LIOCTL,
			   "QUERYCTRL id=0x%x mapping name=%s (%s)",
			   vc->id,pvr2_ctrl_get_name(cptr),
			   pvr2_ctrl_get_desc(cptr));
		strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
		vc->flags = pvr2_ctrl_get_v4lflags(cptr);
609 610 611 612 613 614 615 616
		vc->default_value = pvr2_ctrl_get_def(cptr);
		switch (pvr2_ctrl_get_type(cptr)) {
		case pvr2_ctl_enum:
			vc->type = V4L2_CTRL_TYPE_MENU;
			vc->minimum = 0;
			vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
			vc->step = 1;
			break;
617
		case pvr2_ctl_bool:
618
			vc->type = V4L2_CTRL_TYPE_BOOLEAN;
619 620 621 622
			vc->minimum = 0;
			vc->maximum = 1;
			vc->step = 1;
			break;
623 624 625 626 627 628 629
		case pvr2_ctl_int:
			vc->type = V4L2_CTRL_TYPE_INTEGER;
			vc->minimum = pvr2_ctrl_get_min(cptr);
			vc->maximum = pvr2_ctrl_get_max(cptr);
			vc->step = 1;
			break;
		default:
630
			pvr2_trace(PVR2_TRACE_V4LIOCTL,
631 632
				   "QUERYCTRL id=0x%x name=%s not mappable",
				   vc->id,pvr2_ctrl_get_name(cptr));
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
			ret = -EINVAL;
			break;
		}
		break;
	}

	case VIDIOC_QUERYMENU:
	{
		struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
		unsigned int cnt = 0;
		ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
					    vm->index,
					    vm->name,sizeof(vm->name)-1,
					    &cnt);
		vm->name[cnt] = 0;
		break;
	}

	case VIDIOC_G_CTRL:
	{
		struct v4l2_control *vc = (struct v4l2_control *)arg;
		int val = 0;
		ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
					  &val);
		vc->value = val;
		break;
	}

	case VIDIOC_S_CTRL:
	{
		struct v4l2_control *vc = (struct v4l2_control *)arg;
		ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
					  vc->value);
		break;
	}

669 670 671 672 673 674 675
	case VIDIOC_G_EXT_CTRLS:
	{
		struct v4l2_ext_controls *ctls =
			(struct v4l2_ext_controls *)arg;
		struct v4l2_ext_control *ctrl;
		unsigned int idx;
		int val;
676
		ret = 0;
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
		for (idx = 0; idx < ctls->count; idx++) {
			ctrl = ctls->controls + idx;
			ret = pvr2_ctrl_get_value(
				pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
			if (ret) {
				ctls->error_idx = idx;
				break;
			}
			/* Ensure that if read as a 64 bit value, the user
			   will still get a hopefully sane value */
			ctrl->value64 = 0;
			ctrl->value = val;
		}
		break;
	}

	case VIDIOC_S_EXT_CTRLS:
	{
		struct v4l2_ext_controls *ctls =
			(struct v4l2_ext_controls *)arg;
		struct v4l2_ext_control *ctrl;
		unsigned int idx;
699
		ret = 0;
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
		for (idx = 0; idx < ctls->count; idx++) {
			ctrl = ctls->controls + idx;
			ret = pvr2_ctrl_set_value(
				pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
				ctrl->value);
			if (ret) {
				ctls->error_idx = idx;
				break;
			}
		}
		break;
	}

	case VIDIOC_TRY_EXT_CTRLS:
	{
		struct v4l2_ext_controls *ctls =
			(struct v4l2_ext_controls *)arg;
		struct v4l2_ext_control *ctrl;
		struct pvr2_ctrl *pctl;
		unsigned int idx;
		/* For the moment just validate that the requested control
		   actually exists. */
722
		ret = 0;
723 724 725 726 727 728 729 730 731 732 733 734
		for (idx = 0; idx < ctls->count; idx++) {
			ctrl = ctls->controls + idx;
			pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
			if (!pctl) {
				ret = -EINVAL;
				ctls->error_idx = idx;
				break;
			}
		}
		break;
	}

735 736 737 738 739 740
	case VIDIOC_LOG_STATUS:
	{
		pvr2_hdw_trigger_module_log(hdw);
		ret = 0;
		break;
	}
741
#ifdef CONFIG_VIDEO_ADV_DEBUG
742 743
	case VIDIOC_DBG_S_REGISTER:
	case VIDIOC_DBG_G_REGISTER:
744
	{
745
		u64 val;
746
		struct v4l2_register *req = (struct v4l2_register *)arg;
747
		if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
748
		ret = pvr2_hdw_register_access(
749
			hdw,req->match_type,req->match_chip,req->reg,
750 751
			cmd == VIDIOC_DBG_S_REGISTER,&val);
		if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
752 753 754
		break;
	}
#endif
755 756 757 758 759 760 761 762 763 764

	default :
		ret = v4l_compat_translate_ioctl(inode,file,cmd,
						 arg,pvr2_v4l2_do_ioctl);
	}

	pvr2_hdw_commit_ctl(hdw);

	if (ret < 0) {
		if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
765
			pvr2_trace(PVR2_TRACE_V4LIOCTL,
766 767
				   "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
		} else {
768 769
			if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
				pvr2_trace(PVR2_TRACE_V4LIOCTL,
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
					   "pvr2_v4l2_do_ioctl failure, ret=%d"
					   " command was:",ret);
				v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
						cmd);
			}
		}
	} else {
		pvr2_trace(PVR2_TRACE_V4LIOCTL,
			   "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
			   ret,ret);
	}
	return ret;
}


static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
{
787 788
	int minor_id = dip->devbase.minor;
	struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
789 790
	enum pvr2_config cfg = dip->config;
	int v4l_type = dip->v4l_type;
791

792
	pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
793 794

	/* Paranoia */
795 796
	dip->v4lp = NULL;
	dip->stream = NULL;
797 798 799 800

	/* Actual deallocation happens later when all internal references
	   are gone. */
	video_unregister_device(&dip->devbase);
801

802 803 804
	printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
	       get_v4l_name(v4l_type),minor_id & 0x1f,
	       pvr2_config_get_name(cfg));
805

806 807 808 809 810
}


static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
{
811 812
	if (vp->dev_video) {
		pvr2_v4l2_dev_destroy(vp->dev_video);
Al Viro's avatar
Al Viro committed
813
		vp->dev_video = NULL;
814 815 816
	}
	if (vp->dev_radio) {
		pvr2_v4l2_dev_destroy(vp->dev_radio);
Al Viro's avatar
Al Viro committed
817
		vp->dev_radio = NULL;
818
	}
819 820 821 822 823 824 825

	pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
	pvr2_channel_done(&vp->channel);
	kfree(vp);
}


826 827 828 829 830 831 832 833
static void pvr2_video_device_release(struct video_device *vdev)
{
	struct pvr2_v4l2_dev *dev;
	dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
	kfree(dev);
}


834
static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
835 836 837 838 839 840 841 842 843
{
	struct pvr2_v4l2 *vp;
	vp = container_of(chp,struct pvr2_v4l2,channel);
	if (!vp->channel.mc_head->disconnect_flag) return;
	if (vp->vfirst) return;
	pvr2_v4l2_destroy_no_lock(vp);
}


844 845
static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
			   unsigned int cmd, unsigned long arg)
846 847 848 849 850 851 852 853 854 855
{

/* Temporary hack : use ivtv api until a v4l2 one is available. */
#define IVTV_IOC_G_CODEC        0xFFEE7703
#define IVTV_IOC_S_CODEC        0xFFEE7704
	if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
	return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
}


856
static int pvr2_v4l2_release(struct inode *inode, struct file *file)
857 858 859 860
{
	struct pvr2_v4l2_fh *fhp = file->private_data;
	struct pvr2_v4l2 *vp = fhp->vhead;
	struct pvr2_context *mp = fhp->vhead->channel.mc_head;
861
	struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
862 863 864 865 866 867 868

	pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");

	if (fhp->rhp) {
		struct pvr2_stream *sp;
		pvr2_hdw_set_streaming(hdw,0);
		sp = pvr2_ioread_get_stream(fhp->rhp);
869
		if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
870
		pvr2_ioread_destroy(fhp->rhp);
871
		fhp->rhp = NULL;
872
	}
873

874 875 876 877
	v4l2_prio_close(&vp->prio, &fhp->prio);
	file->private_data = NULL;

	pvr2_context_enter(mp); do {
878 879 880 881 882 883 884 885 886 887 888 889 890 891
		/* Restore the previous input selection, if it makes sense
		   to do so. */
		if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
			struct pvr2_ctrl *cp;
			int pval;
			cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
			pvr2_ctrl_get_value(cp,&pval);
			/* Only restore if we're still selecting the radio */
			if (pval == PVR2_CVAL_INPUT_RADIO) {
				pvr2_ctrl_set_value(cp,fhp->prev_input_val);
				pvr2_hdw_commit_ctl(hdw);
			}
		}

892 893 894 895 896 897 898 899 900 901
		if (fhp->vnext) {
			fhp->vnext->vprev = fhp->vprev;
		} else {
			vp->vlast = fhp->vprev;
		}
		if (fhp->vprev) {
			fhp->vprev->vnext = fhp->vnext;
		} else {
			vp->vfirst = fhp->vnext;
		}
902 903 904
		fhp->vnext = NULL;
		fhp->vprev = NULL;
		fhp->vhead = NULL;
905 906 907 908 909 910 911 912 913 914 915 916
		pvr2_channel_done(&fhp->channel);
		pvr2_trace(PVR2_TRACE_STRUCT,
			   "Destroying pvr_v4l2_fh id=%p",fhp);
		kfree(fhp);
		if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
			pvr2_v4l2_destroy_no_lock(vp);
		}
	} while (0); pvr2_context_exit(mp);
	return 0;
}


917
static int pvr2_v4l2_open(struct inode *inode, struct file *file)
918
{
919
	struct pvr2_v4l2_dev *dip; /* Our own context pointer */
920 921 922 923
	struct pvr2_v4l2_fh *fhp;
	struct pvr2_v4l2 *vp;
	struct pvr2_hdw *hdw;

924
	dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
925 926 927 928 929 930 931 932 933 934 935 936

	vp = dip->v4lp;
	hdw = vp->channel.hdw;

	pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");

	if (!pvr2_hdw_dev_ok(hdw)) {
		pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
			   "pvr2_v4l2_open: hardware not ready");
		return -EIO;
	}

937
	fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
938 939 940 941 942 943 944 945 946 947
	if (!fhp) {
		return -ENOMEM;
	}

	init_waitqueue_head(&fhp->wait_data);
	fhp->dev_info = dip;

	pvr2_context_enter(vp->channel.mc_head); do {
		pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
		pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
948

949
		fhp->vnext = NULL;
950 951 952 953 954 955 956 957
		fhp->vprev = vp->vlast;
		if (vp->vlast) {
			vp->vlast->vnext = fhp;
		} else {
			vp->vfirst = fhp;
		}
		vp->vlast = fhp;
		fhp->vhead = vp;
958 959 960 961 962 963 964 965 966 967 968 969

		/* Opening the /dev/radioX device implies a mode switch.
		   So execute that here.  Note that you can get the
		   IDENTICAL effect merely by opening the normal video
		   device and setting the input appropriately. */
		if (dip->v4l_type == VFL_TYPE_RADIO) {
			struct pvr2_ctrl *cp;
			cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
			pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
			pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
			pvr2_hdw_commit_ctl(hdw);
		}
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	} while (0); pvr2_context_exit(vp->channel.mc_head);

	fhp->file = file;
	file->private_data = fhp;
	v4l2_prio_open(&vp->prio,&fhp->prio);

	fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);

	return 0;
}


static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
{
	wake_up(&fhp->wait_data);
}

static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
{
	int ret;
	struct pvr2_stream *sp;
	struct pvr2_hdw *hdw;
	if (fh->rhp) return 0;

994 995 996 997 998 999
	if (!fh->dev_info->stream) {
		/* No stream defined for this node.  This means that we're
		   not currently allowed to stream from this node. */
		return -EPERM;
	}

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
	/* First read() attempt.  Try to claim the stream and start
	   it... */
	if ((ret = pvr2_channel_claim_stream(&fh->channel,
					     fh->dev_info->stream)) != 0) {
		/* Someone else must already have it */
		return ret;
	}

	fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
	if (!fh->rhp) {
1010
		pvr2_channel_claim_stream(&fh->channel,NULL);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		return -ENOMEM;
	}

	hdw = fh->channel.mc_head->hdw;
	sp = fh->dev_info->stream->stream;
	pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
	pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
	pvr2_hdw_set_streaming(hdw,!0);
	ret = pvr2_ioread_set_enabled(fh->rhp,!0);

	return ret;
}


static ssize_t pvr2_v4l2_read(struct file *file,
			      char __user *buff, size_t count, loff_t *ppos)
{
	struct pvr2_v4l2_fh *fh = file->private_data;
	int ret;

	if (fh->fw_mode_flag) {
		struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
		char *tbuf;
		int c1,c2;
		int tcnt = 0;
		unsigned int offs = *ppos;

		tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
		if (!tbuf) return -ENOMEM;

		while (count) {
			c1 = count;
			if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
			c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
			if (c2 < 0) {
				tcnt = c2;
				break;
			}
			if (!c2) break;
			if (copy_to_user(buff,tbuf,c2)) {
				tcnt = -EFAULT;
				break;
			}
			offs += c2;
			tcnt += c2;
			buff += c2;
			count -= c2;
			*ppos += c2;
		}
		kfree(tbuf);
		return tcnt;
	}

	if (!fh->rhp) {
		ret = pvr2_v4l2_iosetup(fh);
		if (ret) {
			return ret;
		}
	}

	for (;;) {
		ret = pvr2_ioread_read(fh->rhp,buff,count);
		if (ret >= 0) break;
		if (ret != -EAGAIN) break;
		if (file->f_flags & O_NONBLOCK) break;
		/* Doing blocking I/O.  Wait here. */
		ret = wait_event_interruptible(
			fh->wait_data,
			pvr2_ioread_avail(fh->rhp) >= 0);
		if (ret < 0) break;
	}

	return ret;
}


static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;
	struct pvr2_v4l2_fh *fh = file->private_data;
	int ret;

	if (fh->fw_mode_flag) {
		mask |= POLLIN | POLLRDNORM;
		return mask;
	}

	if (!fh->rhp) {
		ret = pvr2_v4l2_iosetup(fh);
		if (ret) return POLLERR;
	}

	poll_wait(file,&fh->wait_data,wait);

	if (pvr2_ioread_avail(fh->rhp) >= 0) {
		mask |= POLLIN | POLLRDNORM;
	}

	return mask;
}


1113
static const struct file_operations vdev_fops = {
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	.owner      = THIS_MODULE,
	.open       = pvr2_v4l2_open,
	.release    = pvr2_v4l2_release,
	.read       = pvr2_v4l2_read,
	.ioctl      = pvr2_v4l2_ioctl,
	.llseek     = no_llseek,
	.poll       = pvr2_v4l2_poll,
};


#define VID_HARDWARE_PVRUSB2    38  /* FIXME : need a good value */

static struct video_device vdev_template = {
	.owner      = THIS_MODULE,
	.type       = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
	.type2      = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
		       | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
		       | V4L2_CAP_READWRITE),
	.hardware   = VID_HARDWARE_PVRUSB2,
	.fops       = &vdev_fops,
};


static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
			       struct pvr2_v4l2 *vp,
1139
			       int v4l_type)
1140 1141 1142
{
	int mindevnum;
	int unit_number;
Al Viro's avatar
Al Viro committed
1143
	int *nr_ptr = NULL;
1144 1145 1146
	dip->v4lp = vp;


1147 1148 1149
	dip->v4l_type = v4l_type;
	switch (v4l_type) {
	case VFL_TYPE_GRABBER:
1150
		dip->stream = &vp->channel.mc_head->video_stream;
1151 1152 1153
		dip->config = pvr2_config_mpeg;
		dip->minor_type = pvr2_v4l_type_video;
		nr_ptr = video_nr;
1154 1155 1156 1157 1158
		if (!dip->stream) {
			err("Failed to set up pvrusb2 v4l video dev"
			    " due to missing stream instance");
			return;
		}
1159
		break;
1160 1161 1162 1163
	case VFL_TYPE_VBI:
		dip->config = pvr2_config_vbi;
		dip->minor_type = pvr2_v4l_type_vbi;
		nr_ptr = vbi_nr;
1164
		break;
1165
	case VFL_TYPE_RADIO:
1166 1167
		dip->stream = &vp->channel.mc_head->video_stream;
		dip->config = pvr2_config_mpeg;
1168 1169
		dip->minor_type = pvr2_v4l_type_radio;
		nr_ptr = radio_nr;
1170 1171 1172 1173 1174 1175 1176 1177
		break;
	default:
		/* Bail out (this should be impossible) */
		err("Failed to set up pvrusb2 v4l dev"
		    " due to unrecognized config");
		return;
	}

1178 1179
	memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
	dip->devbase.release = pvr2_video_device_release;
1180 1181 1182

	mindevnum = -1;
	unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1183 1184
	if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
		mindevnum = nr_ptr[unit_number];
1185
	}
1186 1187 1188 1189
	if ((video_register_device(&dip->devbase,
				   dip->v4l_type, mindevnum) < 0) &&
	    (video_register_device(&dip->devbase,
				   dip->v4l_type, -1) < 0)) {
1190 1191
		err("Failed to register pvrusb2 v4l device");
	}
1192 1193 1194 1195

	printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
	       get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
	       pvr2_config_get_name(dip->config));
1196 1197

	pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
1198
					dip->minor_type,dip->devbase.minor);
1199 1200 1201 1202 1203 1204 1205
}


struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
{
	struct pvr2_v4l2 *vp;

1206
	vp = kzalloc(sizeof(*vp),GFP_KERNEL);
1207
	if (!vp) return vp;
1208 1209
	vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
	vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1210
	if (!(vp->dev_video && vp->dev_radio)) {
1211 1212
		kfree(vp->dev_video);
		kfree(vp->dev_radio);
1213
		kfree(vp);
1214
		return NULL;
1215
	}
1216 1217 1218 1219 1220 1221
	pvr2_channel_init(&vp->channel,mnp);
	pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);

	vp->channel.check_func = pvr2_v4l2_internal_check;

	/* register streams */
1222 1223
	pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
	pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

	return vp;
}

/*
  Stuff for Emacs to see, in order to encourage consistent editing style:
  *** Local Variables: ***
  *** mode: c ***
  *** fill-column: 75 ***
  *** tab-width: 8 ***
  *** c-basic-offset: 8 ***
  *** End: ***
  */