Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dvb-usb-dvb.c
Go to the documentation of this file.
1 /* dvb-usb-dvb.c is part of the DVB USB library.
2  *
3  * Copyright (C) 2004-6 Patrick Boettcher ([email protected])
4  * see dvb-usb-init.c for copyright information.
5  *
6  * This file contains functions for initializing and handling the
7  * linux-dvb API.
8  */
9 #include "dvb-usb-common.h"
10 
11 /* does the complete input transfer handling */
12 static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
13 {
14  struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
15  int newfeedcount, ret;
16 
17  if (adap == NULL)
18  return -ENODEV;
19 
20  if ((adap->active_fe < 0) ||
21  (adap->active_fe >= adap->num_frontends_initialized)) {
22  return -EINVAL;
23  }
24 
25  newfeedcount = adap->feedcount + (onoff ? 1 : -1);
26 
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);
31 
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;
37  }
38  }
39  }
40 
41  adap->feedcount = newfeedcount;
42 
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);
52 
53  /* start the feed if this was the first feed and there is still a feed
54  * for reception.
55  */
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);
59 
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 &
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;
70  }
71  }
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;
78  }
79  }
80 
81  }
82  return 0;
83 }
84 
85 static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
86 {
87  deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
88  return dvb_usb_ctrl_feed(dvbdmxfeed,1);
89 }
90 
91 static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
92 {
93  deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
94  return dvb_usb_ctrl_feed(dvbdmxfeed,0);
95 }
96 
97 int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
98 {
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);
103 
104  if (ret < 0) {
105  deb_info("dvb_register_adapter failed: error %d", ret);
106  goto err;
107  }
108  adap->dvb_adap.priv = adap;
109 
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.");
115  }
116 
117 
118  adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
119  adap->demux.priv = adap;
120 
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;
125  }
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;
133  }
134 
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;
141  }
142 
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;
147  }
148 
149  adap->state |= DVB_USB_ADAP_STATE_DVB;
150  return 0;
151 
152 err_net_init:
153  dvb_dmxdev_release(&adap->dmxdev);
154 err_dmx_dev:
155  dvb_dmx_release(&adap->demux);
156 err_dmx:
158 err:
159  return ret;
160 }
161 
163 {
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);
171  adap->state &= ~DVB_USB_ADAP_STATE_DVB;
172  }
173  return 0;
174 }
175 
176 static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
177 {
178  struct dvb_usb_adapter *adap = fe->dvb->priv;
179 
180  int ret = (adap->props.frontend_ctrl) ?
181  adap->props.frontend_ctrl(fe, onoff) : 0;
182 
183  if (ret < 0) {
184  err("frontend_ctrl request failed");
185  return ret;
186  }
187  if (onoff)
188  adap->active_fe = fe->id;
189 
190  return 0;
191 }
192 
193 static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
194 {
195  struct dvb_usb_adapter *adap = fe->dvb->priv;
196 
197  dvb_usb_device_power_ctrl(adap->dev, 1);
198 
199  dvb_usb_set_active_fe(fe, 1);
200 
201  if (adap->fe_adap[fe->id].fe_init)
202  adap->fe_adap[fe->id].fe_init(fe);
203 
204  return 0;
205 }
206 
207 static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
208 {
209  struct dvb_usb_adapter *adap = fe->dvb->priv;
210 
211  if (adap->fe_adap[fe->id].fe_sleep)
212  adap->fe_adap[fe->id].fe_sleep(fe);
213 
214  dvb_usb_set_active_fe(fe, 0);
215 
216  return dvb_usb_device_power_ctrl(adap->dev, 0);
217 }
218 
220 {
221  int ret, i;
222 
223  /* register all given adapter frontends */
224  for (i = 0; i < adap->props.num_frontends; i++) {
225 
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);
230 
231  return 0;
232  }
233 
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);
240 
241  return 0;
242  }
243 
244  adap->fe_adap[i].fe->id = i;
245 
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;
251 
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;
262  }
263 
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);
267 
269  }
270 
271  return 0;
272 }
273 
275 {
276  int i = adap->num_frontends_initialized - 1;
277 
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);
283  }
284  }
285  adap->num_frontends_initialized = 0;
286 
287  return 0;
288 }