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 "pci.h"
  21
  22struct pci_dynid {
  23        struct list_head node;
  24        struct pci_device_id id;
  25};
  26
  27/**
  28 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
  29 * @drv: target pci driver
  30 * @vendor: PCI vendor ID
  31 * @device: PCI device ID
  32 * @subvendor: PCI subvendor ID
  33 * @subdevice: PCI subdevice ID
  34 * @class: PCI class
  35 * @class_mask: PCI class mask
  36 * @driver_data: private driver data
  37 *
  38 * Adds a new dynamic pci device ID to this driver and causes the
  39 * driver to probe for all devices again.  @drv must have been
  40 * registered prior to calling this function.
  41 *
  42 * CONTEXT:
  43 * Does GFP_KERNEL allocation.
  44 *
  45 * RETURNS:
  46 * 0 on success, -errno on failure.
  47 */
  48int pci_add_dynid(struct pci_driver *drv,
  49                  unsigned int vendor, unsigned int device,
  50                  unsigned int subvendor, unsigned int subdevice,
  51                  unsigned int class, unsigned int class_mask,
  52                  unsigned long driver_data)
  53{
  54        struct pci_dynid *dynid;
  55        int retval;
  56
  57        dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  58        if (!dynid)
  59                return -ENOMEM;
  60
  61        dynid->id.vendor = vendor;
  62        dynid->id.device = device;
  63        dynid->id.subvendor = subvendor;
  64        dynid->id.subdevice = subdevice;
  65        dynid->id.class = class;
  66        dynid->id.class_mask = class_mask;
  67        dynid->id.driver_data = driver_data;
  68
  69        spin_lock(&drv->dynids.lock);
  70        list_add_tail(&dynid->node, &drv->dynids.list);
  71        spin_unlock(&drv->dynids.lock);
  72
  73        get_driver(&drv->driver);
  74        retval = driver_attach(&drv->driver);
  75        put_driver(&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_file(struct pci_driver *drv)
 192{
 193        int error = 0;
 194        if (drv->probe != NULL)
 195                error = driver_create_file(&drv->driver, &driver_attr_new_id);
 196        return error;
 197}
 198
 199static void pci_remove_newid_file(struct pci_driver *drv)
 200{
 201        driver_remove_file(&drv->driver, &driver_attr_new_id);
 202}
 203
 204static int
 205pci_create_removeid_file(struct pci_driver *drv)
 206{
 207        int error = 0;
 208        if (drv->probe != NULL)
 209                error = driver_create_file(&drv->driver,&driver_attr_remove_id);
 210        return error;
 211}
 212
 213static void pci_remove_removeid_file(struct pci_driver *drv)
 214{
 215        driver_remove_file(&drv->driver, &driver_attr_remove_id);
 216}
 217#else /* !CONFIG_HOTPLUG */
 218static inline int pci_create_newid_file(struct pci_driver *drv)
 219{
 220        return 0;
 221}
 222static inline void pci_remove_newid_file(struct pci_driver *drv) {}
 223static inline int pci_create_removeid_file(struct pci_driver *drv)
 224{
 225        return 0;
 226}
 227static inline void pci_remove_removeid_file(struct pci_driver *drv) {}
 228#endif
 229
 230/**
 231 * pci_match_id - See if a pci device matches a given pci_id table
 232 * @ids: array of PCI device id structures to search in
 233 * @dev: the PCI device structure to match against.
 234 *
 235 * Used by a driver to check whether a PCI device present in the
 236 * system is in its list of supported devices.  Returns the matching
 237 * pci_device_id structure or %NULL if there is no match.
 238 *
 239 * Deprecated, don't use this as it will not catch any dynamic ids
 240 * that a driver might want to check for.
 241 */
 242const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
 243                                         struct pci_dev *dev)
 244{
 245        if (ids) {
 246                while (ids->vendor || ids->subvendor || ids->class_mask) {
 247                        if (pci_match_one_device(ids, dev))
 248                                return ids;
 249                        ids++;
 250                }
 251        }
 252        return NULL;
 253}
 254
 255/**
 256 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
 257 * @drv: the PCI driver to match against
 258 * @dev: the PCI device structure to match against
 259 *
 260 * Used by a driver to check whether a PCI device present in the
 261 * system is in its list of supported devices.  Returns the matching
 262 * pci_device_id structure or %NULL if there is no match.
 263 */
 264static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
 265                                                    struct pci_dev *dev)
 266{
 267        struct pci_dynid *dynid;
 268
 269        /* Look at the dynamic ids first, before the static ones */
 270        spin_lock(&drv->dynids.lock);
 271        list_for_each_entry(dynid, &drv->dynids.list, node) {
 272                if (pci_match_one_device(&dynid->id, dev)) {
 273                        spin_unlock(&drv->dynids.lock);
 274                        return &dynid->id;
 275                }
 276        }
 277        spin_unlock(&drv->dynids.lock);
 278
 279        return pci_match_id(drv->id_table, dev);
 280}
 281
 282struct drv_dev_and_id {
 283        struct pci_driver *drv;
 284        struct pci_dev *dev;
 285        const struct pci_device_id *id;
 286};
 287
 288static long local_pci_probe(void *_ddi)
 289{
 290        struct drv_dev_and_id *ddi = _ddi;
 291
 292        return ddi->drv->probe(ddi->dev, ddi->id);
 293}
 294
 295static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
 296                          const struct pci_device_id *id)
 297{
 298        int error, node;
 299        struct drv_dev_and_id ddi = { drv, dev, id };
 300
 301        /* Execute driver initialization on node where the device's
 302           bus is attached to.  This way the driver likely allocates
 303           its local memory on the right node without any need to
 304           change it. */
 305        node = dev_to_node(&dev->dev);
 306        if (node >= 0) {
 307                int cpu;
 308
 309                get_online_cpus();
 310                cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
 311                if (cpu < nr_cpu_ids)
 312                        error = work_on_cpu(cpu, local_pci_probe, &ddi);
 313                else
 314                        error = local_pci_probe(&ddi);
 315                put_online_cpus();
 316        } else
 317                error = local_pci_probe(&ddi);
 318        return error;
 319}
 320
 321/**
 322 * __pci_device_probe()
 323 * @drv: driver to call to check if it wants the PCI device
 324 * @pci_dev: PCI device being probed
 325 * 
 326 * returns 0 on success, else error.
 327 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
 328 */
 329static int
 330__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
 331{
 332        const struct pci_device_id *id;
 333        int error = 0;
 334
 335        if (!pci_dev->driver && drv->probe) {
 336                error = -ENODEV;
 337
 338                id = pci_match_device(drv, pci_dev);
 339                if (id)
 340                        error = pci_call_probe(drv, pci_dev, id);
 341                if (error >= 0) {
 342                        pci_dev->driver = drv;
 343                        error = 0;
 344                }
 345        }
 346        return error;
 347}
 348
 349static int pci_device_probe(struct device * dev)
 350{
 351        int error = 0;
 352        struct pci_driver *drv;
 353        struct pci_dev *pci_dev;
 354
 355        drv = to_pci_driver(dev->driver);
 356        pci_dev = to_pci_dev(dev);
 357        pci_dev_get(pci_dev);
 358        error = __pci_device_probe(drv, pci_dev);
 359        if (error)
 360                pci_dev_put(pci_dev);
 361
 362        return error;
 363}
 364
 365static int pci_device_remove(struct device * dev)
 366{
 367        struct pci_dev * pci_dev = to_pci_dev(dev);
 368        struct pci_driver * drv = pci_dev->driver;
 369
 370        if (drv) {
 371                if (drv->remove)
 372                        drv->remove(pci_dev);
 373                pci_dev->driver = NULL;
 374        }
 375
 376        /*
 377         * If the device is still on, set the power state as "unknown",
 378         * since it might change by the next time we load the driver.
 379         */
 380        if (pci_dev->current_state == PCI_D0)
 381                pci_dev->current_state = PCI_UNKNOWN;
 382
 383        /*
 384         * We would love to complain here if pci_dev->is_enabled is set, that
 385         * the driver should have called pci_disable_device(), but the
 386         * unfortunate fact is there are too many odd BIOS and bridge setups
 387         * that don't like drivers doing that all of the time.  
 388         * Oh well, we can dream of sane hardware when we sleep, no matter how
 389         * horrible the crap we have to deal with is when we are awake...
 390         */
 391
 392        pci_dev_put(pci_dev);
 393        return 0;
 394}
 395
 396static void pci_device_shutdown(struct device *dev)
 397{
 398        struct pci_dev *pci_dev = to_pci_dev(dev);
 399        struct pci_driver *drv = pci_dev->driver;
 400
 401        if (drv && drv->shutdown)
 402                drv->shutdown(pci_dev);
 403        pci_msi_shutdown(pci_dev);
 404        pci_msix_shutdown(pci_dev);
 405}
 406
 407#ifdef CONFIG_PM_SLEEP
 408
 409/*
 410 * Default "suspend" method for devices that have no driver provided suspend,
 411 * or not even a driver at all (second part).
 412 */
 413static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
 414{
 415        /*
 416         * mark its power state as "unknown", since we don't know if
 417         * e.g. the BIOS will change its device state when we suspend.
 418         */
 419        if (pci_dev->current_state == PCI_D0)
 420                pci_dev->current_state = PCI_UNKNOWN;
 421}
 422
 423/*
 424 * Default "resume" method for devices that have no driver provided resume,
 425 * or not even a driver at all (second part).
 426 */
 427static int pci_pm_reenable_device(struct pci_dev *pci_dev)
 428{
 429        int retval;
 430
 431        /* if the device was enabled before suspend, reenable */
 432        retval = pci_reenable_device(pci_dev);
 433        /*
 434         * if the device was busmaster before the suspend, make it busmaster
 435         * again
 436         */
 437        if (pci_dev->is_busmaster)
 438                pci_set_master(pci_dev);
 439
 440        return retval;
 441}
 442
 443static int pci_legacy_suspend(struct device *dev, pm_message_t state)
 444{
 445        struct pci_dev * pci_dev = to_pci_dev(dev);
 446        struct pci_driver * drv = pci_dev->driver;
 447
 448        if (drv && drv->suspend) {
 449                pci_power_t prev = pci_dev->current_state;
 450                int error;
 451
 452                error = drv->suspend(pci_dev, state);
 453                suspend_report_result(drv->suspend, error);
 454                if (error)
 455                        return error;
 456
 457                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 458                    && pci_dev->current_state != PCI_UNKNOWN) {
 459                        WARN_ONCE(pci_dev->current_state != prev,
 460                                "PCI PM: Device state not saved by %pF\n",
 461                                drv->suspend);
 462                }
 463        }
 464
 465        pci_fixup_device(pci_fixup_suspend, pci_dev);
 466
 467        return 0;
 468}
 469
 470static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
 471{
 472        struct pci_dev * pci_dev = to_pci_dev(dev);
 473        struct pci_driver * drv = pci_dev->driver;
 474
 475        if (drv && drv->suspend_late) {
 476                pci_power_t prev = pci_dev->current_state;
 477                int error;
 478
 479                error = drv->suspend_late(pci_dev, state);
 480                suspend_report_result(drv->suspend_late, error);
 481                if (error)
 482                        return error;
 483
 484                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 485                    && pci_dev->current_state != PCI_UNKNOWN) {
 486                        WARN_ONCE(pci_dev->current_state != prev,
 487                                "PCI PM: Device state not saved by %pF\n",
 488                                drv->suspend_late);
 489                        return 0;
 490                }
 491        }
 492
 493        if (!pci_dev->state_saved)
 494                pci_save_state(pci_dev);
 495
 496        pci_pm_set_unknown_state(pci_dev);
 497
 498        return 0;
 499}
 500
 501static int pci_legacy_resume_early(struct device *dev)
 502{
 503        struct pci_dev * pci_dev = to_pci_dev(dev);
 504        struct pci_driver * drv = pci_dev->driver;
 505
 506        return drv && drv->resume_early ?
 507                        drv->resume_early(pci_dev) : 0;
 508}
 509
 510static int pci_legacy_resume(struct device *dev)
 511{
 512        struct pci_dev * pci_dev = to_pci_dev(dev);
 513        struct pci_driver * drv = pci_dev->driver;
 514
 515        pci_fixup_device(pci_fixup_resume, pci_dev);
 516
 517        return drv && drv->resume ?
 518                        drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
 519}
 520
 521/* Auxiliary functions used by the new power management framework */
 522
 523/**
 524 * pci_restore_standard_config - restore standard config registers of PCI device
 525 * @pci_dev: PCI device to handle
 526 */
 527static int pci_restore_standard_config(struct pci_dev *pci_dev)
 528{
 529        pci_update_current_state(pci_dev, PCI_UNKNOWN);
 530
 531        if (pci_dev->current_state != PCI_D0) {
 532                int error = pci_set_power_state(pci_dev, PCI_D0);
 533                if (error)
 534                        return error;
 535        }
 536
 537        return pci_restore_state(pci_dev);
 538}
 539
 540static void pci_pm_default_resume_noirq(struct pci_dev *pci_dev)
 541{
 542        pci_restore_standard_config(pci_dev);
 543        pci_fixup_device(pci_fixup_resume_early, pci_dev);
 544}
 545
 546static void pci_pm_default_resume(struct pci_dev *pci_dev)
 547{
 548        pci_fixup_device(pci_fixup_resume, pci_dev);
 549
 550        if (!pci_is_bridge(pci_dev))
 551                pci_enable_wake(pci_dev, PCI_D0, false);
 552}
 553
 554static void pci_pm_default_suspend(struct pci_dev *pci_dev)
 555{
 556        /* Disable non-bridge devices without PM support */
 557        if (!pci_is_bridge(pci_dev))
 558                pci_disable_enabled_device(pci_dev);
 559}
 560
 561static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
 562{
 563        struct pci_driver *drv = pci_dev->driver;
 564        bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
 565                || drv->resume_early);
 566
 567        /*
 568         * Legacy PM support is used by default, so warn if the new framework is
 569         * supported as well.  Drivers are supposed to support either the
 570         * former, or the latter, but not both at the same time.
 571         */
 572        WARN_ON(ret && drv->driver.pm);
 573
 574        return ret;
 575}
 576
 577/* New power management framework */
 578
 579static int pci_pm_prepare(struct device *dev)
 580{
 581        struct device_driver *drv = dev->driver;
 582        int error = 0;
 583
 584        if (drv && drv->pm && drv->pm->prepare)
 585                error = drv->pm->prepare(dev);
 586
 587        return error;
 588}
 589
 590static void pci_pm_complete(struct device *dev)
 591{
 592        struct device_driver *drv = dev->driver;
 593
 594        if (drv && drv->pm && drv->pm->complete)
 595                drv->pm->complete(dev);
 596}
 597
 598#ifdef CONFIG_SUSPEND
 599
 600static int pci_pm_suspend(struct device *dev)
 601{
 602        struct pci_dev *pci_dev = to_pci_dev(dev);
 603        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 604
 605        if (pci_has_legacy_pm_support(pci_dev))
 606                return pci_legacy_suspend(dev, PMSG_SUSPEND);
 607
 608        if (!pm) {
 609                pci_pm_default_suspend(pci_dev);
 610                goto Fixup;
 611        }
 612
 613        if (pm->suspend) {
 614                pci_power_t prev = pci_dev->current_state;
 615                int error;
 616
 617                error = pm->suspend(dev);
 618                suspend_report_result(pm->suspend, error);
 619                if (error)
 620                        return error;
 621
 622                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 623                    && pci_dev->current_state != PCI_UNKNOWN) {
 624                        WARN_ONCE(pci_dev->current_state != prev,
 625                                "PCI PM: State of device not saved by %pF\n",
 626                                pm->suspend);
 627                }
 628        }
 629
 630 Fixup:
 631        pci_fixup_device(pci_fixup_suspend, pci_dev);
 632
 633        return 0;
 634}
 635
 636static int pci_pm_suspend_noirq(struct device *dev)
 637{
 638        struct pci_dev *pci_dev = to_pci_dev(dev);
 639        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 640
 641        if (pci_has_legacy_pm_support(pci_dev))
 642                return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
 643
 644        if (!pm) {
 645                pci_save_state(pci_dev);
 646                return 0;
 647        }
 648
 649        if (pm->suspend_noirq) {
 650                pci_power_t prev = pci_dev->current_state;
 651                int error;
 652
 653                error = pm->suspend_noirq(dev);
 654                suspend_report_result(pm->suspend_noirq, error);
 655                if (error)
 656                        return error;
 657
 658                if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
 659                    && pci_dev->current_state != PCI_UNKNOWN) {
 660                        WARN_ONCE(pci_dev->current_state != prev,
 661                                "PCI PM: State of device not saved by %pF\n",
 662                                pm->suspend_noirq);
 663                        return 0;
 664                }
 665        }
 666
 667        if (!pci_dev->state_saved) {
 668                pci_save_state(pci_dev);
 669                if (!pci_is_bridge(pci_dev))
 670                        pci_prepare_to_sleep(pci_dev);
 671        }
 672
 673        pci_pm_set_unknown_state(pci_dev);
 674
 675        return 0;
 676}
 677
 678static int pci_pm_resume_noirq(struct device *dev)
 679{
 680        struct pci_dev *pci_dev = to_pci_dev(dev);
 681        struct device_driver *drv = dev->driver;
 682        int error = 0;
 683
 684        pci_pm_default_resume_noirq(pci_dev);
 685
 686        if (pci_has_legacy_pm_support(pci_dev))
 687                return pci_legacy_resume_early(dev);
 688
 689        if (drv && drv->pm && drv->pm->resume_noirq)
 690                error = drv->pm->resume_noirq(dev);
 691
 692        return error;
 693}
 694
 695static int pci_pm_resume(struct device *dev)
 696{
 697        struct pci_dev *pci_dev = to_pci_dev(dev);
 698        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 699        int error = 0;
 700
 701        /*
 702         * This is necessary for the suspend error path in which resume is
 703         * called without restoring the standard config registers of the device.
 704         */
 705        if (pci_dev->state_saved)
 706                pci_restore_standard_config(pci_dev);
 707
 708        if (pci_has_legacy_pm_support(pci_dev))
 709                return pci_legacy_resume(dev);
 710
 711        pci_pm_default_resume(pci_dev);
 712
 713        if (pm) {
 714                if (pm->resume)
 715                        error = pm->resume(dev);
 716        } else {
 717                pci_pm_reenable_device(pci_dev);
 718        }
 719
 720        return error;
 721}
 722
 723#else /* !CONFIG_SUSPEND */
 724
 725#define pci_pm_suspend          NULL
 726#define pci_pm_suspend_noirq    NULL
 727#define pci_pm_resume           NULL
 728#define pci_pm_resume_noirq     NULL
 729
 730#endif /* !CONFIG_SUSPEND */
 731
 732#ifdef CONFIG_HIBERNATION
 733
 734static int pci_pm_freeze(struct device *dev)
 735{
 736        struct pci_dev *pci_dev = to_pci_dev(dev);
 737        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 738
 739        if (pci_has_legacy_pm_support(pci_dev))
 740                return pci_legacy_suspend(dev, PMSG_FREEZE);
 741
 742        if (!pm) {
 743                pci_pm_default_suspend(pci_dev);
 744                return 0;
 745        }
 746
 747        if (pm->freeze) {
 748                int error;
 749
 750                error = pm->freeze(dev);
 751                suspend_report_result(pm->freeze, error);
 752                if (error)
 753                        return error;
 754        }
 755
 756        return 0;
 757}
 758
 759static int pci_pm_freeze_noirq(struct device *dev)
 760{
 761        struct pci_dev *pci_dev = to_pci_dev(dev);
 762        struct device_driver *drv = dev->driver;
 763
 764        if (pci_has_legacy_pm_support(pci_dev))
 765                return pci_legacy_suspend_late(dev, PMSG_FREEZE);
 766
 767        if (drv && drv->pm && drv->pm->freeze_noirq) {
 768                int error;
 769
 770                error = drv->pm->freeze_noirq(dev);
 771                suspend_report_result(drv->pm->freeze_noirq, error);
 772                if (error)
 773                        return error;
 774        }
 775
 776        if (!pci_dev->state_saved)
 777                pci_save_state(pci_dev);
 778
 779        pci_pm_set_unknown_state(pci_dev);
 780
 781        return 0;
 782}
 783
 784static int pci_pm_thaw_noirq(struct device *dev)
 785{
 786        struct pci_dev *pci_dev = to_pci_dev(dev);
 787        struct device_driver *drv = dev->driver;
 788        int error = 0;
 789
 790        if (pci_has_legacy_pm_support(pci_dev))
 791                return pci_legacy_resume_early(dev);
 792
 793        pci_update_current_state(pci_dev, PCI_D0);
 794
 795        if (drv && drv->pm && drv->pm->thaw_noirq)
 796                error = drv->pm->thaw_noirq(dev);
 797
 798        return error;
 799}
 800
 801static int pci_pm_thaw(struct device *dev)
 802{
 803        struct pci_dev *pci_dev = to_pci_dev(dev);
 804        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 805        int error = 0;
 806
 807        if (pci_has_legacy_pm_support(pci_dev))
 808                return pci_legacy_resume(dev);
 809
 810        if (pm) {
 811                if (pm->thaw)
 812                        error = pm->thaw(dev);
 813        } else {
 814                pci_pm_reenable_device(pci_dev);
 815        }
 816
 817        pci_dev->state_saved = false;
 818
 819        return error;
 820}
 821
 822static int pci_pm_poweroff(struct device *dev)
 823{
 824        struct pci_dev *pci_dev = to_pci_dev(dev);
 825        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 826
 827        if (pci_has_legacy_pm_support(pci_dev))
 828                return pci_legacy_suspend(dev, PMSG_HIBERNATE);
 829
 830        if (!pm) {
 831                pci_pm_default_suspend(pci_dev);
 832                goto Fixup;
 833        }
 834
 835        if (pm->poweroff) {
 836                int error;
 837
 838                error = pm->poweroff(dev);
 839                suspend_report_result(pm->poweroff, error);
 840                if (error)
 841                        return error;
 842        }
 843
 844 Fixup:
 845        pci_fixup_device(pci_fixup_suspend, pci_dev);
 846
 847        return 0;
 848}
 849
 850static int pci_pm_poweroff_noirq(struct device *dev)
 851{
 852        struct pci_dev *pci_dev = to_pci_dev(dev);
 853        struct device_driver *drv = dev->driver;
 854
 855        if (pci_has_legacy_pm_support(to_pci_dev(dev)))
 856                return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
 857
 858        if (!drv || !drv->pm)
 859                return 0;
 860
 861        if (drv->pm->poweroff_noirq) {
 862                int error;
 863
 864                error = drv->pm->poweroff_noirq(dev);
 865                suspend_report_result(drv->pm->poweroff_noirq, error);
 866                if (error)
 867                        return error;
 868        }
 869
 870        if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
 871                pci_prepare_to_sleep(pci_dev);
 872
 873        return 0;
 874}
 875
 876static int pci_pm_restore_noirq(struct device *dev)
 877{
 878        struct pci_dev *pci_dev = to_pci_dev(dev);
 879        struct device_driver *drv = dev->driver;
 880        int error = 0;
 881
 882        pci_pm_default_resume_noirq(pci_dev);
 883
 884        if (pci_has_legacy_pm_support(pci_dev))
 885                return pci_legacy_resume_early(dev);
 886
 887        if (drv && drv->pm && drv->pm->restore_noirq)
 888                error = drv->pm->restore_noirq(dev);
 889
 890        return error;
 891}
 892
 893static int pci_pm_restore(struct device *dev)
 894{
 895        struct pci_dev *pci_dev = to_pci_dev(dev);
 896        const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
 897        int error = 0;
 898
 899        /*
 900         * This is necessary for the hibernation error path in which restore is
 901         * called without restoring the standard config registers of the device.
 902         */
 903        if (pci_dev->state_saved)
 904                pci_restore_standard_config(pci_dev);
 905
 906        if (pci_has_legacy_pm_support(pci_dev))
 907                return pci_legacy_resume(dev);
 908
 909        pci_pm_default_resume(pci_dev);
 910
 911        if (pm) {
 912                if (pm->restore)
 913                        error = pm->restore(dev);
 914        } else {
 915                pci_pm_reenable_device(pci_dev);
 916        }
 917
 918        return error;
 919}
 920
 921#else /* !CONFIG_HIBERNATION */
 922
 923#define pci_pm_freeze           NULL
 924#define pci_pm_freeze_noirq     NULL
 925#define pci_pm_thaw             NULL
 926#define pci_pm_thaw_noirq       NULL
 927#define pci_pm_poweroff         NULL
 928#define pci_pm_poweroff_noirq   NULL
 929#define pci_pm_restore          NULL
 930#define pci_pm_restore_noirq    NULL
 931
 932#endif /* !CONFIG_HIBERNATION */
 933
 934const struct dev_pm_ops pci_dev_pm_ops = {
 935        .prepare = pci_pm_prepare,
 936        .complete = pci_pm_complete,
 937        .suspend = pci_pm_suspend,
 938        .resume = pci_pm_resume,
 939        .freeze = pci_pm_freeze,
 940        .thaw = pci_pm_thaw,
 941        .poweroff = pci_pm_poweroff,
 942        .restore = pci_pm_restore,
 943        .suspend_noirq = pci_pm_suspend_noirq,
 944        .resume_noirq = pci_pm_resume_noirq,
 945        .freeze_noirq = pci_pm_freeze_noirq,
 946        .thaw_noirq = pci_pm_thaw_noirq,
 947        .poweroff_noirq = pci_pm_poweroff_noirq,
 948        .restore_noirq = pci_pm_restore_noirq,
 949};
 950
 951#define PCI_PM_OPS_PTR  (&pci_dev_pm_ops)
 952
 953#else /* !CONFIG_PM_SLEEP */
 954
 955#define PCI_PM_OPS_PTR  NULL
 956
 957#endif /* !CONFIG_PM_SLEEP */
 958
 959/**
 960 * __pci_register_driver - register a new pci driver
 961 * @drv: the driver structure to register
 962 * @owner: owner module of drv
 963 * @mod_name: module name string
 964 * 
 965 * Adds the driver structure to the list of registered drivers.
 966 * Returns a negative value on error, otherwise 0. 
 967 * If no error occurred, the driver remains registered even if 
 968 * no device was claimed during registration.
 969 */
 970int __pci_register_driver(struct pci_driver *drv, struct module *owner,
 971                          const char *mod_name)
 972{
 973        int error;
 974
 975        /* initialize common driver fields */
 976        drv->driver.name = drv->name;
 977        drv->driver.bus = &pci_bus_type;
 978        drv->driver.owner = owner;
 979        drv->driver.mod_name = mod_name;
 980
 981        spin_lock_init(&drv->dynids.lock);
 982        INIT_LIST_HEAD(&drv->dynids.list);
 983
 984        /* register with core */
 985        error = driver_register(&drv->driver);
 986        if (error)
 987                goto out;
 988
 989        error = pci_create_newid_file(drv);
 990        if (error)
 991                goto out_newid;
 992
 993        error = pci_create_removeid_file(drv);
 994        if (error)
 995                goto out_removeid;
 996out:
 997        return error;
 998
 999out_removeid:
1000        pci_remove_newid_file(drv);
1001out_newid:
1002        driver_unregister(&drv->driver);
1003        goto out;
1004}
1005
1006/**
1007 * pci_unregister_driver - unregister a pci driver
1008 * @drv: the driver structure to unregister
1009 * 
1010 * Deletes the driver structure from the list of registered PCI drivers,
1011 * gives it a chance to clean up by calling its remove() function for
1012 * each device it was responsible for, and marks those devices as
1013 * driverless.
1014 */
1015
1016void
1017pci_unregister_driver(struct pci_driver *drv)
1018{
1019        pci_remove_removeid_file(drv);
1020        pci_remove_newid_file(drv);
1021        driver_unregister(&drv->driver);
1022        pci_free_dynids(drv);
1023}
1024
1025static struct pci_driver pci_compat_driver = {
1026        .name = "compat"
1027};
1028
1029/**
1030 * pci_dev_driver - get the pci_driver of a device
1031 * @dev: the device to query
1032 *
1033 * Returns the appropriate pci_driver structure or %NULL if there is no 
1034 * registered driver for the device.
1035 */
1036struct pci_driver *
1037pci_dev_driver(const struct pci_dev *dev)
1038{
1039        if (dev->driver)
1040                return dev->driver;
1041        else {
1042                int i;
1043                for(i=0; i<=PCI_ROM_RESOURCE; i++)
1044                        if (dev->resource[i].flags & IORESOURCE_BUSY)
1045                                return &pci_compat_driver;
1046        }
1047        return NULL;
1048}
1049
1050/**
1051 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1052 * @dev: the PCI device structure to match against
1053 * @drv: the device driver to search for matching PCI device id structures
1054 * 
1055 * Used by a driver to check whether a PCI device present in the
1056 * system is in its list of supported devices. Returns the matching
1057 * pci_device_id structure or %NULL if there is no match.
1058 */
1059static int pci_bus_match(struct device *dev, struct device_driver *drv)
1060{
1061        struct pci_dev *pci_dev = to_pci_dev(dev);
1062        struct pci_driver *pci_drv = to_pci_driver(drv);
1063        const struct pci_device_id *found_id;
1064
1065        found_id = pci_match_device(pci_drv, pci_dev);
1066        if (found_id)
1067                return 1;
1068
1069        return 0;
1070}
1071
1072/**
1073 * pci_dev_get - increments the reference count of the pci device structure
1074 * @dev: the device being referenced
1075 *
1076 * Each live reference to a device should be refcounted.
1077 *
1078 * Drivers for PCI devices should normally record such references in
1079 * their probe() methods, when they bind to a device, and release
1080 * them by calling pci_dev_put(), in their disconnect() methods.
1081 *
1082 * A pointer to the device with the incremented reference counter is returned.
1083 */
1084struct pci_dev *pci_dev_get(struct pci_dev *dev)
1085{
1086        if (dev)
1087                get_device(&dev->dev);
1088        return dev;
1089}
1090
1091/**
1092 * pci_dev_put - release a use of the pci device structure
1093 * @dev: device that's been disconnected
1094 *
1095 * Must be called when a user of a device is finished with it.  When the last
1096 * user of the device calls this function, the memory of the device is freed.
1097 */
1098void pci_dev_put(struct pci_dev *dev)
1099{
1100        if (dev)
1101                put_device(&dev->dev);
1102}
1103
1104#ifndef CONFIG_HOTPLUG
1105int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1106{
1107        return -ENODEV;
1108}
1109#endif
1110
1111struct bus_type pci_bus_type = {
1112        .name           = "pci",
1113        .match          = pci_bus_match,
1114        .uevent         = pci_uevent,
1115        .probe          = pci_device_probe,
1116        .remove         = pci_device_remove,
1117        .shutdown       = pci_device_shutdown,
1118        .dev_attrs      = pci_dev_attrs,
1119        .bus_attrs      = pci_bus_attrs,
1120        .pm             = PCI_PM_OPS_PTR,
1121};
1122
1123static int __init pci_driver_init(void)
1124{
1125        return bus_register(&pci_bus_type);
1126}
1127
1128postcore_initcall(pci_driver_init);
1129
1130EXPORT_SYMBOL_GPL(pci_add_dynid);
1131EXPORT_SYMBOL(pci_match_id);
1132EXPORT_SYMBOL(__pci_register_driver);
1133EXPORT_SYMBOL(pci_unregister_driver);
1134EXPORT_SYMBOL(pci_dev_driver);
1135EXPORT_SYMBOL(pci_bus_type);
1136EXPORT_SYMBOL(pci_dev_get);
1137EXPORT_SYMBOL(pci_dev_put);
1138