driver.c 6.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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;
19

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

int compare_pnp_id(struct pnp_id *pos, const char *id)
{
	if (!pos || !id || (strlen(id) != 7))
		return 0;
33
	if (memcmp(id, "ANYDEVS", 7) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
34
		return 1;
35 36 37
	while (pos) {
		if (memcmp(pos->id, id, 3) == 0)
			if (compare_func(pos->id, id) == 1)
Linus Torvalds's avatar
Linus Torvalds committed
38 39 40 41 42 43
				return 1;
		pos = pos->next;
	}
	return 0;
}

44 45
static const struct pnp_device_id *match_device(struct pnp_driver *drv,
						struct pnp_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
46 47
{
	const struct pnp_device_id *drv_id = drv->id_table;
48

Linus Torvalds's avatar
Linus Torvalds committed
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)
{
62
	mutex_lock(&pnp_lock);
63
	if (pnp_dev->status != PNP_READY) {
64
		mutex_unlock(&pnp_lock);
Linus Torvalds's avatar
Linus Torvalds committed
65 66 67
		return -EBUSY;
	}
	pnp_dev->status = PNP_ATTACHED;
68
	mutex_unlock(&pnp_lock);
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73
	return 0;
}

void pnp_device_detach(struct pnp_dev *pnp_dev)
{
74
	mutex_lock(&pnp_lock);
Linus Torvalds's avatar
Linus Torvalds committed
75 76
	if (pnp_dev->status == PNP_ATTACHED)
		pnp_dev->status = PNP_READY;
77
	mutex_unlock(&pnp_lock);
Linus Torvalds's avatar
Linus Torvalds committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}

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)
100
		   == PNP_DRIVER_RES_DISABLE) {
Linus Torvalds's avatar
Linus Torvalds committed
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);
	}
111
	if (error >= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115
		pnp_dev->driver = pnp_drv;
		error = 0;
	} else
		goto fail;
116

Linus Torvalds's avatar
Linus Torvalds committed
117 118
	return error;

119
fail:
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122 123 124 125
	pnp_device_detach(pnp_dev);
	return error;
}

static int pnp_device_remove(struct device *dev)
{
126 127
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *drv = pnp_dev->driver;
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133

	if (drv) {
		if (drv->remove)
			drv->remove(pnp_dev);
		pnp_dev->driver = NULL;
	}
134 135 136 137 138

	if (pnp_dev->active &&
	    (!drv || !(drv->flags & PNP_DRIVER_RES_DO_NOT_CHANGE)))
		pnp_disable_dev(pnp_dev);

Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142
	pnp_device_detach(pnp_dev);
	return 0;
}

143 144 145 146 147 148 149 150 151
static void pnp_device_shutdown(struct device *dev)
{
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *drv = pnp_dev->driver;

	if (drv && drv->shutdown)
		drv->shutdown(pnp_dev);
}

Linus Torvalds's avatar
Linus Torvalds committed
152 153
static int pnp_bus_match(struct device *dev, struct device_driver *drv)
{
154 155
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = to_pnp_driver(drv);
156

Linus Torvalds's avatar
Linus Torvalds committed
157 158 159 160 161
	if (match_device(pnp_drv, pnp_dev) == NULL)
		return 0;
	return 1;
}

162
static int __pnp_bus_suspend(struct device *dev, pm_message_t state)
163
{
164 165
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = pnp_dev->driver;
166 167 168 169 170
	int error;

	if (!pnp_drv)
		return 0;

171 172 173 174 175 176 177
	if (pnp_drv->driver.pm && pnp_drv->driver.pm->suspend) {
		error = pnp_drv->driver.pm->suspend(dev);
		suspend_report_result(pnp_drv->driver.pm->suspend, error);
		if (error)
			return error;
	}

178 179 180 181 182 183
	if (pnp_drv->suspend) {
		error = pnp_drv->suspend(pnp_dev, state);
		if (error)
			return error;
	}

184
	if (pnp_can_disable(pnp_dev)) {
185 186 187
		error = pnp_stop_dev(pnp_dev);
		if (error)
			return error;
188
	}
189

190
	if (pnp_can_suspend(pnp_dev))
191
		pnp_dev->protocol->suspend(pnp_dev, state);
192 193 194
	return 0;
}

195 196 197 198 199 200 201 202 203 204
static int pnp_bus_suspend(struct device *dev)
{
	return __pnp_bus_suspend(dev, PMSG_SUSPEND);
}

static int pnp_bus_freeze(struct device *dev)
{
	return __pnp_bus_suspend(dev, PMSG_FREEZE);
}

205 206 207 208 209
static int pnp_bus_poweroff(struct device *dev)
{
	return __pnp_bus_suspend(dev, PMSG_HIBERNATE);
}

210
static int pnp_bus_resume(struct device *dev)
211
{
212 213
	struct pnp_dev *pnp_dev = to_pnp_dev(dev);
	struct pnp_driver *pnp_drv = pnp_dev->driver;
214 215 216 217 218
	int error;

	if (!pnp_drv)
		return 0;

219 220 221 222 223
	if (pnp_dev->protocol->resume) {
		error = pnp_dev->protocol->resume(pnp_dev);
		if (error)
			return error;
	}
224

225
	if (pnp_can_write(pnp_dev)) {
226 227 228 229
		error = pnp_start_dev(pnp_dev);
		if (error)
			return error;
	}
230

231 232 233 234 235 236
	if (pnp_drv->driver.pm && pnp_drv->driver.pm->resume) {
		error = pnp_drv->driver.pm->resume(dev);
		if (error)
			return error;
	}

237 238 239 240 241
	if (pnp_drv->resume) {
		error = pnp_drv->resume(pnp_dev);
		if (error)
			return error;
	}
242 243

	return 0;
244
}
Linus Torvalds's avatar
Linus Torvalds committed
245

246
static const struct dev_pm_ops pnp_bus_dev_pm_ops = {
247
	/* Suspend callbacks */
248 249
	.suspend = pnp_bus_suspend,
	.resume = pnp_bus_resume,
250 251 252 253 254
	/* Hibernate callbacks */
	.freeze = pnp_bus_freeze,
	.thaw = pnp_bus_resume,
	.poweroff = pnp_bus_poweroff,
	.restore = pnp_bus_resume,
255 256
};

Linus Torvalds's avatar
Linus Torvalds committed
257
struct bus_type pnp_bus_type = {
258 259 260 261
	.name    = "pnp",
	.match   = pnp_bus_match,
	.probe   = pnp_device_probe,
	.remove  = pnp_device_remove,
262
	.shutdown = pnp_device_shutdown,
263
	.pm	 = &pnp_bus_dev_pm_ops,
264
	.dev_groups = pnp_dev_groups,
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270 271
};

int pnp_register_driver(struct pnp_driver *drv)
{
	drv->driver.name = drv->name;
	drv->driver.bus = &pnp_bus_type;

272
	return driver_register(&drv->driver);
Linus Torvalds's avatar
Linus Torvalds committed
273 274 275 276 277 278 279 280 281 282
}

void pnp_unregister_driver(struct pnp_driver *drv)
{
	driver_unregister(&drv->driver);
}

/**
 * pnp_add_id - adds an EISA id to the specified device
 * @dev: pointer to the desired device
283
 * @id: pointer to an EISA id string
Linus Torvalds's avatar
Linus Torvalds committed
284
 */
285
struct pnp_id *pnp_add_id(struct pnp_dev *dev, const char *id)
Linus Torvalds's avatar
Linus Torvalds committed
286
{
287
	struct pnp_id *dev_id, *ptr;
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302
	dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
	if (!dev_id)
		return NULL;

	dev_id->id[0] = id[0];
	dev_id->id[1] = id[1];
	dev_id->id[2] = id[2];
	dev_id->id[3] = tolower(id[3]);
	dev_id->id[4] = tolower(id[4]);
	dev_id->id[5] = tolower(id[5]);
	dev_id->id[6] = tolower(id[6]);
	dev_id->id[7] = '\0';

	dev_id->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306
	ptr = dev->id;
	while (ptr && ptr->next)
		ptr = ptr->next;
	if (ptr)
307
		ptr->next = dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
308
	else
309 310 311
		dev->id = dev_id;

	return dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317
}

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