dvb_usb_dvb.c 10.8 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 38 39 40 41 42 43
{
	int ret;
	struct usb_data_stream_properties stream_props;

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

	/* resolve USB stream configuration for buffer alloc */
44 45
	if (adap->dev->props->get_usb_stream_config) {
		ret = adap->dev->props->get_usb_stream_config(NULL,
46 47 48 49
				&stream_props);
		if (ret < 0)
			return ret;
	} else {
50
		stream_props = adap->props->stream;
51 52 53 54 55
	}

	/* FIXME: can be removed as set later in anyway */
	adap->stream.complete = dvb_usb_data_complete;

56
	return usb_urb_initv2(&adap->stream, &stream_props);
57 58
}

59
int dvb_usbv2_adapter_stream_exit(struct dvb_usb_adapter *adap)
60
{
61
	usb_urb_exitv2(&adap->stream);
62 63 64
	return 0;
}

65 66 67 68 69 70
/* 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;

71
	if (adap == NULL) {
72 73
		ret = -ENODEV;
		goto err;
74 75
	}

76 77 78
	pr_debug("%s: adap=%d active_fe=%d\n", __func__, adap->id,
			adap->active_fe);

79 80 81 82
	newfeedcount = adap->feedcount + (onoff ? 1 : -1);

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

86 87
		if (adap->dev->props->streaming_ctrl != NULL) {
			ret = adap->dev->props->streaming_ctrl(adap, 0);
88
			if (ret < 0) {
89
				pr_err("%s: error while stopping stream\n",
90
						KBUILD_MODNAME);
91 92 93 94 95 96 97 98
				return ret;
			}
		}
	}

	adap->feedcount = newfeedcount;

	/* activate the pid on the device specific pid_filter */
99 100 101 102
	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");
103
	if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
104
			adap->pid_filtering &&
105 106
			adap->props->pid_filter != NULL)
		adap->props->pid_filter(adap, dvbdmxfeed->index,
107
				dvbdmxfeed->pid, onoff);
108 109 110 111 112

	/* start the feed if this was the first feed and there is still a feed
	 * for reception.
	 */
	if (adap->feedcount == onoff && adap->feedcount > 0) {
113
		struct usb_data_stream_properties stream_props;
114 115 116
		unsigned int ts_props;

		/* resolve TS configuration */
117 118
		if (adap->dev->props->get_ts_config) {
			ret = adap->dev->props->get_ts_config(
119
					adap->fe[adap->active_fe],
120 121 122 123 124 125 126 127 128 129 130 131 132
					&ts_props);
			if (ret < 0)
				return ret;
		} 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;
133 134

		/* resolve USB stream configuration */
135 136
		if (adap->dev->props->get_usb_stream_config) {
			ret = adap->dev->props->get_usb_stream_config(
137
					adap->fe[adap->active_fe],
138 139 140 141
					&stream_props);
			if (ret < 0)
				return ret;
		} else {
142
			stream_props = adap->props->stream;
143 144
		}

145 146
		pr_debug("%s: submitting all URBs\n", __func__);

147
		usb_urb_submitv2(&adap->stream, &stream_props);
148

149
		pr_debug("%s: controlling pid parser\n", __func__);
150 151
		if (adap->props->caps & DVB_USB_ADAP_HAS_PID_FILTER &&
				adap->props->caps &
152
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
153 154
				adap->props->pid_filter_ctrl != NULL) {
			ret = adap->props->pid_filter_ctrl(adap,
155
					adap->pid_filtering);
156
			if (ret < 0) {
157
				pr_err("%s: could not handle pid_parser\n",
158
						KBUILD_MODNAME);
159 160 161
				return ret;
			}
		}
162
		pr_debug("%s: start feeding\n", __func__);
163 164
		if (adap->dev->props->streaming_ctrl != NULL) {
			ret = adap->dev->props->streaming_ctrl(adap, 1);
165
			if (ret < 0) {
166
				pr_err("%s: error while enabling fifo\n",
167
						KBUILD_MODNAME);
168 169 170 171 172 173
				return ret;
			}
		}

	}
	return 0;
174 175 176
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
177 178 179 180
}

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

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

193
int dvb_usbv2_adapter_dvb_init(struct dvb_usb_adapter *adap)
194
{
195 196 197 198 199 200
	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);
201
	if (ret < 0) {
202 203
		pr_debug("%s: dvb_register_adapter() failed=%d\n", __func__,
				ret);
204 205 206
		goto err;
	}
	adap->dvb_adap.priv = adap;
207
	adap->dvb_adap.fe_ioctl_override = adap->props->fe_ioctl_override;
208

209 210
	if (adap->dev->props->read_mac_address) {
		if (adap->dev->props->read_mac_address(adap->dev,
211
				adap->dvb_adap.proposed_mac) == 0)
212
			pr_info("%s: MAC address: %pM\n", KBUILD_MODNAME,
213
					adap->dvb_adap.proposed_mac);
214
		else
215
			pr_err("%s: MAC address reading failed\n",
216
					KBUILD_MODNAME);
217 218 219 220 221 222 223
	}


	adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
	adap->demux.priv             = adap;

	adap->demux.filternum        = 0;
224 225
	if (adap->demux.filternum < adap->max_feed_count)
		adap->demux.filternum = adap->max_feed_count;
226 227 228 229
	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;
230 231
	ret = dvb_dmx_init(&adap->demux);
	if (ret < 0) {
232
		pr_err("%s: dvb_dmx_init() failed=%d\n", KBUILD_MODNAME, ret);
233 234 235 236 237 238
		goto err_dmx;
	}

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

246 247
	ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
	if (ret < 0) {
248
		pr_err("%s: dvb_net_init() failed=%d\n", KBUILD_MODNAME, ret);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		goto err_net_init;
	}

	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;
}

265
int dvb_usbv2_adapter_dvb_exit(struct dvb_usb_adapter *adap)
266
{
267 268
	pr_debug("%s: adap=%d\n", __func__, adap->id);

269
	if (adap->state & DVB_USB_ADAP_STATE_DVB) {
270
		pr_debug("%s: unregistering DVB part\n", __func__);
271 272 273 274 275 276 277 278 279 280
		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;
}

281
static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
282
{
283
	int ret;
284
	struct dvb_usb_adapter *adap = fe->dvb->priv;
285
	pr_debug("%s: adap=%d fe=%d\n", __func__, adap->id, fe->id);
286

287
	ret = dvb_usbv2_device_power_ctrl(adap->dev, 1);
288 289
	if (ret < 0)
		goto err;
290

291 292
	if (adap->dev->props->frontend_ctrl) {
		ret = adap->dev->props->frontend_ctrl(fe, 1);
293 294
		if (ret < 0)
			goto err;
295 296
	}

297 298 299 300 301
	if (adap->fe_init[fe->id]) {
		ret = adap->fe_init[fe->id](fe);
		if (ret < 0)
			goto err;
	}
302

303
	adap->active_fe = fe->id;
304 305

	return 0;
306 307 308
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
309 310 311 312
}

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

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

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

329
	ret = dvb_usbv2_device_power_ctrl(adap->dev, 0);
330 331 332 333 334 335 336 337 338
	if (ret < 0)
		goto err;

	adap->active_fe = -1;

	return 0;
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
	return ret;
339 340
}

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

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

349 350
	if (adap->dev->props->frontend_attach) {
		ret = adap->dev->props->frontend_attach(adap);
351 352 353 354 355 356 357
		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__);
358 359 360
		ret = 0;
		goto err;
	}
361

362 363
	if (adap->dev->props->tuner_attach) {
		ret = adap->dev->props->tuner_attach(adap);
364 365 366 367 368
		if (ret < 0) {
			pr_debug("%s: tuner_attach() failed=%d\n", __func__,
					ret);
			goto err_dvb_frontend_detach;
		}
369 370
	}

371
	for (i = 0; i < MAX_NO_OF_FE_PER_ADAP && adap->fe[i]; i++) {
372
		adap->fe[i]->id = i;
373 374

		/* re-assign sleep and wakeup functions */
375 376 377 378
		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;
379

380 381
		ret = dvb_register_frontend(&adap->dvb_adap, adap->fe[i]);
		if (ret < 0) {
382 383 384
			pr_err("%s: frontend%d registration failed\n",
					KBUILD_MODNAME, i);
			goto err_dvb_unregister_frontend;
385 386
		}

387
		count_registered++;
388 389
	}

390
	adap->num_frontends_initialized = count_registered;
391

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

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]);
	}

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

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

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

421 422 423 424
	adap->num_frontends_initialized = 0;

	return 0;
}