0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/module.h>
0019 #include <linux/init.h>
0020 #include <linux/errno.h>
0021 #include <linux/pci.h>
0022 #include <linux/string.h>
0023 #include "cpci_hotplug.h"
0024
0025 #define DRIVER_VERSION "0.1"
0026 #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
0027 #define DRIVER_DESC "Generic port I/O CompactPCI Hot Plug Driver"
0028
0029 #if !defined(MODULE)
0030 #define MY_NAME "cpcihp_generic"
0031 #else
0032 #define MY_NAME THIS_MODULE->name
0033 #endif
0034
0035 #define dbg(format, arg...) \
0036 do { \
0037 if (debug) \
0038 printk(KERN_DEBUG "%s: " format "\n", \
0039 MY_NAME, ## arg); \
0040 } while (0)
0041 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
0042 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
0043 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
0044
0045
0046 static bool debug;
0047 static char *bridge;
0048 static u8 bridge_busnr;
0049 static u8 bridge_slot;
0050 static struct pci_bus *bus;
0051 static u8 first_slot;
0052 static u8 last_slot;
0053 static u16 port;
0054 static unsigned int enum_bit;
0055 static u8 enum_mask;
0056
0057 static struct cpci_hp_controller_ops generic_hpc_ops;
0058 static struct cpci_hp_controller generic_hpc;
0059
0060 static int __init validate_parameters(void)
0061 {
0062 char *str;
0063 char *p;
0064 unsigned long tmp;
0065
0066 if (!bridge) {
0067 info("not configured, disabling.");
0068 return -EINVAL;
0069 }
0070 str = bridge;
0071 if (!*str)
0072 return -EINVAL;
0073
0074 tmp = simple_strtoul(str, &p, 16);
0075 if (p == str || tmp > 0xff) {
0076 err("Invalid hotplug bus bridge device bus number");
0077 return -EINVAL;
0078 }
0079 bridge_busnr = (u8) tmp;
0080 dbg("bridge_busnr = 0x%02x", bridge_busnr);
0081 if (*p != ':') {
0082 err("Invalid hotplug bus bridge device");
0083 return -EINVAL;
0084 }
0085 str = p + 1;
0086 tmp = simple_strtoul(str, &p, 16);
0087 if (p == str || tmp > 0x1f) {
0088 err("Invalid hotplug bus bridge device slot number");
0089 return -EINVAL;
0090 }
0091 bridge_slot = (u8) tmp;
0092 dbg("bridge_slot = 0x%02x", bridge_slot);
0093
0094 dbg("first_slot = 0x%02x", first_slot);
0095 dbg("last_slot = 0x%02x", last_slot);
0096 if (!(first_slot && last_slot)) {
0097 err("Need to specify first_slot and last_slot");
0098 return -EINVAL;
0099 }
0100 if (last_slot < first_slot) {
0101 err("first_slot must be less than last_slot");
0102 return -EINVAL;
0103 }
0104
0105 dbg("port = 0x%04x", port);
0106 dbg("enum_bit = 0x%02x", enum_bit);
0107 if (enum_bit > 7) {
0108 err("Invalid #ENUM bit");
0109 return -EINVAL;
0110 }
0111 enum_mask = 1 << enum_bit;
0112 return 0;
0113 }
0114
0115 static int query_enum(void)
0116 {
0117 u8 value;
0118
0119 value = inb_p(port);
0120 return ((value & enum_mask) == enum_mask);
0121 }
0122
0123 static int __init cpcihp_generic_init(void)
0124 {
0125 int status;
0126 struct resource *r;
0127 struct pci_dev *dev;
0128
0129 info(DRIVER_DESC " version: " DRIVER_VERSION);
0130 status = validate_parameters();
0131 if (status)
0132 return status;
0133
0134 r = request_region(port, 1, "#ENUM hotswap signal register");
0135 if (!r)
0136 return -EBUSY;
0137
0138 dev = pci_get_domain_bus_and_slot(0, bridge_busnr,
0139 PCI_DEVFN(bridge_slot, 0));
0140 if (!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
0141 err("Invalid bridge device %s", bridge);
0142 pci_dev_put(dev);
0143 return -EINVAL;
0144 }
0145 bus = dev->subordinate;
0146 pci_dev_put(dev);
0147
0148 memset(&generic_hpc, 0, sizeof(struct cpci_hp_controller));
0149 generic_hpc_ops.query_enum = query_enum;
0150 generic_hpc.ops = &generic_hpc_ops;
0151
0152 status = cpci_hp_register_controller(&generic_hpc);
0153 if (status != 0) {
0154 err("Could not register cPCI hotplug controller");
0155 return -ENODEV;
0156 }
0157 dbg("registered controller");
0158
0159 status = cpci_hp_register_bus(bus, first_slot, last_slot);
0160 if (status != 0) {
0161 err("Could not register cPCI hotplug bus");
0162 goto init_bus_register_error;
0163 }
0164 dbg("registered bus");
0165
0166 status = cpci_hp_start();
0167 if (status != 0) {
0168 err("Could not started cPCI hotplug system");
0169 goto init_start_error;
0170 }
0171 dbg("started cpci hp system");
0172 return 0;
0173 init_start_error:
0174 cpci_hp_unregister_bus(bus);
0175 init_bus_register_error:
0176 cpci_hp_unregister_controller(&generic_hpc);
0177 err("status = %d", status);
0178 return status;
0179
0180 }
0181
0182 static void __exit cpcihp_generic_exit(void)
0183 {
0184 cpci_hp_stop();
0185 cpci_hp_unregister_bus(bus);
0186 cpci_hp_unregister_controller(&generic_hpc);
0187 release_region(port, 1);
0188 }
0189
0190 module_init(cpcihp_generic_init);
0191 module_exit(cpcihp_generic_exit);
0192
0193 MODULE_AUTHOR(DRIVER_AUTHOR);
0194 MODULE_DESCRIPTION(DRIVER_DESC);
0195 MODULE_LICENSE("GPL");
0196 module_param(debug, bool, S_IRUGO | S_IWUSR);
0197 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
0198 module_param(bridge, charp, 0);
0199 MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
0200 module_param(first_slot, byte, 0);
0201 MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
0202 module_param(last_slot, byte, 0);
0203 MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
0204 module_param_hw(port, ushort, ioport, 0);
0205 MODULE_PARM_DESC(port, "#ENUM signal I/O port");
0206 module_param(enum_bit, uint, 0);
0207 MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");