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