PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / dvb-usb-dvb.c
blob719413b15f208b7ceb165422ff4d1111dfcfd6e1
1 /* dvb-usb-dvb.c is part of the DVB USB library.
3 * Copyright (C) 2004-6 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
7 * linux-dvb API.
8 */
9 #include "dvb-usb-common.h"
11 /* does the complete input transfer handling */
12 static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
14 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
15 int newfeedcount, ret;
17 if (adap == NULL)
18 return -ENODEV;
20 if ((adap->active_fe < 0) ||
21 (adap->active_fe >= adap->num_frontends_initialized)) {
22 return -EINVAL;
25 newfeedcount = adap->feedcount + (onoff ? 1 : -1);
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);
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;
41 adap->feedcount = newfeedcount;
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);
53 /* start the feed if this was the first feed and there is still a feed
54 * for reception.
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);
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;
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;
82 return 0;
85 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
87 deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
88 return dvb_usb_ctrl_feed(dvbdmxfeed,1);
91 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
93 deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
94 return dvb_usb_ctrl_feed(dvbdmxfeed,0);
97 int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
99 int i;
100 int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
101 adap->dev->owner, &adap->dev->udev->dev,
102 adapter_nums);
104 if (ret < 0) {
105 deb_info("dvb_register_adapter failed: error %d", ret);
106 goto err;
108 adap->dvb_adap.priv = adap;
110 if (adap->dev->props.read_mac_address) {
111 if (adap->dev->props.read_mac_address(adap->dev,adap->dvb_adap.proposed_mac) == 0)
112 info("MAC address: %pM",adap->dvb_adap.proposed_mac);
113 else
114 err("MAC address reading failed.");
118 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
119 adap->demux.priv = adap;
121 adap->demux.filternum = 0;
122 for (i = 0; i < adap->props.num_frontends; i++) {
123 if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
124 adap->demux.filternum = adap->fe_adap[i].max_feed_count;
126 adap->demux.feednum = adap->demux.filternum;
127 adap->demux.start_feed = dvb_usb_start_feed;
128 adap->demux.stop_feed = dvb_usb_stop_feed;
129 adap->demux.write_to_decoder = NULL;
130 if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
131 err("dvb_dmx_init failed: error %d",ret);
132 goto err_dmx;
135 adap->dmxdev.filternum = adap->demux.filternum;
136 adap->dmxdev.demux = &adap->demux.dmx;
137 adap->dmxdev.capabilities = 0;
138 if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
139 err("dvb_dmxdev_init failed: error %d",ret);
140 goto err_dmx_dev;
143 if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
144 &adap->demux.dmx)) < 0) {
145 err("dvb_net_init failed: error %d",ret);
146 goto err_net_init;
149 adap->state |= DVB_USB_ADAP_STATE_DVB;
150 return 0;
152 err_net_init:
153 dvb_dmxdev_release(&adap->dmxdev);
154 err_dmx_dev:
155 dvb_dmx_release(&adap->demux);
156 err_dmx:
157 dvb_unregister_adapter(&adap->dvb_adap);
158 err:
159 return ret;
162 int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
164 if (adap->state & DVB_USB_ADAP_STATE_DVB) {
165 deb_info("unregistering DVB part\n");
166 dvb_net_release(&adap->dvb_net);
167 adap->demux.dmx.close(&adap->demux.dmx);
168 dvb_dmxdev_release(&adap->dmxdev);
169 dvb_dmx_release(&adap->demux);
170 dvb_unregister_adapter(&adap->dvb_adap);
171 adap->state &= ~DVB_USB_ADAP_STATE_DVB;
173 return 0;
176 static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
178 struct dvb_usb_adapter *adap = fe->dvb->priv;
180 int ret = (adap->props.frontend_ctrl) ?
181 adap->props.frontend_ctrl(fe, onoff) : 0;
183 if (ret < 0) {
184 err("frontend_ctrl request failed");
185 return ret;
187 if (onoff)
188 adap->active_fe = fe->id;
190 return 0;
193 static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
195 struct dvb_usb_adapter *adap = fe->dvb->priv;
197 dvb_usb_device_power_ctrl(adap->dev, 1);
199 dvb_usb_set_active_fe(fe, 1);
201 if (adap->fe_adap[fe->id].fe_init)
202 adap->fe_adap[fe->id].fe_init(fe);
204 return 0;
207 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
209 struct dvb_usb_adapter *adap = fe->dvb->priv;
211 if (adap->fe_adap[fe->id].fe_sleep)
212 adap->fe_adap[fe->id].fe_sleep(fe);
214 dvb_usb_set_active_fe(fe, 0);
216 return dvb_usb_device_power_ctrl(adap->dev, 0);
219 int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
221 int ret, i;
223 /* register all given adapter frontends */
224 for (i = 0; i < adap->props.num_frontends; i++) {
226 if (adap->props.fe[i].frontend_attach == NULL) {
227 err("strange: '%s' #%d,%d "
228 "doesn't want to attach a frontend.",
229 adap->dev->desc->name, adap->id, i);
231 return 0;
234 ret = adap->props.fe[i].frontend_attach(adap);
235 if (ret || adap->fe_adap[i].fe == NULL) {
236 /* only print error when there is no FE at all */
237 if (i == 0)
238 err("no frontend was attached by '%s'",
239 adap->dev->desc->name);
241 return 0;
244 adap->fe_adap[i].fe->id = i;
246 /* re-assign sleep and wakeup functions */
247 adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
248 adap->fe_adap[i].fe->ops.init = dvb_usb_fe_wakeup;
249 adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
250 adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
252 if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
253 err("Frontend %d registration failed.", i);
254 dvb_frontend_detach(adap->fe_adap[i].fe);
255 adap->fe_adap[i].fe = NULL;
256 /* In error case, do not try register more FEs,
257 * still leaving already registered FEs alive. */
258 if (i == 0)
259 return -ENODEV;
260 else
261 return 0;
264 /* only attach the tuner if the demod is there */
265 if (adap->props.fe[i].tuner_attach != NULL)
266 adap->props.fe[i].tuner_attach(adap);
268 adap->num_frontends_initialized++;
271 return 0;
274 int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
276 int i = adap->num_frontends_initialized - 1;
278 /* unregister all given adapter frontends */
279 for (; i >= 0; i--) {
280 if (adap->fe_adap[i].fe != NULL) {
281 dvb_unregister_frontend(adap->fe_adap[i].fe);
282 dvb_frontend_detach(adap->fe_adap[i].fe);
285 adap->num_frontends_initialized = 0;
287 return 0;