blob: ddf282f355b3c78f9f71608f4f445f196d68d3fc [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* dvb-usb-dvb.c is part of the DVB USB library.
2 *
Patrick Boettcher4d43e132006-09-30 06:53:48 -03003 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07004 * 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
Patrick Boettcher4d43e132006-09-30 06:53:48 -030011/* does the complete input transfer handling */
Johannes Stezenbach776338e2005-06-23 22:02:35 -070012static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
13{
Patrick Boettcher4d43e132006-09-30 06:53:48 -030014 struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv;
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030015 int newfeedcount, ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070016
Patrick Boettcher4d43e132006-09-30 06:53:48 -030017 if (adap == NULL)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070018 return -ENODEV;
19
Michael Krufky2d04c132011-09-08 01:17:39 -030020 if ((adap->active_fe < 0) ||
21 (adap->active_fe >= adap->num_frontends_initialized)) {
Michael Krufky77eed212011-09-06 09:31:57 -030022 return -EINVAL;
23 }
24
Patrick Boettcher4d43e132006-09-30 06:53:48 -030025 newfeedcount = adap->feedcount + (onoff ? 1 : -1);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070026
Patrick Boettcher4d43e132006-09-30 06:53:48 -030027 /* stop feed before setting a new pid if there will be no pid anymore */
Johannes Stezenbach776338e2005-06-23 22:02:35 -070028 if (newfeedcount == 0) {
29 deb_ts("stop feeding\n");
Michael Krufky77eed212011-09-06 09:31:57 -030030 usb_urb_kill(&adap->fe_adap[adap->active_fe].stream);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070031
Michael Krufky77eed212011-09-06 09:31:57 -030032 if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
33 ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 0);
Malcolm Priestleydca6b742011-04-17 16:55:14 -030034 if (ret < 0) {
35 err("error while stopping stream.");
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030036 return ret;
Malcolm Priestleydca6b742011-04-17 16:55:14 -030037 }
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030038 }
Johannes Stezenbach776338e2005-06-23 22:02:35 -070039 }
40
Patrick Boettcher4d43e132006-09-30 06:53:48 -030041 adap->feedcount = newfeedcount;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070042
43 /* activate the pid on the device specific pid_filter */
Michael Krufky2b7a7c32011-09-16 09:40:32 -030044 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");
Michael Krufky77eed212011-09-06 09:31:57 -030048 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);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070052
53 /* start the feed if this was the first feed and there is still a feed
54 * for reception.
55 */
Patrick Boettcher4d43e132006-09-30 06:53:48 -030056 if (adap->feedcount == onoff && adap->feedcount > 0) {
Patrick Boettcher8126fdb2005-08-27 19:30:30 +020057 deb_ts("submitting all URBs\n");
Michael Krufky77eed212011-09-06 09:31:57 -030058 usb_urb_submit(&adap->fe_adap[adap->active_fe].stream);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070059
60 deb_ts("controlling pid parser\n");
Michael Krufky77eed212011-09-06 09:31:57 -030061 if (adap->props.fe[adap->active_fe].caps & DVB_USB_ADAP_HAS_PID_FILTER &&
62 adap->props.fe[adap->active_fe].caps &
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030063 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF &&
Michael Krufky77eed212011-09-06 09:31:57 -030064 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);
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030067 if (ret < 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070068 err("could not handle pid_parser");
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030069 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070070 }
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030071 }
72 deb_ts("start feeding\n");
Michael Krufky77eed212011-09-06 09:31:57 -030073 if (adap->props.fe[adap->active_fe].streaming_ctrl != NULL) {
74 ret = adap->props.fe[adap->active_fe].streaming_ctrl(adap, 1);
Malcolm Priestleyc8b7ced2011-04-16 13:30:32 -030075 if (ret < 0) {
76 err("error while enabling fifo.");
77 return ret;
78 }
79 }
Johannes Stezenbach776338e2005-06-23 22:02:35 -070080
Johannes Stezenbach776338e2005-06-23 22:02:35 -070081 }
82 return 0;
83}
84
85static 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
91static 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
Janne Grunau78e92002008-04-09 19:13:13 -030097int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070098{
Michael Krufky77eed212011-09-06 09:31:57 -030099 int i;
Janne Grunau78e92002008-04-09 19:13:13 -0300100 int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name,
101 adap->dev->owner, &adap->dev->udev->dev,
102 adapter_nums);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700103
Janne Grunau78e92002008-04-09 19:13:13 -0300104 if (ret < 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700105 deb_info("dvb_register_adapter failed: error %d", ret);
106 goto err;
107 }
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300108 adap->dvb_adap.priv = adap;
Michael Krufky94d98312009-09-07 13:32:29 -0300109 adap->dvb_adap.fe_ioctl_override = adap->props.fe_ioctl_override;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700110
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300111 if (adap->dev->props.read_mac_address) {
112 if (adap->dev->props.read_mac_address(adap->dev,adap->dvb_adap.proposed_mac) == 0)
Johannes Berg7c510e42008-10-27 17:47:26 -0700113 info("MAC address: %pM",adap->dvb_adap.proposed_mac);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700114 else
115 err("MAC address reading failed.");
116 }
117
118
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300119 adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
120 adap->demux.priv = adap;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700121
Michael Krufky77eed212011-09-06 09:31:57 -0300122 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;
126 }
127 adap->demux.feednum = adap->demux.filternum;
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300128 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) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700132 err("dvb_dmx_init failed: error %d",ret);
133 goto err_dmx;
134 }
135
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300136 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) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700140 err("dvb_dmxdev_init failed: error %d",ret);
141 goto err_dmx_dev;
142 }
143
Jonathan Nieder60826f02011-12-31 08:10:57 -0300144 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;
148 }
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700149
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300150 adap->state |= DVB_USB_ADAP_STATE_DVB;
Trent Piepho83977032006-05-12 20:36:24 -0300151 return 0;
152
Jonathan Nieder60826f02011-12-31 08:10:57 -0300153err_net_init:
154 dvb_dmxdev_release(&adap->dmxdev);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700155err_dmx_dev:
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300156 dvb_dmx_release(&adap->demux);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700157err_dmx:
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300158 dvb_unregister_adapter(&adap->dvb_adap);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700159err:
160 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700161}
162
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300163int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700164{
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300165 if (adap->state & DVB_USB_ADAP_STATE_DVB) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700166 deb_info("unregistering DVB part\n");
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300167 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;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700173 }
174 return 0;
175}
176
Michael Krufky77eed212011-09-06 09:31:57 -0300177static int dvb_usb_set_active_fe(struct dvb_frontend *fe, int onoff)
178{
179 struct dvb_usb_adapter *adap = fe->dvb->priv;
180
181 int ret = (adap->props.frontend_ctrl) ?
182 adap->props.frontend_ctrl(fe, onoff) : 0;
183
184 if (ret < 0) {
185 err("frontend_ctrl request failed");
186 return ret;
187 }
188 if (onoff)
189 adap->active_fe = fe->id;
190
191 return 0;
192}
193
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700194static int dvb_usb_fe_wakeup(struct dvb_frontend *fe)
195{
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300196 struct dvb_usb_adapter *adap = fe->dvb->priv;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700197
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300198 dvb_usb_device_power_ctrl(adap->dev, 1);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700199
Michael Krufky77eed212011-09-06 09:31:57 -0300200 dvb_usb_set_active_fe(fe, 1);
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300201
Michael Krufky77eed212011-09-06 09:31:57 -0300202 if (adap->fe_adap[fe->id].fe_init)
203 adap->fe_adap[fe->id].fe_init(fe);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700204
205 return 0;
206}
207
208static int dvb_usb_fe_sleep(struct dvb_frontend *fe)
209{
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300210 struct dvb_usb_adapter *adap = fe->dvb->priv;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700211
Michael Krufky77eed212011-09-06 09:31:57 -0300212 if (adap->fe_adap[fe->id].fe_sleep)
213 adap->fe_adap[fe->id].fe_sleep(fe);
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300214
Michael Krufky77eed212011-09-06 09:31:57 -0300215 dvb_usb_set_active_fe(fe, 0);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700216
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300217 return dvb_usb_device_power_ctrl(adap->dev, 0);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700218}
219
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300220int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700221{
Michael Krufky77eed212011-09-06 09:31:57 -0300222 int ret, i;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700223
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300224 /* register all given adapter frontends */
Michael Krufky77eed212011-09-06 09:31:57 -0300225 for (i = 0; i < adap->props.num_frontends; i++) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700226
Michael Krufky77eed212011-09-06 09:31:57 -0300227 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);
231
232 return 0;
233 }
234
235 ret = adap->props.fe[i].frontend_attach(adap);
236 if (ret || adap->fe_adap[i].fe == NULL) {
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300237 /* 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);
241
242 return 0;
243 }
244
Michael Krufky77eed212011-09-06 09:31:57 -0300245 adap->fe_adap[i].fe->id = i;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300246
247 /* re-assign sleep and wakeup functions */
Michael Krufky77eed212011-09-06 09:31:57 -0300248 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;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300252
Michael Krufky77eed212011-09-06 09:31:57 -0300253 if (dvb_register_frontend(&adap->dvb_adap, adap->fe_adap[i].fe)) {
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300254 err("Frontend %d registration failed.", i);
Michael Krufky77eed212011-09-06 09:31:57 -0300255 dvb_frontend_detach(adap->fe_adap[i].fe);
256 adap->fe_adap[i].fe = NULL;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300257 /* 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;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700263 }
Patrick Boettcher5a19f312006-08-02 08:44:31 -0300264
265 /* only attach the tuner if the demod is there */
Michael Krufky77eed212011-09-06 09:31:57 -0300266 if (adap->props.fe[i].tuner_attach != NULL)
267 adap->props.fe[i].tuner_attach(adap);
268
269 adap->num_frontends_initialized++;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300270 }
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700271
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700272 return 0;
273}
274
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300275int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700276{
Michael Krufky77eed212011-09-06 09:31:57 -0300277 int i = adap->num_frontends_initialized - 1;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300278
279 /* unregister all given adapter frontends */
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300280 for (; i >= 0; i--) {
Michael Krufky77eed212011-09-06 09:31:57 -0300281 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);
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300284 }
Patrick Boettcher4e66c972006-08-08 15:48:11 -0300285 }
Michael Krufky77eed212011-09-06 09:31:57 -0300286 adap->num_frontends_initialized = 0;
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -0300287
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700288 return 0;
289}