core.c 4.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * core.c - contains all core device and protocol registration functions
 *
 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
 */

#include <linux/pnp.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/errno.h>
D
David Brownell 已提交
16
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
17 18 19 20 21 22 23

#include "base.h"

static LIST_HEAD(pnp_protocols);
LIST_HEAD(pnp_global);
DEFINE_SPINLOCK(pnp_lock);

24 25 26 27 28 29 30 31
/*
 * ACPI or PNPBIOS should tell us about all platform devices, so we can
 * skip some blind probes.  ISAPNP typically enumerates only plug-in ISA
 * devices, not built-in things like COM ports.
 */
int pnp_platform_devices;
EXPORT_SYMBOL(pnp_platform_devices);

L
Linus Torvalds 已提交
32 33 34 35
void *pnp_alloc(long size)
{
	void *result;

36
	result = kzalloc(size, GFP_KERNEL);
B
Bjorn Helgaas 已提交
37
	if (!result) {
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		printk(KERN_ERR "pnp: Out of Memory\n");
		return NULL;
	}
	return result;
}

/**
 * pnp_protocol_register - adds a pnp protocol to the pnp layer
 * @protocol: pointer to the corresponding pnp_protocol structure
 *
 *  Ex protocols: ISAPNP, PNPBIOS, etc
 */
int pnp_register_protocol(struct pnp_protocol *protocol)
{
	int nodenum;
B
Bjorn Helgaas 已提交
53
	struct list_head *pos;
L
Linus Torvalds 已提交
54 55 56 57 58 59 60

	INIT_LIST_HEAD(&protocol->devices);
	INIT_LIST_HEAD(&protocol->cards);
	nodenum = 0;
	spin_lock(&pnp_lock);

	/* assign the lowest unused number */
B
Bjorn Helgaas 已提交
61 62 63
	list_for_each(pos, &pnp_protocols) {
		struct pnp_protocol *cur = to_pnp_protocol(pos);
		if (cur->number == nodenum) {
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72
			pos = &pnp_protocols;
			nodenum++;
		}
	}

	list_add_tail(&protocol->protocol_list, &pnp_protocols);
	spin_unlock(&pnp_lock);

	protocol->number = nodenum;
73
	dev_set_name(&protocol->dev, "pnp%d", nodenum);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	return device_register(&protocol->dev);
}

/**
 * pnp_protocol_unregister - removes a pnp protocol from the pnp layer
 * @protocol: pointer to the corresponding pnp_protocol structure
 */
void pnp_unregister_protocol(struct pnp_protocol *protocol)
{
	spin_lock(&pnp_lock);
	list_del(&protocol->protocol_list);
	spin_unlock(&pnp_lock);
	device_unregister(&protocol->dev);
}

static void pnp_free_ids(struct pnp_dev *dev)
{
B
Bjorn Helgaas 已提交
91 92
	struct pnp_id *id;
	struct pnp_id *next;
B
Bjorn Helgaas 已提交
93

L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101
	id = dev->id;
	while (id) {
		next = id->next;
		kfree(id);
		id = next;
	}
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
void pnp_free_resource(struct pnp_resource *pnp_res)
{
	list_del(&pnp_res->list);
	kfree(pnp_res);
}

void pnp_free_resources(struct pnp_dev *dev)
{
	struct pnp_resource *pnp_res, *tmp;

	list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
		pnp_free_resource(pnp_res);
	}
}

L
Linus Torvalds 已提交
117 118
static void pnp_release_device(struct device *dmdev)
{
B
Bjorn Helgaas 已提交
119
	struct pnp_dev *dev = to_pnp_dev(dmdev);
B
Bjorn Helgaas 已提交
120

L
Linus Torvalds 已提交
121
	pnp_free_ids(dev);
122
	pnp_free_resources(dev);
123
	pnp_free_options(dev);
L
Linus Torvalds 已提交
124 125 126
	kfree(dev);
}

B
Bjorn Helgaas 已提交
127
struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id, char *pnpid)
L
Linus Torvalds 已提交
128
{
B
Bjorn Helgaas 已提交
129 130
	struct pnp_dev *dev;
	struct pnp_id *dev_id;
B
Bjorn Helgaas 已提交
131

B
Bjorn Helgaas 已提交
132 133 134 135
	dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
	if (!dev)
		return NULL;

136
	INIT_LIST_HEAD(&dev->resources);
137
	INIT_LIST_HEAD(&dev->options);
B
Bjorn Helgaas 已提交
138 139
	dev->protocol = protocol;
	dev->number = id;
140
	dev->dma_mask = DMA_BIT_MASK(24);
B
Bjorn Helgaas 已提交
141 142

	dev->dev.parent = &dev->protocol->dev;
L
Linus Torvalds 已提交
143
	dev->dev.bus = &pnp_bus_type;
D
David Brownell 已提交
144
	dev->dev.dma_mask = &dev->dma_mask;
B
Bjorn Helgaas 已提交
145
	dev->dev.coherent_dma_mask = dev->dma_mask;
L
Linus Torvalds 已提交
146
	dev->dev.release = &pnp_release_device;
B
Bjorn Helgaas 已提交
147

148
	dev_set_name(&dev->dev, "%02x:%02x", dev->protocol->number, dev->number);
B
Bjorn Helgaas 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161

	dev_id = pnp_add_id(dev, pnpid);
	if (!dev_id) {
		kfree(dev);
		return NULL;
	}

	return dev;
}

int __pnp_add_device(struct pnp_dev *dev)
{
	pnp_fixup_device(dev);
L
Linus Torvalds 已提交
162 163 164 165 166
	dev->status = PNP_READY;
	spin_lock(&pnp_lock);
	list_add_tail(&dev->global_list, &pnp_global);
	list_add_tail(&dev->protocol_list, &dev->protocol->devices);
	spin_unlock(&pnp_lock);
167 168 169
	if (dev->protocol->can_wakeup)
		device_set_wakeup_capable(&dev->dev,
				dev->protocol->can_wakeup(dev));
170
	return device_register(&dev->dev);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180
}

/*
 * pnp_add_device - adds a pnp device to the pnp layer
 * @dev: pointer to dev to add
 *
 *  adds to driver model, name database, fixups, interface, etc.
 */
int pnp_add_device(struct pnp_dev *dev)
{
181
	int ret;
182 183 184
	char buf[128];
	int len = 0;
	struct pnp_id *id;
185

B
Bjorn Helgaas 已提交
186
	if (dev->card)
L
Linus Torvalds 已提交
187
		return -EINVAL;
188 189 190 191 192

	ret = __pnp_add_device(dev);
	if (ret)
		return ret;

193 194 195
	buf[0] = '\0';
	for (id = dev->id; id; id = id->next)
		len += scnprintf(buf + len, sizeof(buf) - len, " %s", id->id);
196

B
Bjorn Helgaas 已提交
197
	pnp_dbg(&dev->dev, "%s device, IDs%s (%s)\n",
198
		dev->protocol->name, buf, dev->active ? "active" : "disabled");
199
	return 0;
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

void __pnp_remove_device(struct pnp_dev *dev)
{
	spin_lock(&pnp_lock);
	list_del(&dev->global_list);
	list_del(&dev->protocol_list);
	spin_unlock(&pnp_lock);
	device_unregister(&dev->dev);
}

static int __init pnp_init(void)
{
	return bus_register(&pnp_bus_type);
}

subsys_initcall(pnp_init);
217 218 219 220

int pnp_debug;

#if defined(CONFIG_PNP_DEBUG_MESSAGES)
221
module_param_named(debug, pnp_debug, int, 0644);
222
#endif