dvb_usb_dvb.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* dvb-usb-dvb.c is part of the DVB USB library.
 *
 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
 * see dvb-usb-init.c for copyright information.
 *
 * This file contains functions for initializing and handling the
 * linux-dvb API.
 */
#include "dvb_usb_common.h"

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static void dvb_usb_data_complete(struct usb_data_stream *stream, u8 *buffer,
		size_t length)
{
	struct dvb_usb_adapter *adap = stream->user_priv;
	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
		dvb_dmx_swfilter(&adap->demux, buffer, length);
}

static void dvb_usb_data_complete_204(struct usb_data_stream *stream,
		u8 *buffer, size_t length)
{
	struct dvb_usb_adapter *adap = stream->user_priv;
	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
		dvb_dmx_swfilter_204(&adap->demux, buffer, length);
}

static void dvb_usb_data_complete_raw(struct usb_data_stream *stream,
				      u8 *buffer, size_t length)
{
	struct dvb_usb_adapter *adap = stream->user_priv;
	if (adap->feedcount > 0 && adap->state & DVB_USB_ADAP_STATE_DVB)
		dvb_dmx_swfilter_raw(&adap->demux, buffer, length);
}

35
int dvb_usbv2_adapter_stream_init(struct dvb_usb_adapter *adap)
36
{
37
	pr_debug("%s: adap=%d\n", __func__, adap->id);
38 39 40 41 42

	adap->stream.udev = adap->dev->udev;
	adap->stream.user_priv = adap;
	adap->stream.complete = dvb_usb_data_complete;

43
	return usb_urb_initv2(&adap->stream, &adap->props->stream);
44 45
}

46
int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
47
{
48
	pr_debug("%s: adap=%d\n", __func__, adap->id);
49
	usb_urb_exitv2(&adap->stream);
50

51 52 53
	return 0;
}

54 55 56 57 58 59
/* does the complete input transfer handling */
static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
{
	struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
	int newfeedcount, ret;

60
	if (adap == NULL) {
61 62
		ret = -ENODEV;
		goto err;
63 64
	}

65 66 67
	pr_debug("%s: adap=%d active_fe=%d\n", __func__, adap->id,
			adap->active_fe);

68 69 70 71
	newfeedcount = adap->feedcount + (onoff ? 1 : -1);

	/* stop feed before setting a new pid if there will be no pid anymore */
	if (newfeedcount == 0) {
72
		pr_debug("%s: stop feeding\n", __func__);
73
		usb_urb_killv2(&adap->stream);
74

75 76
		if (adap->dev->props->streaming_ctrl != NULL) {
			ret = adap->dev->props->streaming_ctrl(adap, 0);
77
			if (ret < 0) {
78
				pr_err("%s: error while stopping stream\n",
79
						KBUILD_MODNAME);
80
				goto err_mutex_unlock;
81 82
			}
		}
83
		mutex_unlock(&adap->sync_mutex);
84 85 86 87 88
	}

	adap->feedcount = newfeedcount;

	/* activate the pid on the device specific pid_filter */
89 90 91 92
	pr_debug("%s: setting pid (%s): %5d %04x at index %d '%s'\n", __func__,
			adap->pid_filtering ? "yes" : "no", dvbdmxfeed->pid,
			dvbdmxfeed->pid, dvbdmxfeed->index,
			onoff ? "on" : "off");
93
	if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
94
			adap->pid_filtering &&
95 96
			adap->props->pid_filter != NULL)
		adap->props->pid_filter(adap, dvbdmxfeed->index,
97
				dvbdmxfeed->pid, onoff);
98

99 100
	/*
	 * Start the feed if this was the first feed and there is still a feed
101 102 103
	 * for reception.
	 */
	if (adap->feedcount == onoff && adap->feedcount > 0) {
104
		struct usb_data_stream_properties stream_props;
105
		unsigned int ts_props;
106
		mutex_lock(&adap->sync_mutex);
107 108

		/* resolve TS configuration */
109 110
		if (adap->dev->props->get_ts_config) {
			ret = adap->dev->props->get_ts_config(
111
					adap->fe[adap->active_fe],
112 113
					&ts_props);
			if (ret < 0)
114
				goto err_mutex_unlock;
115 116 117 118 119 120 121 122 123 124
		} else {
			ts_props = 0; /* normal 188 payload only TS */
		}

		if (ts_props & DVB_USB_ADAP_RECEIVES_204_BYTE_TS)
			adap->stream.complete = dvb_usb_data_complete_204;
		else if (ts_props & DVB_USB_ADAP_RECEIVES_RAW_PAYLOAD)
			adap->stream.complete = dvb_usb_data_complete_raw;
		else
			adap->stream.complete = dvb_usb_data_complete;
125 126

		/* resolve USB stream configuration */
127
		if (adap->dev->props->get_usb_stream_config) {
128 129
			memcpy(&stream_props, &adap->props->stream,
				sizeof(struct usb_data_stream_properties));
130
			ret = adap->dev->props->get_usb_stream_config(
131
					adap->fe[adap->active_fe],
132 133
					&stream_props);
			if (ret < 0)
134
				goto err_mutex_unlock;
135
		} else {
136
			stream_props = adap->props->stream;
137 138
		}

139
		pr_debug("%s: submitting all URBs\n", __func__);
140
		usb_urb_submitv2(&adap->stream, &stream_props);
141

142
		pr_debug("%s: controlling pid parser\n", __func__);
143 144
		if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
				adap->props->caps &
145
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
146 147
				adap->props->pid_filter_ctrl != NULL) {
			ret = adap->props->pid_filter_ctrl(adap,
148
					adap->pid_filtering);
149
			if (ret < 0) {
150
				pr_err("%s: could not handle pid_parser\n",
151
						KBUILD_MODNAME);
152
				goto err_mutex_unlock;
153 154
			}
		}
155
		pr_debug("%s: start feeding\n", __func__);
156 157
		if (adap->dev->props->streaming_ctrl != NULL) {
			ret = adap->dev->props->streaming_ctrl(adap, 1);
158
			if (ret < 0) {
159
				pr_err("%s: error while enabling fifo\n",
160
						KBUILD_MODNAME);
161
				goto err_mutex_unlock;
162 163 164 165
			}
		}

	}
166

167
	return 0;
168 169
err_mutex_unlock:
	mutex_unlock(&adap->sync_mutex);
170 171 172
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
173 174 175 176
}

static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
177
	pr_debug("%s: start pid=%04x feedtype=%d\n", __func__, dvbdmxfeed->pid,
178
			dvbdmxfeed->type);
179
	return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
180 181 182 183
}

static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
184
	pr_debug("%s: stop pid=%04x feedtype=%d\n", __func__, dvbdmxfeed->pid,
185
			dvbdmxfeed->type);
186
	return dvb_usb_ctrl_feed(dvbdmxfeed, 0);
187 188
}

189
int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
190
{
191 192 193 194 195 196
	int ret;
	pr_debug("%s: adap=%d\n", __func__, adap->id);

	ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->name,
			adap->dev->props->owner, &adap->dev->udev->dev,
			adap->dev->props->adapter_nr);
197
	if (ret < 0) {
198 199
		pr_debug("%s: dvb_register_adapter() failed=%d\n", __func__,
				ret);
200 201
		goto err;
	}
202

203 204
	adap->dvb_adap.priv = adap;

205
	if (adap->dev->props->read_mac_address) {
206 207 208 209
		ret = adap->dev->props->read_mac_address(adap,
				adap->dvb_adap.proposed_mac);
		if (ret < 0)
			goto err_dmx;
210

211 212 213
		pr_info("%s: MAC address: %pM\n", KBUILD_MODNAME,
				adap->dvb_adap.proposed_mac);
	}
214 215 216 217

	adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
	adap->demux.priv             = adap;
	adap->demux.filternum        = 0;
218 219
	if (adap->demux.filternum < adap->max_feed_count)
		adap->demux.filternum = adap->max_feed_count;
220 221 222 223
	adap->demux.feednum          = adap->demux.filternum;
	adap->demux.start_feed       = dvb_usb_start_feed;
	adap->demux.stop_feed        = dvb_usb_stop_feed;
	adap->demux.write_to_decoder = NULL;
224 225
	ret = dvb_dmx_init(&adap->demux);
	if (ret < 0) {
226
		pr_err("%s: dvb_dmx_init() failed=%d\n", KBUILD_MODNAME, ret);
227 228 229 230 231 232
		goto err_dmx;
	}

	adap->dmxdev.filternum       = adap->demux.filternum;
	adap->dmxdev.demux           = &adap->demux.dmx;
	adap->dmxdev.capabilities    = 0;
233 234
	ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
	if (ret < 0) {
235 236
		pr_err("%s: dvb_dmxdev_init() failed=%d\n", KBUILD_MODNAME,
				ret);
237 238 239
		goto err_dmx_dev;
	}

240 241
	ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
	if (ret < 0) {
242
		pr_err("%s: dvb_net_init() failed=%d\n", KBUILD_MODNAME, ret);
243 244 245
		goto err_net_init;
	}

246
	mutex_init(&adap->sync_mutex);
247 248 249 250 251 252 253 254 255 256 257 258 259
	adap->state |= DVB_USB_ADAP_STATE_DVB;
	return 0;

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

260
int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
261
{
262 263
	pr_debug("%s: adap=%d\n", __func__, adap->id);

264
	if (adap->state & DVB_USB_ADAP_STATE_DVB) {
265
		pr_debug("%s: unregistering DVB part\n", __func__);
266 267 268 269 270 271 272 273 274 275
		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->state &= ~DVB_USB_ADAP_STATE_DVB;
	}
	return 0;
}

276
static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
277
{
278
	int ret;
279
	struct dvb_usb_adapter *adap = fe->dvb->priv;
280
	mutex_lock(&adap->sync_mutex);
281
	pr_debug("%s: adap=%d fe=%d\n", __func__, adap->id, fe->id);
282

283
	ret = dvb_usbv2_device_power_ctrl(adap->dev, 1);
284 285
	if (ret < 0)
		goto err;
286

287 288
	if (adap->dev->props->frontend_ctrl) {
		ret = adap->dev->props->frontend_ctrl(fe, 1);
289 290
		if (ret < 0)
			goto err;
291 292
	}

293 294 295 296 297
	if (adap->fe_init[fe->id]) {
		ret = adap->fe_init[fe->id](fe);
		if (ret < 0)
			goto err;
	}
298

299
	adap->active_fe = fe->id;
300
	mutex_unlock(&adap->sync_mutex);
301 302

	return 0;
303
err:
304
	mutex_unlock(&adap->sync_mutex);
305 306
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
307 308 309 310
}

static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
{
311
	int ret;
312
	struct dvb_usb_adapter *adap = fe->dvb->priv;
313
	mutex_lock(&adap->sync_mutex);
314
	pr_debug("%s: adap=%d fe=%d\n", __func__, adap->id, fe->id);
315

316 317 318 319 320
	if (adap->fe_sleep[fe->id]) {
		ret = adap->fe_sleep[fe->id](fe);
		if (ret < 0)
			goto err;
	}
321

322 323
	if (adap->dev->props->frontend_ctrl) {
		ret = adap->dev->props->frontend_ctrl(fe, 0);
324 325 326
		if (ret < 0)
			goto err;
	}
327

328
	ret = dvb_usbv2_device_power_ctrl(adap->dev, 0);
329 330 331 332
	if (ret < 0)
		goto err;

	adap->active_fe = -1;
333
	mutex_unlock(&adap->sync_mutex);
334 335 336

	return 0;
err:
337
	mutex_unlock(&adap->sync_mutex);
338 339
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
340 341
}

342
int dvb_usbv2_adapter_frontend_init(struct dvb_usb_adapter *adap)
343
{
344
	int ret, i, count_registered = 0;
345
	pr_debug("%s: adap=%d\n", __func__, adap->id);
346

347
	memset(adap->fe, 0, sizeof(adap->fe));
348
	adap->active_fe = -1;
349

350 351
	if (adap->dev->props->frontend_attach) {
		ret = adap->dev->props->frontend_attach(adap);
352 353 354 355 356 357 358
		if (ret < 0) {
			pr_debug("%s: frontend_attach() failed=%d\n", __func__,
					ret);
			goto err_dvb_frontend_detach;
		}
	} else {
		pr_debug("%s: frontend_attach() do not exists\n", __func__);
359 360 361
		ret = 0;
		goto err;
	}
362

363
	for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
364
		adap->fe[i]->id = i;
365 366

		/* re-assign sleep and wakeup functions */
367 368 369 370
		adap->fe_init[i] = adap->fe[i]->ops.init;
		adap->fe[i]->ops.init  = dvb_usb_fe_wakeup;
		adap->fe_sleep[i] = adap->fe[i]->ops.sleep;
		adap->fe[i]->ops.sleep = dvb_usb_fe_sleep;
371

372 373
		ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
		if (ret < 0) {
374 375 376
			pr_err("%s: frontend%d registration failed\n",
					KBUILD_MODNAME, i);
			goto err_dvb_unregister_frontend;
377 378
		}

379
		count_registered++;
380 381
	}

382 383 384 385 386 387 388 389 390
	if (adap->dev->props->tuner_attach) {
		ret = adap->dev->props->tuner_attach(adap);
		if (ret < 0) {
			pr_debug("%s: tuner_attach() failed=%d\n", __func__,
					ret);
			goto err_dvb_unregister_frontend;
		}
	}

391
	return 0;
392 393 394 395 396 397 398 399 400 401 402

err_dvb_unregister_frontend:
	for (i = count_registered - 1; i >= 0; i--)
		dvb_unregister_frontend(adap->fe[i]);

err_dvb_frontend_detach:
	for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
		if (adap->fe[i])
			dvb_frontend_detach(adap->fe[i]);
	}

403 404 405
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
406 407
}

408
int dvb_usbv2_adapter_frontend_exit(struct dvb_usb_adapter *adap)
409
{
410
	int i;
411
	pr_debug("%s: adap=%d\n", __func__, adap->id);
412

413
	for (i = MAX_NO_OF_FE_PER_ADAP - 1; i >= 0; i--) {
414
		if (adap->fe[i]) {
415 416
			dvb_unregister_frontend(adap->fe[i]);
			dvb_frontend_detach(adap->fe[i]);
417 418
		}
	}
419

420 421
	return 0;
}