1 /* dvb-usb-dvb.c is part of the DVB USB library.
3 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
4 * see dvb-usb-init.c for copyright information.
6 * This file contains functions for initializing and handling the
9 #include "dvb-usb-common.h"
11 static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
13 struct dvb_usb_device *d = dvbdmxfeed->demux->priv;
19 newfeedcount = d->feedcount + (onoff ? 1 : -1);
22 * stop feed before setting a new pid if there will be no pid anymore
24 if (newfeedcount == 0) {
25 deb_ts("stop feeding\n");
28 if (d->props.streaming_ctrl != NULL)
29 if ((ret = d->props.streaming_ctrl(d,0)))
30 err("error while stopping stream.");
34 d->feedcount = newfeedcount;
36 /* activate the pid on the device specific pid_filter */
37 deb_ts("setting pid: %5d %04x at index %d '%s'\n",dvbdmxfeed->pid,dvbdmxfeed->pid,dvbdmxfeed->index,onoff ? "on" : "off");
38 if (d->props.caps & DVB_USB_HAS_PID_FILTER &&
40 d->props.pid_filter != NULL)
41 d->props.pid_filter(d,dvbdmxfeed->index,dvbdmxfeed->pid,onoff);
43 /* start the feed if this was the first feed and there is still a feed
46 if (d->feedcount == onoff && d->feedcount > 0) {
47 deb_ts("submitting all URBs\n");
48 dvb_usb_urb_submit(d);
50 deb_ts("controlling pid parser\n");
51 if (d->props.caps & DVB_USB_HAS_PID_FILTER &&
52 d->props.caps & DVB_USB_PID_FILTER_CAN_BE_TURNED_OFF &&
53 d->props.pid_filter_ctrl != NULL)
54 if (d->props.pid_filter_ctrl(d,d->pid_filtering) < 0)
55 err("could not handle pid_parser");
57 deb_ts("start feeding\n");
58 if (d->props.streaming_ctrl != NULL)
59 if (d->props.streaming_ctrl(d,1)) {
60 err("error while enabling fifo.");
68 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
70 deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
71 return dvb_usb_ctrl_feed(dvbdmxfeed,1);
74 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
76 deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
77 return dvb_usb_ctrl_feed(dvbdmxfeed,0);
80 int dvb_usb_dvb_init(struct dvb_usb_device *d)
84 if ((ret = dvb_register_adapter(&d->dvb_adap, d->desc->name,
85 d->owner, &d->udev->dev)) < 0) {
86 deb_info("dvb_register_adapter failed: error %d", ret);
91 if (d->props.read_mac_address) {
92 if (d->props.read_mac_address(d,d->dvb_adap.proposed_mac) == 0)
93 info("MAC address: %02x:%02x:%02x:%02x:%02x:%02x",d->dvb_adap.proposed_mac[0],
94 d->dvb_adap.proposed_mac[1],d->dvb_adap.proposed_mac[2],
95 d->dvb_adap.proposed_mac[3],d->dvb_adap.proposed_mac[4],
96 d->dvb_adap.proposed_mac[5]);
98 err("MAC address reading failed.");
102 d->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
105 d->demux.feednum = d->demux.filternum = d->max_feed_count;
106 d->demux.start_feed = dvb_usb_start_feed;
107 d->demux.stop_feed = dvb_usb_stop_feed;
108 d->demux.write_to_decoder = NULL;
109 if ((ret = dvb_dmx_init(&d->demux)) < 0) {
110 err("dvb_dmx_init failed: error %d",ret);
114 d->dmxdev.filternum = d->demux.filternum;
115 d->dmxdev.demux = &d->demux.dmx;
116 d->dmxdev.capabilities = 0;
117 if ((ret = dvb_dmxdev_init(&d->dmxdev, &d->dvb_adap)) < 0) {
118 err("dvb_dmxdev_init failed: error %d",ret);
122 dvb_net_init(&d->dvb_adap, &d->dvb_net, &d->demux.dmx);
124 d->state |= DVB_USB_STATE_DVB;
128 dvb_dmx_release(&d->demux);
130 dvb_unregister_adapter(&d->dvb_adap);
135 int dvb_usb_dvb_exit(struct dvb_usb_device *d)
137 if (d->state & DVB_USB_STATE_DVB) {
138 deb_info("unregistering DVB part\n");
139 dvb_net_release(&d->dvb_net);
140 d->demux.dmx.close(&d->demux.dmx);
141 dvb_dmxdev_release(&d->dmxdev);
142 dvb_dmx_release(&d->demux);
143 dvb_unregister_adapter(&d->dvb_adap);
144 d->state &= ~DVB_USB_STATE_DVB;
149 static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
151 struct dvb_usb_device *d = fe->dvb->priv;
153 if (d->props.power_ctrl)
154 d->props.power_ctrl(d,1);
162 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
164 struct dvb_usb_device *d = fe->dvb->priv;
169 if (d->props.power_ctrl)
170 d->props.power_ctrl(d,0);
175 int dvb_usb_fe_init(struct dvb_usb_device* d)
177 if (d->props.frontend_attach == NULL) {
178 err("strange '%s' doesn't want to attach a frontend.",d->desc->name);
182 d->props.frontend_attach(d);
184 /* re-assign sleep and wakeup functions */
186 d->fe_init = d->fe->ops.init; d->fe->ops.init = dvb_usb_fe_wakeup;
187 d->fe_sleep = d->fe->ops.sleep; d->fe->ops.sleep = dvb_usb_fe_sleep;
189 if (dvb_register_frontend(&d->dvb_adap, d->fe)) {
190 err("Frontend registration failed.");
191 dvb_detach(d->fe->ops.release_sec, d->fe);
192 dvb_detach(d->fe->ops.tuner_ops.release, d->fe);
193 dvb_detach(d->fe->ops.release, d->fe);
198 err("no frontend was attached by '%s'",d->desc->name);
200 if (d->props.tuner_attach != NULL)
201 d->props.tuner_attach(d);
206 int dvb_usb_fe_exit(struct dvb_usb_device *d)
209 dvb_unregister_frontend(d->fe);
210 dvb_detach(d->fe->ops.release_sec, d->fe);
211 dvb_detach(d->fe->ops.tuner_ops.release, d->fe);
212 dvb_detach(d->fe->ops.release, d->fe);