Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * core.c - contains all core device and protocol registration functions |
| 3 | * |
| 4 | * Copyright 2002 Adam Belay <ambx1@neo.rr.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/pnp.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/list.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/module.h> |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame] | 12 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/errno.h> |
David Brownell | 2e17c55 | 2007-05-08 00:25:29 -0700 | [diff] [blame] | 17 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "base.h" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | static LIST_HEAD(pnp_protocols); |
| 22 | LIST_HEAD(pnp_global); |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame] | 23 | DEFINE_MUTEX(pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Bjorn Helgaas | 8f81dd1 | 2007-05-08 00:35:54 -0700 | [diff] [blame] | 25 | /* |
| 26 | * ACPI or PNPBIOS should tell us about all platform devices, so we can |
| 27 | * skip some blind probes. ISAPNP typically enumerates only plug-in ISA |
| 28 | * devices, not built-in things like COM ports. |
| 29 | */ |
| 30 | int pnp_platform_devices; |
| 31 | EXPORT_SYMBOL(pnp_platform_devices); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | void *pnp_alloc(long size) |
| 34 | { |
| 35 | void *result; |
| 36 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 37 | result = kzalloc(size, GFP_KERNEL); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 38 | if (!result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | printk(KERN_ERR "pnp: Out of Memory\n"); |
| 40 | return NULL; |
| 41 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | return result; |
| 43 | } |
| 44 | |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 45 | static void pnp_remove_protocol(struct pnp_protocol *protocol) |
| 46 | { |
| 47 | mutex_lock(&pnp_lock); |
| 48 | list_del(&protocol->protocol_list); |
| 49 | mutex_unlock(&pnp_lock); |
| 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /** |
| 53 | * pnp_protocol_register - adds a pnp protocol to the pnp layer |
| 54 | * @protocol: pointer to the corresponding pnp_protocol structure |
| 55 | * |
| 56 | * Ex protocols: ISAPNP, PNPBIOS, etc |
| 57 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | int pnp_register_protocol(struct pnp_protocol *protocol) |
| 59 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 60 | struct list_head *pos; |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 61 | int nodenum, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | INIT_LIST_HEAD(&protocol->devices); |
| 64 | INIT_LIST_HEAD(&protocol->cards); |
| 65 | nodenum = 0; |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 66 | |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame] | 67 | mutex_lock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* assign the lowest unused number */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 70 | list_for_each(pos, &pnp_protocols) { |
| 71 | struct pnp_protocol *cur = to_pnp_protocol(pos); |
| 72 | if (cur->number == nodenum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | pos = &pnp_protocols; |
| 74 | nodenum++; |
| 75 | } |
| 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | protocol->number = nodenum; |
Kay Sievers | c85e37c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 79 | dev_set_name(&protocol->dev, "pnp%d", nodenum); |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 80 | |
| 81 | list_add_tail(&protocol->protocol_list, &pnp_protocols); |
| 82 | |
| 83 | mutex_unlock(&pnp_lock); |
| 84 | |
| 85 | ret = device_register(&protocol->dev); |
| 86 | if (ret) |
| 87 | pnp_remove_protocol(protocol); |
| 88 | |
| 89 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * pnp_protocol_unregister - removes a pnp protocol from the pnp layer |
| 94 | * @protocol: pointer to the corresponding pnp_protocol structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
| 96 | void pnp_unregister_protocol(struct pnp_protocol *protocol) |
| 97 | { |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 98 | pnp_remove_protocol(protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | device_unregister(&protocol->dev); |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | static void pnp_free_ids(struct pnp_dev *dev) |
| 103 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 104 | struct pnp_id *id; |
| 105 | struct pnp_id *next; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | id = dev->id; |
| 108 | while (id) { |
| 109 | next = id->next; |
| 110 | kfree(id); |
| 111 | id = next; |
| 112 | } |
| 113 | } |
| 114 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 115 | void pnp_free_resource(struct pnp_resource *pnp_res) |
| 116 | { |
| 117 | list_del(&pnp_res->list); |
| 118 | kfree(pnp_res); |
| 119 | } |
| 120 | |
| 121 | void pnp_free_resources(struct pnp_dev *dev) |
| 122 | { |
| 123 | struct pnp_resource *pnp_res, *tmp; |
| 124 | |
| 125 | list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) { |
| 126 | pnp_free_resource(pnp_res); |
| 127 | } |
| 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static void pnp_release_device(struct device *dmdev) |
| 131 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 132 | struct pnp_dev *dev = to_pnp_dev(dmdev); |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | pnp_free_ids(dev); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 135 | pnp_free_resources(dev); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 136 | pnp_free_options(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | kfree(dev); |
| 138 | } |
| 139 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 140 | struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id, |
| 141 | const char *pnpid) |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 142 | { |
| 143 | struct pnp_dev *dev; |
| 144 | struct pnp_id *dev_id; |
| 145 | |
| 146 | dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL); |
| 147 | if (!dev) |
| 148 | return NULL; |
| 149 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 150 | INIT_LIST_HEAD(&dev->resources); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 151 | INIT_LIST_HEAD(&dev->options); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 152 | dev->protocol = protocol; |
| 153 | dev->number = id; |
Yang Hongyang | 2f4f27d | 2009-04-06 19:01:18 -0700 | [diff] [blame] | 154 | dev->dma_mask = DMA_BIT_MASK(24); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 155 | |
| 156 | dev->dev.parent = &dev->protocol->dev; |
| 157 | dev->dev.bus = &pnp_bus_type; |
| 158 | dev->dev.dma_mask = &dev->dma_mask; |
| 159 | dev->dev.coherent_dma_mask = dev->dma_mask; |
| 160 | dev->dev.release = &pnp_release_device; |
| 161 | |
Kay Sievers | c85e37c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 162 | dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 163 | |
| 164 | dev_id = pnp_add_id(dev, pnpid); |
| 165 | if (!dev_id) { |
| 166 | kfree(dev); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | return dev; |
| 171 | } |
| 172 | |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 173 | static void pnp_delist_device(struct pnp_dev *dev) |
| 174 | { |
| 175 | mutex_lock(&pnp_lock); |
| 176 | list_del(&dev->global_list); |
| 177 | list_del(&dev->protocol_list); |
| 178 | mutex_unlock(&pnp_lock); |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int __pnp_add_device(struct pnp_dev *dev) |
| 182 | { |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 183 | int ret; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | pnp_fixup_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | dev->status = PNP_READY; |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 187 | |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame] | 188 | mutex_lock(&pnp_lock); |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | list_add_tail(&dev->global_list, &pnp_global); |
| 191 | list_add_tail(&dev->protocol_list, &dev->protocol->devices); |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 192 | |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame] | 193 | mutex_unlock(&pnp_lock); |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 194 | |
| 195 | ret = device_register(&dev->dev); |
| 196 | if (ret) |
| 197 | pnp_delist_device(dev); |
| 198 | else if (dev->protocol->can_wakeup) |
Alan Stern | b14e033 | 2010-06-29 22:49:24 +0200 | [diff] [blame] | 199 | device_set_wakeup_capable(&dev->dev, |
| 200 | dev->protocol->can_wakeup(dev)); |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 201 | |
| 202 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * pnp_add_device - adds a pnp device to the pnp layer |
| 207 | * @dev: pointer to dev to add |
| 208 | * |
| 209 | * adds to driver model, name database, fixups, interface, etc. |
| 210 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | int pnp_add_device(struct pnp_dev *dev) |
| 212 | { |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 213 | int ret; |
Bjorn Helgaas | 2663f60 | 2008-08-19 16:53:36 -0600 | [diff] [blame] | 214 | char buf[128]; |
| 215 | int len = 0; |
| 216 | struct pnp_id *id; |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 217 | |
Bjorn Helgaas | b173491 | 2007-08-15 10:32:13 -0600 | [diff] [blame] | 218 | if (dev->card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return -EINVAL; |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 220 | |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 221 | ret = __pnp_add_device(dev); |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | |
Bjorn Helgaas | 2663f60 | 2008-08-19 16:53:36 -0600 | [diff] [blame] | 225 | buf[0] = '\0'; |
| 226 | for (id = dev->id; id; id = id->next) |
| 227 | len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id); |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 228 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 229 | dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n", |
| 230 | dev->protocol->name, buf, |
| 231 | dev->active ? "active" : "disabled"); |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void __pnp_remove_device(struct pnp_dev *dev) |
| 236 | { |
Rafael J. Wysocki | 71150d2 | 2015-03-18 22:40:04 +0100 | [diff] [blame] | 237 | pnp_delist_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | device_unregister(&dev->dev); |
| 239 | } |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | static int __init pnp_init(void) |
| 242 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return bus_register(&pnp_bus_type); |
| 244 | } |
| 245 | |
| 246 | subsys_initcall(pnp_init); |
Bjorn Helgaas | 97ef062 | 2008-08-19 16:53:41 -0600 | [diff] [blame] | 247 | |
| 248 | int pnp_debug; |
| 249 | |
| 250 | #if defined(CONFIG_PNP_DEBUG_MESSAGES) |
Thomas Renninger | cdefba0 | 2010-10-25 21:11:16 +0200 | [diff] [blame] | 251 | module_param_named(debug, pnp_debug, int, 0644); |
Bjorn Helgaas | 97ef062 | 2008-08-19 16:53:41 -0600 | [diff] [blame] | 252 | #endif |