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 | |
| 45 | /** |
| 46 | * pnp_protocol_register - adds a pnp protocol to the pnp layer |
| 47 | * @protocol: pointer to the corresponding pnp_protocol structure |
| 48 | * |
| 49 | * Ex protocols: ISAPNP, PNPBIOS, etc |
| 50 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int pnp_register_protocol(struct pnp_protocol *protocol) |
| 52 | { |
| 53 | int nodenum; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 54 | struct list_head *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | INIT_LIST_HEAD(&protocol->devices); |
| 57 | INIT_LIST_HEAD(&protocol->cards); |
| 58 | nodenum = 0; |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 59 | mutex_lock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* assign the lowest unused number */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 62 | list_for_each(pos, &pnp_protocols) { |
| 63 | struct pnp_protocol *cur = to_pnp_protocol(pos); |
| 64 | if (cur->number == nodenum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | pos = &pnp_protocols; |
| 66 | nodenum++; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | list_add_tail(&protocol->protocol_list, &pnp_protocols); |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 71 | mutex_unlock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | protocol->number = nodenum; |
Kay Sievers | c85e37c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 74 | dev_set_name(&protocol->dev, "pnp%d", nodenum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return device_register(&protocol->dev); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * pnp_protocol_unregister - removes a pnp protocol from the pnp layer |
| 80 | * @protocol: pointer to the corresponding pnp_protocol structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | void pnp_unregister_protocol(struct pnp_protocol *protocol) |
| 83 | { |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 84 | mutex_lock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | list_del(&protocol->protocol_list); |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 86 | mutex_unlock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | device_unregister(&protocol->dev); |
| 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static void pnp_free_ids(struct pnp_dev *dev) |
| 91 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 92 | struct pnp_id *id; |
| 93 | struct pnp_id *next; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | id = dev->id; |
| 96 | while (id) { |
| 97 | next = id->next; |
| 98 | kfree(id); |
| 99 | id = next; |
| 100 | } |
| 101 | } |
| 102 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 103 | void pnp_free_resource(struct pnp_resource *pnp_res) |
| 104 | { |
| 105 | list_del(&pnp_res->list); |
| 106 | kfree(pnp_res); |
| 107 | } |
| 108 | |
| 109 | void pnp_free_resources(struct pnp_dev *dev) |
| 110 | { |
| 111 | struct pnp_resource *pnp_res, *tmp; |
| 112 | |
| 113 | list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) { |
| 114 | pnp_free_resource(pnp_res); |
| 115 | } |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | static void pnp_release_device(struct device *dmdev) |
| 119 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 120 | struct pnp_dev *dev = to_pnp_dev(dmdev); |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | pnp_free_ids(dev); |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 123 | pnp_free_resources(dev); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 124 | pnp_free_options(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | kfree(dev); |
| 126 | } |
| 127 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 128 | struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id, |
| 129 | const char *pnpid) |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 130 | { |
| 131 | struct pnp_dev *dev; |
| 132 | struct pnp_id *dev_id; |
| 133 | |
| 134 | dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL); |
| 135 | if (!dev) |
| 136 | return NULL; |
| 137 | |
Bjorn Helgaas | aee3ad8 | 2008-06-27 16:56:57 -0600 | [diff] [blame] | 138 | INIT_LIST_HEAD(&dev->resources); |
Bjorn Helgaas | 1f32ca3 | 2008-06-27 16:57:17 -0600 | [diff] [blame] | 139 | INIT_LIST_HEAD(&dev->options); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 140 | dev->protocol = protocol; |
| 141 | dev->number = id; |
Yang Hongyang | 2f4f27d | 2009-04-06 19:01:18 -0700 | [diff] [blame] | 142 | dev->dma_mask = DMA_BIT_MASK(24); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 143 | |
| 144 | dev->dev.parent = &dev->protocol->dev; |
| 145 | dev->dev.bus = &pnp_bus_type; |
| 146 | dev->dev.dma_mask = &dev->dma_mask; |
| 147 | dev->dev.coherent_dma_mask = dev->dma_mask; |
| 148 | dev->dev.release = &pnp_release_device; |
| 149 | |
Kay Sievers | c85e37c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 150 | dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number); |
Bjorn Helgaas | bda1e4e | 2008-04-28 16:33:54 -0600 | [diff] [blame] | 151 | |
| 152 | dev_id = pnp_add_id(dev, pnpid); |
| 153 | if (!dev_id) { |
| 154 | kfree(dev); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | return dev; |
| 159 | } |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | int __pnp_add_device(struct pnp_dev *dev) |
| 162 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | pnp_fixup_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | dev->status = PNP_READY; |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 165 | mutex_lock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | list_add_tail(&dev->global_list, &pnp_global); |
| 167 | list_add_tail(&dev->protocol_list, &dev->protocol->devices); |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 168 | mutex_unlock(&pnp_lock); |
Alan Stern | b14e033 | 2010-06-29 22:49:24 +0200 | [diff] [blame] | 169 | if (dev->protocol->can_wakeup) |
| 170 | device_set_wakeup_capable(&dev->dev, |
| 171 | dev->protocol->can_wakeup(dev)); |
Drew Moseley | 8a89efd | 2008-09-28 01:31:35 +0200 | [diff] [blame] | 172 | return device_register(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * pnp_add_device - adds a pnp device to the pnp layer |
| 177 | * @dev: pointer to dev to add |
| 178 | * |
| 179 | * adds to driver model, name database, fixups, interface, etc. |
| 180 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int pnp_add_device(struct pnp_dev *dev) |
| 182 | { |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 183 | int ret; |
Bjorn Helgaas | 2663f60 | 2008-08-19 16:53:36 -0600 | [diff] [blame] | 184 | char buf[128]; |
| 185 | int len = 0; |
| 186 | struct pnp_id *id; |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 187 | |
Bjorn Helgaas | b173491 | 2007-08-15 10:32:13 -0600 | [diff] [blame] | 188 | if (dev->card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return -EINVAL; |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 190 | |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 191 | ret = __pnp_add_device(dev); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
Bjorn Helgaas | 2663f60 | 2008-08-19 16:53:36 -0600 | [diff] [blame] | 195 | buf[0] = '\0'; |
| 196 | for (id = dev->id; id; id = id->next) |
| 197 | len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id); |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 198 | |
Bjorn Helgaas | c1f3f28 | 2010-09-29 12:24:23 -0600 | [diff] [blame] | 199 | dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n", |
| 200 | dev->protocol->name, buf, |
| 201 | dev->active ? "active" : "disabled"); |
Bjorn Helgaas | 348366b | 2007-10-16 23:31:12 -0700 | [diff] [blame] | 202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void __pnp_remove_device(struct pnp_dev *dev) |
| 206 | { |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 207 | mutex_lock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | list_del(&dev->global_list); |
| 209 | list_del(&dev->protocol_list); |
Rafael J. Wysocki | 38f6b38 | 2015-03-18 22:39:55 +0100 | [diff] [blame^] | 210 | mutex_unlock(&pnp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | device_unregister(&dev->dev); |
| 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | static int __init pnp_init(void) |
| 215 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return bus_register(&pnp_bus_type); |
| 217 | } |
| 218 | |
| 219 | subsys_initcall(pnp_init); |
Bjorn Helgaas | 97ef062 | 2008-08-19 16:53:41 -0600 | [diff] [blame] | 220 | |
| 221 | int pnp_debug; |
| 222 | |
| 223 | #if defined(CONFIG_PNP_DEBUG_MESSAGES) |
Thomas Renninger | cdefba0 | 2010-10-25 21:11:16 +0200 | [diff] [blame] | 224 | module_param_named(debug, pnp_debug, int, 0644); |
Bjorn Helgaas | 97ef062 | 2008-08-19 16:53:41 -0600 | [diff] [blame] | 225 | #endif |