Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Sonics Silicon Backplane |
| 3 | * PCI Hostdevice wrapper |
| 4 | * |
| 5 | * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de> |
| 6 | * Copyright (c) 2005 Stefano Brivio <st3@riseup.net> |
| 7 | * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org> |
| 8 | * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch> |
| 9 | * Copyright (c) 2005-2007 Michael Buesch <mbuesch@freenet.de> |
| 10 | * |
| 11 | * Licensed under the GNU/GPL. See COPYING for details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 16 | #include <linux/ssb/ssb.h> |
| 17 | |
| 18 | |
| 19 | #ifdef CONFIG_PM |
| 20 | static int ssb_pcihost_suspend(struct pci_dev *dev, pm_message_t state) |
| 21 | { |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 22 | struct ssb_bus *ssb = pci_get_drvdata(dev); |
| 23 | int err; |
| 24 | |
| 25 | err = ssb_bus_suspend(ssb); |
| 26 | if (err) |
| 27 | return err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 28 | pci_save_state(dev); |
| 29 | pci_disable_device(dev); |
| 30 | pci_set_power_state(dev, pci_choose_state(dev, state)); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int ssb_pcihost_resume(struct pci_dev *dev) |
| 36 | { |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 37 | struct ssb_bus *ssb = pci_get_drvdata(dev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 38 | int err; |
| 39 | |
| 40 | pci_set_power_state(dev, 0); |
| 41 | err = pci_enable_device(dev); |
| 42 | if (err) |
| 43 | return err; |
| 44 | pci_restore_state(dev); |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 45 | err = ssb_bus_resume(ssb); |
| 46 | if (err) |
| 47 | return err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | #else /* CONFIG_PM */ |
| 52 | # define ssb_pcihost_suspend NULL |
| 53 | # define ssb_pcihost_resume NULL |
| 54 | #endif /* CONFIG_PM */ |
| 55 | |
| 56 | static int ssb_pcihost_probe(struct pci_dev *dev, |
| 57 | const struct pci_device_id *id) |
| 58 | { |
| 59 | struct ssb_bus *ssb; |
| 60 | int err = -ENOMEM; |
| 61 | const char *name; |
| 62 | |
| 63 | ssb = kzalloc(sizeof(*ssb), GFP_KERNEL); |
| 64 | if (!ssb) |
| 65 | goto out; |
| 66 | err = pci_enable_device(dev); |
| 67 | if (err) |
| 68 | goto err_kfree_ssb; |
Kay Sievers | b7b05fe | 2008-10-30 15:51:57 +0100 | [diff] [blame] | 69 | name = dev_name(&dev->dev); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 70 | if (dev->driver && dev->driver->name) |
| 71 | name = dev->driver->name; |
| 72 | err = pci_request_regions(dev, name); |
| 73 | if (err) |
| 74 | goto err_pci_disable; |
| 75 | pci_set_master(dev); |
| 76 | |
| 77 | err = ssb_bus_pcibus_register(ssb, dev); |
| 78 | if (err) |
| 79 | goto err_pci_release_regions; |
| 80 | |
| 81 | pci_set_drvdata(dev, ssb); |
| 82 | |
| 83 | out: |
| 84 | return err; |
| 85 | |
| 86 | err_pci_release_regions: |
| 87 | pci_release_regions(dev); |
| 88 | err_pci_disable: |
| 89 | pci_disable_device(dev); |
| 90 | err_kfree_ssb: |
| 91 | kfree(ssb); |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | static void ssb_pcihost_remove(struct pci_dev *dev) |
| 96 | { |
| 97 | struct ssb_bus *ssb = pci_get_drvdata(dev); |
| 98 | |
| 99 | ssb_bus_unregister(ssb); |
| 100 | pci_release_regions(dev); |
| 101 | pci_disable_device(dev); |
| 102 | kfree(ssb); |
| 103 | pci_set_drvdata(dev, NULL); |
| 104 | } |
| 105 | |
| 106 | int ssb_pcihost_register(struct pci_driver *driver) |
| 107 | { |
| 108 | driver->probe = ssb_pcihost_probe; |
| 109 | driver->remove = ssb_pcihost_remove; |
| 110 | driver->suspend = ssb_pcihost_suspend; |
| 111 | driver->resume = ssb_pcihost_resume; |
| 112 | |
| 113 | return pci_register_driver(driver); |
| 114 | } |
| 115 | EXPORT_SYMBOL(ssb_pcihost_register); |