blob: b54620e53830da9745ef21e1c6352be972a7189d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * core.c - contains all core device and protocol registration functions
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
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. Wysocki38f6b382015-03-18 22:39:55 +010012#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/string.h>
15#include <linux/slab.h>
16#include <linux/errno.h>
David Brownell2e17c552007-05-08 00:25:29 -070017#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "base.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static LIST_HEAD(pnp_protocols);
22LIST_HEAD(pnp_global);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010023DEFINE_MUTEX(pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Bjorn Helgaas8f81dd12007-05-08 00:35:54 -070025/*
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 */
30int pnp_platform_devices;
31EXPORT_SYMBOL(pnp_platform_devices);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033void *pnp_alloc(long size)
34{
35 void *result;
36
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070037 result = kzalloc(size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070038 if (!result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 printk(KERN_ERR "pnp: Out of Memory\n");
40 return NULL;
41 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return result;
43}
44
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010045static 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 Torvalds1da177e2005-04-16 15:20:36 -070052/**
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 Torvalds1da177e2005-04-16 15:20:36 -070058int pnp_register_protocol(struct pnp_protocol *protocol)
59{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070060 struct list_head *pos;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010061 int nodenum, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 INIT_LIST_HEAD(&protocol->devices);
64 INIT_LIST_HEAD(&protocol->cards);
65 nodenum = 0;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010066
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010067 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* assign the lowest unused number */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070070 list_for_each(pos, &pnp_protocols) {
71 struct pnp_protocol *cur = to_pnp_protocol(pos);
72 if (cur->number == nodenum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 pos = &pnp_protocols;
74 nodenum++;
75 }
76 }
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 protocol->number = nodenum;
Kay Sieversc85e37c2009-01-06 10:44:38 -080079 dev_set_name(&protocol->dev, "pnp%d", nodenum);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010080
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 Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/**
93 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
94 * @protocol: pointer to the corresponding pnp_protocol structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96void pnp_unregister_protocol(struct pnp_protocol *protocol)
97{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010098 pnp_remove_protocol(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 device_unregister(&protocol->dev);
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void pnp_free_ids(struct pnp_dev *dev)
103{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700104 struct pnp_id *id;
105 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 id = dev->id;
108 while (id) {
109 next = id->next;
110 kfree(id);
111 id = next;
112 }
113}
114
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600115void pnp_free_resource(struct pnp_resource *pnp_res)
116{
117 list_del(&pnp_res->list);
118 kfree(pnp_res);
119}
120
121void 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 Torvalds1da177e2005-04-16 15:20:36 -0700130static void pnp_release_device(struct device *dmdev)
131{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700132 struct pnp_dev *dev = to_pnp_dev(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 pnp_free_ids(dev);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600135 pnp_free_resources(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600136 pnp_free_options(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 kfree(dev);
138}
139
Thomas Renninger620e1122010-10-01 10:54:00 +0200140struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
141 const char *pnpid)
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600142{
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 Helgaasaee3ad82008-06-27 16:56:57 -0600150 INIT_LIST_HEAD(&dev->resources);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600151 INIT_LIST_HEAD(&dev->options);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600152 dev->protocol = protocol;
153 dev->number = id;
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700154 dev->dma_mask = DMA_BIT_MASK(24);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600155
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 Sieversc85e37c2009-01-06 10:44:38 -0800162 dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600163
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. Wysocki71150d22015-03-18 22:40:04 +0100173static 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 Torvalds1da177e2005-04-16 15:20:36 -0700181int __pnp_add_device(struct pnp_dev *dev)
182{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100183 int ret;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 pnp_fixup_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dev->status = PNP_READY;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100187
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100188 mutex_lock(&pnp_lock);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 list_add_tail(&dev->global_list, &pnp_global);
191 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100192
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100193 mutex_unlock(&pnp_lock);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100194
195 ret = device_register(&dev->dev);
196 if (ret)
197 pnp_delist_device(dev);
198 else if (dev->protocol->can_wakeup)
Alan Sternb14e0332010-06-29 22:49:24 +0200199 device_set_wakeup_capable(&dev->dev,
200 dev->protocol->can_wakeup(dev));
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100201
202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
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 Torvalds1da177e2005-04-16 15:20:36 -0700211int pnp_add_device(struct pnp_dev *dev)
212{
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700213 int ret;
Bjorn Helgaas2663f602008-08-19 16:53:36 -0600214 char buf[128];
215 int len = 0;
216 struct pnp_id *id;
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700217
Bjorn Helgaasb1734912007-08-15 10:32:13 -0600218 if (dev->card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700220
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700221 ret = __pnp_add_device(dev);
222 if (ret)
223 return ret;
224
Bjorn Helgaas2663f602008-08-19 16:53:36 -0600225 buf[0] = '\0';
226 for (id = dev->id; id; id = id->next)
227 len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700228
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600229 dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
230 dev->protocol->name, buf,
231 dev->active ? "active" : "disabled");
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235void __pnp_remove_device(struct pnp_dev *dev)
236{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100237 pnp_delist_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 device_unregister(&dev->dev);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static int __init pnp_init(void)
242{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return bus_register(&pnp_bus_type);
244}
245
246subsys_initcall(pnp_init);
Bjorn Helgaas97ef0622008-08-19 16:53:41 -0600247
248int pnp_debug;
249
250#if defined(CONFIG_PNP_DEBUG_MESSAGES)
Thomas Renningercdefba02010-10-25 21:11:16 +0200251module_param_named(debug, pnp_debug, int, 0644);
Bjorn Helgaas97ef0622008-08-19 16:53:41 -0600252#endif