blob: 169196ec2d4eceb0b4ef0616bdcd55e7a626624b [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;
Dan Carpentercd3172d2010-07-01 01:50:04 -030018module_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);
Michael Mauche927b3f2007-08-18 18:02:31 -030027MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0).");
Patrick Boettcher4d43e132006-09-30 06:53:48 -030028
Janne Grunau78e92002008-04-09 19:13:13 -030029static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs)
Patrick Boettcher4d43e132006-09-30 06:53:48 -030030{
31 struct dvb_usb_adapter *adap;
Michael Krufky77eed212011-09-06 09:31:57 -030032 int ret, n, o;
Patrick Boettcher4d43e132006-09-30 06:53:48 -030033
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
Michael Krufky77eed212011-09-06 09:31:57 -030041 for (o = 0; o < adap->props.num_frontends; o++) {
42 struct dvb_usb_adapter_fe_properties *props = &adap->props.fe[o];
Dan Carpentercd3172d2010-07-01 01:50:04 -030043 /* speed - when running at FULL speed we need a HW PID filter */
Michael Krufky77eed212011-09-06 09:31:57 -030044 if (d->udev->speed == USB_SPEED_FULL && !(props->caps & DVB_USB_ADAP_HAS_PID_FILTER)) {
Patrick Boettcher4d43e132006-09-30 06:53:48 -030045 err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
46 return -ENODEV;
47 }
48
Michael Krufky77eed212011-09-06 09:31:57 -030049 if ((d->udev->speed == USB_SPEED_FULL && props->caps & DVB_USB_ADAP_HAS_PID_FILTER) ||
50 (props->caps & DVB_USB_ADAP_NEED_PID_FILTERING)) {
51 info("will use the device's hardware PID filter (table count: %d).", props->pid_filter_count);
52 adap->fe_adap[o].pid_filtering = 1;
53 adap->fe_adap[o].max_feed_count = props->pid_filter_count;
Patrick Boettcher4d43e132006-09-30 06:53:48 -030054 } else {
55 info("will pass the complete MPEG2 transport stream to the software demuxer.");
Michael Krufky77eed212011-09-06 09:31:57 -030056 adap->fe_adap[o].pid_filtering = 0;
57 adap->fe_adap[o].max_feed_count = 255;
Patrick Boettcher4d43e132006-09-30 06:53:48 -030058 }
59
Michael Krufky77eed212011-09-06 09:31:57 -030060 if (!adap->fe_adap[o].pid_filtering &&
Patrick Boettcher4d43e132006-09-30 06:53:48 -030061 dvb_usb_force_pid_filter_usage &&
Michael Krufky77eed212011-09-06 09:31:57 -030062 props->caps & DVB_USB_ADAP_HAS_PID_FILTER) {
Patrick Boettcher4d43e132006-09-30 06:53:48 -030063 info("pid filter enabled by module option.");
Michael Krufky77eed212011-09-06 09:31:57 -030064 adap->fe_adap[o].pid_filtering = 1;
65 adap->fe_adap[o].max_feed_count = props->pid_filter_count;
Patrick Boettcher4d43e132006-09-30 06:53:48 -030066 }
67
Michael Krufky77eed212011-09-06 09:31:57 -030068 if (props->size_of_priv > 0) {
69 adap->fe_adap[o].priv = kzalloc(props->size_of_priv, GFP_KERNEL);
70 if (adap->fe_adap[o].priv == NULL) {
71 err("no memory for priv for adapter %d fe %d.", n, o);
72 return -ENOMEM;
73 }
74 }
75 }
76
Patrick Boettcher4d43e132006-09-30 06:53:48 -030077 if (adap->props.size_of_priv > 0) {
Dan Carpentercd3172d2010-07-01 01:50:04 -030078 adap->priv = kzalloc(adap->props.size_of_priv, GFP_KERNEL);
Patrick Boettcher4d43e132006-09-30 06:53:48 -030079 if (adap->priv == NULL) {
Dan Carpentercd3172d2010-07-01 01:50:04 -030080 err("no memory for priv for adapter %d.", n);
Patrick Boettcher4d43e132006-09-30 06:53:48 -030081 return -ENOMEM;
82 }
83 }
84
85 if ((ret = dvb_usb_adapter_stream_init(adap)) ||
Janne Grunau78e92002008-04-09 19:13:13 -030086 (ret = dvb_usb_adapter_dvb_init(adap, adapter_nrs)) ||
Patrick Boettcher4d43e132006-09-30 06:53:48 -030087 (ret = dvb_usb_adapter_frontend_init(adap))) {
88 return ret;
89 }
90
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -030091 /* use exclusive FE lock if there is multiple shared FEs */
Michael Krufky77eed212011-09-06 09:31:57 -030092 if (adap->fe_adap[1].fe)
Antti Palosaari9bd9e3b2011-07-25 20:16:13 -030093 adap->dvb_adap.mfe_shared = 1;
94
Patrick Boettcher4d43e132006-09-30 06:53:48 -030095 d->num_adapters_initialized++;
96 d->state |= DVB_USB_STATE_DVB;
97 }
98
99 /*
100 * when reloading the driver w/o replugging the device
101 * sometimes a timeout occures, this helps
102 */
103 if (d->props.generic_bulk_ctrl_endpoint != 0) {
Dan Carpentercd3172d2010-07-01 01:50:04 -0300104 usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
105 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint));
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300106 }
107
108 return 0;
109}
110
111static int dvb_usb_adapter_exit(struct dvb_usb_device *d)
112{
113 int n;
Dan Carpentercd3172d2010-07-01 01:50:04 -0300114
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300115 for (n = 0; n < d->num_adapters_initialized; n++) {
116 dvb_usb_adapter_frontend_exit(&d->adapter[n]);
117 dvb_usb_adapter_dvb_exit(&d->adapter[n]);
118 dvb_usb_adapter_stream_exit(&d->adapter[n]);
119 kfree(d->adapter[n].priv);
120 }
121 d->num_adapters_initialized = 0;
122 d->state &= ~DVB_USB_STATE_DVB;
123 return 0;
124}
125
126
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700127/* general initialization functions */
Adrian Bunk15ac8e62005-12-01 00:51:53 -0800128static int dvb_usb_exit(struct dvb_usb_device *d)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700129{
Dan Carpentercd3172d2010-07-01 01:50:04 -0300130 deb_info("state before exiting everything: %x\n", d->state);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700131 dvb_usb_remote_exit(d);
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300132 dvb_usb_adapter_exit(d);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700133 dvb_usb_i2c_exit(d);
Dan Carpentercd3172d2010-07-01 01:50:04 -0300134 deb_info("state should be zero now: %x\n", d->state);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700135 d->state = DVB_USB_STATE_INIT;
136 kfree(d->priv);
137 kfree(d);
138 return 0;
139}
140
Janne Grunau78e92002008-04-09 19:13:13 -0300141static int dvb_usb_init(struct dvb_usb_device *d, short *adapter_nums)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700142{
143 int ret = 0;
144
Ingo Molnar3593cab2006-02-07 06:49:14 -0200145 mutex_init(&d->usb_mutex);
146 mutex_init(&d->i2c_mutex);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700147
148 d->state = DVB_USB_STATE_INIT;
149
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300150 if (d->props.size_of_priv > 0) {
Dan Carpentercd3172d2010-07-01 01:50:04 -0300151 d->priv = kzalloc(d->props.size_of_priv, GFP_KERNEL);
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300152 if (d->priv == NULL) {
153 err("no memory for priv in 'struct dvb_usb_device'");
154 return -ENOMEM;
Patrick Boettchera37ddce2006-09-20 06:06:11 -0300155 }
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300156 }
157
Dan Carpentercd3172d2010-07-01 01:50:04 -0300158 /* check the capabilities and set appropriate variables */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300159 dvb_usb_device_power_ctrl(d, 1);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700160
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300161 if ((ret = dvb_usb_i2c_init(d)) ||
Janne Grunau78e92002008-04-09 19:13:13 -0300162 (ret = dvb_usb_adapter_init(d, adapter_nums))) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700163 dvb_usb_exit(d);
164 return ret;
165 }
166
167 if ((ret = dvb_usb_remote_init(d)))
168 err("could not initialize remote control.");
169
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300170 dvb_usb_device_power_ctrl(d, 0);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700171
172 return 0;
173}
174
175/* determine the name and the state of the just found USB device */
Dan Carpentercd3172d2010-07-01 01:50:04 -0300176static 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 -0700177{
Dan Carpentercd3172d2010-07-01 01:50:04 -0300178 int i, j;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700179 struct dvb_usb_device_description *desc = NULL;
Dan Carpentercd3172d2010-07-01 01:50:04 -0300180
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700181 *cold = -1;
182
183 for (i = 0; i < props->num_device_descs; i++) {
184
185 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
Dan Carpentercd3172d2010-07-01 01:50:04 -0300186 deb_info("check for cold %x %x\n", props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700187 if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
188 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
189 *cold = 1;
190 desc = &props->devices[i];
191 break;
192 }
193 }
194
195 if (desc != NULL)
196 break;
197
198 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
Dan Carpentercd3172d2010-07-01 01:50:04 -0300199 deb_info("check for warm %x %x\n", props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700200 if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
201 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
202 *cold = 0;
203 desc = &props->devices[i];
204 break;
205 }
206 }
207 }
208
209 if (desc != NULL && props->identify_state != NULL)
Dan Carpentercd3172d2010-07-01 01:50:04 -0300210 props->identify_state(udev, props, &desc, cold);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700211
212 return desc;
213}
214
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300215int dvb_usb_device_power_ctrl(struct dvb_usb_device *d, int onoff)
216{
Patrick Boettchera37ddce2006-09-20 06:06:11 -0300217 if (onoff)
218 d->powered++;
219 else
220 d->powered--;
221
Dan Carpentercd3172d2010-07-01 01:50:04 -0300222 if (d->powered == 0 || (onoff && d->powered == 1)) { /* when switching from 1 to 0 or from 0 to 1 */
Patrick Boettchera37ddce2006-09-20 06:06:11 -0300223 deb_info("power control: %d\n", onoff);
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300224 if (d->props.power_ctrl)
225 return d->props.power_ctrl(d, onoff);
226 }
227 return 0;
228}
229
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700230/*
231 * USB
232 */
Janne Grunau78e92002008-04-09 19:13:13 -0300233int dvb_usb_device_init(struct usb_interface *intf,
234 struct dvb_usb_device_properties *props,
235 struct module *owner, struct dvb_usb_device **du,
236 short *adapter_nums)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700237{
238 struct usb_device *udev = interface_to_usbdev(intf);
239 struct dvb_usb_device *d = NULL;
240 struct dvb_usb_device_description *desc = NULL;
241
Dan Carpentercd3172d2010-07-01 01:50:04 -0300242 int ret = -ENOMEM, cold = 0;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700243
Patrick Boettcher6ff5ef22006-01-09 15:25:33 -0200244 if (du != NULL)
245 *du = NULL;
246
Dan Carpentercd3172d2010-07-01 01:50:04 -0300247 if ((desc = dvb_usb_find_device(udev, props, &cold)) == NULL) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700248 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
249 return -ENODEV;
250 }
251
252 if (cold) {
Dan Carpentercd3172d2010-07-01 01:50:04 -0300253 info("found a '%s' in cold state, will try to load a firmware", desc->name);
254 ret = dvb_usb_download_firmware(udev, props);
Patrick Boettcher5bc63602006-09-19 12:51:46 -0300255 if (!props->no_reconnect || ret != 0)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700256 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700257 }
258
Dan Carpentercd3172d2010-07-01 01:50:04 -0300259 info("found a '%s' in warm state.", desc->name);
260 d = kzalloc(sizeof(struct dvb_usb_device), GFP_KERNEL);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200261 if (d == NULL) {
262 err("no memory for 'struct dvb_usb_device'");
Roel Kluin407df292010-02-02 11:29:46 -0300263 return -ENOMEM;
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200264 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200265
266 d->udev = udev;
Dan Carpentercd3172d2010-07-01 01:50:04 -0300267 memcpy(&d->props, props, sizeof(struct dvb_usb_device_properties));
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200268 d->desc = desc;
269 d->owner = owner;
270
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200271 usb_set_intfdata(intf, d);
272
273 if (du != NULL)
274 *du = d;
275
Janne Grunau78e92002008-04-09 19:13:13 -0300276 ret = dvb_usb_init(d, adapter_nums);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200277
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700278 if (ret == 0)
Dan Carpentercd3172d2010-07-01 01:50:04 -0300279 info("%s successfully initialized and connected.", desc->name);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700280 else
Dan Carpentercd3172d2010-07-01 01:50:04 -0300281 info("%s error while loading driver (%d)", desc->name, ret);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700282 return ret;
283}
284EXPORT_SYMBOL(dvb_usb_device_init);
285
286void dvb_usb_device_exit(struct usb_interface *intf)
287{
288 struct dvb_usb_device *d = usb_get_intfdata(intf);
289 const char *name = "generic DVB-USB module";
290
Dan Carpentercd3172d2010-07-01 01:50:04 -0300291 usb_set_intfdata(intf, NULL);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700292 if (d != NULL && d->desc != NULL) {
293 name = d->desc->name;
294 dvb_usb_exit(d);
295 }
Dan Carpentercd3172d2010-07-01 01:50:04 -0300296 info("%s successfully deinitialized and disconnected.", name);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700297
298}
299EXPORT_SYMBOL(dvb_usb_device_exit);
300
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300301MODULE_VERSION("1.0");
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700302MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
303MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
304MODULE_LICENSE("GPL");