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