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