[media] dvb-usb: multi-frontend support (MFE)
[pandora-kernel.git] / drivers / media / dvb / dvb-usb / dvb-usb-init.c
1 /*
2  * DVB USB library - provides a generic interface for a DVB USB device driver.
3  *
4  * dvb-usb-init.c
5  *
6  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the Free
10  *      Software Foundation, version 2.
11  *
12  * see Documentation/dvb/README.dvb-usb for more information
13  */
14 #include "dvb-usb-common.h"
15
16 /* debug */
17 int dvb_usb_debug;
18 module_param_named(debug, dvb_usb_debug, int, 0644);
19 MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64,mem=128,uxfer=256  (or-able))." DVB_USB_DEBUG_STATUS);
20
21 int dvb_usb_disable_rc_polling;
22 module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
23 MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
24
25 static int dvb_usb_force_pid_filter_usage;
26 module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
27 MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
28
29 static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
30 {
31         struct dvb_usb_adapter *adap;
32         int ret, n;
33
34         for (n = 0; n < d->props.num_adapters; n++) {
35                 adap = &d->adapter[n];
36                 adap->dev = d;
37                 adap->id  = n;
38
39                 memcpy(&adap->props, &d->props.adapter[n], sizeof(struct dvb_usb_adapter_properties));
40
41                 /* speed - when running at FULL speed we need a HW PID filter */
42                 if (d->udev->speed == USB_SPEED_FULL && !(adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
43                         err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
44                         return -ENODEV;
45                 }
46
47                 if ((d->udev->speed == USB_SPEED_FULL && adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
48                         (adap->props.caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
49                         info("will use the device's hardware PID filter (table count: %d).", adap->props.pid_filter_count);
50                         adap->pid_filtering  = 1;
51                         adap->max_feed_count = adap->props.pid_filter_count;
52                 } else {
53                         info("will pass the complete MPEG2 transport stream to the software demuxer.");
54                         adap->pid_filtering  = 0;
55                         adap->max_feed_count = 255;
56                 }
57
58                 if (!adap->pid_filtering &&
59                         dvb_usb_force_pid_filter_usage &&
60                         adap->props.caps & DVB_USB_ADAP_HAS_PID_FILTER) {
61                         info("pid filter enabled by module option.");
62                         adap->pid_filtering  = 1;
63                         adap->max_feed_count = adap->props.pid_filter_count;
64                 }
65
66                 if (adap->props.size_of_priv > 0) {
67                         adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
68                         if (adap->priv == NULL) {
69                                 err("no memory for priv for adapter %d.", n);
70                                 return -ENOMEM;
71                         }
72                 }
73
74                 if ((ret = dvb_usb_adapter_stream_init(adap)) ||
75                         (ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
76                         (ret = dvb_usb_adapter_frontend_init(adap))) {
77                         return ret;
78                 }
79
80                 /* use exclusive FE lock if there is multiple shared FEs */
81                 if (adap->fe[1])
82                         adap->dvb_adap.mfe_shared = 1;
83
84                 d->num_adapters_initialized++;
85                 d->state |= DVB_USB_STATE_DVB;
86         }
87
88         /*
89          * when reloading the driver w/o replugging the device
90          * sometimes a timeout occures, this helps
91          */
92         if (d->props.generic_bulk_ctrl_endpoint != 0) {
93                 usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
94                 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
95         }
96
97         return 0;
98 }
99
100 static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
101 {
102         int n;
103
104         for (n = 0; n < d->num_adapters_initialized; n++) {
105                 dvb_usb_adapter_frontend_exit(&d->adapter[n]);
106                 dvb_usb_adapter_dvb_exit(&d->adapter[n]);
107                 dvb_usb_adapter_stream_exit(&d->adapter[n]);
108                 kfree(d->adapter[n].priv);
109         }
110         d->num_adapters_initialized = 0;
111         d->state &= ~DVB_USB_STATE_DVB;
112         return 0;
113 }
114
115
116 /* general initialization functions */
117 static int dvb_usb_exit(struct dvb_usb_device *d)
118 {
119         deb_info("state before exiting everything: %x\n", d->state);
120         dvb_usb_remote_exit(d);
121         dvb_usb_adapter_exit(d);
122         dvb_usb_i2c_exit(d);
123         deb_info("state should be zero now: %x\n", d->state);
124         d->state = DVB_USB_STATE_INIT;
125         kfree(d->priv);
126         kfree(d);
127         return 0;
128 }
129
130 static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
131 {
132         int ret = 0;
133
134         mutex_init(&d->usb_mutex);
135         mutex_init(&d->i2c_mutex);
136
137         d->state = DVB_USB_STATE_INIT;
138
139         if (d->props.size_of_priv > 0) {
140                 d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
141                 if (d->priv == NULL) {
142                         err("no memory for priv in 'struct dvb_usb_device'");
143                         return -ENOMEM;
144                 }
145         }
146
147         /* check the capabilities and set appropriate variables */
148         dvb_usb_device_power_ctrl(d, 1);
149
150         if ((ret = dvb_usb_i2c_init(d)) ||
151                 (ret = dvb_usb_adapter_init(d, adapter_nums))) {
152                 dvb_usb_exit(d);
153                 return ret;
154         }
155
156         if ((ret = dvb_usb_remote_init(d)))
157                 err("could not initialize remote control.");
158
159         dvb_usb_device_power_ctrl(d, 0);
160
161         return 0;
162 }
163
164 /* determine the name and the state of the just found USB device */
165 static struct dvb_usb_device_description *dvb_usb_find_device(struct usb_device *udev, struct dvb_usb_device_properties *props, int *cold)
166 {
167         int i, j;
168         struct dvb_usb_device_description *desc = NULL;
169
170         *cold = -1;
171
172         for (i = 0; i < props->num_device_descs; i++) {
173
174                 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
175                         deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
176                         if (props->devices[i].cold_ids[j]->idVendor  == le16_to_cpu(udev->descriptor.idVendor) &&
177                                 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
178                                 *cold = 1;
179                                 desc = &props->devices[i];
180                                 break;
181                         }
182                 }
183
184                 if (desc != NULL)
185                         break;
186
187                 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
188                         deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
189                         if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
190                                 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
191                                 *cold = 0;
192                                 desc = &props->devices[i];
193                                 break;
194                         }
195                 }
196         }
197
198         if (desc != NULL && props->identify_state != NULL)
199                 props->identify_state(udev, props, &desc, cold);
200
201         return desc;
202 }
203
204 int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
205 {
206         if (onoff)
207                 d->powered++;
208         else
209                 d->powered--;
210
211         if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
212                 deb_info("power control: %d\n", onoff);
213                 if (d->props.power_ctrl)
214                         return d->props.power_ctrl(d, onoff);
215         }
216         return 0;
217 }
218
219 /*
220  * USB
221  */
222 int dvb_usb_device_init(struct usb_interface *intf,
223                         struct dvb_usb_device_properties *props,
224                         struct module *owner, struct dvb_usb_device **du,
225                         short *adapter_nums)
226 {
227         struct usb_device *udev = interface_to_usbdev(intf);
228         struct dvb_usb_device *d = NULL;
229         struct dvb_usb_device_description *desc = NULL;
230
231         int ret = -ENOMEM, cold = 0;
232
233         if (du != NULL)
234                 *du = NULL;
235
236         if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
237                 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
238                 return -ENODEV;
239         }
240
241         if (cold) {
242                 info("found a '%s' in cold state, will try to load a firmware", desc->name);
243                 ret = dvb_usb_download_firmware(udev, props);
244                 if (!props->no_reconnect || ret != 0)
245                         return ret;
246         }
247
248         info("found a '%s' in warm state.", desc->name);
249         d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
250         if (d == NULL) {
251                 err("no memory for 'struct dvb_usb_device'");
252                 return -ENOMEM;
253         }
254
255         d->udev = udev;
256         memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
257         d->desc = desc;
258         d->owner = owner;
259
260         usb_set_intfdata(intf, d);
261
262         if (du != NULL)
263                 *du = d;
264
265         ret = dvb_usb_init(d, adapter_nums);
266
267         if (ret == 0)
268                 info("%s successfully initialized and connected.", desc->name);
269         else
270                 info("%s error while loading driver (%d)", desc->name, ret);
271         return ret;
272 }
273 EXPORT_SYMBOL(dvb_usb_device_init);
274
275 void dvb_usb_device_exit(struct usb_interface *intf)
276 {
277         struct dvb_usb_device *d = usb_get_intfdata(intf);
278         const char *name = "generic DVB-USB module";
279
280         usb_set_intfdata(intf, NULL);
281         if (d != NULL && d->desc != NULL) {
282                 name = d->desc->name;
283                 dvb_usb_exit(d);
284         }
285         info("%s successfully deinitialized and disconnected.", name);
286
287 }
288 EXPORT_SYMBOL(dvb_usb_device_exit);
289
290 MODULE_VERSION("1.0");
291 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
292 MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
293 MODULE_LICENSE("GPL");