aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/video/pvrusb2/pvrusb2-dvb.c
blob: bde85c9730583940cca430a4aa16045d402c52d2 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
102
103
104
105
106
107
108
109
110
111
112
113
114
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
151
152
153
154
155
156
157
158
159
160
161
162
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
206
207
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
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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 *  pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
 *
 *  Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
 *
 *  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/kthread.h>
#include <linux/freezer.h>
#include "dvbdev.h"
#include "pvrusb2-hdw-internal.h"
#include "pvrusb2-hdw.h"
#include "pvrusb2-io.h"
#include "pvrusb2-dvb.h"

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

#define BUFFER_COUNT 32
#define BUFFER_SIZE PAGE_ALIGN(0x4000)

struct pvr2_dvb_fh {
	struct pvr2_channel	channel;
	struct pvr2_stream	*stream;
	struct pvr2_dvb_adapter	*adap;
	wait_queue_head_t	wait_data;
	char			*buffer_storage[BUFFER_COUNT];
};

static void pvr2_dvb_notify(struct pvr2_dvb_fh *fhp)
{
	wake_up(&fhp->wait_data);
}

static int pvr2_dvb_fh_init(struct pvr2_dvb_fh *fh,
			    struct pvr2_dvb_adapter *adap)
{
	struct pvr2_context *pvr = adap->pvr;
	unsigned int idx;
	int ret;
	struct pvr2_buffer *bp;

	init_waitqueue_head(&fh->wait_data);

	fh->adap = adap;

	pvr2_channel_init(&fh->channel, adap->pvr);

	ret = pvr2_channel_claim_stream(&fh->channel, &pvr->video_stream);
	/* somebody else already has the stream */
	if (ret != 0)
		return ret;

	fh->stream = pvr->video_stream.stream;

	for (idx = 0; idx < BUFFER_COUNT; idx++) {
		fh->buffer_storage[idx] = kmalloc(BUFFER_SIZE, GFP_KERNEL);
		if (!(fh->buffer_storage[idx]))
			break;
	}

	if (idx < BUFFER_COUNT) {
		/* An allocation appears to have failed */
		ret = -ENOMEM;
		goto cleanup;
	}

	pvr2_stream_set_callback(pvr->video_stream.stream,
				 (pvr2_stream_callback) pvr2_dvb_notify, fh);

	ret = pvr2_stream_set_buffer_count(fh->stream, BUFFER_COUNT);
	if (ret < 0)
		return ret;

	for (idx = 0; idx < BUFFER_COUNT; idx++) {
		bp = pvr2_stream_get_buffer(fh->stream, idx);
		pvr2_buffer_set_buffer(bp,
				       fh->buffer_storage[idx],
				       BUFFER_SIZE);
	}

	ret = pvr2_hdw_set_streaming(fh->channel.hdw, 1);
	if (ret < 0)
		goto cleanup;

	while ((bp = pvr2_stream_get_idle_buffer(fh->stream)) != 0) {
		ret = pvr2_buffer_queue(bp);
		if (ret < 0)
			goto cleanup;
	}

	return ret;

cleanup:
	if (fh->stream)
		pvr2_stream_kill(fh->stream);

	for (idx = 0; idx < BUFFER_COUNT; idx++) {
		if (!(fh->buffer_storage[idx]))
			continue;

		kfree(fh->buffer_storage[idx]);
	}
	pvr2_channel_done(&fh->channel);

	return ret;
}

static void pvr2_dvb_fh_done(struct pvr2_dvb_fh *fh)
{
	unsigned int idx;

	pvr2_hdw_set_streaming(fh->channel.hdw, 0);

	pvr2_stream_kill(fh->stream);

//	pvr2_channel_claim_stream(&fh->channel, NULL);

	for (idx = 0; idx < BUFFER_COUNT; idx++) {
		if (!(fh->buffer_storage[idx]))
			continue;

		kfree(fh->buffer_storage[idx]);
	}

	pvr2_channel_done(&fh->channel);
}

static int pvr2_dvb_feed_thread(void *data)
{
	struct pvr2_dvb_adapter *adap = data;
	struct pvr2_dvb_fh fh;
	int ret;
	unsigned int count;
	struct pvr2_buffer *bp;

	printk(KERN_DEBUG "dvb thread started\n");
	set_freezable();

	memset(&fh, 0, sizeof(fh));

	ret = pvr2_dvb_fh_init(&fh, adap);
	if (ret != 0)
		return ret;

	for (;;) {
		if ((0 == adap->feedcount) || (kthread_should_stop()))
			break;

		/* Not sure about this... */
		try_to_freeze();

		bp = pvr2_stream_get_ready_buffer(fh.stream);
		if (bp != NULL) {
			count = pvr2_buffer_get_count(bp);
			if (count) {
				dvb_dmx_swfilter(
					&adap->demux,
					fh.buffer_storage[
						pvr2_buffer_get_id(bp)],
					count);
			} else {
				ret = pvr2_buffer_get_status(bp);
				if (ret < 0)
					break;
			}
			ret = pvr2_buffer_queue(bp);
			if (ret < 0)
				break;

			/* Since we know we did something to a buffer,
			   just go back and try again.  No point in
			   blocking unless we really ran out of
			   buffers to process. */
			continue;
		}


		/* Wait until more buffers become available. */
		ret = wait_event_interruptible(
			fh.wait_data,
			pvr2_stream_get_ready_count(fh.stream) > 0);
		if (ret < 0)
			break;
	}

	pvr2_dvb_fh_done(&fh);

	/* If we get here and ret is < 0, then an error has occurred.
	   Probably would be a good idea to communicate that to DVB core... */

	printk(KERN_DEBUG "dvb thread stopped\n");

	/* from videobuf-dvb.c: */
	while (!kthread_should_stop()) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule();
	}
	return 0;
}

static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
{
	struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
	int newfeedcount, ret = 0;

	if (adap == NULL)
		return -ENODEV;

	mutex_lock(&adap->lock);
	newfeedcount = adap->feedcount + (onoff ? 1 : -1);

	if (newfeedcount == 0) {
		printk(KERN_DEBUG "stop feeding\n");

		ret = kthread_stop(adap->thread);
		adap->thread = NULL;
	}

	adap->feedcount = newfeedcount;

	if (adap->feedcount == onoff && adap->feedcount > 0) {
		if (NULL != adap->thread)
			goto fail;

		printk(KERN_DEBUG "start feeding\n");

		adap->thread = kthread_run(pvr2_dvb_feed_thread,
					   adap, "pvrusb2-dvb");
		if (IS_ERR(adap->thread)) {
			ret = PTR_ERR(adap->thread);
			adap->thread = NULL;
		}
		//ret = newfeedcount;
	}
fail:
	mutex_unlock(&adap->lock);

	return ret;
}

static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
	       dvbdmxfeed->pid, dvbdmxfeed->type);
	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
}

static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
	       dvbdmxfeed->pid, dvbdmxfeed->type);
	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
}

static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
{
	/* TO DO: This function will call into the core and request for
	 * input to be set to 'dtv' if (acquire) and if it isn't set already.
	 *
	 * If (!acquire) then we should do nothing -- don't switch inputs
	 * again unless the analog side of the driver requests the bus.
	 */
	return 0;
}

static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
{
	int ret;

	ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
				   THIS_MODULE/*&hdw->usb_dev->owner*/,
				   &adap->pvr->hdw->usb_dev->dev,
				   adapter_nr);
	if (ret < 0) {
		err("dvb_register_adapter failed: error %d", ret);
		goto err;
	}
	adap->dvb_adap.priv = adap;

	adap->demux.dmx.capabilities = DMX_TS_FILTERING |
				       DMX_SECTION_FILTERING |
				       DMX_MEMORY_BASED_FILTERING;
	adap->demux.priv             = adap;
	adap->demux.filternum        = 256;
	adap->demux.feednum          = 256;
	adap->demux.start_feed       = pvr2_dvb_start_feed;
	adap->demux.stop_feed        = pvr2_dvb_stop_feed;
	adap->demux.write_to_decoder = NULL;

	ret = dvb_dmx_init(&adap->demux);
	if (ret < 0) {
		err("dvb_dmx_init failed: error %d", ret);
		goto err_dmx;
	}

	adap->dmxdev.filternum       = adap->demux.filternum;
	adap->dmxdev.demux           = &adap->demux.dmx;
	adap->dmxdev.capabilities    = 0;

	ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
	if (ret < 0) {
		err("dvb_dmxdev_init failed: error %d", ret);
		goto err_dmx_dev;
	}

	dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);

	adap->digital_up = 1;

	return 0;

err_dmx_dev:
	dvb_dmx_release(&adap->demux);
err_dmx:
	dvb_unregister_adapter(&adap->dvb_adap);
err:
	return ret;
}

static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
{
	if (adap->digital_up) {
		printk(KERN_DEBUG "unregistering DVB devices\n");
		dvb_net_release(&adap->dvb_net);
		adap->demux.dmx.close(&adap->demux.dmx);
		dvb_dmxdev_release(&adap->dmxdev);
		dvb_dmx_release(&adap->demux);
		dvb_unregister_adapter(&adap->dvb_adap);
		adap->digital_up = 0;
	}
	return 0;
}

static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
{
	struct pvr2_dvb_props *dvb_props = adap->pvr->hdw->hdw_desc->dvb_props;

	if (dvb_props == NULL) {
		err("fe_props not defined!");
		return -EINVAL;
	}

	if (dvb_props->frontend_attach == NULL) {
		err("frontend_attach not defined!");
		return -EINVAL;
	}

	if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {

		if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
			err("frontend registration failed!");
			dvb_frontend_detach(adap->fe);
			adap->fe = NULL;
			return -ENODEV;
		}

		if (dvb_props->tuner_attach)
			dvb_props->tuner_attach(adap);

		if (adap->fe->ops.analog_ops.standby)
			adap->fe->ops.analog_ops.standby(adap->fe);

		/* Ensure all frontends negotiate bus access */
		adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;

	} else {
		err("no frontend was attached!");
		return -ENODEV;
	}

	return 0;
}

static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
{
	if (adap->fe != NULL) {
		dvb_unregister_frontend(adap->fe);
		dvb_frontend_detach(adap->fe);
	}
	return 0;
}

int pvr2_dvb_init(struct pvr2_context *pvr)
{
	int ret = 0;

	pvr->hdw->dvb.pvr = pvr;
	mutex_init(&pvr->hdw->dvb.lock);

	ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb);
	if (ret < 0)
		goto fail;

	ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb);
fail:
	return ret;
}

int pvr2_dvb_exit(struct pvr2_context *pvr)
{
	pvr2_dvb_frontend_exit(&pvr->hdw->dvb);
	pvr2_dvb_adapter_exit(&pvr->hdw->dvb);

	pvr->hdw->dvb.pvr = NULL;

	return 0;
}