Commit | Line | Data |
---|---|---|
776338e1 JS |
1 | /* dvb-usb-dvb.c is part of the DVB USB library. |
2 | * | |
4d43e13f | 3 | * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de) |
776338e1 JS |
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 | ||
4d43e13f | 11 | /* does the complete input transfer handling */ |
776338e1 JS |
12 | static int dvb_usb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff) |
13 | { | |
4d43e13f | 14 | struct dvb_usb_adapter *adap = dvbdmxfeed->demux->priv; |
776338e1 JS |
15 | int newfeedcount,ret; |
16 | ||
4d43e13f | 17 | if (adap == NULL) |
776338e1 JS |
18 | return -ENODEV; |
19 | ||
4d43e13f | 20 | newfeedcount = adap->feedcount + (onoff ? 1 : -1); |
776338e1 | 21 | |
4d43e13f | 22 | /* stop feed before setting a new pid if there will be no pid anymore */ |
776338e1 JS |
23 | if (newfeedcount == 0) { |
24 | deb_ts("stop feeding\n"); | |
4d43e13f | 25 | usb_urb_kill(&adap->stream); |
776338e1 | 26 | |
4d43e13f PB |
27 | if (adap->props.streaming_ctrl != NULL) |
28 | if ((ret = adap->props.streaming_ctrl(adap,0))) | |
776338e1 | 29 | err("error while stopping stream."); |
776338e1 JS |
30 | } |
31 | ||
4d43e13f | 32 | adap->feedcount = newfeedcount; |
776338e1 JS |
33 | |
34 | /* activate the pid on the device specific pid_filter */ | |
4d43e13f PB |
35 | deb_ts("setting pid (%s): %5d %04x at index %d '%s'\n",adap->pid_filtering ? |
36 | "yes" : "no", dvbdmxfeed->pid,dvbdmxfeed->pid,dvbdmxfeed->index,onoff ? | |
37 | "on" : "off"); | |
38 | if (adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER && | |
39 | adap->pid_filtering && | |
40 | adap->props.pid_filter != NULL) | |
41 | adap->props.pid_filter(adap, dvbdmxfeed->index, dvbdmxfeed->pid,onoff); | |
776338e1 JS |
42 | |
43 | /* start the feed if this was the first feed and there is still a feed | |
44 | * for reception. | |
45 | */ | |
4d43e13f | 46 | if (adap->feedcount == onoff && adap->feedcount > 0) { |
8126fdbc | 47 | deb_ts("submitting all URBs\n"); |
4d43e13f | 48 | usb_urb_submit(&adap->stream); |
776338e1 JS |
49 | |
50 | deb_ts("controlling pid parser\n"); | |
4d43e13f PB |
51 | if (adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER && |
52 | adap->props.caps & DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF && | |
53 | adap->props.pid_filter_ctrl != NULL) | |
54 | if (adap->props.pid_filter_ctrl(adap,adap->pid_filtering) < 0) | |
776338e1 JS |
55 | err("could not handle pid_parser"); |
56 | ||
57 | deb_ts("start feeding\n"); | |
4d43e13f PB |
58 | if (adap->props.streaming_ctrl != NULL) |
59 | if (adap->props.streaming_ctrl(adap,1)) { | |
776338e1 JS |
60 | err("error while enabling fifo."); |
61 | return -ENODEV; | |
62 | } | |
63 | ||
776338e1 JS |
64 | } |
65 | return 0; | |
66 | } | |
67 | ||
68 | static int dvb_usb_start_feed(struct dvb_demux_feed *dvbdmxfeed) | |
69 | { | |
70 | deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type); | |
71 | return dvb_usb_ctrl_feed(dvbdmxfeed,1); | |
72 | } | |
73 | ||
74 | static int dvb_usb_stop_feed(struct dvb_demux_feed *dvbdmxfeed) | |
75 | { | |
76 | deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type); | |
77 | return dvb_usb_ctrl_feed(dvbdmxfeed,0); | |
78 | } | |
79 | ||
78e92006 | 80 | int dvb_usb_adapter_dvb_init(struct dvb_usb_adapter *adap, short *adapter_nums) |
776338e1 | 81 | { |
78e92006 JG |
82 | int ret = dvb_register_adapter(&adap->dvb_adap, adap->dev->desc->name, |
83 | adap->dev->owner, &adap->dev->udev->dev, | |
84 | adapter_nums); | |
776338e1 | 85 | |
78e92006 | 86 | if (ret < 0) { |
776338e1 JS |
87 | deb_info("dvb_register_adapter failed: error %d", ret); |
88 | goto err; | |
89 | } | |
4d43e13f PB |
90 | adap->dvb_adap.priv = adap; |
91 | ||
92 | if (adap->dev->props.read_mac_address) { | |
93 | if (adap->dev->props.read_mac_address(adap->dev,adap->dvb_adap.proposed_mac) == 0) | |
94 | info("MAC address: %02x:%02x:%02x:%02x:%02x:%02x",adap->dvb_adap.proposed_mac[0], | |
95 | adap->dvb_adap.proposed_mac[1], adap->dvb_adap.proposed_mac[2], | |
96 | adap->dvb_adap.proposed_mac[3], adap->dvb_adap.proposed_mac[4], | |
97 | adap->dvb_adap.proposed_mac[5]); | |
776338e1 JS |
98 | else |
99 | err("MAC address reading failed."); | |
100 | } | |
101 | ||
102 | ||
4d43e13f PB |
103 | adap->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING; |
104 | adap->demux.priv = adap; | |
776338e1 | 105 | |
4d43e13f PB |
106 | adap->demux.feednum = adap->demux.filternum = adap->max_feed_count; |
107 | adap->demux.start_feed = dvb_usb_start_feed; | |
108 | adap->demux.stop_feed = dvb_usb_stop_feed; | |
109 | adap->demux.write_to_decoder = NULL; | |
110 | if ((ret = dvb_dmx_init(&adap->demux)) < 0) { | |
776338e1 JS |
111 | err("dvb_dmx_init failed: error %d",ret); |
112 | goto err_dmx; | |
113 | } | |
114 | ||
4d43e13f PB |
115 | adap->dmxdev.filternum = adap->demux.filternum; |
116 | adap->dmxdev.demux = &adap->demux.dmx; | |
117 | adap->dmxdev.capabilities = 0; | |
118 | if ((ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap)) < 0) { | |
776338e1 JS |
119 | err("dvb_dmxdev_init failed: error %d",ret); |
120 | goto err_dmx_dev; | |
121 | } | |
122 | ||
4d43e13f | 123 | dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx); |
776338e1 | 124 | |
4d43e13f | 125 | adap->state |= DVB_USB_ADAP_STATE_DVB; |
8397703e TP |
126 | return 0; |
127 | ||
776338e1 | 128 | err_dmx_dev: |
4d43e13f | 129 | dvb_dmx_release(&adap->demux); |
776338e1 | 130 | err_dmx: |
4d43e13f | 131 | dvb_unregister_adapter(&adap->dvb_adap); |
776338e1 JS |
132 | err: |
133 | return ret; | |
776338e1 JS |
134 | } |
135 | ||
4d43e13f | 136 | int dvb_usb_adapter_dvb_exit(struct dvb_usb_adapter *adap) |
776338e1 | 137 | { |
4d43e13f | 138 | if (adap->state & DVB_USB_ADAP_STATE_DVB) { |
776338e1 | 139 | deb_info("unregistering DVB part\n"); |
4d43e13f PB |
140 | dvb_net_release(&adap->dvb_net); |
141 | adap->demux.dmx.close(&adap->demux.dmx); | |
142 | dvb_dmxdev_release(&adap->dmxdev); | |
143 | dvb_dmx_release(&adap->demux); | |
144 | dvb_unregister_adapter(&adap->dvb_adap); | |
145 | adap->state &= ~DVB_USB_ADAP_STATE_DVB; | |
776338e1 JS |
146 | } |
147 | return 0; | |
148 | } | |
149 | ||
150 | static int dvb_usb_fe_wakeup(struct dvb_frontend *fe) | |
151 | { | |
4d43e13f | 152 | struct dvb_usb_adapter *adap = fe->dvb->priv; |
776338e1 | 153 | |
4d43e13f | 154 | dvb_usb_device_power_ctrl(adap->dev, 1); |
776338e1 | 155 | |
4d43e13f PB |
156 | if (adap->fe_init) |
157 | adap->fe_init(fe); | |
776338e1 JS |
158 | |
159 | return 0; | |
160 | } | |
161 | ||
162 | static int dvb_usb_fe_sleep(struct dvb_frontend *fe) | |
163 | { | |
4d43e13f | 164 | struct dvb_usb_adapter *adap = fe->dvb->priv; |
776338e1 | 165 | |
4d43e13f PB |
166 | if (adap->fe_sleep) |
167 | adap->fe_sleep(fe); | |
776338e1 | 168 | |
4d43e13f | 169 | return dvb_usb_device_power_ctrl(adap->dev, 0); |
776338e1 JS |
170 | } |
171 | ||
4d43e13f | 172 | int dvb_usb_adapter_frontend_init(struct dvb_usb_adapter *adap) |
776338e1 | 173 | { |
4d43e13f PB |
174 | if (adap->props.frontend_attach == NULL) { |
175 | err("strange: '%s' #%d doesn't want to attach a frontend.",adap->dev->desc->name, adap->id); | |
776338e1 JS |
176 | return 0; |
177 | } | |
178 | ||
776338e1 | 179 | /* re-assign sleep and wakeup functions */ |
4d43e13f PB |
180 | if (adap->props.frontend_attach(adap) == 0 && adap->fe != NULL) { |
181 | adap->fe_init = adap->fe->ops.init; adap->fe->ops.init = dvb_usb_fe_wakeup; | |
182 | adap->fe_sleep = adap->fe->ops.sleep; adap->fe->ops.sleep = dvb_usb_fe_sleep; | |
776338e1 | 183 | |
4d43e13f | 184 | if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) { |
776338e1 | 185 | err("Frontend registration failed."); |
4d43e13f PB |
186 | dvb_frontend_detach(adap->fe); |
187 | adap->fe = NULL; | |
776338e1 JS |
188 | return -ENODEV; |
189 | } | |
5a19f312 PB |
190 | |
191 | /* only attach the tuner if the demod is there */ | |
4d43e13f PB |
192 | if (adap->props.tuner_attach != NULL) |
193 | adap->props.tuner_attach(adap); | |
776338e1 | 194 | } else |
4d43e13f | 195 | err("no frontend was attached by '%s'",adap->dev->desc->name); |
776338e1 | 196 | |
776338e1 JS |
197 | return 0; |
198 | } | |
199 | ||
4d43e13f | 200 | int dvb_usb_adapter_frontend_exit(struct dvb_usb_adapter *adap) |
776338e1 | 201 | { |
4d43e13f PB |
202 | if (adap->fe != NULL) { |
203 | dvb_unregister_frontend(adap->fe); | |
204 | dvb_frontend_detach(adap->fe); | |
4e66c97c | 205 | } |
776338e1 JS |
206 | return 0; |
207 | } |