saa7134-empress.c 14.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 *
 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  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, or
 *  (at your option) any later version.
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/delay.h>

#include "saa7134-reg.h"
#include "saa7134.h"

#include <media/saa6752hs.h>
31
#include <media/v4l2-common.h>
32
#include <media/v4l2-chip-ident.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39

/* ------------------------------------------------------------------ */

MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
MODULE_LICENSE("GPL");

static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
40

L
Linus Torvalds 已提交
41 42 43
module_param_array(empress_nr, int, NULL, 0444);
MODULE_PARM_DESC(empress_nr,"ts device number");

44
static unsigned int debug;
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug,"enable debug messages");

#define dprintk(fmt, arg...)	if (debug)			\
	printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)

/* ------------------------------------------------------------------ */

static void ts_reset_encoder(struct saa7134_dev* dev)
{
	if (!dev->empress_started)
		return;

	saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
	msleep(10);
60
	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
L
Linus Torvalds 已提交
61 62 63 64 65 66
	msleep(100);
	dev->empress_started = 0;
}

static int ts_init_encoder(struct saa7134_dev* dev)
{
67 68
	struct v4l2_ext_controls ctrls = { V4L2_CTRL_CLASS_MPEG, 0 };

L
Linus Torvalds 已提交
69
	ts_reset_encoder(dev);
70
	saa7134_i2c_call_clients(dev, VIDIOC_S_EXT_CTRLS, &ctrls);
L
Linus Torvalds 已提交
71
	dev->empress_started = 1;
72
	return 0;
L
Linus Torvalds 已提交
73 74 75 76 77 78 79
}

/* ------------------------------------------------------------------ */

static int ts_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
80
	struct saa7134_dev *dev;
L
Linus Torvalds 已提交
81 82
	int err;

83
	lock_kernel();
84 85 86
	list_for_each_entry(dev, &saa7134_devlist, devlist)
		if (dev->empress_dev && dev->empress_dev->minor == minor)
			goto found;
87
	unlock_kernel();
88 89
	return -ENODEV;
 found:
L
Linus Torvalds 已提交
90 91 92

	dprintk("open minor=%d\n",minor);
	err = -EBUSY;
93
	if (!mutex_trylock(&dev->empress_tsq.vb_lock))
L
Linus Torvalds 已提交
94
		goto done;
95
	if (atomic_read(&dev->empress_users))
L
Linus Torvalds 已提交
96 97
		goto done_up;

98 99 100 101
	/* Unmute audio */
	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
		saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));

102
	atomic_inc(&dev->empress_users);
L
Linus Torvalds 已提交
103 104 105 106
	file->private_data = dev;
	err = 0;

done_up:
107
	mutex_unlock(&dev->empress_tsq.vb_lock);
L
Linus Torvalds 已提交
108
done:
109
	unlock_kernel();
L
Linus Torvalds 已提交
110 111 112 113 114 115 116
	return err;
}

static int ts_release(struct inode *inode, struct file *file)
{
	struct saa7134_dev *dev = file->private_data;

117
	videobuf_stop(&dev->empress_tsq);
L
Linus Torvalds 已提交
118 119 120 121 122
	videobuf_mmap_free(&dev->empress_tsq);

	/* stop the encoder */
	ts_reset_encoder(dev);

123 124 125 126
	/* Mute audio */
	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
		saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));

127
	atomic_dec(&dev->empress_users);
128

L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	return 0;
}

static ssize_t
ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
	struct saa7134_dev *dev = file->private_data;

	if (!dev->empress_started)
		ts_init_encoder(dev);

	return videobuf_read_stream(&dev->empress_tsq,
				    data, count, ppos, 0,
				    file->f_flags & O_NONBLOCK);
}

static unsigned int
ts_poll(struct file *file, struct poll_table_struct *wait)
{
	struct saa7134_dev *dev = file->private_data;

	return videobuf_poll_stream(file, &dev->empress_tsq, wait);
}


static int
ts_mmap(struct file *file, struct vm_area_struct * vma)
{
	struct saa7134_dev *dev = file->private_data;

	return videobuf_mmap_mapper(&dev->empress_tsq, vma);
}

/*
 * This function is _not_ called directly, but from
 * video_generic_ioctl (and maybe others).  userspace
 * copying is done already, arg is a kernel pointer.
 */
167 168 169

static int empress_querycap(struct file *file, void  *priv,
					struct v4l2_capability *cap)
L
Linus Torvalds 已提交
170
{
D
Dmitri Belimov 已提交
171
	struct saa7134_dev *dev = file->private_data;
172 173 174 175 176 177 178 179 180 181 182 183

	strcpy(cap->driver, "saa7134");
	strlcpy(cap->card, saa7134_boards[dev->board].name,
		sizeof(cap->card));
	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
	cap->version = SAA7134_VERSION_CODE;
	cap->capabilities =
		V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_READWRITE |
		V4L2_CAP_STREAMING;
	return 0;
}
L
Linus Torvalds 已提交
184

185 186 187 188 189
static int empress_enum_input(struct file *file, void *priv,
					struct v4l2_input *i)
{
	if (i->index != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
190

191 192
	i->type = V4L2_INPUT_TYPE_CAMERA;
	strcpy(i->name, "CCIR656");
L
Linus Torvalds 已提交
193

194 195
	return 0;
}
L
Linus Torvalds 已提交
196

197 198 199 200 201
static int empress_g_input(struct file *file, void *priv, unsigned int *i)
{
	*i = 0;
	return 0;
}
L
Linus Torvalds 已提交
202

203 204 205 206
static int empress_s_input(struct file *file, void *priv, unsigned int i)
{
	if (i != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
207

208 209
	return 0;
}
L
Linus Torvalds 已提交
210

211
static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
212 213 214 215
					struct v4l2_fmtdesc *f)
{
	if (f->index != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
216

217 218
	strlcpy(f->description, "MPEG TS", sizeof(f->description));
	f->pixelformat = V4L2_PIX_FMT_MPEG;
L
Linus Torvalds 已提交
219

220 221
	return 0;
}
L
Linus Torvalds 已提交
222

223
static int empress_g_fmt_vid_cap(struct file *file, void *priv,
224 225
				struct v4l2_format *f)
{
226
	struct saa7134_dev *dev = file->private_data;
L
Linus Torvalds 已提交
227

228
	saa7134_i2c_call_clients(dev, VIDIOC_G_FMT, f);
L
Linus Torvalds 已提交
229

230 231
	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
L
Linus Torvalds 已提交
232

233 234
	return 0;
}
L
Linus Torvalds 已提交
235

236
static int empress_s_fmt_vid_cap(struct file *file, void *priv,
237 238
				struct v4l2_format *f)
{
239
	struct saa7134_dev *dev = file->private_data;
L
Linus Torvalds 已提交
240

241
	saa7134_i2c_call_clients(dev, VIDIOC_S_FMT, f);
L
Linus Torvalds 已提交
242

243 244
	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
L
Linus Torvalds 已提交
245

246 247 248 249 250 251 252
	return 0;
}


static int empress_reqbufs(struct file *file, void *priv,
					struct v4l2_requestbuffers *p)
{
253
	struct saa7134_dev *dev = file->private_data;
254 255 256 257 258 259 260

	return videobuf_reqbufs(&dev->empress_tsq, p);
}

static int empress_querybuf(struct file *file, void *priv,
					struct v4l2_buffer *b)
{
261
	struct saa7134_dev *dev = file->private_data;
262 263 264 265 266 267

	return videobuf_querybuf(&dev->empress_tsq, b);
}

static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
268
	struct saa7134_dev *dev = file->private_data;
269 270 271 272 273 274

	return videobuf_qbuf(&dev->empress_tsq, b);
}

static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
{
275
	struct saa7134_dev *dev = file->private_data;
276 277 278 279 280 281 282 283

	return videobuf_dqbuf(&dev->empress_tsq, b,
				file->f_flags & O_NONBLOCK);
}

static int empress_streamon(struct file *file, void *priv,
					enum v4l2_buf_type type)
{
284
	struct saa7134_dev *dev = file->private_data;
285 286 287 288 289 290 291

	return videobuf_streamon(&dev->empress_tsq);
}

static int empress_streamoff(struct file *file, void *priv,
					enum v4l2_buf_type type)
{
292
	struct saa7134_dev *dev = file->private_data;
293 294 295 296

	return videobuf_streamoff(&dev->empress_tsq);
}

297 298 299 300 301 302 303 304 305
static int saa7134_i2c_call_saa6752(struct saa7134_dev *dev,
					      unsigned int cmd, void *arg)
{
	if (dev->mpeg_i2c_client == NULL)
		return -EINVAL;
	return dev->mpeg_i2c_client->driver->command(dev->mpeg_i2c_client,
								cmd, arg);
}

306 307 308
static int empress_s_ext_ctrls(struct file *file, void *priv,
			       struct v4l2_ext_controls *ctrls)
{
309
	struct saa7134_dev *dev = file->private_data;
310
	int err;
311 312 313 314

	/* count == 0 is abused in saa6752hs.c, so that special
		case is handled here explicitly. */
	if (ctrls->count == 0)
315
		return 0;
L
Linus Torvalds 已提交
316

317 318 319
	if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
		return -EINVAL;

320
	err = saa7134_i2c_call_saa6752(dev, VIDIOC_S_EXT_CTRLS, ctrls);
321 322
	ts_init_encoder(dev);

323
	return err;
L
Linus Torvalds 已提交
324 325
}

326 327
static int empress_g_ext_ctrls(struct file *file, void *priv,
			       struct v4l2_ext_controls *ctrls)
L
Linus Torvalds 已提交
328
{
329
	struct saa7134_dev *dev = file->private_data;
330 331 332

	if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
		return -EINVAL;
333 334
	return saa7134_i2c_call_saa6752(dev, VIDIOC_G_EXT_CTRLS, ctrls);
}
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static int empress_g_ctrl(struct file *file, void *priv,
					struct v4l2_control *c)
{
	struct saa7134_dev *dev = file->private_data;

	return saa7134_g_ctrl_internal(dev, NULL, c);
}

static int empress_s_ctrl(struct file *file, void *priv,
					struct v4l2_control *c)
{
	struct saa7134_dev *dev = file->private_data;

	return saa7134_s_ctrl_internal(dev, NULL, c);
}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static int empress_queryctrl(struct file *file, void *priv,
					struct v4l2_queryctrl *c)
{
	static const u32 user_ctrls[] = {
		V4L2_CID_USER_CLASS,
		V4L2_CID_BRIGHTNESS,
		V4L2_CID_CONTRAST,
		V4L2_CID_SATURATION,
		V4L2_CID_HUE,
		V4L2_CID_AUDIO_VOLUME,
		V4L2_CID_AUDIO_MUTE,
		V4L2_CID_HFLIP,
		0
	};

	static const u32 mpeg_ctrls[] = {
		V4L2_CID_MPEG_CLASS,
		V4L2_CID_MPEG_STREAM_TYPE,
		V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
		V4L2_CID_MPEG_AUDIO_ENCODING,
		V4L2_CID_MPEG_AUDIO_L2_BITRATE,
		V4L2_CID_MPEG_VIDEO_ENCODING,
		V4L2_CID_MPEG_VIDEO_ASPECT,
		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
		V4L2_CID_MPEG_VIDEO_BITRATE,
		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
		0
	};
	static const u32 *ctrl_classes[] = {
		user_ctrls,
		mpeg_ctrls,
		NULL
	};
	struct saa7134_dev *dev = file->private_data;

	c->id = v4l2_ctrl_next(ctrl_classes, c->id);
	if (c->id == 0)
		return -EINVAL;
	if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
		return v4l2_ctrl_query_fill_std(c);
	if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
		return saa7134_queryctrl(file, priv, c);
	return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYCTRL, c);
}

static int empress_querymenu(struct file *file, void *priv,
					struct v4l2_querymenu *c)
{
	struct saa7134_dev *dev = file->private_data;

	if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
		return -EINVAL;
	return saa7134_i2c_call_saa6752(dev, VIDIOC_QUERYMENU, c);
L
Linus Torvalds 已提交
405 406
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
static int empress_g_chip_ident(struct file *file, void *fh,
	       struct v4l2_chip_ident *chip)
{
	struct saa7134_dev *dev = file->private_data;

	chip->ident = V4L2_IDENT_NONE;
	chip->revision = 0;
	if (dev->mpeg_i2c_client == NULL)
		return -EINVAL;
	if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER &&
	    chip->match_chip == I2C_DRIVERID_SAA6752HS)
		return saa7134_i2c_call_saa6752(dev, VIDIOC_G_CHIP_IDENT, chip);
	if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR &&
	    chip->match_chip == dev->mpeg_i2c_client->addr)
		return saa7134_i2c_call_saa6752(dev, VIDIOC_G_CHIP_IDENT, chip);
	return -EINVAL;
}


426
static const struct file_operations ts_fops =
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
{
	.owner	  = THIS_MODULE,
	.open	  = ts_open,
	.release  = ts_release,
	.read	  = ts_read,
	.poll	  = ts_poll,
	.mmap	  = ts_mmap,
434
	.ioctl	  = video_ioctl2,
L
Linus Torvalds 已提交
435 436 437
	.llseek   = no_llseek,
};

438
static const struct v4l2_ioctl_ops ts_ioctl_ops = {
439
	.vidioc_querycap		= empress_querycap,
440 441 442
	.vidioc_enum_fmt_vid_cap	= empress_enum_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap		= empress_s_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap		= empress_g_fmt_vid_cap,
443 444 445 446 447 448 449 450 451 452 453
	.vidioc_reqbufs			= empress_reqbufs,
	.vidioc_querybuf		= empress_querybuf,
	.vidioc_qbuf			= empress_qbuf,
	.vidioc_dqbuf			= empress_dqbuf,
	.vidioc_streamon		= empress_streamon,
	.vidioc_streamoff		= empress_streamoff,
	.vidioc_s_ext_ctrls		= empress_s_ext_ctrls,
	.vidioc_g_ext_ctrls		= empress_g_ext_ctrls,
	.vidioc_enum_input		= empress_enum_input,
	.vidioc_g_input			= empress_g_input,
	.vidioc_s_input			= empress_s_input,
454 455
	.vidioc_queryctrl		= empress_queryctrl,
	.vidioc_querymenu		= empress_querymenu,
456 457
	.vidioc_g_ctrl			= empress_g_ctrl,
	.vidioc_s_ctrl			= empress_s_ctrl,
458
	.vidioc_g_chip_ident 		= empress_g_chip_ident,
459 460 461 462 463 464 465 466 467
};

/* ----------------------------------------------------------- */

static struct video_device saa7134_empress_template = {
	.name          = "saa7134-empress",
	.fops          = &ts_fops,
	.minor	       = -1,
	.ioctl_ops     = &ts_ioctl_ops,
468 469 470

	.tvnorms			= SAA7134_NORMS,
	.current_norm			= V4L2_STD_PAL,
L
Linus Torvalds 已提交
471 472
};

D
David Howells 已提交
473
static void empress_signal_update(struct work_struct *work)
L
Linus Torvalds 已提交
474
{
D
David Howells 已提交
475 476
	struct saa7134_dev* dev =
		container_of(work, struct saa7134_dev, empress_workqueue);
L
Linus Torvalds 已提交
477 478 479 480 481 482

	if (dev->nosignal) {
		dprintk("no video signal\n");
		ts_reset_encoder(dev);
	} else {
		dprintk("video signal acquired\n");
483
		if (atomic_read(&dev->empress_users))
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497
			ts_init_encoder(dev);
	}
}

static void empress_signal_change(struct saa7134_dev *dev)
{
	schedule_work(&dev->empress_workqueue);
}


static int empress_init(struct saa7134_dev *dev)
{
	int err;

498
	dprintk("%s: %s\n",dev->name,__func__);
L
Linus Torvalds 已提交
499 500 501 502
	dev->empress_dev = video_device_alloc();
	if (NULL == dev->empress_dev)
		return -ENOMEM;
	*(dev->empress_dev) = saa7134_empress_template;
503
	dev->empress_dev->parent  = &dev->pci->dev;
L
Linus Torvalds 已提交
504 505 506 507 508
	dev->empress_dev->release = video_device_release;
	snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
		 "%s empress (%s)", dev->name,
		 saa7134_boards[dev->board].name);

D
David Howells 已提交
509
	INIT_WORK(&dev->empress_workqueue, empress_signal_update);
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522

	err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
				    empress_nr[dev->nr]);
	if (err < 0) {
		printk(KERN_INFO "%s: can't register video device\n",
		       dev->name);
		video_device_release(dev->empress_dev);
		dev->empress_dev = NULL;
		return err;
	}
	printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
	       dev->name,dev->empress_dev->minor & 0x1f);

523 524
	videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
			    &dev->pci->dev, &dev->slock,
L
Linus Torvalds 已提交
525 526 527 528 529
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
			    V4L2_FIELD_ALTERNATE,
			    sizeof(struct saa7134_buf),
			    dev);

D
David Howells 已提交
530
	empress_signal_update(&dev->empress_workqueue);
L
Linus Torvalds 已提交
531 532 533 534 535
	return 0;
}

static int empress_fini(struct saa7134_dev *dev)
{
536
	dprintk("%s: %s\n",dev->name,__func__);
L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

	if (NULL == dev->empress_dev)
		return 0;
	flush_scheduled_work();
	video_unregister_device(dev->empress_dev);
	dev->empress_dev = NULL;
	return 0;
}

static struct saa7134_mpeg_ops empress_ops = {
	.type          = SAA7134_MPEG_EMPRESS,
	.init          = empress_init,
	.fini          = empress_fini,
	.signal_change = empress_signal_change,
};

static int __init empress_register(void)
{
	return saa7134_ts_register(&empress_ops);
}

static void __exit empress_unregister(void)
{
	saa7134_ts_unregister(&empress_ops);
}

module_init(empress_register);
module_exit(empress_unregister);

/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */