saa7134-empress.c 10.7 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
/*
 *
 * (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/delay.h>

26
#include <media/v4l2-common.h>
27 28 29 30
#include <media/v4l2-event.h>

#include "saa7134-reg.h"
#include "saa7134.h"
L
Linus Torvalds 已提交
31 32 33 34 35 36 37

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

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

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

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

42
static unsigned int debug;
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50
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)

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

51
static void ts_reset_encoder(struct saa7134_dev* dev);
L
Linus Torvalds 已提交
52 53 54

static int ts_init_encoder(struct saa7134_dev* dev)
{
55
	u32 leading_null_bytes = 0;
56

57 58 59 60 61 62 63 64 65
	/* If more cards start to need this, then this
	   should probably be added to the card definitions. */
	switch (dev->board) {
	case SAA7134_BOARD_BEHOLD_M6:
	case SAA7134_BOARD_BEHOLD_M63:
	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
		leading_null_bytes = 1;
		break;
	}
L
Linus Torvalds 已提交
66
	ts_reset_encoder(dev);
67
	saa_call_all(dev, core, init, leading_null_bytes);
L
Linus Torvalds 已提交
68
	dev->empress_started = 1;
69
	return 0;
L
Linus Torvalds 已提交
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83
static void ts_reset_encoder(struct saa7134_dev* dev)
{
	if (!dev->empress_started)
		return;

	saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
	msleep(10);
	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
	msleep(100);
	dev->empress_started = 0;
}

L
Linus Torvalds 已提交
84 85
/* ------------------------------------------------------------------ */

86
static int ts_open(struct file *file)
L
Linus Torvalds 已提交
87
{
88
	struct video_device *vdev = video_devdata(file);
89
	struct saa7134_dev *dev = video_drvdata(file);
90
	struct saa7134_fh *fh;
L
Linus Torvalds 已提交
91

92 93 94
	/* allocate + initialize per filehandle data */
	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
	if (NULL == fh)
95
		return -ENOMEM;
96 97 98 99 100 101

	v4l2_fh_init(&fh->fh, vdev);
	file->private_data = fh;
	fh->is_empress = true;
	v4l2_fh_add(&fh->fh);

102 103 104 105
	/* Unmute audio */
	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
		saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));

106
	return 0;
L
Linus Torvalds 已提交
107 108
}

109
static int ts_release(struct file *file)
L
Linus Torvalds 已提交
110
{
111 112
	struct saa7134_dev *dev = video_drvdata(file);
	struct saa7134_fh *fh = file->private_data;
L
Linus Torvalds 已提交
113

114
	if (res_check(fh, RESOURCE_EMPRESS)) {
115 116
		videobuf_stop(&dev->empress_vbq);
		videobuf_mmap_free(&dev->empress_vbq);
L
Linus Torvalds 已提交
117

118 119
		/* stop the encoder */
		ts_reset_encoder(dev);
L
Linus Torvalds 已提交
120

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

126 127
	v4l2_fh_del(&fh->fh);
	v4l2_fh_exit(&fh->fh);
L
Linus Torvalds 已提交
128 129 130 131 132 133
	return 0;
}

static ssize_t
ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
134
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
135

136 137
	if (res_locked(dev, RESOURCE_EMPRESS))
		return -EBUSY;
L
Linus Torvalds 已提交
138 139 140
	if (!dev->empress_started)
		ts_init_encoder(dev);

141
	return videobuf_read_stream(&dev->empress_vbq,
L
Linus Torvalds 已提交
142 143 144 145 146 147 148
				    data, count, ppos, 0,
				    file->f_flags & O_NONBLOCK);
}

static unsigned int
ts_poll(struct file *file, struct poll_table_struct *wait)
{
149
	unsigned long req_events = poll_requested_events(wait);
150
	struct saa7134_dev *dev = video_drvdata(file);
151 152
	struct saa7134_fh *fh = file->private_data;
	unsigned int rc = 0;
L
Linus Torvalds 已提交
153

154 155 156 157
	if (v4l2_event_pending(&fh->fh))
		rc = POLLPRI;
	else if (req_events & POLLPRI)
		poll_wait(file, &fh->fh.wait, wait);
158
	return rc | videobuf_poll_stream(file, &dev->empress_vbq, wait);
L
Linus Torvalds 已提交
159 160 161 162 163 164
}


static int
ts_mmap(struct file *file, struct vm_area_struct * vma)
{
165
	struct saa7134_dev *dev = video_drvdata(file);
L
Linus Torvalds 已提交
166

167
	return videobuf_mmap_mapper(&dev->empress_vbq, vma);
L
Linus Torvalds 已提交
168 169
}

170
static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
171 172 173 174
					struct v4l2_fmtdesc *f)
{
	if (f->index != 0)
		return -EINVAL;
L
Linus Torvalds 已提交
175

176 177
	strlcpy(f->description, "MPEG TS", sizeof(f->description));
	f->pixelformat = V4L2_PIX_FMT_MPEG;
178
	f->flags = V4L2_FMT_FLAG_COMPRESSED;
179 180
	return 0;
}
L
Linus Torvalds 已提交
181

182
static int empress_g_fmt_vid_cap(struct file *file, void *priv,
183 184
				struct v4l2_format *f)
{
185
	struct saa7134_dev *dev = video_drvdata(file);
186
	struct v4l2_mbus_framefmt mbus_fmt;
L
Linus Torvalds 已提交
187

188
	saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
L
Linus Torvalds 已提交
189

190
	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
191 192
	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
193 194
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.priv = 0;
L
Linus Torvalds 已提交
195

196 197
	return 0;
}
L
Linus Torvalds 已提交
198

199
static int empress_s_fmt_vid_cap(struct file *file, void *priv,
200 201
				struct v4l2_format *f)
{
202
	struct saa7134_dev *dev = video_drvdata(file);
203
	struct v4l2_mbus_framefmt mbus_fmt;
L
Linus Torvalds 已提交
204

205 206 207
	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
	saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
L
Linus Torvalds 已提交
208

209 210
	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
211 212
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.priv = 0;
L
Linus Torvalds 已提交
213

214 215 216
	return 0;
}

217 218 219
static int empress_try_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
220
	struct saa7134_dev *dev = video_drvdata(file);
221 222 223 224 225
	struct v4l2_mbus_framefmt mbus_fmt;

	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
	saa_call_all(dev, video, try_mbus_fmt, &mbus_fmt);
	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
226 227 228

	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
229 230
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.priv = 0;
231 232 233

	return 0;
}
234

235
static const struct v4l2_file_operations ts_fops =
L
Linus Torvalds 已提交
236 237 238 239 240 241 242
{
	.owner	  = THIS_MODULE,
	.open	  = ts_open,
	.release  = ts_release,
	.read	  = ts_read,
	.poll	  = ts_poll,
	.mmap	  = ts_mmap,
243
	.ioctl	  = video_ioctl2,
L
Linus Torvalds 已提交
244 245
};

246
static const struct v4l2_ioctl_ops ts_ioctl_ops = {
247
	.vidioc_querycap		= saa7134_querycap,
248
	.vidioc_enum_fmt_vid_cap	= empress_enum_fmt_vid_cap,
249
	.vidioc_try_fmt_vid_cap		= empress_try_fmt_vid_cap,
250 251
	.vidioc_s_fmt_vid_cap		= empress_s_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap		= empress_g_fmt_vid_cap,
252 253 254 255 256 257
	.vidioc_reqbufs			= saa7134_reqbufs,
	.vidioc_querybuf		= saa7134_querybuf,
	.vidioc_qbuf			= saa7134_qbuf,
	.vidioc_dqbuf			= saa7134_dqbuf,
	.vidioc_streamon		= saa7134_streamon,
	.vidioc_streamoff		= saa7134_streamoff,
258 259 260 261 262 263 264 265 266
	.vidioc_g_frequency		= saa7134_g_frequency,
	.vidioc_s_frequency		= saa7134_s_frequency,
	.vidioc_g_tuner			= saa7134_g_tuner,
	.vidioc_s_tuner			= saa7134_s_tuner,
	.vidioc_enum_input		= saa7134_enum_input,
	.vidioc_g_input			= saa7134_g_input,
	.vidioc_s_input			= saa7134_s_input,
	.vidioc_s_std			= saa7134_s_std,
	.vidioc_g_std			= saa7134_g_std,
267
	.vidioc_querystd		= saa7134_querystd,
268 269 270
	.vidioc_log_status		= v4l2_ctrl_log_status,
	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
271 272 273 274 275 276 277 278
};

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

static struct video_device saa7134_empress_template = {
	.name          = "saa7134-empress",
	.fops          = &ts_fops,
	.ioctl_ops     = &ts_ioctl_ops,
279 280

	.tvnorms			= SAA7134_NORMS,
L
Linus Torvalds 已提交
281 282
};

D
David Howells 已提交
283
static void empress_signal_update(struct work_struct *work)
L
Linus Torvalds 已提交
284
{
D
David Howells 已提交
285 286
	struct saa7134_dev* dev =
		container_of(work, struct saa7134_dev, empress_workqueue);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299

	if (dev->nosignal) {
		dprintk("no video signal\n");
	} else {
		dprintk("video signal acquired\n");
	}
}

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

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
{
	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
	case V4L2_CID_HUE:
	case V4L2_CID_CONTRAST:
	case V4L2_CID_SATURATION:
	case V4L2_CID_AUDIO_MUTE:
	case V4L2_CID_AUDIO_VOLUME:
	case V4L2_CID_PRIVATE_INVERT:
	case V4L2_CID_PRIVATE_AUTOMUTE:
		return true;
	default:
		return false;
	}
}
L
Linus Torvalds 已提交
316 317 318

static int empress_init(struct saa7134_dev *dev)
{
319
	struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
L
Linus Torvalds 已提交
320 321
	int err;

322
	dprintk("%s: %s\n",dev->name,__func__);
L
Linus Torvalds 已提交
323 324 325 326
	dev->empress_dev = video_device_alloc();
	if (NULL == dev->empress_dev)
		return -ENOMEM;
	*(dev->empress_dev) = saa7134_empress_template;
327
	dev->empress_dev->v4l2_dev  = &dev->v4l2_dev;
L
Linus Torvalds 已提交
328 329 330 331
	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);
332
	set_bit(V4L2_FL_USE_FH_PRIO, &dev->empress_dev->flags);
333 334 335 336 337 338 339 340 341
	v4l2_ctrl_handler_init(hdl, 21);
	v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
	if (dev->empress_sd)
		v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
	if (hdl->error) {
		video_device_release(dev->empress_dev);
		return hdl->error;
	}
	dev->empress_dev->ctrl_handler = hdl;
L
Linus Torvalds 已提交
342

D
David Howells 已提交
343
	INIT_WORK(&dev->empress_workqueue, empress_signal_update);
L
Linus Torvalds 已提交
344

345
	video_set_drvdata(dev->empress_dev, dev);
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354
	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;
	}
355 356
	printk(KERN_INFO "%s: registered device %s [mpeg]\n",
	       dev->name, video_device_node_name(dev->empress_dev));
L
Linus Torvalds 已提交
357

358
	videobuf_queue_sg_init(&dev->empress_vbq, &saa7134_ts_qops,
359
			    &dev->pci->dev, &dev->slock,
L
Linus Torvalds 已提交
360 361 362
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
			    V4L2_FIELD_ALTERNATE,
			    sizeof(struct saa7134_buf),
363
			    &dev->ts_q, NULL);
L
Linus Torvalds 已提交
364

D
David Howells 已提交
365
	empress_signal_update(&dev->empress_workqueue);
L
Linus Torvalds 已提交
366 367 368 369 370
	return 0;
}

static int empress_fini(struct saa7134_dev *dev)
{
371
	dprintk("%s: %s\n",dev->name,__func__);
L
Linus Torvalds 已提交
372 373 374

	if (NULL == dev->empress_dev)
		return 0;
375
	flush_work(&dev->empress_workqueue);
L
Linus Torvalds 已提交
376
	video_unregister_device(dev->empress_dev);
377
	v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	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);