PNP: Convert pnp_lock into a mutex
[linux-2.6-block.git] / drivers / pnp / core.c
CommitLineData
1da177e4
LT
1/*
2 * core.c - contains all core device and protocol registration functions
3 *
4 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
1da177e4
LT
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>
38f6b38d 12#include <linux/mutex.h>
1da177e4
LT
13#include <linux/init.h>
14#include <linux/string.h>
15#include <linux/slab.h>
16#include <linux/errno.h>
2e17c550 17#include <linux/dma-mapping.h>
1da177e4
LT
18
19#include "base.h"
20
1da177e4
LT
21static LIST_HEAD(pnp_protocols);
22LIST_HEAD(pnp_global);
38f6b38d 23DEFINE_MUTEX(pnp_lock);
1da177e4 24
8f81dd14
BH
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
1da177e4
LT
33void *pnp_alloc(long size)
34{
35 void *result;
36
dd00cc48 37 result = kzalloc(size, GFP_KERNEL);
9dd78466 38 if (!result) {
1da177e4
LT
39 printk(KERN_ERR "pnp: Out of Memory\n");
40 return NULL;
41 }
1da177e4
LT
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 */
1da177e4
LT
51int pnp_register_protocol(struct pnp_protocol *protocol)
52{
53 int nodenum;
9dd78466 54 struct list_head *pos;
1da177e4 55
1da177e4
LT
56 INIT_LIST_HEAD(&protocol->devices);
57 INIT_LIST_HEAD(&protocol->cards);
58 nodenum = 0;
38f6b38d 59 mutex_lock(&pnp_lock);
1da177e4
LT
60
61 /* assign the lowest unused number */
9dd78466
BH
62 list_for_each(pos, &pnp_protocols) {
63 struct pnp_protocol *cur = to_pnp_protocol(pos);
64 if (cur->number == nodenum) {
1da177e4
LT
65 pos = &pnp_protocols;
66 nodenum++;
67 }
68 }
69
70 list_add_tail(&protocol->protocol_list, &pnp_protocols);
38f6b38d 71 mutex_unlock(&pnp_lock);
1da177e4
LT
72
73 protocol->number = nodenum;
c85e37c5 74 dev_set_name(&protocol->dev, "pnp%d", nodenum);
1da177e4
LT
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
1da177e4
LT
81 */
82void pnp_unregister_protocol(struct pnp_protocol *protocol)
83{
38f6b38d 84 mutex_lock(&pnp_lock);
1da177e4 85 list_del(&protocol->protocol_list);
38f6b38d 86 mutex_unlock(&pnp_lock);
1da177e4
LT
87 device_unregister(&protocol->dev);
88}
89
1da177e4
LT
90static void pnp_free_ids(struct pnp_dev *dev)
91{
9dd78466
BH
92 struct pnp_id *id;
93 struct pnp_id *next;
07d4e9af 94
1da177e4
LT
95 id = dev->id;
96 while (id) {
97 next = id->next;
98 kfree(id);
99 id = next;
100 }
101}
102
aee3ad81
BH
103void pnp_free_resource(struct pnp_resource *pnp_res)
104{
105 list_del(&pnp_res->list);
106 kfree(pnp_res);
107}
108
109void 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
1da177e4
LT
118static void pnp_release_device(struct device *dmdev)
119{
9dd78466 120 struct pnp_dev *dev = to_pnp_dev(dmdev);
07d4e9af 121
1da177e4 122 pnp_free_ids(dev);
aee3ad81 123 pnp_free_resources(dev);
1f32ca31 124 pnp_free_options(dev);
1da177e4
LT
125 kfree(dev);
126}
127
620e112c
TR
128struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id,
129 const char *pnpid)
1da177e4 130{
bda1e4e5
BH
131 struct pnp_dev *dev;
132 struct pnp_id *dev_id;
07d4e9af 133
bda1e4e5
BH
134 dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
135 if (!dev)
136 return NULL;
137
aee3ad81 138 INIT_LIST_HEAD(&dev->resources);
1f32ca31 139 INIT_LIST_HEAD(&dev->options);
bda1e4e5
BH
140 dev->protocol = protocol;
141 dev->number = id;
2f4f27d4 142 dev->dma_mask = DMA_BIT_MASK(24);
bda1e4e5
BH
143
144 dev->dev.parent = &dev->protocol->dev;
1da177e4 145 dev->dev.bus = &pnp_bus_type;
2e17c550 146 dev->dev.dma_mask = &dev->dma_mask;
bda1e4e5 147 dev->dev.coherent_dma_mask = dev->dma_mask;
1da177e4 148 dev->dev.release = &pnp_release_device;
bda1e4e5 149
c85e37c5 150 dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
bda1e4e5
BH
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
161int __pnp_add_device(struct pnp_dev *dev)
162{
bda1e4e5 163 pnp_fixup_device(dev);
1da177e4 164 dev->status = PNP_READY;
38f6b38d 165 mutex_lock(&pnp_lock);
1da177e4
LT
166 list_add_tail(&dev->global_list, &pnp_global);
167 list_add_tail(&dev->protocol_list, &dev->protocol->devices);
38f6b38d 168 mutex_unlock(&pnp_lock);
b14e033e
AS
169 if (dev->protocol->can_wakeup)
170 device_set_wakeup_capable(&dev->dev,
171 dev->protocol->can_wakeup(dev));
8a89efd1 172 return device_register(&dev->dev);
1da177e4
LT
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 */
1da177e4
LT
181int pnp_add_device(struct pnp_dev *dev)
182{
348366b9 183 int ret;
2663f60d
BH
184 char buf[128];
185 int len = 0;
186 struct pnp_id *id;
348366b9 187
b1734913 188 if (dev->card)
1da177e4 189 return -EINVAL;
348366b9 190
348366b9
BH
191 ret = __pnp_add_device(dev);
192 if (ret)
193 return ret;
194
2663f60d
BH
195 buf[0] = '\0';
196 for (id = dev->id; id; id = id->next)
197 len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
348366b9 198
c1f3f281
BH
199 dev_printk(KERN_DEBUG, &dev->dev, "%s device, IDs%s (%s)\n",
200 dev->protocol->name, buf,
201 dev->active ? "active" : "disabled");
348366b9 202 return 0;
1da177e4
LT
203}
204
205void __pnp_remove_device(struct pnp_dev *dev)
206{
38f6b38d 207 mutex_lock(&pnp_lock);
1da177e4
LT
208 list_del(&dev->global_list);
209 list_del(&dev->protocol_list);
38f6b38d 210 mutex_unlock(&pnp_lock);
1da177e4
LT
211 device_unregister(&dev->dev);
212}
213
1da177e4
LT
214static int __init pnp_init(void)
215{
1da177e4
LT
216 return bus_register(&pnp_bus_type);
217}
218
219subsys_initcall(pnp_init);
97ef062b
BH
220
221int pnp_debug;
222
223#if defined(CONFIG_PNP_DEBUG_MESSAGES)
cdefba03 224module_param_named(debug, pnp_debug, int, 0644);
97ef062b 225#endif