ia64/linux-2.6.18-xen.hg

diff drivers/pci/pci-driver.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/pci/pci-driver.c	Wed Apr 11 14:15:44 2007 +0100
     1.3 @@ -0,0 +1,531 @@
     1.4 +/*
     1.5 + * drivers/pci/pci-driver.c
     1.6 + *
     1.7 + */
     1.8 +
     1.9 +#include <linux/pci.h>
    1.10 +#include <linux/module.h>
    1.11 +#include <linux/init.h>
    1.12 +#include <linux/device.h>
    1.13 +#include <linux/mempolicy.h>
    1.14 +#include <linux/string.h>
    1.15 +#include <linux/slab.h>
    1.16 +#include <linux/sched.h>
    1.17 +#include "pci.h"
    1.18 +
    1.19 +/*
    1.20 + *  Registration of PCI drivers and handling of hot-pluggable devices.
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 + * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
    1.25 + */
    1.26 +
    1.27 +struct pci_dynid {
    1.28 +	struct list_head node;
    1.29 +	struct pci_device_id id;
    1.30 +};
    1.31 +
    1.32 +#ifdef CONFIG_HOTPLUG
    1.33 +
    1.34 +/**
    1.35 + * store_new_id - add a new PCI device ID to this driver and re-probe devices
    1.36 + * @driver: target device driver
    1.37 + * @buf: buffer for scanning device ID data
    1.38 + * @count: input size
    1.39 + *
    1.40 + * Adds a new dynamic pci device ID to this driver,
    1.41 + * and causes the driver to probe for all devices again.
    1.42 + */
    1.43 +static ssize_t
    1.44 +store_new_id(struct device_driver *driver, const char *buf, size_t count)
    1.45 +{
    1.46 +	struct pci_dynid *dynid;
    1.47 +	struct pci_driver *pdrv = to_pci_driver(driver);
    1.48 +	__u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
    1.49 +		subdevice=PCI_ANY_ID, class=0, class_mask=0;
    1.50 +	unsigned long driver_data=0;
    1.51 +	int fields=0;
    1.52 +
    1.53 +	fields = sscanf(buf, "%x %x %x %x %x %x %lux",
    1.54 +			&vendor, &device, &subvendor, &subdevice,
    1.55 +			&class, &class_mask, &driver_data);
    1.56 +	if (fields < 0)
    1.57 +		return -EINVAL;
    1.58 +
    1.59 +	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
    1.60 +	if (!dynid)
    1.61 +		return -ENOMEM;
    1.62 +
    1.63 +	INIT_LIST_HEAD(&dynid->node);
    1.64 +	dynid->id.vendor = vendor;
    1.65 +	dynid->id.device = device;
    1.66 +	dynid->id.subvendor = subvendor;
    1.67 +	dynid->id.subdevice = subdevice;
    1.68 +	dynid->id.class = class;
    1.69 +	dynid->id.class_mask = class_mask;
    1.70 +	dynid->id.driver_data = pdrv->dynids.use_driver_data ?
    1.71 +		driver_data : 0UL;
    1.72 +
    1.73 +	spin_lock(&pdrv->dynids.lock);
    1.74 +	list_add_tail(&pdrv->dynids.list, &dynid->node);
    1.75 +	spin_unlock(&pdrv->dynids.lock);
    1.76 +
    1.77 +	if (get_driver(&pdrv->driver)) {
    1.78 +		driver_attach(&pdrv->driver);
    1.79 +		put_driver(&pdrv->driver);
    1.80 +	}
    1.81 +
    1.82 +	return count;
    1.83 +}
    1.84 +static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
    1.85 +
    1.86 +static void
    1.87 +pci_free_dynids(struct pci_driver *drv)
    1.88 +{
    1.89 +	struct pci_dynid *dynid, *n;
    1.90 +
    1.91 +	spin_lock(&drv->dynids.lock);
    1.92 +	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
    1.93 +		list_del(&dynid->node);
    1.94 +		kfree(dynid);
    1.95 +	}
    1.96 +	spin_unlock(&drv->dynids.lock);
    1.97 +}
    1.98 +
    1.99 +static int
   1.100 +pci_create_newid_file(struct pci_driver *drv)
   1.101 +{
   1.102 +	int error = 0;
   1.103 +	if (drv->probe != NULL)
   1.104 +		error = sysfs_create_file(&drv->driver.kobj,
   1.105 +					  &driver_attr_new_id.attr);
   1.106 +	return error;
   1.107 +}
   1.108 +
   1.109 +#else /* !CONFIG_HOTPLUG */
   1.110 +static inline void pci_free_dynids(struct pci_driver *drv) {}
   1.111 +static inline int pci_create_newid_file(struct pci_driver *drv)
   1.112 +{
   1.113 +	return 0;
   1.114 +}
   1.115 +#endif
   1.116 +
   1.117 +/**
   1.118 + * pci_match_id - See if a pci device matches a given pci_id table
   1.119 + * @ids: array of PCI device id structures to search in
   1.120 + * @dev: the PCI device structure to match against.
   1.121 + *
   1.122 + * Used by a driver to check whether a PCI device present in the
   1.123 + * system is in its list of supported devices.  Returns the matching
   1.124 + * pci_device_id structure or %NULL if there is no match.
   1.125 + *
   1.126 + * Depreciated, don't use this as it will not catch any dynamic ids
   1.127 + * that a driver might want to check for.
   1.128 + */
   1.129 +const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
   1.130 +					 struct pci_dev *dev)
   1.131 +{
   1.132 +	if (ids) {
   1.133 +		while (ids->vendor || ids->subvendor || ids->class_mask) {
   1.134 +			if (pci_match_one_device(ids, dev))
   1.135 +				return ids;
   1.136 +			ids++;
   1.137 +		}
   1.138 +	}
   1.139 +	return NULL;
   1.140 +}
   1.141 +
   1.142 +/**
   1.143 + * pci_match_device - Tell if a PCI device structure has a matching
   1.144 + *                    PCI device id structure
   1.145 + * @drv: the PCI driver to match against
   1.146 + * @dev: the PCI device structure to match against
   1.147 + *
   1.148 + * Used by a driver to check whether a PCI device present in the
   1.149 + * system is in its list of supported devices.  Returns the matching
   1.150 + * pci_device_id structure or %NULL if there is no match.
   1.151 + */
   1.152 +const struct pci_device_id *pci_match_device(struct pci_driver *drv,
   1.153 +					     struct pci_dev *dev)
   1.154 +{
   1.155 +	const struct pci_device_id *id;
   1.156 +	struct pci_dynid *dynid;
   1.157 +
   1.158 +	id = pci_match_id(drv->id_table, dev);
   1.159 +	if (id)
   1.160 +		return id;
   1.161 +
   1.162 +	/* static ids didn't match, lets look at the dynamic ones */
   1.163 +	spin_lock(&drv->dynids.lock);
   1.164 +	list_for_each_entry(dynid, &drv->dynids.list, node) {
   1.165 +		if (pci_match_one_device(&dynid->id, dev)) {
   1.166 +			spin_unlock(&drv->dynids.lock);
   1.167 +			return &dynid->id;
   1.168 +		}
   1.169 +	}
   1.170 +	spin_unlock(&drv->dynids.lock);
   1.171 +	return NULL;
   1.172 +}
   1.173 +
   1.174 +static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
   1.175 +			  const struct pci_device_id *id)
   1.176 +{
   1.177 +	int error;
   1.178 +#ifdef CONFIG_NUMA
   1.179 +	/* Execute driver initialization on node where the
   1.180 +	   device's bus is attached to.  This way the driver likely
   1.181 +	   allocates its local memory on the right node without
   1.182 +	   any need to change it. */
   1.183 +	struct mempolicy *oldpol;
   1.184 +	cpumask_t oldmask = current->cpus_allowed;
   1.185 +	int node = pcibus_to_node(dev->bus);
   1.186 +	if (node >= 0 && node_online(node))
   1.187 +	    set_cpus_allowed(current, node_to_cpumask(node));
   1.188 +	/* And set default memory allocation policy */
   1.189 +	oldpol = current->mempolicy;
   1.190 +	current->mempolicy = &default_policy;
   1.191 +	mpol_get(current->mempolicy);
   1.192 +#endif
   1.193 +	error = drv->probe(dev, id);
   1.194 +#ifdef CONFIG_NUMA
   1.195 +	set_cpus_allowed(current, oldmask);
   1.196 +	mpol_free(current->mempolicy);
   1.197 +	current->mempolicy = oldpol;
   1.198 +#endif
   1.199 +	return error;
   1.200 +}
   1.201 +
   1.202 +/**
   1.203 + * __pci_device_probe()
   1.204 + * @drv: driver to call to check if it wants the PCI device
   1.205 + * @pci_dev: PCI device being probed
   1.206 + * 
   1.207 + * returns 0 on success, else error.
   1.208 + * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
   1.209 + */
   1.210 +static int
   1.211 +__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
   1.212 +{
   1.213 +	const struct pci_device_id *id;
   1.214 +	int error = 0;
   1.215 +
   1.216 +	if (!pci_dev->driver && drv->probe) {
   1.217 +		error = -ENODEV;
   1.218 +
   1.219 +		id = pci_match_device(drv, pci_dev);
   1.220 +		if (id)
   1.221 +			error = pci_call_probe(drv, pci_dev, id);
   1.222 +		if (error >= 0) {
   1.223 +			pci_dev->driver = drv;
   1.224 +			error = 0;
   1.225 +		}
   1.226 +	}
   1.227 +	return error;
   1.228 +}
   1.229 +
   1.230 +static int pci_device_probe(struct device * dev)
   1.231 +{
   1.232 +	int error = 0;
   1.233 +	struct pci_driver *drv;
   1.234 +	struct pci_dev *pci_dev;
   1.235 +
   1.236 +	drv = to_pci_driver(dev->driver);
   1.237 +	pci_dev = to_pci_dev(dev);
   1.238 +	pci_dev_get(pci_dev);
   1.239 +	error = __pci_device_probe(drv, pci_dev);
   1.240 +	if (error)
   1.241 +		pci_dev_put(pci_dev);
   1.242 +
   1.243 +	return error;
   1.244 +}
   1.245 +
   1.246 +static int pci_device_remove(struct device * dev)
   1.247 +{
   1.248 +	struct pci_dev * pci_dev = to_pci_dev(dev);
   1.249 +	struct pci_driver * drv = pci_dev->driver;
   1.250 +
   1.251 +	if (drv) {
   1.252 +		if (drv->remove)
   1.253 +			drv->remove(pci_dev);
   1.254 +		pci_dev->driver = NULL;
   1.255 +	}
   1.256 +
   1.257 +	/*
   1.258 +	 * We would love to complain here if pci_dev->is_enabled is set, that
   1.259 +	 * the driver should have called pci_disable_device(), but the
   1.260 +	 * unfortunate fact is there are too many odd BIOS and bridge setups
   1.261 +	 * that don't like drivers doing that all of the time.  
   1.262 +	 * Oh well, we can dream of sane hardware when we sleep, no matter how
   1.263 +	 * horrible the crap we have to deal with is when we are awake...
   1.264 +	 */
   1.265 +
   1.266 +	pci_dev_put(pci_dev);
   1.267 +	return 0;
   1.268 +}
   1.269 +
   1.270 +static int pci_device_suspend(struct device * dev, pm_message_t state)
   1.271 +{
   1.272 +	struct pci_dev * pci_dev = to_pci_dev(dev);
   1.273 +	struct pci_driver * drv = pci_dev->driver;
   1.274 +	int i = 0;
   1.275 +
   1.276 +	if (drv && drv->suspend) {
   1.277 +		i = drv->suspend(pci_dev, state);
   1.278 +		suspend_report_result(drv->suspend, i);
   1.279 +	} else {
   1.280 +		pci_save_state(pci_dev);
   1.281 +	}
   1.282 +	return i;
   1.283 +}
   1.284 +
   1.285 +
   1.286 +/*
   1.287 + * Default resume method for devices that have no driver provided resume,
   1.288 + * or not even a driver at all.
   1.289 + */
   1.290 +static int pci_default_resume(struct pci_dev *pci_dev)
   1.291 +{
   1.292 +	int retval = 0;
   1.293 +
   1.294 +	/* restore the PCI config space */
   1.295 +	pci_restore_state(pci_dev);
   1.296 +	/* if the device was enabled before suspend, reenable */
   1.297 +	if (pci_dev->is_enabled)
   1.298 +		retval = pci_enable_device(pci_dev);
   1.299 +	/* if the device was busmaster before the suspend, make it busmaster again */
   1.300 +	if (pci_dev->is_busmaster)
   1.301 +		pci_set_master(pci_dev);
   1.302 +
   1.303 +	return retval;
   1.304 +}
   1.305 +
   1.306 +static int pci_device_resume(struct device * dev)
   1.307 +{
   1.308 +	int error;
   1.309 +	struct pci_dev * pci_dev = to_pci_dev(dev);
   1.310 +	struct pci_driver * drv = pci_dev->driver;
   1.311 +
   1.312 +	if (drv && drv->resume)
   1.313 +		error = drv->resume(pci_dev);
   1.314 +	else
   1.315 +		error = pci_default_resume(pci_dev);
   1.316 +	return error;
   1.317 +}
   1.318 +
   1.319 +static void pci_device_shutdown(struct device *dev)
   1.320 +{
   1.321 +	struct pci_dev *pci_dev = to_pci_dev(dev);
   1.322 +	struct pci_driver *drv = pci_dev->driver;
   1.323 +
   1.324 +	if (drv && drv->shutdown)
   1.325 +		drv->shutdown(pci_dev);
   1.326 +}
   1.327 +
   1.328 +#define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
   1.329 +#define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
   1.330 +
   1.331 +static ssize_t
   1.332 +pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
   1.333 +{
   1.334 +	struct device_driver *driver = kobj_to_pci_driver(kobj);
   1.335 +	struct driver_attribute *dattr = attr_to_driver_attribute(attr);
   1.336 +	ssize_t ret;
   1.337 +
   1.338 +	if (!get_driver(driver))
   1.339 +		return -ENODEV;
   1.340 +
   1.341 +	ret = dattr->show ? dattr->show(driver, buf) : -EIO;
   1.342 +
   1.343 +	put_driver(driver);
   1.344 +	return ret;
   1.345 +}
   1.346 +
   1.347 +static ssize_t
   1.348 +pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
   1.349 +		      const char *buf, size_t count)
   1.350 +{
   1.351 +	struct device_driver *driver = kobj_to_pci_driver(kobj);
   1.352 +	struct driver_attribute *dattr = attr_to_driver_attribute(attr);
   1.353 +	ssize_t ret;
   1.354 +
   1.355 +	if (!get_driver(driver))
   1.356 +		return -ENODEV;
   1.357 +
   1.358 +	ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
   1.359 +
   1.360 +	put_driver(driver);
   1.361 +	return ret;
   1.362 +}
   1.363 +
   1.364 +static struct sysfs_ops pci_driver_sysfs_ops = {
   1.365 +	.show = pci_driver_attr_show,
   1.366 +	.store = pci_driver_attr_store,
   1.367 +};
   1.368 +static struct kobj_type pci_driver_kobj_type = {
   1.369 +	.sysfs_ops = &pci_driver_sysfs_ops,
   1.370 +};
   1.371 +
   1.372 +/**
   1.373 + * __pci_register_driver - register a new pci driver
   1.374 + * @drv: the driver structure to register
   1.375 + * @owner: owner module of drv
   1.376 + * 
   1.377 + * Adds the driver structure to the list of registered drivers.
   1.378 + * Returns a negative value on error, otherwise 0. 
   1.379 + * If no error occurred, the driver remains registered even if 
   1.380 + * no device was claimed during registration.
   1.381 + */
   1.382 +int __pci_register_driver(struct pci_driver *drv, struct module *owner)
   1.383 +{
   1.384 +	int error;
   1.385 +
   1.386 +	/* initialize common driver fields */
   1.387 +	drv->driver.name = drv->name;
   1.388 +	drv->driver.bus = &pci_bus_type;
   1.389 +	drv->driver.owner = owner;
   1.390 +	drv->driver.kobj.ktype = &pci_driver_kobj_type;
   1.391 +
   1.392 +	spin_lock_init(&drv->dynids.lock);
   1.393 +	INIT_LIST_HEAD(&drv->dynids.list);
   1.394 +
   1.395 +	/* register with core */
   1.396 +	error = driver_register(&drv->driver);
   1.397 +
   1.398 +	if (!error)
   1.399 +		error = pci_create_newid_file(drv);
   1.400 +
   1.401 +	return error;
   1.402 +}
   1.403 +
   1.404 +/**
   1.405 + * pci_unregister_driver - unregister a pci driver
   1.406 + * @drv: the driver structure to unregister
   1.407 + * 
   1.408 + * Deletes the driver structure from the list of registered PCI drivers,
   1.409 + * gives it a chance to clean up by calling its remove() function for
   1.410 + * each device it was responsible for, and marks those devices as
   1.411 + * driverless.
   1.412 + */
   1.413 +
   1.414 +void
   1.415 +pci_unregister_driver(struct pci_driver *drv)
   1.416 +{
   1.417 +	driver_unregister(&drv->driver);
   1.418 +	pci_free_dynids(drv);
   1.419 +}
   1.420 +
   1.421 +static struct pci_driver pci_compat_driver = {
   1.422 +	.name = "compat"
   1.423 +};
   1.424 +
   1.425 +/**
   1.426 + * pci_dev_driver - get the pci_driver of a device
   1.427 + * @dev: the device to query
   1.428 + *
   1.429 + * Returns the appropriate pci_driver structure or %NULL if there is no 
   1.430 + * registered driver for the device.
   1.431 + */
   1.432 +struct pci_driver *
   1.433 +pci_dev_driver(const struct pci_dev *dev)
   1.434 +{
   1.435 +	if (dev->driver)
   1.436 +		return dev->driver;
   1.437 +	else {
   1.438 +		int i;
   1.439 +		for(i=0; i<=PCI_ROM_RESOURCE; i++)
   1.440 +			if (dev->resource[i].flags & IORESOURCE_BUSY)
   1.441 +				return &pci_compat_driver;
   1.442 +	}
   1.443 +	return NULL;
   1.444 +}
   1.445 +
   1.446 +/**
   1.447 + * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
   1.448 + * @dev: the PCI device structure to match against
   1.449 + * @drv: the device driver to search for matching PCI device id structures
   1.450 + * 
   1.451 + * Used by a driver to check whether a PCI device present in the
   1.452 + * system is in its list of supported devices. Returns the matching
   1.453 + * pci_device_id structure or %NULL if there is no match.
   1.454 + */
   1.455 +static int pci_bus_match(struct device *dev, struct device_driver *drv)
   1.456 +{
   1.457 +	struct pci_dev *pci_dev = to_pci_dev(dev);
   1.458 +	struct pci_driver *pci_drv = to_pci_driver(drv);
   1.459 +	const struct pci_device_id *found_id;
   1.460 +
   1.461 +	found_id = pci_match_device(pci_drv, pci_dev);
   1.462 +	if (found_id)
   1.463 +		return 1;
   1.464 +
   1.465 +	return 0;
   1.466 +}
   1.467 +
   1.468 +/**
   1.469 + * pci_dev_get - increments the reference count of the pci device structure
   1.470 + * @dev: the device being referenced
   1.471 + *
   1.472 + * Each live reference to a device should be refcounted.
   1.473 + *
   1.474 + * Drivers for PCI devices should normally record such references in
   1.475 + * their probe() methods, when they bind to a device, and release
   1.476 + * them by calling pci_dev_put(), in their disconnect() methods.
   1.477 + *
   1.478 + * A pointer to the device with the incremented reference counter is returned.
   1.479 + */
   1.480 +struct pci_dev *pci_dev_get(struct pci_dev *dev)
   1.481 +{
   1.482 +	if (dev)
   1.483 +		get_device(&dev->dev);
   1.484 +	return dev;
   1.485 +}
   1.486 +
   1.487 +/**
   1.488 + * pci_dev_put - release a use of the pci device structure
   1.489 + * @dev: device that's been disconnected
   1.490 + *
   1.491 + * Must be called when a user of a device is finished with it.  When the last
   1.492 + * user of the device calls this function, the memory of the device is freed.
   1.493 + */
   1.494 +void pci_dev_put(struct pci_dev *dev)
   1.495 +{
   1.496 +	if (dev)
   1.497 +		put_device(&dev->dev);
   1.498 +}
   1.499 +
   1.500 +#ifndef CONFIG_HOTPLUG
   1.501 +int pci_uevent(struct device *dev, char **envp, int num_envp,
   1.502 +	       char *buffer, int buffer_size)
   1.503 +{
   1.504 +	return -ENODEV;
   1.505 +}
   1.506 +#endif
   1.507 +
   1.508 +struct bus_type pci_bus_type = {
   1.509 +	.name		= "pci",
   1.510 +	.match		= pci_bus_match,
   1.511 +	.uevent		= pci_uevent,
   1.512 +	.probe		= pci_device_probe,
   1.513 +	.remove		= pci_device_remove,
   1.514 +	.suspend	= pci_device_suspend,
   1.515 +	.shutdown	= pci_device_shutdown,
   1.516 +	.resume		= pci_device_resume,
   1.517 +	.dev_attrs	= pci_dev_attrs,
   1.518 +};
   1.519 +
   1.520 +static int __init pci_driver_init(void)
   1.521 +{
   1.522 +	return bus_register(&pci_bus_type);
   1.523 +}
   1.524 +
   1.525 +postcore_initcall(pci_driver_init);
   1.526 +
   1.527 +EXPORT_SYMBOL(pci_match_id);
   1.528 +EXPORT_SYMBOL(pci_match_device);
   1.529 +EXPORT_SYMBOL(__pci_register_driver);
   1.530 +EXPORT_SYMBOL(pci_unregister_driver);
   1.531 +EXPORT_SYMBOL(pci_dev_driver);
   1.532 +EXPORT_SYMBOL(pci_bus_type);
   1.533 +EXPORT_SYMBOL(pci_dev_get);
   1.534 +EXPORT_SYMBOL(pci_dev_put);