linux/drivers/pci/pci-driver.c
<<
>>
Prefs
   1/*
   2 * drivers/pci/pci-driver.c
   3 *
   4 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
   5 * (C) Copyright 2007 Novell Inc.
   6 *
   7 * Released under the GPL v2 only.
   8 *
   9 */
  10
  11#include <linux/pci.h>
  12#include <linux/module.h>
  13#include <linux/init.h>
  14#include <linux/device.h>
  15#include <linux/mempolicy.h>
  16#include <linux/string.h>
  17#include <linux/slab.h>
  18#include <linux/sched.h>
  19#include <linux/cpu.h>
  20#include <linux/pm_runtime.h>
  21#include <linux/suspend.h>
  22#include "pci.h"
  23
  24struct pci_dynid {
  25        struct list_head node;
  26        struct pci_device_id id;
  27};
  28
  29/**
  30 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  31 * @drv: target pci driver
  32 * @vendor: PCI vendor ID
  33 * @device: PCI device ID
  34 * @subvendor: PCI subvendor ID
  35 * @subdevice: PCI subdevice ID
  36 * @class: PCI class
  37 * @class_mask: PCI class mask
  38 * @driver_data: private driver data
  39 *
  40 * Adds a new dynamic pci device ID to this driver and causes the
  41 * driver to probe for all devices again.  @drv must have been
  42 * registered prior to calling this function.
  43 *
  44 * CONTEXT:
  45 * Does GFP_KERNEL allocation.
  46 *
  47 * RETURNS:
  48 * 0 on success, -errno on failure.
  49 */
  50int pci_add_dynid(struct pci_driver *drv,
  51                  unsigned int vendor, unsigned int device,
  52                  unsigned int subvendor, unsigned int subdevice,
  53                  unsigned int class, unsigned int class_mask,
  54                  unsigned long driver_data)
  55{
  56        struct pci_dynid *dynid;
  57        int retval;
  58
  59        dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  60        if (!dynid)
  61                return -ENOMEM;
  62
  63        dynid->id.vendor = vendor;
  64        dynid->id.device = device;
  65        dynid->id.subvendor = subvendor;
  66        dynid->id.subdevice = subdevice;
  67        dynid->id.class = class;
  68        dynid->id.class_mask = class_mask;
  69        dynid->id.driver_data = driver_data;
  70
  71        spin_lock(&drv->dynids.lock);
  72        list_add_tail(&dynid->node, &drv->dynids.list);
  73        spin_unlock(&drv->dynids.lock);
  74
  75        retval = driver_attach(&drv->driver);
  76
  77        return retval;
  78}
  79
  80static void pci_free_dynids(struct pci_driver *drv)
  81{
  82        struct pci_dynid *dynid, *n;
  83
  84        spin_lock(&drv->dynids.lock);
  85        list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  86                list_del(&dynid->node);
  87                kfree(dynid);
  88        }
  89        spin_unlock(&drv->dynids.lock);
  90}
  91
  92/*
  93 * Dynamic device ID manipulation via sysfs is disabled for !CONFIG_HOTPLUG
  94 */
  95#ifdef CONFIG_HOTPLUG
  96/**
  97 * store_new_id - sysfs frontend to pci_add_dynid()
  98 * @driver: target device driver
  99 * @buf: buffer for scanning device ID data
 100 * @count: input size
 101 *
 102 * Allow PCI IDs to be added to an existing driver via sysfs.
 103 */
 104static ssize_t
 105store_new_id(struct device_driver *driver, const char *buf, size_t count)
 106{
 107        struct pci_driver *pdrv = to_pci_driver(driver);
 108        const struct pci_device_id *ids = pdrv->id_table;
 109        __u32 vendor, device, subvendor=PCI_ANY_ID,
 110                subdevice=PCI_ANY_ID, class=0, class_mask=0;
 111        unsigned long driver_data=0;
 112        int fields=0;
 113        int retval;
 114
 115        fields = sscanf(buf, "%x %x %x %x %x %x %lx",
 116                        &vendor, &device, &subvendor, &subdevice,
 117                        &class, &class_mask, &driver_data);
 118        if (fields < 2)
 119                return -EINVAL;
 120
 121        /* Only accept driver_data values that match an existing id_table
 122           entry */
 123        if (ids) {
 124                retval = -EINVAL;
 125                while (ids->vendor || ids->subvendor || ids->class_mask) {
 126                        if (driver_data == ids->driver_data) {
 127                                retval = 0;
 128                                break;
 129                        }
 130                        ids++;
 131                }
 132                if (retval)     /* No match */
 133                        return retval;
 134        }
 135
 136        retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
 137                               class, class_mask, driver_data);
 138        if (retval)
 139                return retval;
 140        return count;
 141}
 142static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
 143
 144/**
 145 * store_remove_id - remove a PCI device ID from this driver
 146 * @driver: target device driver
 147 * @buf: buffer for scanning device ID data
 148 * @count: input size
 149 *
 150 * Removes a dynamic pci device ID to this driver.
 151 */
 152static ssize_t
 153store_remove_id(struct device_driver *driver, const char *buf, size_t count)
 154{
 155        struct pci_dynid *dynid, *n;
 156        struct pci_driver *pdrv = to_pci_driver(driver);
 157        __u32 vendor, device, subvendor = PCI_ANY_ID,
 158                subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
 159        int fields = 0;
 160        int retval = -ENODEV;
 161
 162        fields = sscanf(buf, "%x %x %x %x %x %x",
 163                        &vendor, &device, &subvendor, &subdevice,
 164                        &class, &class_mask);
 165        if (fields < 2)
 166                return -EINVAL;
 167
 168        spin_lock(&pdrv->dynids.lock);
 169        list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
 170                struct pci_device_id *id = &dynid->id;
 171                if ((id->vendor == vendor) &&
 172                    (id->device == device) &&
 173                    (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
 174                    (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
 175                    !((id->class ^ class) & class_mask)) {
 176                        list_del(&dynid->node);
 177                        kfree(dynid);
 178                        retval = 0;
 179                        break;
 180                }
 181        }
 182        spin_unlock(&pdrv->dynids.lock);
 183
 184        if (retval)
 185                return retval;
 186        return count;
 187}
 188static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
 189
 190static int
 191pci_create_newid_files(struct pci_driver *drv)
 192{
 193        int error = 0;
 194
 195        if (drv->probe != NULL) {
 196                error = driver_create_file(&drv->driver, &driver_attr_new_id);
 197                if (error == 0) {
 198                        error = driver_create_file(&drv->driver,
 199                                        &driver_attr_remove_id);
 200                        if (error)
 201                                driver_remove_file(&drv->driver,
 202                                                &driver_attr_new_id);
 203                }
 204        }
 205        return error;
 206}
 207
 208static void pci_remove_newid_files(struct pci_driver *drv)
 209{
 210        driver_remove_file(&drv->driver, &driver_attr_remove_id);
 211        driver_remove_file(&drv->driver, &driver_attr_new_id);
 212}
 213#else /* !CONFIG_HOTPLUG */
 214static inline int pci_create_newid_files(struct pci_driver *drv)
 215{
 216        return 0;
 217}
 218static inline void pci_remove_newid_files(struct pci_driver *drv) {}
 219#endif
 220
 221/**
 222 * pci_match_id - See if a pci device matches a given pci_id table
 223 * @ids: array of PCI device id structures to search in
 224 * @dev: the PCI device structure to match against.
 225 *
 226 * Used by a driver to check whether a PCI device present in the
 227 * system is in its list of supported devices.  Returns the matching
 228 * pci_device_id structure or %NULL if there is no match.
 229 *
 230 * Deprecated, don't use this as it will not catch any dynamic ids
 231 * that a driver might want to check for.
 232 */
 233const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
 234                                         struct pci_dev *dev)
 235{
 236        if (ids) {
 237                while (ids->vendor || ids->subvendor || ids->class_mask) {
 238                        if (pci_match_one_device(ids, dev))
 239                                return ids;
 240                        ids++;
 241                }
 242        }
 243        return NULL;
 244}
 245
 246/**
 247 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
 248 * @drv: the PCI driver to match against
 249 * @dev: the PCI device structure to match against
 250 *
 251 * Used by a driver to check whether a PCI device present in the
 252 * system is in its list of supported devices.  Returns the matching
 253 * pci_device_id structure or %NULL if there is no match.
 254 */
 255static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
 256                                                    struct pci_dev *dev)
 257{
 258        struct pci_dynid *dynid;
 259
 260        /* Look at the dynamic ids first, before the static ones */
 261        spin_lock(&drv->dynids.lock);
 262        list_for_each_entry(dynid, &drv->dynids.list, node) {
 263                if (pci_match_one_device(&dynid->id, dev)) {
 264                        spin_unlock(&drv->dynids.lock);
 265                        return &dynid->id;
 266                }
 267        }
 268        spin_unlock(&drv->dynids.lock);
 269
 270        return pci_match_id(drv->id_table, dev);
 271}
 272
 273struct drv_dev_and_id {
 274        struct pci_driver *drv;
 275        struct pci_dev *dev;
 276        const struct pci_device_id *id;
 277};
 278
 279static long local_pci_probe(void *_ddi)
 280{
 281        struct drv_dev_and_id *ddi = _ddi;
 282        struct device *dev = &ddi->dev->dev;
 283        struct device *parent = dev->parent;
 284        int rc;
 285
 286        /* The parent bridge must be in active state when probing */
 287        if (parent)
 288                pm_runtime_get_sync(parent);
 289        /* Unbound PCI devices are always set to disabled and suspended.
 290         * During probe, the device is set to enabled and active and the
 291         * usage count is incremented.  If the driver supports runtime PM,
 292         * it should call pm_runtime_put_noidle() in its probe routine and
 293         * pm_runtime_get_noresume() in its remove routine.
 294         */
 295        pm_runtime_get_noresume(dev);
 296        pm_runtime_set_active(dev);
 297        pm_runtime_enable(dev);
 298
 299        rc = ddi->drv->probe(ddi->dev, ddi->id);
 300        if (rc) {
 301                pm_runtime_disable(dev);
 302                pm_runtime_set_suspended(dev);
 303                pm_runtime_put_noidle(dev);
 304        }
 305        if (parent)
 306                pm_runtime_put(parent);
 307        return rc;
 308}
 309
 310static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 311                          const struct pci_device_id *id)
 312{
 313        int error, node;
 314        struct drv_dev_and_id ddi = { drv, dev, id };
 315
 316        /* Execute driver initialization on node where the device's
 317           bus is attached to.  This way the driver likely allocates
 318           its local memory on the right node without any need to
 319           change it. */
 320        node = dev_to_node(&dev->dev);
 321        if (node >= 0) {
 322                int cpu;
 323
 324                get_online_cpus();
 325                cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
 326                if (cpu < nr_cpu_ids)
 327                        error = work_on_cpu(cpu, local_pci_probe, &ddi);
 328                else
 329                        error = local_pci_probe(&ddi);
 330                put_online_cpus();
 331        } else
 332                error = local_pci_probe(&ddi);
 333        return error;
 334}
 335
 336/**
 337 * __pci_device_probe - check if a driver wants to claim a specific PCI device
 338 * @drv: driver to call to check if it wants the PCI device
 339 * @pci_dev: PCI device being probed
 340 * 
 341 * returns 0 on success, else error.
 342 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
 343 */
 344static int
 345__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
 346{
 347        const struct pci_device_id *id;
 348        int error = 0;
 349
 350        if (!pci_dev->driver && drv->probe) {
 351                error = -ENODEV;
 352
 353                id = pci_match_device(drv, pci_dev);
 354                if (id)
 355                        error = pci_call_probe(drv, pci_dev, id);
 356                if (error >= 0) {
 357                        pci_dev->driver = drv;
 358                        error = 0;
 359                }
 360        }
 361        return error;
 362}
 363
 364static int pci_device_probe(struct device * dev)
 365{
 366        int error = 0;
 367        struct pci_driver *drv;
 368        struct pci_dev *pci_dev;
 369
 370        drv = to_pci_driver(dev->driver);
 371        pci_dev = to_pci_dev(dev);
 372        pci_dev_get(pci_dev);
 373        error = __pci_device_probe(drv, pci_dev);
 374        if (error)
 375                pci_dev_put(pci_dev);
 376
 377        return error;
 378}
 379
 380static int pci_device_remove(struct device * dev)
 381{
 382        struct pci_dev * pci_dev = to_pci_dev(dev);
 383        struct pci_driver * drv = pci_dev->driver;
 384
 385        if (drv) {
 386                if (drv->remove) {
 387                        pm_runtime_get_sync(dev);
 388                        drv->remove(pci_dev);
 389                        pm_runtime_put_noidle(dev);
 390                }
 391                pci_dev->driver = NULL;
 392        }
 393
 394        /* Undo the runtime PM settings in local_pci_probe() */
 395        pm_runtime_disable(dev);
 396        pm_runtime_set_suspended(dev);
 397        pm_runtime_put_noidle(dev);
 398
 399        /*
 400         * If the device is still on, set the power state as "unknown",
 401         * since it might change by the next time we load the driver.
 402         */
 403        if (pci_dev->current_state == PCI_D0)
 404                pci_dev->current_state = PCI_UNKNOWN;
 405
 406        /*
 407         * We would love to complain here if pci_dev->is_enabled is set, that
 408         * the driver should have called pci_disable_device(), but the
 409         * unfortunate fact is there are too many odd BIOS and bridge setups
 410         * that don't like drivers doing that all of the time.  
 411         * Oh well, we can dream of sane hardware when we sleep, no matter how
 412         * horrible the crap we have to deal with is when we are awake...
 413         */
 414
 415        pci_dev_put(pci_dev);
 416        return 0;
 417}
 418
 419static void pci_device_shutdown(struct device *dev)
 420{
 421        struct pci_dev *pci_dev = to_pci_dev(dev);
 422        struct pci_driver *drv = pci_dev->driver;
 423
 424        pm_runtime_resume(dev);
 425
 426        if (drv && drv->shutdown)
 427                drv->shutdown(pci_dev);
 428        pci_msi_shutdown(pci_dev);
 429        pci_msix_shutdown(pci_dev);
 430
 431        /*
 432         * Turn off Bus Master bit on the device to tell it to not
 433         * continue to do DMA
 434         */
 435        pci_disable_device(pci_dev);
 436}
 437
 438#ifdef CONFIG_PM
 439
 440/* Auxiliary functions used for system resume and run-time resume. */
 441
 442/**
 443 * pci_restore_standard_config - restore standard config registers of PCI device
 444 * @pci_dev: PCI device to handle
 445 */
 446static int pci_restore_standard_config(struct pci_dev *pci_dev)
 447{
 448        pci_update_current_state(pci_dev, PCI_UNKNOWN);
 449
 450        if (pci_dev->current_state != PCI_D0) {
 451                int error = pci_set_power_state(pci_dev, PCI_D0);
 452                if (error)
 453                        return error;
 454        }
 455
 456        pci_restore_state(pci_dev);
 457        return 0;
 458}
 459
 460#endif
 461
 462#ifdef CONFIG_PM_SLEEP
 463
 464static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
 465{
 466        pci_power_up(pci_dev);
 467        pci_restore_state(pci_dev);
 468        pci_fixup_device(pci_fixup_resume_early, pci_dev);
 469}
 470
 471/*
 472 * Default "suspend" method for devices that have no driver provided suspend,
 473 * or not even a driver at all (second part).
 474 */
 475static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
 476{
 477        /*
 478         * mark its power state as "unknown", since we don't know if
 479         * e.g. the BIOS will change its device state when we suspend.
 480         */
 481        if (pci_dev->current_state == PCI_D0)
 482                pci_dev->current_state = PCI_UNKNOWN;
 483}
 484
 485/*
 486 * Default "resume" method for devices that have no driver provided resume,
 487 * or not even a driver at all (second part).
 488 */
 489static int pci_pm_reenable_device(struct pci_dev *pci_dev)
 490{
 491        int retval;
 492
 493        /* if the device was enabled before suspend, reenable */
 494        retval = pci_reenable_device(pci_dev);
 495        /*
 496         * if the device was busmaster before the suspend, make it busmaster
 497         * again
 498         */
 499        if (pci_dev->is_busmaster)
 500                pci_set_master(pci_dev);
 501
 502        return retval;
 503}
 504
 505static int pci_legacy_suspend(struct device *dev, pm_message_t state)
 506{
 507        struct pci_dev * pci_dev = to_pci_dev(dev);
 508        struct pci_driver * drv = pci_dev->driver;
 509
 510        if (drv && drv->suspend) {
 511                pci_power_t prev = pci_dev->current_state;
 512                int error;
 513
 514                error = drv->suspend(pci_dev, state);
 515                suspend_report_result(drv->suspend, error);
 516                if (error)
 517                        return error;
 518
 519                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 520                    && pci_dev->current_state != PCI_UNKNOWN) {
 521                        WARN_ONCE(pci_dev->current_state != prev,
 522                                "PCI PM: Device state not saved by %pF\n",
 523                                drv->suspend);
 524                }
 525        }
 526
 527        pci_fixup_device(pci_fixup_suspend, pci_dev);
 528
 529        return 0;
 530}
 531
 532static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
 533{
 534        struct pci_dev * pci_dev = to_pci_dev(dev);
 535        struct pci_driver * drv = pci_dev->driver;
 536
 537        if (drv && drv->suspend_late) {
 538                pci_power_t prev = pci_dev->current_state;
 539                int error;
 540
 541                error = drv->suspend_late(pci_dev, state);
 542                suspend_report_result(drv->suspend_late, error);
 543                if (error)
 544                        return error;
 545
 546                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 547                    && pci_dev->current_state != PCI_UNKNOWN) {
 548                        WARN_ONCE(pci_dev->current_state != prev,
 549                                "PCI PM: Device state not saved by %pF\n",
 550                                drv->suspend_late);
 551                        return 0;
 552                }
 553        }
 554
 555        if (!pci_dev->state_saved)
 556                pci_save_state(pci_dev);
 557
 558        pci_pm_set_unknown_state(pci_dev);
 559
 560        return 0;
 561}
 562
 563static int pci_legacy_resume_early(struct device *dev)
 564{
 565        struct pci_dev * pci_dev = to_pci_dev(dev);
 566        struct pci_driver * drv = pci_dev->driver;
 567
 568        return drv && drv->resume_early ?
 569                        drv->resume_early(pci_dev) : 0;
 570}
 571
 572static int pci_legacy_resume(struct device *dev)
 573{
 574        struct pci_dev * pci_dev = to_pci_dev(dev);
 575        struct pci_driver * drv = pci_dev->driver;
 576
 577        pci_fixup_device(pci_fixup_resume, pci_dev);
 578
 579        return drv && drv->resume ?
 580                        drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
 581}
 582
 583/* Auxiliary functions used by the new power management framework */
 584
 585static void pci_pm_default_resume(struct pci_dev *pci_dev)
 586{
 587        pci_fixup_device(pci_fixup_resume, pci_dev);
 588
 589        if (!pci_is_bridge(pci_dev))
 590                pci_enable_wake(pci_dev, PCI_D0, false);
 591}
 592
 593static void pci_pm_default_suspend(struct pci_dev *pci_dev)
 594{
 595        /* Disable non-bridge devices without PM support */
 596        if (!pci_is_bridge(pci_dev))
 597                pci_disable_enabled_device(pci_dev);
 598}
 599
 600static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
 601{
 602        struct pci_driver *drv = pci_dev->driver;
 603        bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
 604                || drv->resume_early);
 605
 606        /*
 607         * Legacy PM support is used by default, so warn if the new framework is
 608         * supported as well.  Drivers are supposed to support either the
 609         * former, or the latter, but not both at the same time.
 610         */
 611        WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
 612                drv->name, pci_dev->vendor, pci_dev->device);
 613
 614        return ret;
 615}
 616
 617/* New power management framework */
 618
 619static int pci_pm_prepare(struct device *dev)
 620{
 621        struct device_driver *drv = dev->driver;
 622        int error = 0;
 623
 624        /*
 625         * If a PCI device configured to wake up the system from sleep states
 626         * has been suspended at run time and there's a resume request pending
 627         * for it, this is equivalent to the device signaling wakeup, so the
 628         * system suspend operation should be aborted.
 629         */
 630        pm_runtime_get_noresume(dev);
 631        if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
 632                pm_wakeup_event(dev, 0);
 633
 634        if (pm_wakeup_pending()) {
 635                pm_runtime_put_sync(dev);
 636                return -EBUSY;
 637        }
 638
 639        /*
 640         * PCI devices suspended at run time need to be resumed at this
 641         * point, because in general it is necessary to reconfigure them for
 642         * system suspend.  Namely, if the device is supposed to wake up the
 643         * system from the sleep state, we may need to reconfigure it for this
 644         * purpose.  In turn, if the device is not supposed to wake up the
 645         * system from the sleep state, we'll have to prevent it from signaling
 646         * wake-up.
 647         */
 648        pm_runtime_resume(dev);
 649
 650        if (drv && drv->pm && drv->pm->prepare)
 651                error = drv->pm->prepare(dev);
 652
 653        return error;
 654}
 655
 656static void pci_pm_complete(struct device *dev)
 657{
 658        struct device_driver *drv = dev->driver;
 659
 660        if (drv && drv->pm && drv->pm->complete)
 661                drv->pm->complete(dev);
 662
 663        pm_runtime_put_sync(dev);
 664}
 665
 666#else /* !CONFIG_PM_SLEEP */
 667
 668#define pci_pm_prepare  NULL
 669#define pci_pm_complete NULL
 670
 671#endif /* !CONFIG_PM_SLEEP */
 672
 673#ifdef CONFIG_SUSPEND
 674
 675static int pci_pm_suspend(struct device *dev)
 676{
 677        struct pci_dev *pci_dev = to_pci_dev(dev);
 678        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 679
 680        if (pci_has_legacy_pm_support(pci_dev))
 681                return pci_legacy_suspend(dev, PMSG_SUSPEND);
 682
 683        if (!pm) {
 684                pci_pm_default_suspend(pci_dev);
 685                goto Fixup;
 686        }
 687
 688        if (pm->suspend) {
 689                pci_power_t prev = pci_dev->current_state;
 690                int error;
 691
 692                error = pm->suspend(dev);
 693                suspend_report_result(pm->suspend, error);
 694                if (error)
 695                        return error;
 696
 697                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 698                    && pci_dev->current_state != PCI_UNKNOWN) {
 699                        WARN_ONCE(pci_dev->current_state != prev,
 700                                "PCI PM: State of device not saved by %pF\n",
 701                                pm->suspend);
 702                }
 703        }
 704
 705 Fixup:
 706        pci_fixup_device(pci_fixup_suspend, pci_dev);
 707
 708        return 0;
 709}
 710
 711static int pci_pm_suspend_noirq(struct device *dev)
 712{
 713        struct pci_dev *pci_dev = to_pci_dev(dev);
 714        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 715
 716        if (pci_has_legacy_pm_support(pci_dev))
 717                return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
 718
 719        if (!pm) {
 720                pci_save_state(pci_dev);
 721                return 0;
 722        }
 723
 724        if (pm->suspend_noirq) {
 725                pci_power_t prev = pci_dev->current_state;
 726                int error;
 727
 728                error = pm->suspend_noirq(dev);
 729                suspend_report_result(pm->suspend_noirq, error);
 730                if (error)
 731                        return error;
 732
 733                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 734                    && pci_dev->current_state != PCI_UNKNOWN) {
 735                        WARN_ONCE(pci_dev->current_state != prev,
 736                                "PCI PM: State of device not saved by %pF\n",
 737                                pm->suspend_noirq);
 738                        return 0;
 739                }
 740        }
 741
 742        if (!pci_dev->state_saved) {
 743                pci_save_state(pci_dev);
 744                if (!pci_is_bridge(pci_dev))
 745                        pci_prepare_to_sleep(pci_dev);
 746        }
 747
 748        pci_pm_set_unknown_state(pci_dev);
 749
 750        /*
 751         * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
 752         * PCI COMMAND register isn't 0, the BIOS assumes that the controller
 753         * hasn't been quiesced and tries to turn it off.  If the controller
 754         * is already in D3, this can hang or cause memory corruption.
 755         *
 756         * Since the value of the COMMAND register doesn't matter once the
 757         * device has been suspended, we can safely set it to 0 here.
 758         */
 759        if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
 760                pci_write_config_word(pci_dev, PCI_COMMAND, 0);
 761
 762        return 0;
 763}
 764
 765static int pci_pm_resume_noirq(struct device *dev)
 766{
 767        struct pci_dev *pci_dev = to_pci_dev(dev);
 768        struct device_driver *drv = dev->driver;
 769        int error = 0;
 770
 771        pci_pm_default_resume_early(pci_dev);
 772
 773        if (pci_has_legacy_pm_support(pci_dev))
 774                return pci_legacy_resume_early(dev);
 775
 776        if (drv && drv->pm && drv->pm->resume_noirq)
 777                error = drv->pm->resume_noirq(dev);
 778
 779        return error;
 780}
 781
 782static int pci_pm_resume(struct device *dev)
 783{
 784        struct pci_dev *pci_dev = to_pci_dev(dev);
 785        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 786        int error = 0;
 787
 788        /*
 789         * This is necessary for the suspend error path in which resume is
 790         * called without restoring the standard config registers of the device.
 791         */
 792        if (pci_dev->state_saved)
 793                pci_restore_standard_config(pci_dev);
 794
 795        if (pci_has_legacy_pm_support(pci_dev))
 796                return pci_legacy_resume(dev);
 797
 798        pci_pm_default_resume(pci_dev);
 799
 800        if (pm) {
 801                if (pm->resume)
 802                        error = pm->resume(dev);
 803        } else {
 804                pci_pm_reenable_device(pci_dev);
 805        }
 806
 807        return error;
 808}
 809
 810#else /* !CONFIG_SUSPEND */
 811
 812#define pci_pm_suspend          NULL
 813#define pci_pm_suspend_noirq    NULL
 814#define pci_pm_resume           NULL
 815#define pci_pm_resume_noirq     NULL
 816
 817#endif /* !CONFIG_SUSPEND */
 818
 819#ifdef CONFIG_HIBERNATE_CALLBACKS
 820
 821static int pci_pm_freeze(struct device *dev)
 822{
 823        struct pci_dev *pci_dev = to_pci_dev(dev);
 824        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 825
 826        if (pci_has_legacy_pm_support(pci_dev))
 827                return pci_legacy_suspend(dev, PMSG_FREEZE);
 828
 829        if (!pm) {
 830                pci_pm_default_suspend(pci_dev);
 831                return 0;
 832        }
 833
 834        if (pm->freeze) {
 835                int error;
 836
 837                error = pm->freeze(dev);
 838                suspend_report_result(pm->freeze, error);
 839                if (error)
 840                        return error;
 841        }
 842
 843        return 0;
 844}
 845
 846static int pci_pm_freeze_noirq(struct device *dev)
 847{
 848        struct pci_dev *pci_dev = to_pci_dev(dev);
 849        struct device_driver *drv = dev->driver;
 850
 851        if (pci_has_legacy_pm_support(pci_dev))
 852                return pci_legacy_suspend_late(dev, PMSG_FREEZE);
 853
 854        if (drv && drv->pm && drv->pm->freeze_noirq) {
 855                int error;
 856
 857                error = drv->pm->freeze_noirq(dev);
 858                suspend_report_result(drv->pm->freeze_noirq, error);
 859                if (error)
 860                        return error;
 861        }
 862
 863        if (!pci_dev->state_saved)
 864                pci_save_state(pci_dev);
 865
 866        pci_pm_set_unknown_state(pci_dev);
 867
 868        return 0;
 869}
 870
 871static int pci_pm_thaw_noirq(struct device *dev)
 872{
 873        struct pci_dev *pci_dev = to_pci_dev(dev);
 874        struct device_driver *drv = dev->driver;
 875        int error = 0;
 876
 877        if (pci_has_legacy_pm_support(pci_dev))
 878                return pci_legacy_resume_early(dev);
 879
 880        pci_update_current_state(pci_dev, PCI_D0);
 881
 882        if (drv && drv->pm && drv->pm->thaw_noirq)
 883                error = drv->pm->thaw_noirq(dev);
 884
 885        return error;
 886}
 887
 888static int pci_pm_thaw(struct device *dev)
 889{
 890        struct pci_dev *pci_dev = to_pci_dev(dev);
 891        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 892        int error = 0;
 893
 894        if (pci_has_legacy_pm_support(pci_dev))
 895                return pci_legacy_resume(dev);
 896
 897        if (pm) {
 898                if (pm->thaw)
 899                        error = pm->thaw(dev);
 900        } else {
 901                pci_pm_reenable_device(pci_dev);
 902        }
 903
 904        pci_dev->state_saved = false;
 905
 906        return error;
 907}
 908
 909static int pci_pm_poweroff(struct device *dev)
 910{
 911        struct pci_dev *pci_dev = to_pci_dev(dev);
 912        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 913
 914        if (pci_has_legacy_pm_support(pci_dev))
 915                return pci_legacy_suspend(dev, PMSG_HIBERNATE);
 916
 917        if (!pm) {
 918                pci_pm_default_suspend(pci_dev);
 919                goto Fixup;
 920        }
 921
 922        if (pm->poweroff) {
 923                int error;
 924
 925                error = pm->poweroff(dev);
 926                suspend_report_result(pm->poweroff, error);
 927                if (error)
 928                        return error;
 929        }
 930
 931 Fixup:
 932        pci_fixup_device(pci_fixup_suspend, pci_dev);
 933
 934        return 0;
 935}
 936
 937static int pci_pm_poweroff_noirq(struct device *dev)
 938{
 939        struct pci_dev *pci_dev = to_pci_dev(dev);
 940        struct device_driver *drv = dev->driver;
 941
 942        if (pci_has_legacy_pm_support(to_pci_dev(dev)))
 943                return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
 944
 945        if (!drv || !drv->pm)
 946                return 0;
 947
 948        if (drv->pm->poweroff_noirq) {
 949                int error;
 950
 951                error = drv->pm->poweroff_noirq(dev);
 952                suspend_report_result(drv->pm->poweroff_noirq, error);
 953                if (error)
 954                        return error;
 955        }
 956
 957        if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
 958                pci_prepare_to_sleep(pci_dev);
 959
 960        /*
 961         * The reason for doing this here is the same as for the analogous code
 962         * in pci_pm_suspend_noirq().
 963         */
 964        if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
 965                pci_write_config_word(pci_dev, PCI_COMMAND, 0);
 966
 967        return 0;
 968}
 969
 970static int pci_pm_restore_noirq(struct device *dev)
 971{
 972        struct pci_dev *pci_dev = to_pci_dev(dev);
 973        struct device_driver *drv = dev->driver;
 974        int error = 0;
 975
 976        pci_pm_default_resume_early(pci_dev);
 977
 978        if (pci_has_legacy_pm_support(pci_dev))
 979                return pci_legacy_resume_early(dev);
 980
 981        if (drv && drv->pm && drv->pm->restore_noirq)
 982                error = drv->pm->restore_noirq(dev);
 983
 984        return error;
 985}
 986
 987static int pci_pm_restore(struct device *dev)
 988{
 989        struct pci_dev *pci_dev = to_pci_dev(dev);
 990        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 991        int error = 0;
 992
 993        /*
 994         * This is necessary for the hibernation error path in which restore is
 995         * called without restoring the standard config registers of the device.
 996         */
 997        if (pci_dev->state_saved)
 998                pci_restore_standard_config(pci_dev);
 999
1000        if (pci_has_legacy_pm_support(pci_dev))
1001                return pci_legacy_resume(dev);
1002
1003        pci_pm_default_resume(pci_dev);
1004
1005        if (pm) {
1006                if (pm->restore)
1007                        error = pm->restore(dev);
1008        } else {
1009                pci_pm_reenable_device(pci_dev);
1010        }
1011
1012        return error;
1013}
1014
1015#else /* !CONFIG_HIBERNATE_CALLBACKS */
1016
1017#define pci_pm_freeze           NULL
1018#define pci_pm_freeze_noirq     NULL
1019#define pci_pm_thaw             NULL
1020#define pci_pm_thaw_noirq       NULL
1021#define pci_pm_poweroff         NULL
1022#define pci_pm_poweroff_noirq   NULL
1023#define pci_pm_restore          NULL
1024#define pci_pm_restore_noirq    NULL
1025
1026#endif /* !CONFIG_HIBERNATE_CALLBACKS */
1027
1028#ifdef CONFIG_PM_RUNTIME
1029
1030static int pci_pm_runtime_suspend(struct device *dev)
1031{
1032        struct pci_dev *pci_dev = to_pci_dev(dev);
1033        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1034        pci_power_t prev = pci_dev->current_state;
1035        int error;
1036
1037        if (!pm || !pm->runtime_suspend)
1038                return -ENOSYS;
1039
1040        pci_dev->no_d3cold = false;
1041        error = pm->runtime_suspend(dev);
1042        suspend_report_result(pm->runtime_suspend, error);
1043        if (error)
1044                return error;
1045        if (!pci_dev->d3cold_allowed)
1046                pci_dev->no_d3cold = true;
1047
1048        pci_fixup_device(pci_fixup_suspend, pci_dev);
1049
1050        if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
1051            && pci_dev->current_state != PCI_UNKNOWN) {
1052                WARN_ONCE(pci_dev->current_state != prev,
1053                        "PCI PM: State of device not saved by %pF\n",
1054                        pm->runtime_suspend);
1055                return 0;
1056        }
1057
1058        if (!pci_dev->state_saved)
1059                pci_save_state(pci_dev);
1060
1061        pci_finish_runtime_suspend(pci_dev);
1062
1063        return 0;
1064}
1065
1066static int pci_pm_runtime_resume(struct device *dev)
1067{
1068        int rc;
1069        struct pci_dev *pci_dev = to_pci_dev(dev);
1070        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1071
1072        if (!pm || !pm->runtime_resume)
1073                return -ENOSYS;
1074
1075        pci_restore_standard_config(pci_dev);
1076        pci_fixup_device(pci_fixup_resume_early, pci_dev);
1077        __pci_enable_wake(pci_dev, PCI_D0, true, false);
1078        pci_fixup_device(pci_fixup_resume, pci_dev);
1079
1080        rc = pm->runtime_resume(dev);
1081
1082        pci_dev->runtime_d3cold = false;
1083
1084        return rc;
1085}
1086
1087static int pci_pm_runtime_idle(struct device *dev)
1088{
1089        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1090
1091        if (!pm)
1092                return -ENOSYS;
1093
1094        if (pm->runtime_idle) {
1095                int ret = pm->runtime_idle(dev);
1096                if (ret)
1097                        return ret;
1098        }
1099
1100        pm_runtime_suspend(dev);
1101
1102        return 0;
1103}
1104
1105#else /* !CONFIG_PM_RUNTIME */
1106
1107#define pci_pm_runtime_suspend  NULL
1108#define pci_pm_runtime_resume   NULL
1109#define pci_pm_runtime_idle     NULL
1110
1111#endif /* !CONFIG_PM_RUNTIME */
1112
1113#ifdef CONFIG_PM
1114
1115const struct dev_pm_ops pci_dev_pm_ops = {
1116        .prepare = pci_pm_prepare,
1117        .complete = pci_pm_complete,
1118        .suspend = pci_pm_suspend,
1119        .resume = pci_pm_resume,
1120        .freeze = pci_pm_freeze,
1121        .thaw = pci_pm_thaw,
1122        .poweroff = pci_pm_poweroff,
1123        .restore = pci_pm_restore,
1124        .suspend_noirq = pci_pm_suspend_noirq,
1125        .resume_noirq = pci_pm_resume_noirq,
1126        .freeze_noirq = pci_pm_freeze_noirq,
1127        .thaw_noirq = pci_pm_thaw_noirq,
1128        .poweroff_noirq = pci_pm_poweroff_noirq,
1129        .restore_noirq = pci_pm_restore_noirq,
1130        .runtime_suspend = pci_pm_runtime_suspend,
1131        .runtime_resume = pci_pm_runtime_resume,
1132        .runtime_idle = pci_pm_runtime_idle,
1133};
1134
1135#define PCI_PM_OPS_PTR  (&pci_dev_pm_ops)
1136
1137#else /* !COMFIG_PM_OPS */
1138
1139#define PCI_PM_OPS_PTR  NULL
1140
1141#endif /* !COMFIG_PM_OPS */
1142
1143/**
1144 * __pci_register_driver - register a new pci driver
1145 * @drv: the driver structure to register
1146 * @owner: owner module of drv
1147 * @mod_name: module name string
1148 * 
1149 * Adds the driver structure to the list of registered drivers.
1150 * Returns a negative value on error, otherwise 0. 
1151 * If no error occurred, the driver remains registered even if 
1152 * no device was claimed during registration.
1153 */
1154int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1155                          const char *mod_name)
1156{
1157        int error;
1158
1159        /* initialize common driver fields */
1160        drv->driver.name = drv->name;
1161        drv->driver.bus = &pci_bus_type;
1162        drv->driver.owner = owner;
1163        drv->driver.mod_name = mod_name;
1164
1165        spin_lock_init(&drv->dynids.lock);
1166        INIT_LIST_HEAD(&drv->dynids.list);
1167
1168        /* register with core */
1169        error = driver_register(&drv->driver);
1170        if (error)
1171                goto out;
1172
1173        error = pci_create_newid_files(drv);
1174        if (error)
1175                goto out_newid;
1176out:
1177        return error;
1178
1179out_newid:
1180        driver_unregister(&drv->driver);
1181        goto out;
1182}
1183
1184/**
1185 * pci_unregister_driver - unregister a pci driver
1186 * @drv: the driver structure to unregister
1187 * 
1188 * Deletes the driver structure from the list of registered PCI drivers,
1189 * gives it a chance to clean up by calling its remove() function for
1190 * each device it was responsible for, and marks those devices as
1191 * driverless.
1192 */
1193
1194void
1195pci_unregister_driver(struct pci_driver *drv)
1196{
1197        pci_remove_newid_files(drv);
1198        driver_unregister(&drv->driver);
1199        pci_free_dynids(drv);
1200}
1201
1202static struct pci_driver pci_compat_driver = {
1203        .name = "compat"
1204};
1205
1206/**
1207 * pci_dev_driver - get the pci_driver of a device
1208 * @dev: the device to query
1209 *
1210 * Returns the appropriate pci_driver structure or %NULL if there is no 
1211 * registered driver for the device.
1212 */
1213struct pci_driver *
1214pci_dev_driver(const struct pci_dev *dev)
1215{
1216        if (dev->driver)
1217                return dev->driver;
1218        else {
1219                int i;
1220                for(i=0; i<=PCI_ROM_RESOURCE; i++)
1221                        if (dev->resource[i].flags & IORESOURCE_BUSY)
1222                                return &pci_compat_driver;
1223        }
1224        return NULL;
1225}
1226
1227/**
1228 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1229 * @dev: the PCI device structure to match against
1230 * @drv: the device driver to search for matching PCI device id structures
1231 * 
1232 * Used by a driver to check whether a PCI device present in the
1233 * system is in its list of supported devices. Returns the matching
1234 * pci_device_id structure or %NULL if there is no match.
1235 */
1236static int pci_bus_match(struct device *dev, struct device_driver *drv)
1237{
1238        struct pci_dev *pci_dev = to_pci_dev(dev);
1239        struct pci_driver *pci_drv = to_pci_driver(drv);
1240        const struct pci_device_id *found_id;
1241
1242        found_id = pci_match_device(pci_drv, pci_dev);
1243        if (found_id)
1244                return 1;
1245
1246        return 0;
1247}
1248
1249/**
1250 * pci_dev_get - increments the reference count of the pci device structure
1251 * @dev: the device being referenced
1252 *
1253 * Each live reference to a device should be refcounted.
1254 *
1255 * Drivers for PCI devices should normally record such references in
1256 * their probe() methods, when they bind to a device, and release
1257 * them by calling pci_dev_put(), in their disconnect() methods.
1258 *
1259 * A pointer to the device with the incremented reference counter is returned.
1260 */
1261struct pci_dev *pci_dev_get(struct pci_dev *dev)
1262{
1263        if (dev)
1264                get_device(&dev->dev);
1265        return dev;
1266}
1267
1268/**
1269 * pci_dev_put - release a use of the pci device structure
1270 * @dev: device that's been disconnected
1271 *
1272 * Must be called when a user of a device is finished with it.  When the last
1273 * user of the device calls this function, the memory of the device is freed.
1274 */
1275void pci_dev_put(struct pci_dev *dev)
1276{
1277        if (dev)
1278                put_device(&dev->dev);
1279}
1280
1281#ifndef CONFIG_HOTPLUG
1282int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1283{
1284        return -ENODEV;
1285}
1286#endif
1287
1288struct bus_type pci_bus_type = {
1289        .name           = "pci",
1290        .match          = pci_bus_match,
1291        .uevent         = pci_uevent,
1292        .probe          = pci_device_probe,
1293        .remove         = pci_device_remove,
1294        .shutdown       = pci_device_shutdown,
1295        .dev_attrs      = pci_dev_attrs,
1296        .bus_attrs      = pci_bus_attrs,
1297        .pm             = PCI_PM_OPS_PTR,
1298};
1299
1300static int __init pci_driver_init(void)
1301{
1302        return bus_register(&pci_bus_type);
1303}
1304
1305postcore_initcall(pci_driver_init);
1306
1307EXPORT_SYMBOL_GPL(pci_add_dynid);
1308EXPORT_SYMBOL(pci_match_id);
1309EXPORT_SYMBOL(__pci_register_driver);
1310EXPORT_SYMBOL(pci_unregister_driver);
1311EXPORT_SYMBOL(pci_dev_driver);
1312EXPORT_SYMBOL(pci_bus_type);
1313EXPORT_SYMBOL(pci_dev_get);
1314EXPORT_SYMBOL(pci_dev_put);
1315
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.