dvb-usb-dvb.c 9.4 KB
Newer Older
1 2
/* dvb-usb-dvb.c is part of the DVB USB library.
 *
3
 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
4 5 6 7 8 9 10
 * 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
/* does the complete input transfer handling */
12 13
static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
{
14
	struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
15
	int newfeedcount, ret;
16

17
	if (adap == NULL)
18 19
		return -ENODEV;

20 21
	if ((adap->active_fe < 0) ||
	    (adap->active_fe >= adap->num_frontends_initialized)) {
22 23 24
		return -EINVAL;
	}

25
	newfeedcount = adap->feedcount + (onoff ? 1 : -1);
26

27
	/* stop feed before setting a new pid if there will be no pid anymore */
28 29
	if (newfeedcount == 0) {
		deb_ts("stop feeding\n");
30
		usb_urb_kill(&adap->fe_adap[adap->active_fe].stream);
31

32 33
		if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
			ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 0);
34 35
			if (ret < 0) {
				err("error while stopping stream.");
36
				return ret;
37
			}
38
		}
39 40
	}

41
	adap->feedcount = newfeedcount;
42 43

	/* activate the pid on the device specific pid_filter */
44 45 46 47
	deb_ts("setting pid (%s): %5d %04x at index %d '%s'\n",
		adap->fe_adap[adap->active_fe].pid_filtering ?
		"yes" : "no", dvbdmxfeed->pid, dvbdmxfeed->pid,
		dvbdmxfeed->index, onoff ? "on" : "off");
48 49 50 51
	if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
		adap->fe_adap[adap->active_fe].pid_filtering &&
		adap->props.fe[adap->active_fe].pid_filter != NULL)
		adap->props.fe[adap->active_fe].pid_filter(adap, dvbdmxfeed->index, dvbdmxfeed->pid, onoff);
52 53 54 55

	/* start the feed if this was the first feed and there is still a feed
	 * for reception.
	 */
56
	if (adap->feedcount == onoff && adap->feedcount > 0) {
57
		deb_ts("submitting all URBs\n");
58
		usb_urb_submit(&adap->fe_adap[adap->active_fe].stream);
59 60

		deb_ts("controlling pid parser\n");
61 62
		if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
			adap->props.fe[adap->active_fe].caps &
63
			DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
64 65 66
			adap->props.fe[adap->active_fe].pid_filter_ctrl != NULL) {
			ret = adap->props.fe[adap->active_fe].pid_filter_ctrl(adap,
				adap->fe_adap[adap->active_fe].pid_filtering);
67
			if (ret < 0) {
68
				err("could not handle pid_parser");
69 70 71
				return ret;
			}
		}
72
		deb_ts("start feeding\n");
73 74
		if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
			ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 1);
75
			if (ret < 0) {
76
				err("error while enabling fifo.");
77
				return ret;
78
			}
79
		}
80 81 82 83 84 85 86

	}
	return 0;
}

static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
87 88 89
	deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,
	       dvbdmxfeed->type);
	return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
90 91 92 93 94
}

static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
	deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
95
	return dvb_usb_ctrl_feed(dvbdmxfeed, 0);
96 97
}

98
static int dvb_usb_media_device_init(struct dvb_usb_adapter *adap)
99 100 101
{
#ifdef CONFIG_MEDIA_CONTROLLER_DVB
	struct media_device *mdev;
102
	struct dvb_usb_device *d = adap->dev;
103 104 105 106
	struct usb_device *udev = d->udev;

	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
	if (!mdev)
107
		return -ENOMEM;
108 109 110 111 112 113 114 115 116

	mdev->dev = &udev->dev;
	strlcpy(mdev->model, d->desc->name, sizeof(mdev->model));
	if (udev->serial)
		strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
	strcpy(mdev->bus_info, udev->devpath);
	mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
	mdev->driver_version = LINUX_VERSION_CODE;

117 118
	media_device_init(mdev);

119
	dvb_register_media_controller(&adap->dvb_adap, mdev);
120 121 122

	dev_info(&d->udev->dev, "media controller created\n");
#endif
123
	return 0;
124 125
}

126
static int  dvb_usb_media_device_register(struct dvb_usb_adapter *adap)
127 128
{
#ifdef CONFIG_MEDIA_CONTROLLER_DVB
129 130 131
	return media_device_register(adap->dvb_adap.mdev);
#else
	return 0;
132 133 134
#endif
}

135
static void dvb_usb_media_device_unregister(struct dvb_usb_adapter *adap)
136 137
{
#ifdef CONFIG_MEDIA_CONTROLLER_DVB
138
	if (!adap->dvb_adap.mdev)
139 140
		return;

141
	media_device_unregister(adap->dvb_adap.mdev);
142
	media_device_cleanup(adap->dvb_adap.mdev);
143 144
	kfree(adap->dvb_adap.mdev);
	adap->dvb_adap.mdev = NULL;
145 146 147
#endif
}

148
int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
149
{
150
	int i;
151 152 153
	int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
				       adap->dev->owner, &adap->dev->udev->dev,
				       adapter_nums);
154

155
	if (ret < 0) {
156 157 158
		deb_info("dvb_register_adapter failed: error %d", ret);
		goto err;
	}
159 160
	adap->dvb_adap.priv = adap;

161 162 163 164 165
	ret = dvb_usb_media_device_init(adap);
	if (ret < 0) {
		deb_info("dvb_usb_media_device_init failed: error %d", ret);
		goto err_mc;
	}
166

167
	if (adap->dev->props.read_mac_address) {
168 169
		if (adap->dev->props.read_mac_address(adap->dev, adap->dvb_adap.proposed_mac) == 0)
			info("MAC address: %pM", adap->dvb_adap.proposed_mac);
170 171 172 173 174
		else
			err("MAC address reading failed.");
	}


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

178 179 180 181 182 183
	adap->demux.filternum        = 0;
	for (i = 0; i < adap->props.num_frontends; i++) {
		if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
			adap->demux.filternum = adap->fe_adap[i].max_feed_count;
	}
	adap->demux.feednum          = adap->demux.filternum;
184 185 186 187
	adap->demux.start_feed       = dvb_usb_start_feed;
	adap->demux.stop_feed        = dvb_usb_stop_feed;
	adap->demux.write_to_decoder = NULL;
	if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
188
		err("dvb_dmx_init failed: error %d", ret);
189 190 191
		goto err_dmx;
	}

192 193 194 195
	adap->dmxdev.filternum       = adap->demux.filternum;
	adap->dmxdev.demux           = &adap->demux.dmx;
	adap->dmxdev.capabilities    = 0;
	if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
196
		err("dvb_dmxdev_init failed: error %d", ret);
197 198 199
		goto err_dmx_dev;
	}

200 201
	if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
						&adap->demux.dmx)) < 0) {
202
		err("dvb_net_init failed: error %d", ret);
203 204
		goto err_net_init;
	}
205

206
	adap->state |= DVB_USB_ADAP_STATE_DVB;
207 208
	return 0;

209 210
err_net_init:
	dvb_dmxdev_release(&adap->dmxdev);
211
err_dmx_dev:
212
	dvb_dmx_release(&adap->demux);
213
err_dmx:
214
	dvb_usb_media_device_unregister(adap);
215
err_mc:
216
	dvb_unregister_adapter(&adap->dvb_adap);
217 218 219 220
err:
	return ret;
}

221
int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
222
{
223
	if (adap->state & DVB_USB_ADAP_STATE_DVB) {
224
		deb_info("unregistering DVB part\n");
225 226 227 228
		dvb_net_release(&adap->dvb_net);
		adap->demux.dmx.close(&adap->demux.dmx);
		dvb_dmxdev_release(&adap->dmxdev);
		dvb_dmx_release(&adap->demux);
229
		dvb_usb_media_device_unregister(adap);
230 231
		dvb_unregister_adapter(&adap->dvb_adap);
		adap->state &= ~DVB_USB_ADAP_STATE_DVB;
232 233 234 235
	}
	return 0;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
{
	struct dvb_usb_adapter *adap = fe->dvb->priv;

	int ret = (adap->props.frontend_ctrl) ?
		adap->props.frontend_ctrl(fe, onoff) : 0;

	if (ret < 0) {
		err("frontend_ctrl request failed");
		return ret;
	}
	if (onoff)
		adap->active_fe = fe->id;

	return 0;
}

253 254
static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
{
255
	struct dvb_usb_adapter *adap = fe->dvb->priv;
256

257
	dvb_usb_device_power_ctrl(adap->dev, 1);
258

259
	dvb_usb_set_active_fe(fe, 1);
260

261 262
	if (adap->fe_adap[fe->id].fe_init)
		adap->fe_adap[fe->id].fe_init(fe);
263 264 265 266 267 268

	return 0;
}

static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
{
269
	struct dvb_usb_adapter *adap = fe->dvb->priv;
270

271 272
	if (adap->fe_adap[fe->id].fe_sleep)
		adap->fe_adap[fe->id].fe_sleep(fe);
273

274
	dvb_usb_set_active_fe(fe, 0);
275

276
	return dvb_usb_device_power_ctrl(adap->dev, 0);
277 278
}

279
int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
280
{
281
	int ret, i;
282

283 284
	/* register all given adapter frontends */
	for (i = 0; i < adap->props.num_frontends; i++) {
285

286 287 288 289
		if (adap->props.fe[i].frontend_attach == NULL) {
			err("strange: '%s' #%d,%d "
			    "doesn't want to attach a frontend.",
			    adap->dev->desc->name, adap->id, i);
290

291 292
			return 0;
		}
293

294 295
		ret = adap->props.fe[i].frontend_attach(adap);
		if (ret || adap->fe_adap[i].fe == NULL) {
296 297 298 299 300 301 302
			/* only print error when there is no FE at all */
			if (i == 0)
				err("no frontend was attached by '%s'",
					adap->dev->desc->name);

			return 0;
		}
303

304
		adap->fe_adap[i].fe->id = i;
305 306

		/* re-assign sleep and wakeup functions */
307 308 309 310
		adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
		adap->fe_adap[i].fe->ops.init  = dvb_usb_fe_wakeup;
		adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
		adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
311

312
		if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
313
			err("Frontend %d registration failed.", i);
314 315
			dvb_frontend_detach(adap->fe_adap[i].fe);
			adap->fe_adap[i].fe = NULL;
316 317 318 319 320 321
			/* In error case, do not try register more FEs,
			 * still leaving already registered FEs alive. */
			if (i == 0)
				return -ENODEV;
			else
				return 0;
322
		}
323 324

		/* only attach the tuner if the demod is there */
325 326 327 328
		if (adap->props.fe[i].tuner_attach != NULL)
			adap->props.fe[i].tuner_attach(adap);

		adap->num_frontends_initialized++;
329
	}
330 331
	if (ret)
		return ret;
332

333
	ret = dvb_create_media_graph(&adap->dvb_adap, true);
334 335
	if (ret)
		return ret;
336

337
	ret = dvb_usb_media_device_register(adap);
338

339
	return ret;
340 341
}

342
int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
343
{
344
	int i = adap->num_frontends_initialized - 1;
345 346 347

	/* unregister all given adapter frontends */
	for (; i >= 0; i--) {
348 349 350
		if (adap->fe_adap[i].fe != NULL) {
			dvb_unregister_frontend(adap->fe_adap[i].fe);
			dvb_frontend_detach(adap->fe_adap[i].fe);
351
		}
352
	}
353
	adap->num_frontends_initialized = 0;
354

355 356
	return 0;
}