1/* dvb-usb-dvb.c is part of the DVB USB library.
2 *
3 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
4 * see dvb-usb-init.c for copyright information.
5 *
6 * This file contains functions for initializing and handling the
7 * linux-dvb API.
8 */
9#include "dvb-usb-common.h"
10
11/* does the complete input transfer handling */
12static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
13{
14	struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
15	int newfeedcount, ret;
16
17	if (adap == NULL)
18		return -ENODEV;
19
20	if ((adap->active_fe < 0) ||
21	    (adap->active_fe >= adap->num_frontends_initialized)) {
22		return -EINVAL;
23	}
24
25	newfeedcount = adap->feedcount + (onoff ? 1 : -1);
26
27	/* stop feed before setting a new pid if there will be no pid anymore */
28	if (newfeedcount == 0) {
29		deb_ts("stop feeding\n");
30		usb_urb_kill(&adap->fe_adap[adap->active_fe].stream);
31
32		if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
33			ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 0);
34			if (ret < 0) {
35				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	deb_ts("setting pid (%s): %5d %04x at index %d '%s'\n",
45		adap->fe_adap[adap->active_fe].pid_filtering ?
46		"yes" : "no", dvbdmxfeed->pid, dvbdmxfeed->pid,
47		dvbdmxfeed->index, onoff ? "on" : "off");
48	if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
49		adap->fe_adap[adap->active_fe].pid_filtering &&
50		adap->props.fe[adap->active_fe].pid_filter != NULL)
51		adap->props.fe[adap->active_fe].pid_filter(adap, dvbdmxfeed->index, dvbdmxfeed->pid, onoff);
52
53	/* start the feed if this was the first feed and there is still a feed
54	 * for reception.
55	 */
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		if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
62			adap->props.fe[adap->active_fe].caps &
63			DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
64			adap->props.fe[adap->active_fe].pid_filter_ctrl != NULL) {
65			ret = adap->props.fe[adap->active_fe].pid_filter_ctrl(adap,
66				adap->fe_adap[adap->active_fe].pid_filtering);
67			if (ret < 0) {
68				err("could not handle pid_parser");
69				return ret;
70			}
71		}
72		deb_ts("start feeding\n");
73		if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
74			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	return 0;
83}
84
85static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
86{
87	deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,
88	       dvbdmxfeed->type);
89	return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
90}
91
92static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
93{
94	deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
95	return dvb_usb_ctrl_feed(dvbdmxfeed, 0);
96}
97
98static void dvb_usb_media_device_register(struct dvb_usb_adapter *adap)
99{
100#ifdef CONFIG_MEDIA_CONTROLLER_DVB
101	struct media_device *mdev;
102	struct dvb_usb_device *d = adap->dev;
103	struct usb_device *udev = d->udev;
104	int ret;
105
106	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
107	if (!mdev)
108		return;
109
110	mdev->dev = &udev->dev;
111	strlcpy(mdev->model, d->desc->name, sizeof(mdev->model));
112	if (udev->serial)
113		strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
114	strcpy(mdev->bus_info, udev->devpath);
115	mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
116	mdev->driver_version = LINUX_VERSION_CODE;
117
118	ret = media_device_register(mdev);
119	if (ret) {
120		dev_err(&d->udev->dev,
121			"Couldn't create a media device. Error: %d\n",
122			ret);
123		kfree(mdev);
124		return;
125	}
126	dvb_register_media_controller(&adap->dvb_adap, mdev);
127
128	dev_info(&d->udev->dev, "media controller created\n");
129#endif
130}
131
132static void dvb_usb_media_device_unregister(struct dvb_usb_adapter *adap)
133{
134#ifdef CONFIG_MEDIA_CONTROLLER_DVB
135	if (!adap->dvb_adap.mdev)
136		return;
137
138	media_device_unregister(adap->dvb_adap.mdev);
139	kfree(adap->dvb_adap.mdev);
140	adap->dvb_adap.mdev = NULL;
141#endif
142}
143
144int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
145{
146	int i;
147	int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
148				       adap->dev->owner, &adap->dev->udev->dev,
149				       adapter_nums);
150
151	if (ret < 0) {
152		deb_info("dvb_register_adapter failed: error %d", ret);
153		goto err;
154	}
155	adap->dvb_adap.priv = adap;
156
157	dvb_usb_media_device_register(adap);
158
159	if (adap->dev->props.read_mac_address) {
160		if (adap->dev->props.read_mac_address(adap->dev, adap->dvb_adap.proposed_mac) == 0)
161			info("MAC address: %pM", adap->dvb_adap.proposed_mac);
162		else
163			err("MAC address reading failed.");
164	}
165
166
167	adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
168	adap->demux.priv             = adap;
169
170	adap->demux.filternum        = 0;
171	for (i = 0; i < adap->props.num_frontends; i++) {
172		if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
173			adap->demux.filternum = adap->fe_adap[i].max_feed_count;
174	}
175	adap->demux.feednum          = adap->demux.filternum;
176	adap->demux.start_feed       = dvb_usb_start_feed;
177	adap->demux.stop_feed        = dvb_usb_stop_feed;
178	adap->demux.write_to_decoder = NULL;
179	if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
180		err("dvb_dmx_init failed: error %d", ret);
181		goto err_dmx;
182	}
183
184	adap->dmxdev.filternum       = adap->demux.filternum;
185	adap->dmxdev.demux           = &adap->demux.dmx;
186	adap->dmxdev.capabilities    = 0;
187	if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
188		err("dvb_dmxdev_init failed: error %d", ret);
189		goto err_dmx_dev;
190	}
191
192	if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
193						&adap->demux.dmx)) < 0) {
194		err("dvb_net_init failed: error %d", ret);
195		goto err_net_init;
196	}
197
198	adap->state |= DVB_USB_ADAP_STATE_DVB;
199	return 0;
200
201err_net_init:
202	dvb_dmxdev_release(&adap->dmxdev);
203err_dmx_dev:
204	dvb_dmx_release(&adap->demux);
205err_dmx:
206	dvb_usb_media_device_unregister(adap);
207	dvb_unregister_adapter(&adap->dvb_adap);
208err:
209	return ret;
210}
211
212int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
213{
214	if (adap->state & DVB_USB_ADAP_STATE_DVB) {
215		deb_info("unregistering DVB part\n");
216		dvb_net_release(&adap->dvb_net);
217		adap->demux.dmx.close(&adap->demux.dmx);
218		dvb_dmxdev_release(&adap->dmxdev);
219		dvb_dmx_release(&adap->demux);
220		dvb_usb_media_device_unregister(adap);
221		dvb_unregister_adapter(&adap->dvb_adap);
222		adap->state &= ~DVB_USB_ADAP_STATE_DVB;
223	}
224	return 0;
225}
226
227static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
228{
229	struct dvb_usb_adapter *adap = fe->dvb->priv;
230
231	int ret = (adap->props.frontend_ctrl) ?
232		adap->props.frontend_ctrl(fe, onoff) : 0;
233
234	if (ret < 0) {
235		err("frontend_ctrl request failed");
236		return ret;
237	}
238	if (onoff)
239		adap->active_fe = fe->id;
240
241	return 0;
242}
243
244static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
245{
246	struct dvb_usb_adapter *adap = fe->dvb->priv;
247
248	dvb_usb_device_power_ctrl(adap->dev, 1);
249
250	dvb_usb_set_active_fe(fe, 1);
251
252	if (adap->fe_adap[fe->id].fe_init)
253		adap->fe_adap[fe->id].fe_init(fe);
254
255	return 0;
256}
257
258static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
259{
260	struct dvb_usb_adapter *adap = fe->dvb->priv;
261
262	if (adap->fe_adap[fe->id].fe_sleep)
263		adap->fe_adap[fe->id].fe_sleep(fe);
264
265	dvb_usb_set_active_fe(fe, 0);
266
267	return dvb_usb_device_power_ctrl(adap->dev, 0);
268}
269
270int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
271{
272	int ret, i;
273
274	/* register all given adapter frontends */
275	for (i = 0; i < adap->props.num_frontends; i++) {
276
277		if (adap->props.fe[i].frontend_attach == NULL) {
278			err("strange: '%s' #%d,%d "
279			    "doesn't want to attach a frontend.",
280			    adap->dev->desc->name, adap->id, i);
281
282			return 0;
283		}
284
285		ret = adap->props.fe[i].frontend_attach(adap);
286		if (ret || adap->fe_adap[i].fe == NULL) {
287			/* only print error when there is no FE at all */
288			if (i == 0)
289				err("no frontend was attached by '%s'",
290					adap->dev->desc->name);
291
292			return 0;
293		}
294
295		adap->fe_adap[i].fe->id = i;
296
297		/* re-assign sleep and wakeup functions */
298		adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
299		adap->fe_adap[i].fe->ops.init  = dvb_usb_fe_wakeup;
300		adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
301		adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
302
303		if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
304			err("Frontend %d registration failed.", i);
305			dvb_frontend_detach(adap->fe_adap[i].fe);
306			adap->fe_adap[i].fe = NULL;
307			/* In error case, do not try register more FEs,
308			 * still leaving already registered FEs alive. */
309			if (i == 0)
310				return -ENODEV;
311			else
312				return 0;
313		}
314
315		/* only attach the tuner if the demod is there */
316		if (adap->props.fe[i].tuner_attach != NULL)
317			adap->props.fe[i].tuner_attach(adap);
318
319		adap->num_frontends_initialized++;
320	}
321
322	dvb_create_media_graph(&adap->dvb_adap);
323
324	return 0;
325}
326
327int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
328{
329	int i = adap->num_frontends_initialized - 1;
330
331	/* unregister all given adapter frontends */
332	for (; i >= 0; i--) {
333		if (adap->fe_adap[i].fe != NULL) {
334			dvb_unregister_frontend(adap->fe_adap[i].fe);
335			dvb_frontend_detach(adap->fe_adap[i].fe);
336		}
337	}
338	adap->num_frontends_initialized = 0;
339
340	return 0;
341}
342