driver.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * driver.c - device id matching, driver model, etc.
 *
 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
 */

#include <linux/string.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/pnp.h>
#include "base.h"

static int compare_func(const char *ida, const char *idb)
{
	int i;
B
Bjorn Helgaas 已提交
18

L
Linus Torvalds 已提交
19
	/* we only need to compare the last 4 chars */
B
Bjorn Helgaas 已提交
20
	for (i = 3; i < 7; i++) {
L
Linus Torvalds 已提交
21
		if (ida[i] != 'X' &&
B
Bjorn Helgaas 已提交
22
		    idb[i] != 'X' && toupper(ida[i]) != toupper(idb[i]))
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
			return 0;
	}
	return 1;
}

int compare_pnp_id(struct pnp_id *pos, const char *id)
{
	if (!pos || !id || (strlen(id) != 7))
		return 0;
B
Bjorn Helgaas 已提交
32
	if (memcmp(id, "ANYDEVS", 7) == 0)
L
Linus Torvalds 已提交
33
		return 1;
B
Bjorn Helgaas 已提交
34 35 36
	while (pos) {
		if (memcmp(pos->id, id, 3) == 0)
			if (compare_func(pos->id, id) == 1)
L
Linus Torvalds 已提交
37 38 39 40 41 42
				return 1;
		pos = pos->next;
	}
	return 0;
}

B
Bjorn Helgaas 已提交
43 44
static const struct pnp_device_id *match_device(struct pnp_driver *drv,
						struct pnp_dev *dev)
L
Linus Torvalds 已提交
45 46
{
	const struct pnp_device_id *drv_id = drv->id_table;
B
Bjorn Helgaas 已提交
47

L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
	if (!drv_id)
		return NULL;

	while (*drv_id->id) {
		if (compare_pnp_id(dev->id, drv_id->id))
			return drv_id;
		drv_id++;
	}
	return NULL;
}

int pnp_device_attach(struct pnp_dev *pnp_dev)
{
	spin_lock(&pnp_lock);
B
Bjorn Helgaas 已提交
62
	if (pnp_dev->status != PNP_READY) {
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		spin_unlock(&pnp_lock);
		return -EBUSY;
	}
	pnp_dev->status = PNP_ATTACHED;
	spin_unlock(&pnp_lock);
	return 0;
}

void pnp_device_detach(struct pnp_dev *pnp_dev)
{
	spin_lock(&pnp_lock);
	if (pnp_dev->status == PNP_ATTACHED)
		pnp_dev->status = PNP_READY;
	spin_unlock(&pnp_lock);
	pnp_disable_dev(pnp_dev);
}

static int pnp_device_probe(struct device *dev)
{
	int error;
	struct pnp_driver *pnp_drv;
	struct pnp_dev *pnp_dev;
	const struct pnp_device_id *dev_id = NULL;
	pnp_dev = to_pnp_dev(dev);
	pnp_drv = to_pnp_driver(dev->driver);

	error = pnp_device_attach(pnp_dev);
	if (error < 0)
		return error;

	if (pnp_dev->active == 0) {
		if (!(pnp_drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)) {
			error = pnp_activate_dev(pnp_dev);
			if (error < 0)
				return error;
		}
	} else if ((pnp_drv->flags & PNP_DRIVER_RES_DISABLE)
B
Bjorn Helgaas 已提交
100
		   == PNP_DRIVER_RES_DISABLE) {
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110
		error = pnp_disable_dev(pnp_dev);
		if (error < 0)
			return error;
	}
	error = 0;
	if (pnp_drv->probe) {
		dev_id = match_device(pnp_drv, pnp_dev);
		if (dev_id != NULL)
			error = pnp_drv->probe(pnp_dev, dev_id);
	}
B
Bjorn Helgaas 已提交
111
	if (error >= 0) {
L
Linus Torvalds 已提交
112 113 114 115
		pnp_dev->driver = pnp_drv;
		error = 0;
	} else
		goto fail;
116 117

	dev_dbg(dev, "driver attached\n");
L
Linus Torvalds 已提交
118 119
	return error;

B
Bjorn Helgaas 已提交
120
fail:
L
Linus Torvalds 已提交
121 122 123 124 125 126
	pnp_device_detach(pnp_dev);
	return error;
}

static int pnp_device_remove(struct device *dev)
{
B
Bjorn Helgaas 已提交
127 128
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *drv = pnp_dev->driver;
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140

	if (drv) {
		if (drv->remove)
			drv->remove(pnp_dev);
		pnp_dev->driver = NULL;
	}
	pnp_device_detach(pnp_dev);
	return 0;
}

static int pnp_bus_match(struct device *dev, struct device_driver *drv)
{
B
Bjorn Helgaas 已提交
141 142
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = to_pnp_driver(drv);
B
Bjorn Helgaas 已提交
143

L
Linus Torvalds 已提交
144 145 146 147 148
	if (match_device(pnp_drv, pnp_dev) == NULL)
		return 0;
	return 1;
}

149 150
static int pnp_bus_suspend(struct device *dev, pm_message_t state)
{
B
Bjorn Helgaas 已提交
151 152
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = pnp_dev->driver;
153 154 155 156 157 158 159 160 161 162 163
	int error;

	if (!pnp_drv)
		return 0;

	if (pnp_drv->suspend) {
		error = pnp_drv->suspend(pnp_dev, state);
		if (error)
			return error;
	}

164
	if (pnp_can_disable(pnp_dev)) {
B
Bjorn Helgaas 已提交
165 166 167
		error = pnp_stop_dev(pnp_dev);
		if (error)
			return error;
168
	}
169

170
	if (pnp_dev->protocol->suspend)
171
		pnp_dev->protocol->suspend(pnp_dev, state);
172 173 174
	return 0;
}

175
static int pnp_bus_resume(struct device *dev)
176
{
B
Bjorn Helgaas 已提交
177 178
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = pnp_dev->driver;
179 180 181 182 183
	int error;

	if (!pnp_drv)
		return 0;

184
	if (pnp_dev->protocol->resume)
185 186
		pnp_dev->protocol->resume(pnp_dev);

187
	if (pnp_can_write(pnp_dev)) {
188 189 190 191
		error = pnp_start_dev(pnp_dev);
		if (error)
			return error;
	}
192

193 194 195 196 197
	if (pnp_drv->resume) {
		error = pnp_drv->resume(pnp_dev);
		if (error)
			return error;
	}
198 199

	return 0;
200
}
L
Linus Torvalds 已提交
201 202

struct bus_type pnp_bus_type = {
B
Bjorn Helgaas 已提交
203 204 205 206
	.name    = "pnp",
	.match   = pnp_bus_match,
	.probe   = pnp_device_probe,
	.remove  = pnp_device_remove,
207
	.suspend = pnp_bus_suspend,
B
Bjorn Helgaas 已提交
208
	.resume  = pnp_bus_resume,
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217
};

int pnp_register_driver(struct pnp_driver *drv)
{
	pnp_dbg("the driver '%s' has been registered", drv->name);

	drv->driver.name = drv->name;
	drv->driver.bus = &pnp_bus_type;

218
	return driver_register(&drv->driver);
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

void pnp_unregister_driver(struct pnp_driver *drv)
{
	driver_unregister(&drv->driver);
	pnp_dbg("the driver '%s' has been unregistered", drv->name);
}

/**
 * pnp_add_id - adds an EISA id to the specified device
 * @id: pointer to a pnp_id structure
 * @dev: pointer to the desired device
 */
int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev)
{
	struct pnp_id *ptr;
B
Bjorn Helgaas 已提交
235

L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	id->next = NULL;
	ptr = dev->id;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
		ptr->next = id;
	else
		dev->id = id;
	return 0;
}

EXPORT_SYMBOL(pnp_register_driver);
EXPORT_SYMBOL(pnp_unregister_driver);
EXPORT_SYMBOL(pnp_device_attach);
EXPORT_SYMBOL(pnp_device_detach);