1 // SPDX-License-Identifier: GPL-2.0
2 /* dvb-usb-dvb.c is part of the DVB USB library.
4 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5 * see dvb-usb-init.c for copyright information.
7 * This file contains functions for initializing and handling the
10 #include "dvb-usb-common.h"
11 #include <media/media-device.h>
13 /* does the complete input transfer handling */
14 static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
16 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
17 int newfeedcount, ret;
22 if ((adap->active_fe < 0) ||
23 (adap->active_fe >= adap->num_frontends_initialized)) {
27 newfeedcount = adap->feedcount + (onoff ? 1 : -1);
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);
34 if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
35 ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 0);
37 err("error while stopping stream.");
43 adap->feedcount = newfeedcount;
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);
55 /* start the feed if this was the first feed and there is still a feed
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);
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);
70 err("could not handle pid_parser");
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);
78 err("error while enabling fifo.");
87 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
89 deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,
91 return dvb_usb_ctrl_feed(dvbdmxfeed, 1);
94 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
96 deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
97 return dvb_usb_ctrl_feed(dvbdmxfeed, 0);
100 static int dvb_usb_media_device_init(struct dvb_usb_adapter *adap)
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;
107 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
111 media_device_usb_init(mdev, udev, d->desc->name);
113 dvb_register_media_controller(&adap->dvb_adap, mdev);
115 dev_info(&d->udev->dev, "media controller created\n");
120 static int dvb_usb_media_device_register(struct dvb_usb_adapter *adap)
122 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
123 return media_device_register(adap->dvb_adap.mdev);
129 static void dvb_usb_media_device_unregister(struct dvb_usb_adapter *adap)
131 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
132 if (!adap->dvb_adap.mdev)
135 media_device_unregister(adap->dvb_adap.mdev);
136 media_device_cleanup(adap->dvb_adap.mdev);
137 kfree(adap->dvb_adap.mdev);
138 adap->dvb_adap.mdev = NULL;
142 int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
145 int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
146 adap->dev->owner, &adap->dev->udev->dev,
150 deb_info("dvb_register_adapter failed: error %d", ret);
153 adap->dvb_adap.priv = adap;
155 ret = dvb_usb_media_device_init(adap);
157 deb_info("dvb_usb_media_device_init failed: error %d", ret);
161 if (adap->dev->props.read_mac_address) {
162 if (adap->dev->props.read_mac_address(adap->dev, adap->dvb_adap.proposed_mac) == 0)
163 info("MAC address: %pM", adap->dvb_adap.proposed_mac);
165 err("MAC address reading failed.");
169 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
170 adap->demux.priv = adap;
172 adap->demux.filternum = 0;
173 for (i = 0; i < adap->props.num_frontends; i++) {
174 if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
175 adap->demux.filternum = adap->fe_adap[i].max_feed_count;
177 adap->demux.feednum = adap->demux.filternum;
178 adap->demux.start_feed = dvb_usb_start_feed;
179 adap->demux.stop_feed = dvb_usb_stop_feed;
180 adap->demux.write_to_decoder = NULL;
181 if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
182 err("dvb_dmx_init failed: error %d", ret);
186 adap->dmxdev.filternum = adap->demux.filternum;
187 adap->dmxdev.demux = &adap->demux.dmx;
188 adap->dmxdev.capabilities = 0;
189 if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
190 err("dvb_dmxdev_init failed: error %d", ret);
194 if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
195 &adap->demux.dmx)) < 0) {
196 err("dvb_net_init failed: error %d", ret);
200 adap->state |= DVB_USB_ADAP_STATE_DVB;
204 dvb_dmxdev_release(&adap->dmxdev);
206 dvb_dmx_release(&adap->demux);
208 dvb_usb_media_device_unregister(adap);
210 dvb_unregister_adapter(&adap->dvb_adap);
215 int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
217 if (adap->state & DVB_USB_ADAP_STATE_DVB) {
218 deb_info("unregistering DVB part\n");
219 dvb_net_release(&adap->dvb_net);
220 adap->demux.dmx.close(&adap->demux.dmx);
221 dvb_dmxdev_release(&adap->dmxdev);
222 dvb_dmx_release(&adap->demux);
223 dvb_usb_media_device_unregister(adap);
224 dvb_unregister_adapter(&adap->dvb_adap);
225 adap->state &= ~DVB_USB_ADAP_STATE_DVB;
230 static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
232 struct dvb_usb_adapter *adap = fe->dvb->priv;
234 int ret = (adap->props.frontend_ctrl) ?
235 adap->props.frontend_ctrl(fe, onoff) : 0;
238 err("frontend_ctrl request failed");
242 adap->active_fe = fe->id;
247 static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
249 struct dvb_usb_adapter *adap = fe->dvb->priv;
251 dvb_usb_device_power_ctrl(adap->dev, 1);
253 dvb_usb_set_active_fe(fe, 1);
255 if (adap->fe_adap[fe->id].fe_init)
256 adap->fe_adap[fe->id].fe_init(fe);
261 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
263 struct dvb_usb_adapter *adap = fe->dvb->priv;
265 if (adap->fe_adap[fe->id].fe_sleep)
266 adap->fe_adap[fe->id].fe_sleep(fe);
268 dvb_usb_set_active_fe(fe, 0);
270 return dvb_usb_device_power_ctrl(adap->dev, 0);
273 int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
277 /* register all given adapter frontends */
278 for (i = 0; i < adap->props.num_frontends; i++) {
280 if (adap->props.fe[i].frontend_attach == NULL) {
281 err("strange: '%s' #%d,%d doesn't want to attach a frontend.",
282 adap->dev->desc->name, adap->id, i);
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 */
291 err("no frontend was attached by '%s'",
292 adap->dev->desc->name);
297 adap->fe_adap[i].fe->id = i;
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;
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. */
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);
321 adap->num_frontends_initialized++;
324 ret = dvb_create_media_graph(&adap->dvb_adap, true);
328 ret = dvb_usb_media_device_register(adap);
333 int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
335 int i = adap->num_frontends_initialized - 1;
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);
344 adap->num_frontends_initialized = 0;