videobuf-dvb.c 9.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
/*
 *
 * some helper function for simple DVB cards which simply DMA the
 * complete transport stream and let the computer sort everything else
 * (i.e. we are using the software demux, ...).  Also uses the
 * video-buf to manage DMA buffers.
 *
 * (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.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/file.h>
22

23
#include <linux/freezer.h>
L
Linus Torvalds 已提交
24

25
#include <media/videobuf-core.h>
26
#include <media/videobuf-dvb.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32

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

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

33
static unsigned int debug;
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug,"enable debug messages");

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

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

static int videobuf_dvb_thread(void *data)
{
	struct videobuf_dvb *dvb = data;
	struct videobuf_buffer *buf;
	unsigned long flags;
	int err;
48
	void *outp;
L
Linus Torvalds 已提交
49 50

	dprintk("dvb thread started\n");
51
	set_freezable();
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
	videobuf_read_start(&dvb->dvbq);

	for (;;) {
		/* fetch next buffer */
		buf = list_entry(dvb->dvbq.stream.next,
				 struct videobuf_buffer, stream);
		list_del(&buf->stream);
		err = videobuf_waiton(buf,0,1);

		/* no more feeds left or stop_feed() asked us to quit */
		if (0 == dvb->nfeeds)
			break;
		if (kthread_should_stop())
			break;
66
		try_to_freeze();
L
Linus Torvalds 已提交
67 68

		/* feed buffer data to demux */
69 70
		outp = videobuf_queue_to_vmalloc (&dvb->dvbq, buf);

71
		if (buf->state == VIDEOBUF_DONE)
72
			dvb_dmx_swfilter(&dvb->demux, outp,
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
					 buf->size);

		/* requeue buffer */
		list_add_tail(&buf->stream,&dvb->dvbq.stream);
		spin_lock_irqsave(dvb->dvbq.irqlock,flags);
		dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
		spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
	}

	videobuf_read_stop(&dvb->dvbq);
	dprintk("dvb thread stopped\n");

	/* Hmm, linux becomes *very* unhappy without this ... */
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
	}
	return 0;
}

static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux  = feed->demux;
	struct videobuf_dvb *dvb = demux->priv;
	int rc;

	if (!demux->dmx.frontend)
		return -EINVAL;

102
	mutex_lock(&dvb->lock);
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
	dvb->nfeeds++;
	rc = dvb->nfeeds;

	if (NULL != dvb->thread)
		goto out;
	dvb->thread = kthread_run(videobuf_dvb_thread,
				  dvb, "%s dvb", dvb->name);
	if (IS_ERR(dvb->thread)) {
		rc = PTR_ERR(dvb->thread);
		dvb->thread = NULL;
	}

out:
116
	mutex_unlock(&dvb->lock);
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125
	return rc;
}

static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
{
	struct dvb_demux *demux  = feed->demux;
	struct videobuf_dvb *dvb = demux->priv;
	int err = 0;

126
	mutex_lock(&dvb->lock);
L
Linus Torvalds 已提交
127 128 129 130 131 132
	dvb->nfeeds--;
	if (0 == dvb->nfeeds  &&  NULL != dvb->thread) {
		// FIXME: cx8802_cancel_buffers(dev);
		err = kthread_stop(dvb->thread);
		dvb->thread = NULL;
	}
133
	mutex_unlock(&dvb->lock);
L
Linus Torvalds 已提交
134 135 136 137
	return err;
}

/* ------------------------------------------------------------------ */
138 139 140 141 142
/* Register a single adapter and one or more frontends */
int videobuf_dvb_register_bus(struct videobuf_dvb_frontends *f,
			  struct module *module,
			  void *adapter_priv,
			  struct device *device,
143 144
			  short *adapter_nr,
			  int mfe_shared)
145 146 147 148 149 150 151 152 153 154 155 156
{
	struct list_head *list, *q;
	struct videobuf_dvb_frontend *fe;
	int res = -EINVAL;

	fe = videobuf_dvb_get_frontend(f, 1);
	if (!fe) {
		printk(KERN_WARNING "Unable to register the adapter which has no frontends\n");
		goto err;
	}

	/* Bring up the adapter */
157
	res = videobuf_dvb_register_adapter(f, module, adapter_priv, device, fe->dvb.name, adapter_nr, mfe_shared);
158 159 160 161
	if (res < 0) {
		printk(KERN_WARNING "videobuf_dvb_register_adapter failed (errno = %d)\n", res);
		goto err;
	}
L
Linus Torvalds 已提交
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	/* Attach all of the frontends to the adapter */
	mutex_lock(&f->lock);
	list_for_each_safe(list, q, &f->frontend.felist) {
		fe = list_entry(list, struct videobuf_dvb_frontend, felist);

		res = videobuf_dvb_register_frontend(&f->adapter, &fe->dvb);
		if (res < 0) {
			printk(KERN_WARNING "%s: videobuf_dvb_register_frontend failed (errno = %d)\n",
				fe->dvb.name, res);
		}
	}
	mutex_unlock(&f->lock);

err:
	return res;
}

int videobuf_dvb_register_adapter(struct videobuf_dvb_frontends *fe,
L
Linus Torvalds 已提交
181
			  struct module *module,
182
			  void *adapter_priv,
183
			  struct device *device,
184
			  char *adapter_name,
185 186
			  short *adapter_nr,
			  int mfe_shared)
L
Linus Torvalds 已提交
187 188 189
{
	int result;

190
	mutex_init(&fe->lock);
L
Linus Torvalds 已提交
191 192

	/* register adapter */
193
	result = dvb_register_adapter(&fe->adapter, adapter_name, module, device, adapter_nr);
L
Linus Torvalds 已提交
194 195
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
196
		       adapter_name, result);
L
Linus Torvalds 已提交
197
	}
198
	fe->adapter.priv = adapter_priv;
199
	fe->adapter.mfe_shared = mfe_shared;
200 201 202 203 204 205 206

	return result;
}

int videobuf_dvb_register_frontend(struct dvb_adapter *adapter, struct videobuf_dvb *dvb)
{
	int result;
L
Linus Torvalds 已提交
207 208

	/* register frontend */
209
	result = dvb_register_frontend(adapter, dvb->frontend);
L
Linus Torvalds 已提交
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
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
		       dvb->name, result);
		goto fail_frontend;
	}

	/* register demux stuff */
	dvb->demux.dmx.capabilities =
		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
		DMX_MEMORY_BASED_FILTERING;
	dvb->demux.priv       = dvb;
	dvb->demux.filternum  = 256;
	dvb->demux.feednum    = 256;
	dvb->demux.start_feed = videobuf_dvb_start_feed;
	dvb->demux.stop_feed  = videobuf_dvb_stop_feed;
	result = dvb_dmx_init(&dvb->demux);
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
		       dvb->name, result);
		goto fail_dmx;
	}

	dvb->dmxdev.filternum    = 256;
	dvb->dmxdev.demux        = &dvb->demux.dmx;
	dvb->dmxdev.capabilities = 0;
235 236
	result = dvb_dmxdev_init(&dvb->dmxdev, adapter);

L
Linus Torvalds 已提交
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
	if (result < 0) {
		printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
		       dvb->name, result);
		goto fail_dmxdev;
	}

	dvb->fe_hw.source = DMX_FRONTEND_0;
	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
		printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
		       dvb->name, result);
		goto fail_fe_hw;
	}

	dvb->fe_mem.source = DMX_MEMORY_FE;
	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
	if (result < 0) {
		printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
		       dvb->name, result);
		goto fail_fe_mem;
	}

	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
	if (result < 0) {
		printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
		       dvb->name, result);
		goto fail_fe_conn;
	}

	/* register network adapter */
267
	dvb_net_init(adapter, &dvb->net, &dvb->demux.dmx);
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280
	return 0;

fail_fe_conn:
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
fail_fe_mem:
	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
fail_fe_hw:
	dvb_dmxdev_release(&dvb->dmxdev);
fail_dmxdev:
	dvb_dmx_release(&dvb->demux);
fail_dmx:
	dvb_unregister_frontend(dvb->frontend);
fail_frontend:
281
	dvb_frontend_detach(dvb->frontend);
282
	dvb_unregister_adapter(adapter);
283

L
Linus Torvalds 已提交
284 285 286
	return result;
}

287
void videobuf_dvb_unregister_bus(struct videobuf_dvb_frontends *f)
L
Linus Torvalds 已提交
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	struct list_head *list, *q;
	struct videobuf_dvb_frontend *fe;

	mutex_lock(&f->lock);
	list_for_each_safe(list, q, &f->frontend.felist) {
		fe = list_entry(list, struct videobuf_dvb_frontend, felist);

		dvb_net_release(&fe->dvb.net);
		fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx, &fe->dvb.fe_mem);
		fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx, &fe->dvb.fe_hw);
		dvb_dmxdev_release(&fe->dvb.dmxdev);
		dvb_dmx_release(&fe->dvb.demux);
		dvb_unregister_frontend(fe->dvb.frontend);
		dvb_frontend_detach(fe->dvb.frontend);

		list_del(list);
		kfree(fe);
	}
	mutex_unlock(&f->lock);

	dvb_unregister_adapter(&f->adapter);
}

struct videobuf_dvb_frontend * videobuf_dvb_get_frontend(struct videobuf_dvb_frontends *f, int id)
{
	struct list_head *list, *q;
	struct videobuf_dvb_frontend *fe, *ret = NULL;

	mutex_lock(&f->lock);

	list_for_each_safe(list, q, &f->frontend.felist) {
		fe = list_entry(list, struct videobuf_dvb_frontend, felist);
		if (fe->id == id) {
			ret = fe;
			break;
		}
	}

	mutex_unlock(&f->lock);

	return ret;
}

int videobuf_dvb_find_frontend(struct videobuf_dvb_frontends *f, struct dvb_frontend *p)
{
	struct list_head *list, *q;
	struct videobuf_dvb_frontend *fe = NULL;
	int ret = 0;

	mutex_lock(&f->lock);

	list_for_each_safe(list, q, &f->frontend.felist) {
		fe = list_entry(list, struct videobuf_dvb_frontend, felist);
		if (fe->dvb.frontend == p) {
			ret = fe->id;
			break;
		}
	}

	mutex_unlock(&f->lock);

	return ret;
}

struct videobuf_dvb_frontend * videobuf_dvb_alloc_frontend(void *private, struct videobuf_dvb_frontends *f, int id)
{
	struct videobuf_dvb_frontend *fe;

	fe = kzalloc(sizeof(struct videobuf_dvb_frontend),GFP_KERNEL);
	if (fe == NULL)
		goto fail_alloc;

	fe->dev = private;
	fe->id = id;
	mutex_init(&fe->dvb.lock);

	mutex_lock(&f->lock);
	list_add_tail(&fe->felist,&f->frontend.felist);
	mutex_unlock(&f->lock);

fail_alloc:
	return fe;
L
Linus Torvalds 已提交
371 372
}

373 374 375 376 377
EXPORT_SYMBOL(videobuf_dvb_register_bus);
EXPORT_SYMBOL(videobuf_dvb_unregister_bus);
EXPORT_SYMBOL(videobuf_dvb_alloc_frontend);
EXPORT_SYMBOL(videobuf_dvb_get_frontend);
EXPORT_SYMBOL(videobuf_dvb_find_frontend);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385

/* ------------------------------------------------------------------ */
/*
 * Local variables:
 * c-basic-offset: 8
 * compile-command: "make DVB=1"
 * End:
 */