ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / pci / hotplug / cpcihp_generic.c
1 /*
2  * cpcihp_generic.c
3  *
4  * Generic port I/O CompactPCI driver
5  *
6  * Copyright 2002 SOMA Networks, Inc.
7  * Copyright 2001 Intel San Luis Obispo
8  * Copyright 2000,2001 MontaVista Software Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2 of the License, or (at your
13  * option) any later version.
14  *
15  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
17  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
18  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, write to the Free Software Foundation, Inc.,
28  * 675 Mass Ave, Cambridge, MA 02139, USA.
29  *
30  * This generic CompactPCI hotplug driver should allow using the PCI hotplug
31  * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit
32  * in a system register that can be read through standard port I/O.
33  *
34  * Send feedback to <scottm@somanetworks.com>
35  */
36
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/errno.h>
41 #include <linux/pci.h>
42 #include "cpci_hotplug.h"
43
44 #define DRIVER_VERSION  "0.1"
45 #define DRIVER_AUTHOR   "Scott Murray <scottm@somanetworks.com>"
46 #define DRIVER_DESC     "Generic port I/O CompactPCI Hot Plug Driver"
47
48 #if !defined(CONFIG_HOTPLUG_CPCI_GENERIC_MODULE)
49 #define MY_NAME "cpcihp_generic"
50 #else
51 #define MY_NAME THIS_MODULE->name
52 #endif
53
54 #define dbg(format, arg...)                                     \
55         do {                                                    \
56                 if(debug)                                       \
57                         printk (KERN_DEBUG "%s: " format "\n",  \
58                                 MY_NAME , ## arg);              \
59         } while(0)
60 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
61 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
62 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
63
64 /* local variables */
65 static int debug;
66 static char* bridge;
67 static u8 bridge_busnr;
68 static u8 bridge_slot;
69 static struct pci_bus *bus;
70 static u8 first_slot;
71 static u8 last_slot;
72 static u16 port;
73 static unsigned int enum_bit;
74 static u8 enum_mask;
75
76 static struct cpci_hp_controller_ops generic_hpc_ops;
77 static struct cpci_hp_controller generic_hpc;
78
79 /* The following allows configuring the driver when it's compiled into the kernel */
80 #ifndef MODULE
81 static int __init cpcihp_generic_setup(char* str)
82 {
83         char* p;
84         unsigned long tmp;
85
86         if(!str)
87                 return -EINVAL;
88         bridge = str;
89
90         p = strchr(str, ',');
91         str = p + 1;
92         if(!(p && *str && *p == ','))
93                 goto setup_error;
94         tmp = simple_strtoul(str, &p, 0);
95         if(p == str || tmp > 0xff) {
96                 err("hotplug bus first slot number out of range");
97                 goto setup_error;
98         }
99         first_slot = (u8) tmp;
100
101         str = p + 1;            
102         if(!(*str && *p == ','))
103                 return -EINVAL;
104         tmp = simple_strtoul(str, &p, 0);
105         if(p == str || tmp > 0xff) {
106                 err("hotplug bus last slot number out of range");
107                 goto setup_error;
108         }
109         last_slot = (u8) tmp;
110
111         str = p + 1;
112         if(!(*str && *p == ','))
113                 goto setup_error;
114         tmp = simple_strtoul(str, &p, 0);
115         if(p == str || tmp > 0xffff) {
116                 err("port number out of range");
117                 goto setup_error;
118         }
119         port = (u16) tmp;
120
121         str = p + 1;
122         if(!(*str && *p == ','))
123                 goto setup_error;
124         tmp = simple_strtoul(str, &p, 0);
125         if(p == str) {
126                 err("invalid #ENUM bit number");
127                 goto setup_error;
128         }
129         enum_bit = (u8) tmp;
130
131         str = p + 1;
132         if(*str && *p == ',') {
133                 tmp = simple_strtoul(str, &p, 0);
134                 if(p != str)
135                         debug = (int) tmp;
136         }
137         return 0;
138 setup_error:
139         bridge = NULL;
140         return -EINVAL;
141 }
142
143 __setup("cpcihp_generic=", cpcihp_generic_setup);
144 #endif
145
146 static int __init validate_parameters(void)
147 {
148         char* str;
149         char* p;
150         unsigned long tmp;
151
152         if(!bridge) {
153                 info("not configured, disabling.");
154                 return 1;
155         }
156         str = bridge;
157         if(!*str)
158                 return -EINVAL;
159
160         tmp = simple_strtoul(str, &p, 16);
161         if(p == str || tmp > 0xff) {
162                 err("Invalid hotplug bus bridge device bus number");
163                 return -EINVAL;
164         }
165         bridge_busnr = (u8) tmp;
166         dbg("bridge_busnr = 0x%02x", bridge_busnr);
167         if(*p != ':') {
168                 err("Invalid hotplug bus bridge device");
169                 return -EINVAL;
170         }
171         str = p + 1;
172         tmp = simple_strtoul(str, &p, 16);
173         if(p == str || tmp > 0x1f) {
174                 err("Invalid hotplug bus bridge device slot number");
175                 return -EINVAL;
176         }
177         bridge_slot = (u8) tmp;
178         dbg("bridge_slot = 0x%02x", bridge_slot);
179
180         dbg("first_slot = 0x%02x", first_slot);
181         dbg("last_slot = 0x%02x", last_slot);
182         if(!(first_slot && last_slot)) {
183                 err("Need to specify first_slot and last_slot");
184                 return -EINVAL;
185         }
186         if(last_slot < first_slot) {
187                 err("first_slot must be less than last_slot");
188                 return -EINVAL;
189         }
190
191         dbg("port = 0x%04x", port);
192         dbg("enum_bit = 0x%02x", enum_bit);
193         if(enum_bit > 7) {
194                 err("Invalid #ENUM bit");
195                 return -EINVAL;
196         }
197         enum_mask = 1 << enum_bit;
198         return 0;
199 }
200
201 static int query_enum(void)
202 {
203         u8 value;
204
205         value = inb_p(port);
206         return ((value & enum_mask) == enum_mask);
207 }
208
209 static int __init cpcihp_generic_init(void)
210 {
211         int status;
212         struct resource* r;
213         struct pci_dev* dev;
214
215         info(DRIVER_DESC " version: " DRIVER_VERSION);
216         status = validate_parameters();
217         if(status != 0)
218                 return status;
219
220         r = request_region(port, 1, "#ENUM hotswap signal register");
221         if(!r)
222                 return -EBUSY;
223
224         dev = pci_find_slot(bridge_busnr, PCI_DEVFN(bridge_slot, 0));
225         if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
226                 err("Invalid bridge device %s", bridge);
227                 return -EINVAL;
228         }
229         bus = dev->subordinate;
230
231         memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller));
232         generic_hpc_ops.query_enum = query_enum;
233         generic_hpc.ops = &generic_hpc_ops;
234
235         status = cpci_hp_register_controller(&generic_hpc);
236         if(status != 0) {
237                 err("Could not register cPCI hotplug controller");
238                 return -ENODEV;
239         }
240         dbg("registered controller");
241
242         status = cpci_hp_register_bus(bus, first_slot, last_slot);
243         if(status != 0) {
244                 err("Could not register cPCI hotplug bus");
245                 goto init_bus_register_error;
246         }
247         dbg("registered bus");
248
249         status = cpci_hp_start();
250         if(status != 0) {
251                 err("Could not started cPCI hotplug system");
252                 goto init_start_error;
253         }
254         dbg("started cpci hp system");
255         return 0;
256 init_start_error:
257         cpci_hp_unregister_bus(bus);
258 init_bus_register_error:
259         cpci_hp_unregister_controller(&generic_hpc);
260         err("status = %d", status);
261         return status;
262
263 }
264
265 static void __exit cpcihp_generic_exit(void)
266 {
267         cpci_hp_stop();
268         cpci_hp_unregister_bus(bus);
269         cpci_hp_unregister_controller(&generic_hpc);
270         release_region(port, 1);
271 }
272
273 module_init(cpcihp_generic_init);
274 module_exit(cpcihp_generic_exit);
275
276 MODULE_AUTHOR(DRIVER_AUTHOR);
277 MODULE_DESCRIPTION(DRIVER_DESC);
278 MODULE_LICENSE("GPL");
279 MODULE_PARM(debug, "i");
280 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
281 MODULE_PARM(bridge, "s");
282 MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
283 MODULE_PARM(first_slot, "b");
284 MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
285 MODULE_PARM(last_slot, "b");
286 MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
287 MODULE_PARM(port, "h");
288 MODULE_PARM_DESC(port, "#ENUM signal I/O port");
289 MODULE_PARM(enum_bit, "i");
290 MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");