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