blob: ffdde83d1e77be6be34e5068f9a1b34adbdcd5e6 [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/*
2 * DVB USB library - provides a generic interface for a DVB USB device driver.
3 *
4 * dvb-usb-init.c
5 *
Patrick Boettcher4d43e132006-09-30 06:53:48 -03006 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07007 *
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 */
17int dvb_usb_debug;
18module_param_named(debug,dvb_usb_debug, int, 0644);
Patrick Boettcher4d43e132006-09-30 06:53:48 -030019MODULE_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);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070020
Patrick Boettcherc9b06fa2005-07-07 17:58:11 -070021int dvb_usb_disable_rc_polling;
22module_param_named(disable_rc_polling, dvb_usb_disable_rc_polling, int, 0644);
23MODULE_PARM_DESC(disable_rc_polling, "disable remote control polling (default: 0).");
24
Patrick Boettcher4d43e132006-09-30 06:53:48 -030025static int dvb_usb_force_pid_filter_usage;
26module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444);
27MODULE_PARM_DESC(disable_rc_polling, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
28
29static int dvb_usb_adapter_init(struct dvb_usb_device *d)
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)) ||
76 (ret = dvb_usb_adapter_frontend_init(adap))) {
77 return ret;
78 }
79
80 d->num_adapters_initialized++;
81 d->state |= DVB_USB_STATE_DVB;
82 }
83
84 /*
85 * when reloading the driver w/o replugging the device
86 * sometimes a timeout occures, this helps
87 */
88 if (d->props.generic_bulk_ctrl_endpoint != 0) {
89 usb_clear_halt(d->udev,usb_sndbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
90 usb_clear_halt(d->udev,usb_rcvbulkpipe(d->udev,d->props.generic_bulk_ctrl_endpoint));
91 }
92
93 return 0;
94}
95
96static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
97{
98 int n;
99 for (n = 0; n < d->num_adapters_initialized; n++) {
100 dvb_usb_adapter_frontend_exit(&d->adapter[n]);
101 dvb_usb_adapter_dvb_exit(&d->adapter[n]);
102 dvb_usb_adapter_stream_exit(&d->adapter[n]);
103 kfree(d->adapter[n].priv);
104 }
105 d->num_adapters_initialized = 0;
106 d->state &= ~DVB_USB_STATE_DVB;
107 return 0;
108}
109
110
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700111/* general initialization functions */
Adrian Bunk15ac8e62005-12-01 00:51:53 -0800112static int dvb_usb_exit(struct dvb_usb_device *d)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700113{
114 deb_info("state before exiting everything: %x\n",d->state);
115 dvb_usb_remote_exit(d);
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300116 dvb_usb_adapter_exit(d);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700117 dvb_usb_i2c_exit(d);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700118 deb_info("state should be zero now: %x\n",d->state);
119 d->state = DVB_USB_STATE_INIT;
120 kfree(d->priv);
121 kfree(d);
122 return 0;
123}
124
125static int dvb_usb_init(struct dvb_usb_device *d)
126{
127 int ret = 0;
128
Ingo Molnar3593cab2006-02-07 06:49:14 -0200129 mutex_init(&d->usb_mutex);
130 mutex_init(&d->i2c_mutex);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700131
132 d->state = DVB_USB_STATE_INIT;
133
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300134 if (d->props.size_of_priv > 0) {
135 d->priv = kzalloc(d->props.size_of_priv,GFP_KERNEL);
136 if (d->priv == NULL) {
137 err("no memory for priv in 'struct dvb_usb_device'");
138 return -ENOMEM;
Patrick Boettchera37ddce2006-09-20 06:06:11 -0300139 }
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300140 }
141
Alexey Dobriyanbe787ac2006-03-07 22:20:23 -0300142/* check the capabilities and set appropriate variables */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300143 dvb_usb_device_power_ctrl(d, 1);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700144
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300145 if ((ret = dvb_usb_i2c_init(d)) ||
146 (ret = dvb_usb_adapter_init(d))) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700147 dvb_usb_exit(d);
148 return ret;
149 }
150
151 if ((ret = dvb_usb_remote_init(d)))
152 err("could not initialize remote control.");
153
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300154 dvb_usb_device_power_ctrl(d, 0);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700155
156 return 0;
157}
158
159/* determine the name and the state of the just found USB device */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300160static struct dvb_usb_device_description * dvb_usb_find_device(struct usb_device *udev,struct dvb_usb_device_properties *props, int *cold)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700161{
162 int i,j;
163 struct dvb_usb_device_description *desc = NULL;
164 *cold = -1;
165
166 for (i = 0; i < props->num_device_descs; i++) {
167
168 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
169 deb_info("check for cold %x %x\n",props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
170 if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
171 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
172 *cold = 1;
173 desc = &props->devices[i];
174 break;
175 }
176 }
177
178 if (desc != NULL)
179 break;
180
181 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
182 deb_info("check for warm %x %x\n",props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
183 if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
184 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
185 *cold = 0;
186 desc = &props->devices[i];
187 break;
188 }
189 }
190 }
191
192 if (desc != NULL && props->identify_state != NULL)
193 props->identify_state(udev,props,&desc,cold);
194
195 return desc;
196}
197
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300198int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
199{
Patrick Boettchera37ddce2006-09-20 06:06:11 -0300200 if (onoff)
201 d->powered++;
202 else
203 d->powered--;
204
205 if (d->powered == 0 || (onoff && d->powered == 1)) { // when switching from 1 to 0 or from 0 to 1
206 deb_info("power control: %d\n", onoff);
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300207 if (d->props.power_ctrl)
208 return d->props.power_ctrl(d, onoff);
209 }
210 return 0;
211}
212
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700213/*
214 * USB
215 */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300216int dvb_usb_device_init(struct usb_interface *intf, struct dvb_usb_device_properties
Patrick Boettcher47dc3d62005-09-09 13:02:47 -0700217 *props, struct module *owner,struct dvb_usb_device **du)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700218{
219 struct usb_device *udev = interface_to_usbdev(intf);
220 struct dvb_usb_device *d = NULL;
221 struct dvb_usb_device_description *desc = NULL;
222
223 int ret = -ENOMEM,cold=0;
224
Patrick Boettcher6ff5ef22006-01-09 15:25:33 -0200225 if (du != NULL)
226 *du = NULL;
227
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700228 if ((desc = dvb_usb_find_device(udev,props,&cold)) == NULL) {
229 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
230 return -ENODEV;
231 }
232
233 if (cold) {
234 info("found a '%s' in cold state, will try to load a firmware",desc->name);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200235 ret = dvb_usb_download_firmware(udev,props);
Patrick Boettcher5bc63602006-09-19 12:51:46 -0300236 if (!props->no_reconnect || ret != 0)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700237 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700238 }
239
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200240 info("found a '%s' in warm state.",desc->name);
Panagiotis Issaris74081872006-01-11 19:40:56 -0200241 d = kzalloc(sizeof(struct dvb_usb_device),GFP_KERNEL);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200242 if (d == NULL) {
243 err("no memory for 'struct dvb_usb_device'");
244 return ret;
245 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200246
247 d->udev = udev;
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300248 memcpy(&d->props,props,sizeof(struct dvb_usb_device_properties));
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200249 d->desc = desc;
250 d->owner = owner;
251
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200252 usb_set_intfdata(intf, d);
253
254 if (du != NULL)
255 *du = d;
256
257 ret = dvb_usb_init(d);
258
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700259 if (ret == 0)
260 info("%s successfully initialized and connected.",desc->name);
261 else
262 info("%s error while loading driver (%d)",desc->name,ret);
263 return ret;
264}
265EXPORT_SYMBOL(dvb_usb_device_init);
266
267void dvb_usb_device_exit(struct usb_interface *intf)
268{
269 struct dvb_usb_device *d = usb_get_intfdata(intf);
270 const char *name = "generic DVB-USB module";
271
272 usb_set_intfdata(intf,NULL);
273 if (d != NULL && d->desc != NULL) {
274 name = d->desc->name;
275 dvb_usb_exit(d);
276 }
277 info("%s successfully deinitialized and disconnected.",name);
278
279}
280EXPORT_SYMBOL(dvb_usb_device_exit);
281
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300282MODULE_VERSION("1.0");
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700283MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
284MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
285MODULE_LICENSE("GPL");