blob: a1705ecb9a544ac8c99872daa9f658a0f4051815 [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 *
6 * Copyright (C) 2004-5 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 */
17int dvb_usb_debug;
18module_param_named(debug,dvb_usb_debug, int, 0644);
19MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,pll=4,ts=8,err=16,rc=32,fw=64 (or-able))." DVB_USB_DEBUG_STATUS);
20
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
Johannes Stezenbach776338e2005-06-23 22:02:35 -070025/* general initialization functions */
Adrian Bunk15ac8e62005-12-01 00:51:53 -080026static int dvb_usb_exit(struct dvb_usb_device *d)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070027{
28 deb_info("state before exiting everything: %x\n",d->state);
29 dvb_usb_remote_exit(d);
30 dvb_usb_fe_exit(d);
31 dvb_usb_i2c_exit(d);
32 dvb_usb_dvb_exit(d);
33 dvb_usb_urb_exit(d);
34 deb_info("state should be zero now: %x\n",d->state);
35 d->state = DVB_USB_STATE_INIT;
36 kfree(d->priv);
37 kfree(d);
38 return 0;
39}
40
41static int dvb_usb_init(struct dvb_usb_device *d)
42{
43 int ret = 0;
44
Ingo Molnar3593cab2006-02-07 06:49:14 -020045 mutex_init(&d->usb_mutex);
46 mutex_init(&d->i2c_mutex);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070047
48 d->state = DVB_USB_STATE_INIT;
49
Alexey Dobriyanbe787ac2006-03-07 22:20:23 -030050/* check the capabilities and set appropriate variables */
Johannes Stezenbach776338e2005-06-23 22:02:35 -070051
52/* speed - when running at FULL speed we need a HW PID filter */
53 if (d->udev->speed == USB_SPEED_FULL && !(d->props.caps & DVB_USB_HAS_PID_FILTER)) {
Patrick Boettcher8df3d462005-07-12 13:58:38 -070054 err("This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)");
Johannes Stezenbach776338e2005-06-23 22:02:35 -070055 return -ENODEV;
56 }
57
58 if ((d->udev->speed == USB_SPEED_FULL && d->props.caps & DVB_USB_HAS_PID_FILTER) ||
59 (d->props.caps & DVB_USB_NEED_PID_FILTERING)) {
Patrick Boettcher8df3d462005-07-12 13:58:38 -070060 info("will use the device's hardware PID filter (table count: %d).",d->props.pid_filter_count);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070061 d->pid_filtering = 1;
62 d->max_feed_count = d->props.pid_filter_count;
63 } else {
Patrick Boettcher8df3d462005-07-12 13:58:38 -070064 info("will pass the complete MPEG2 transport stream to the software demuxer.");
Johannes Stezenbach776338e2005-06-23 22:02:35 -070065 d->pid_filtering = 0;
66 d->max_feed_count = 255;
67 }
68
69 if (d->props.power_ctrl)
70 d->props.power_ctrl(d,1);
71
72 if ((ret = dvb_usb_urb_init(d)) ||
73 (ret = dvb_usb_dvb_init(d)) ||
74 (ret = dvb_usb_i2c_init(d)) ||
75 (ret = dvb_usb_fe_init(d))) {
76 dvb_usb_exit(d);
77 return ret;
78 }
79
80 if ((ret = dvb_usb_remote_init(d)))
81 err("could not initialize remote control.");
82
83 if (d->props.power_ctrl)
84 d->props.power_ctrl(d,0);
85
86 return 0;
87}
88
89/* determine the name and the state of the just found USB device */
90static struct dvb_usb_device_description * dvb_usb_find_device(struct usb_device *udev,struct dvb_usb_properties *props, int *cold)
91{
92 int i,j;
93 struct dvb_usb_device_description *desc = NULL;
94 *cold = -1;
95
96 for (i = 0; i < props->num_device_descs; i++) {
97
98 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].cold_ids[j] != NULL; j++) {
99 deb_info("check for cold %x %x\n",props->devices[i].cold_ids[j]->idVendor, props->devices[i].cold_ids[j]->idProduct);
100 if (props->devices[i].cold_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
101 props->devices[i].cold_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
102 *cold = 1;
103 desc = &props->devices[i];
104 break;
105 }
106 }
107
108 if (desc != NULL)
109 break;
110
111 for (j = 0; j < DVB_USB_ID_MAX_NUM && props->devices[i].warm_ids[j] != NULL; j++) {
112 deb_info("check for warm %x %x\n",props->devices[i].warm_ids[j]->idVendor, props->devices[i].warm_ids[j]->idProduct);
113 if (props->devices[i].warm_ids[j]->idVendor == le16_to_cpu(udev->descriptor.idVendor) &&
114 props->devices[i].warm_ids[j]->idProduct == le16_to_cpu(udev->descriptor.idProduct)) {
115 *cold = 0;
116 desc = &props->devices[i];
117 break;
118 }
119 }
120 }
121
122 if (desc != NULL && props->identify_state != NULL)
123 props->identify_state(udev,props,&desc,cold);
124
125 return desc;
126}
127
128/*
129 * USB
130 */
Patrick Boettcher47dc3d62005-09-09 13:02:47 -0700131
132int dvb_usb_device_init(struct usb_interface *intf, struct dvb_usb_properties
133 *props, struct module *owner,struct dvb_usb_device **du)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700134{
135 struct usb_device *udev = interface_to_usbdev(intf);
136 struct dvb_usb_device *d = NULL;
137 struct dvb_usb_device_description *desc = NULL;
138
139 int ret = -ENOMEM,cold=0;
140
Patrick Boettcher6ff5ef22006-01-09 15:25:33 -0200141 if (du != NULL)
142 *du = NULL;
143
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700144 if ((desc = dvb_usb_find_device(udev,props,&cold)) == NULL) {
145 deb_err("something went very wrong, device was not found in current device list - let's see what comes next.\n");
146 return -ENODEV;
147 }
148
149 if (cold) {
150 info("found a '%s' in cold state, will try to load a firmware",desc->name);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200151 ret = dvb_usb_download_firmware(udev,props);
152 if (!props->no_reconnect)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700153 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700154 }
155
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200156 info("found a '%s' in warm state.",desc->name);
Panagiotis Issaris74081872006-01-11 19:40:56 -0200157 d = kzalloc(sizeof(struct dvb_usb_device),GFP_KERNEL);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200158 if (d == NULL) {
159 err("no memory for 'struct dvb_usb_device'");
160 return ret;
161 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200162
163 d->udev = udev;
164 memcpy(&d->props,props,sizeof(struct dvb_usb_properties));
165 d->desc = desc;
166 d->owner = owner;
167
168 if (d->props.size_of_priv > 0) {
Panagiotis Issaris74081872006-01-11 19:40:56 -0200169 d->priv = kzalloc(d->props.size_of_priv,GFP_KERNEL);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200170 if (d->priv == NULL) {
171 err("no memory for priv in 'struct dvb_usb_device'");
172 kfree(d);
173 return -ENOMEM;
174 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200175 }
176
177 usb_set_intfdata(intf, d);
178
179 if (du != NULL)
180 *du = d;
181
182 ret = dvb_usb_init(d);
183
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700184 if (ret == 0)
185 info("%s successfully initialized and connected.",desc->name);
186 else
187 info("%s error while loading driver (%d)",desc->name,ret);
188 return ret;
189}
190EXPORT_SYMBOL(dvb_usb_device_init);
191
192void dvb_usb_device_exit(struct usb_interface *intf)
193{
194 struct dvb_usb_device *d = usb_get_intfdata(intf);
195 const char *name = "generic DVB-USB module";
196
197 usb_set_intfdata(intf,NULL);
198 if (d != NULL && d->desc != NULL) {
199 name = d->desc->name;
200 dvb_usb_exit(d);
201 }
202 info("%s successfully deinitialized and disconnected.",name);
203
204}
205EXPORT_SYMBOL(dvb_usb_device_exit);
206
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700207MODULE_VERSION("0.3");
208MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
209MODULE_DESCRIPTION("A library module containing commonly used USB and DVB function USB DVB devices");
210MODULE_LICENSE("GPL");