blob: 3bf18d7189750f2970753aded98930b03f068621 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * core.c - contains all core device and protocol registration functions
4 *
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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. Wysocki38f6b382015-03-18 22:39:55 +010013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/string.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
David Brownell2e17c552007-05-08 00:25:29 -070018#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "base.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static LIST_HEAD(pnp_protocols);
23LIST_HEAD(pnp_global);
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010024DEFINE_MUTEX(pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Bjorn Helgaas8f81dd12007-05-08 00:35:54 -070026/*
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 */
31int pnp_platform_devices;
32EXPORT_SYMBOL(pnp_platform_devices);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034void *pnp_alloc(long size)
35{
36 void *result;
37
Yoann Padioleaudd00cc42007-07-19 01:49:03 -070038 result = kzalloc(size, GFP_KERNEL);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070039 if (!result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 printk(KERN_ERR "pnp: Out of Memory\n");
41 return NULL;
42 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return result;
44}
45
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010046static 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 Torvalds1da177e2005-04-16 15:20:36 -070053/**
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 Torvalds1da177e2005-04-16 15:20:36 -070059int pnp_register_protocol(struct pnp_protocol *protocol)
60{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070061 struct list_head *pos;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010062 int nodenum, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 INIT_LIST_HEAD(&protocol->devices);
65 INIT_LIST_HEAD(&protocol->cards);
66 nodenum = 0;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010067
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +010068 mutex_lock(&pnp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* assign the lowest unused number */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070071 list_for_each(pos, &pnp_protocols) {
72 struct pnp_protocol *cur = to_pnp_protocol(pos);
73 if (cur->number == nodenum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 pos = &pnp_protocols;
75 nodenum++;
76 }
77 }
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 protocol->number = nodenum;
Kay Sieversc85e37c2009-01-06 10:44:38 -080080 dev_set_name(&protocol->dev, "pnp%d", nodenum);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010081
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 Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/**
94 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
95 * @protocol: pointer to the corresponding pnp_protocol structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97void pnp_unregister_protocol(struct pnp_protocol *protocol)
98{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +010099 pnp_remove_protocol(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 device_unregister(&protocol->dev);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static void pnp_free_ids(struct pnp_dev *dev)
104{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700105 struct pnp_id *id;
106 struct pnp_id *next;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 id = dev->id;
109 while (id) {
110 next = id->next;
111 kfree(id);
112 id = next;
113 }
114}
115
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600116void pnp_free_resource(struct pnp_resource *pnp_res)
117{
118 list_del(&pnp_res->list);
119 kfree(pnp_res);
120}
121
122void 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 Torvalds1da177e2005-04-16 15:20:36 -0700131static void pnp_release_device(struct device *dmdev)
132{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700133 struct pnp_dev *dev = to_pnp_dev(dmdev);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 pnp_free_ids(dev);
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600136 pnp_free_resources(dev);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600137 pnp_free_options(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree(dev);
139}
140
Thomas Renninger620e1122010-10-01 10:54:00 +0200141struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
142 const char *pnpid)
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600143{
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 Helgaasaee3ad82008-06-27 16:56:57 -0600151 INIT_LIST_HEAD(&dev->resources);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600152 INIT_LIST_HEAD(&dev->options);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600153 dev->protocol = protocol;
154 dev->number = id;
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700155 dev->dma_mask = DMA_BIT_MASK(24);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600156
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 Sieversc85e37c2009-01-06 10:44:38 -0800163 dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600164
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. Wysocki71150d22015-03-18 22:40:04 +0100174static 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 Torvalds1da177e2005-04-16 15:20:36 -0700182int __pnp_add_device(struct pnp_dev *dev)
183{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100184 int ret;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 pnp_fixup_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dev->status = PNP_READY;
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100188
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100189 mutex_lock(&pnp_lock);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 list_add_tail(&dev->global_list, &pnp_global);
192 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100193
Rafael J. Wysocki38f6b382015-03-18 22:39:55 +0100194 mutex_unlock(&pnp_lock);
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100195
196 ret = device_register(&dev->dev);
197 if (ret)
198 pnp_delist_device(dev);
199 else if (dev->protocol->can_wakeup)
Alan Sternb14e033e2010-06-29 22:49:24 +0200200 device_set_wakeup_capable(&dev->dev,
201 dev->protocol->can_wakeup(dev));
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100202
203 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
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 Torvalds1da177e2005-04-16 15:20:36 -0700212int pnp_add_device(struct pnp_dev *dev)
213{
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700214 int ret;
Bjorn Helgaas2663f602008-08-19 16:53:36 -0600215 char buf[128];
216 int len = 0;
217 struct pnp_id *id;
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700218
Bjorn Helgaasb1734912007-08-15 10:32:13 -0600219 if (dev->card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -EINVAL;
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700221
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700222 ret = __pnp_add_device(dev);
223 if (ret)
224 return ret;
225
Bjorn Helgaas2663f602008-08-19 16:53:36 -0600226 buf[0] = '\0';
227 for (id = dev->id; id; id = id->next)
228 len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700229
Bjorn Helgaasc1f3f282010-09-29 12:24:23 -0600230 dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
231 dev->protocol->name, buf,
232 dev->active ? "active" : "disabled");
Bjorn Helgaas348366b2007-10-16 23:31:12 -0700233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236void __pnp_remove_device(struct pnp_dev *dev)
237{
Rafael J. Wysocki71150d22015-03-18 22:40:04 +0100238 pnp_delist_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 device_unregister(&dev->dev);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int __init pnp_init(void)
243{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return bus_register(&pnp_bus_type);
245}
246
247subsys_initcall(pnp_init);
Bjorn Helgaas97ef0622008-08-19 16:53:41 -0600248
249int pnp_debug;
250
251#if defined(CONFIG_PNP_DEBUG_MESSAGES)
Thomas Renningercdefba02010-10-25 21:11:16 +0200252module_param_named(debug, pnp_debug, int, 0644);
Bjorn Helgaas97ef0622008-08-19 16:53:41 -0600253#endif