Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / media / dvb / dvb-usb / dvb-usb-dvb.c
blobddf282f355b3c78f9f71608f4f445f196d68d3fc
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;
109 adap->dvb_adap.fe_ioctl_override = adap->props.fe_ioctl_override;
111 if (adap->dev->props.read_mac_address) {
112 if (adap->dev->props.read_mac_address(adap->dev,adap->dvb_adap.proposed_mac) == 0)
113 info("MAC address: %pM",adap->dvb_adap.proposed_mac);
114 else
115 err("MAC address reading failed.");
119 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
120 adap->demux.priv = adap;
122 adap->demux.filternum = 0;
123 for (i = 0; i < adap->props.num_frontends; i++) {
124 if (adap->demux.filternum < adap->fe_adap[i].max_feed_count)
125 adap->demux.filternum = adap->fe_adap[i].max_feed_count;
127 adap->demux.feednum = adap->demux.filternum;
128 adap->demux.start_feed = dvb_usb_start_feed;
129 adap->demux.stop_feed = dvb_usb_stop_feed;
130 adap->demux.write_to_decoder = NULL;
131 if ((ret = dvb_dmx_init(&adap->demux)) < 0) {
132 err("dvb_dmx_init failed: error %d",ret);
133 goto err_dmx;
136 adap->dmxdev.filternum = adap->demux.filternum;
137 adap->dmxdev.demux = &adap->demux.dmx;
138 adap->dmxdev.capabilities = 0;
139 if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) {
140 err("dvb_dmxdev_init failed: error %d",ret);
141 goto err_dmx_dev;
144 if ((ret = dvb_net_init(&adap->dvb_adap, &adap->dvb_net,
145 &adap->demux.dmx)) < 0) {
146 err("dvb_net_init failed: error %d",ret);
147 goto err_net_init;
150 adap->state |= DVB_USB_ADAP_STATE_DVB;
151 return 0;
153 err_net_init:
154 dvb_dmxdev_release(&adap->dmxdev);
155 err_dmx_dev:
156 dvb_dmx_release(&adap->demux);
157 err_dmx:
158 dvb_unregister_adapter(&adap->dvb_adap);
159 err:
160 return ret;
163 int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
165 if (adap->state & DVB_USB_ADAP_STATE_DVB) {
166 deb_info("unregistering DVB part\n");
167 dvb_net_release(&adap->dvb_net);
168 adap->demux.dmx.close(&adap->demux.dmx);
169 dvb_dmxdev_release(&adap->dmxdev);
170 dvb_dmx_release(&adap->demux);
171 dvb_unregister_adapter(&adap->dvb_adap);
172 adap->state &= ~DVB_USB_ADAP_STATE_DVB;
174 return 0;
177 static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
179 struct dvb_usb_adapter *adap = fe->dvb->priv;
181 int ret = (adap->props.frontend_ctrl) ?
182 adap->props.frontend_ctrl(fe, onoff) : 0;
184 if (ret < 0) {
185 err("frontend_ctrl request failed");
186 return ret;
188 if (onoff)
189 adap->active_fe = fe->id;
191 return 0;
194 static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
196 struct dvb_usb_adapter *adap = fe->dvb->priv;
198 dvb_usb_device_power_ctrl(adap->dev, 1);
200 dvb_usb_set_active_fe(fe, 1);
202 if (adap->fe_adap[fe->id].fe_init)
203 adap->fe_adap[fe->id].fe_init(fe);
205 return 0;
208 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
210 struct dvb_usb_adapter *adap = fe->dvb->priv;
212 if (adap->fe_adap[fe->id].fe_sleep)
213 adap->fe_adap[fe->id].fe_sleep(fe);
215 dvb_usb_set_active_fe(fe, 0);
217 return dvb_usb_device_power_ctrl(adap->dev, 0);
220 int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
222 int ret, i;
224 /* register all given adapter frontends */
225 for (i = 0; i < adap->props.num_frontends; i++) {
227 if (adap->props.fe[i].frontend_attach == NULL) {
228 err("strange: '%s' #%d,%d "
229 "doesn't want to attach a frontend.",
230 adap->dev->desc->name, adap->id, i);
232 return 0;
235 ret = adap->props.fe[i].frontend_attach(adap);
236 if (ret || adap->fe_adap[i].fe == NULL) {
237 /* only print error when there is no FE at all */
238 if (i == 0)
239 err("no frontend was attached by '%s'",
240 adap->dev->desc->name);
242 return 0;
245 adap->fe_adap[i].fe->id = i;
247 /* re-assign sleep and wakeup functions */
248 adap->fe_adap[i].fe_init = adap->fe_adap[i].fe->ops.init;
249 adap->fe_adap[i].fe->ops.init = dvb_usb_fe_wakeup;
250 adap->fe_adap[i].fe_sleep = adap->fe_adap[i].fe->ops.sleep;
251 adap->fe_adap[i].fe->ops.sleep = dvb_usb_fe_sleep;
253 if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
254 err("Frontend %d registration failed.", i);
255 dvb_frontend_detach(adap->fe_adap[i].fe);
256 adap->fe_adap[i].fe = NULL;
257 /* In error case, do not try register more FEs,
258 * still leaving already registered FEs alive. */
259 if (i == 0)
260 return -ENODEV;
261 else
262 return 0;
265 /* only attach the tuner if the demod is there */
266 if (adap->props.fe[i].tuner_attach != NULL)
267 adap->props.fe[i].tuner_attach(adap);
269 adap->num_frontends_initialized++;
272 return 0;
275 int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
277 int i = adap->num_frontends_initialized - 1;
279 /* unregister all given adapter frontends */
280 for (; i >= 0; i--) {
281 if (adap->fe_adap[i].fe != NULL) {
282 dvb_unregister_frontend(adap->fe_adap[i].fe);
283 dvb_frontend_detach(adap->fe_adap[i].fe);
286 adap->num_frontends_initialized = 0;
288 return 0;