1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/errno.h>
40#include <linux/pci.h>
41#include <linux/string.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(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
65static int debug;
66static char *bridge;
67static u8 bridge_busnr;
68static u8 bridge_slot;
69static struct pci_bus *bus;
70static u8 first_slot;
71static u8 last_slot;
72static u16 port;
73static unsigned int enum_bit;
74static u8 enum_mask;
75
76static struct cpci_hp_controller_ops generic_hpc_ops;
77static struct cpci_hp_controller generic_hpc;
78
79static int __init validate_parameters(void)
80{
81 char* str;
82 char* p;
83 unsigned long tmp;
84
85 if(!bridge) {
86 info("not configured, disabling.");
87 return -EINVAL;
88 }
89 str = bridge;
90 if(!*str)
91 return -EINVAL;
92
93 tmp = simple_strtoul(str, &p, 16);
94 if(p == str || tmp > 0xff) {
95 err("Invalid hotplug bus bridge device bus number");
96 return -EINVAL;
97 }
98 bridge_busnr = (u8) tmp;
99 dbg("bridge_busnr = 0x%02x", bridge_busnr);
100 if(*p != ':') {
101 err("Invalid hotplug bus bridge device");
102 return -EINVAL;
103 }
104 str = p + 1;
105 tmp = simple_strtoul(str, &p, 16);
106 if(p == str || tmp > 0x1f) {
107 err("Invalid hotplug bus bridge device slot number");
108 return -EINVAL;
109 }
110 bridge_slot = (u8) tmp;
111 dbg("bridge_slot = 0x%02x", bridge_slot);
112
113 dbg("first_slot = 0x%02x", first_slot);
114 dbg("last_slot = 0x%02x", last_slot);
115 if(!(first_slot && last_slot)) {
116 err("Need to specify first_slot and last_slot");
117 return -EINVAL;
118 }
119 if(last_slot < first_slot) {
120 err("first_slot must be less than last_slot");
121 return -EINVAL;
122 }
123
124 dbg("port = 0x%04x", port);
125 dbg("enum_bit = 0x%02x", enum_bit);
126 if(enum_bit > 7) {
127 err("Invalid #ENUM bit");
128 return -EINVAL;
129 }
130 enum_mask = 1 << enum_bit;
131 return 0;
132}
133
134static int query_enum(void)
135{
136 u8 value;
137
138 value = inb_p(port);
139 return ((value & enum_mask) == enum_mask);
140}
141
142static int __init cpcihp_generic_init(void)
143{
144 int status;
145 struct resource* r;
146 struct pci_dev* dev;
147
148 info(DRIVER_DESC " version: " DRIVER_VERSION);
149 status = validate_parameters();
150 if (status)
151 return status;
152
153 r = request_region(port, 1, "#ENUM hotswap signal register");
154 if(!r)
155 return -EBUSY;
156
157 bus = pci_find_bus(0, bridge_busnr);
158 if (!bus) {
159 err("Invalid bus number %d", bridge_busnr);
160 return -EINVAL;
161 }
162 dev = pci_get_slot(bus, PCI_DEVFN(bridge_slot, 0));
163 if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
164 err("Invalid bridge device %s", bridge);
165 pci_dev_put(dev);
166 return -EINVAL;
167 }
168 bus = dev->subordinate;
169 pci_dev_put(dev);
170
171 memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller));
172 generic_hpc_ops.query_enum = query_enum;
173 generic_hpc.ops = &generic_hpc_ops;
174
175 status = cpci_hp_register_controller(&generic_hpc);
176 if(status != 0) {
177 err("Could not register cPCI hotplug controller");
178 return -ENODEV;
179 }
180 dbg("registered controller");
181
182 status = cpci_hp_register_bus(bus, first_slot, last_slot);
183 if(status != 0) {
184 err("Could not register cPCI hotplug bus");
185 goto init_bus_register_error;
186 }
187 dbg("registered bus");
188
189 status = cpci_hp_start();
190 if(status != 0) {
191 err("Could not started cPCI hotplug system");
192 goto init_start_error;
193 }
194 dbg("started cpci hp system");
195 return 0;
196init_start_error:
197 cpci_hp_unregister_bus(bus);
198init_bus_register_error:
199 cpci_hp_unregister_controller(&generic_hpc);
200 err("status = %d", status);
201 return status;
202
203}
204
205static void __exit cpcihp_generic_exit(void)
206{
207 cpci_hp_stop();
208 cpci_hp_unregister_bus(bus);
209 cpci_hp_unregister_controller(&generic_hpc);
210 release_region(port, 1);
211}
212
213module_init(cpcihp_generic_init);
214module_exit(cpcihp_generic_exit);
215
216MODULE_AUTHOR(DRIVER_AUTHOR);
217MODULE_DESCRIPTION(DRIVER_DESC);
218MODULE_LICENSE("GPL");
219module_param(debug, bool, S_IRUGO | S_IWUSR);
220MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
221module_param(bridge, charp, 0);
222MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
223module_param(first_slot, byte, 0);
224MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
225module_param(last_slot, byte, 0);
226MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
227module_param(port, ushort, 0);
228MODULE_PARM_DESC(port, "#ENUM signal I/O port");
229module_param(enum_bit, uint, 0);
230MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");
231