1/*
2 * core.c - contains all core device and protocol registration functions
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
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>
12#include <linux/mutex.h>
13#include <linux/init.h>
14#include <linux/string.h>
15#include <linux/slab.h>
16#include <linux/errno.h>
17#include <linux/dma-mapping.h>
18
19#include "base.h"
20
21static LIST_HEAD(pnp_protocols);
22LIST_HEAD(pnp_global);
23DEFINE_MUTEX(pnp_lock);
24
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 */
30int pnp_platform_devices;
31EXPORT_SYMBOL(pnp_platform_devices);
32
33void *pnp_alloc(long size)
34{
35	void *result;
36
37	result = kzalloc(size, GFP_KERNEL);
38	if (!result) {
39		printk(KERN_ERR "pnp: Out of Memory\n");
40		return NULL;
41	}
42	return result;
43}
44
45static 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
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 */
58int pnp_register_protocol(struct pnp_protocol *protocol)
59{
60	struct list_head *pos;
61	int nodenum, ret;
62
63	INIT_LIST_HEAD(&protocol->devices);
64	INIT_LIST_HEAD(&protocol->cards);
65	nodenum = 0;
66
67	mutex_lock(&pnp_lock);
68
69	/* assign the lowest unused number */
70	list_for_each(pos, &pnp_protocols) {
71		struct pnp_protocol *cur = to_pnp_protocol(pos);
72		if (cur->number == nodenum) {
73			pos = &pnp_protocols;
74			nodenum++;
75		}
76	}
77
78	protocol->number = nodenum;
79	dev_set_name(&protocol->dev, "pnp%d", nodenum);
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;
90}
91
92/**
93 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
94 * @protocol: pointer to the corresponding pnp_protocol structure
95 */
96void pnp_unregister_protocol(struct pnp_protocol *protocol)
97{
98	pnp_remove_protocol(protocol);
99	device_unregister(&protocol->dev);
100}
101
102static void pnp_free_ids(struct pnp_dev *dev)
103{
104	struct pnp_id *id;
105	struct pnp_id *next;
106
107	id = dev->id;
108	while (id) {
109		next = id->next;
110		kfree(id);
111		id = next;
112	}
113}
114
115void 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
130static void pnp_release_device(struct device *dmdev)
131{
132	struct pnp_dev *dev = to_pnp_dev(dmdev);
133
134	pnp_free_ids(dev);
135	pnp_free_resources(dev);
136	pnp_free_options(dev);
137	kfree(dev);
138}
139
140struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
141			      const char *pnpid)
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
150	INIT_LIST_HEAD(&dev->resources);
151	INIT_LIST_HEAD(&dev->options);
152	dev->protocol = protocol;
153	dev->number = id;
154	dev->dma_mask = DMA_BIT_MASK(24);
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
162	dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
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
173static 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
181int __pnp_add_device(struct pnp_dev *dev)
182{
183	int ret;
184
185	pnp_fixup_device(dev);
186	dev->status = PNP_READY;
187
188	mutex_lock(&pnp_lock);
189
190	list_add_tail(&dev->global_list, &pnp_global);
191	list_add_tail(&dev->protocol_list, &dev->protocol->devices);
192
193	mutex_unlock(&pnp_lock);
194
195	ret = device_register(&dev->dev);
196	if (ret)
197		pnp_delist_device(dev);
198	else if (dev->protocol->can_wakeup)
199		device_set_wakeup_capable(&dev->dev,
200				dev->protocol->can_wakeup(dev));
201
202	return ret;
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 */
211int pnp_add_device(struct pnp_dev *dev)
212{
213	int ret;
214	char buf[128];
215	int len = 0;
216	struct pnp_id *id;
217
218	if (dev->card)
219		return -EINVAL;
220
221	ret = __pnp_add_device(dev);
222	if (ret)
223		return ret;
224
225	buf[0] = '\0';
226	for (id = dev->id; id; id = id->next)
227		len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
228
229	dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
230		   dev->protocol->name, buf,
231		   dev->active ? "active" : "disabled");
232	return 0;
233}
234
235void __pnp_remove_device(struct pnp_dev *dev)
236{
237	pnp_delist_device(dev);
238	device_unregister(&dev->dev);
239}
240
241static int __init pnp_init(void)
242{
243	return bus_register(&pnp_bus_type);
244}
245
246subsys_initcall(pnp_init);
247
248int pnp_debug;
249
250#if defined(CONFIG_PNP_DEBUG_MESSAGES)
251module_param_named(debug, pnp_debug, int, 0644);
252#endif
253