rt2x00pci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Module: rt2x00pci
  17. Abstract: rt2x00 generic pci device routines.
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include "rt2x00.h"
  25. #include "rt2x00pci.h"
  26. /*
  27. * PCI driver handlers.
  28. */
  29. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  30. {
  31. kfree(rt2x00dev->rf);
  32. rt2x00dev->rf = NULL;
  33. kfree(rt2x00dev->eeprom);
  34. rt2x00dev->eeprom = NULL;
  35. if (rt2x00dev->csr.base) {
  36. iounmap(rt2x00dev->csr.base);
  37. rt2x00dev->csr.base = NULL;
  38. }
  39. }
  40. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  41. {
  42. struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
  43. rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
  44. if (!rt2x00dev->csr.base)
  45. goto exit;
  46. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  47. if (!rt2x00dev->eeprom)
  48. goto exit;
  49. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  50. if (!rt2x00dev->rf)
  51. goto exit;
  52. return 0;
  53. exit:
  54. rt2x00_probe_err("Failed to allocate registers\n");
  55. rt2x00pci_free_reg(rt2x00dev);
  56. return -ENOMEM;
  57. }
  58. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct rt2x00_ops *ops)
  59. {
  60. struct ieee80211_hw *hw;
  61. struct rt2x00_dev *rt2x00dev;
  62. int retval;
  63. u16 chip;
  64. retval = pci_enable_device(pci_dev);
  65. if (retval) {
  66. rt2x00_probe_err("Enable device failed\n");
  67. return retval;
  68. }
  69. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  70. if (retval) {
  71. rt2x00_probe_err("PCI request regions failed\n");
  72. goto exit_disable_device;
  73. }
  74. pci_set_master(pci_dev);
  75. if (pci_set_mwi(pci_dev))
  76. rt2x00_probe_err("MWI not available\n");
  77. if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
  78. rt2x00_probe_err("PCI DMA not supported\n");
  79. retval = -EIO;
  80. goto exit_release_regions;
  81. }
  82. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  83. if (!hw) {
  84. rt2x00_probe_err("Failed to allocate hardware\n");
  85. retval = -ENOMEM;
  86. goto exit_release_regions;
  87. }
  88. pci_set_drvdata(pci_dev, hw);
  89. rt2x00dev = hw->priv;
  90. rt2x00dev->dev = &pci_dev->dev;
  91. rt2x00dev->ops = ops;
  92. rt2x00dev->hw = hw;
  93. rt2x00dev->irq = pci_dev->irq;
  94. rt2x00dev->name = ops->name;
  95. if (pci_is_pcie(pci_dev))
  96. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
  97. else
  98. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
  99. retval = rt2x00pci_alloc_reg(rt2x00dev);
  100. if (retval)
  101. goto exit_free_device;
  102. /*
  103. * Because rt3290 chip use different efuse offset to read efuse data.
  104. * So before read efuse it need to indicate it is the
  105. * rt3290 or not.
  106. */
  107. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &chip);
  108. rt2x00dev->chip.rt = chip;
  109. retval = rt2x00lib_probe_dev(rt2x00dev);
  110. if (retval)
  111. goto exit_free_reg;
  112. return 0;
  113. exit_free_reg:
  114. rt2x00pci_free_reg(rt2x00dev);
  115. exit_free_device:
  116. ieee80211_free_hw(hw);
  117. exit_release_regions:
  118. pci_release_regions(pci_dev);
  119. exit_disable_device:
  120. pci_disable_device(pci_dev);
  121. return retval;
  122. }
  123. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  124. void rt2x00pci_remove(struct pci_dev *pci_dev)
  125. {
  126. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  127. struct rt2x00_dev *rt2x00dev = hw->priv;
  128. /*
  129. * Free all allocated data.
  130. */
  131. rt2x00lib_remove_dev(rt2x00dev);
  132. rt2x00pci_free_reg(rt2x00dev);
  133. ieee80211_free_hw(hw);
  134. /*
  135. * Free the PCI device data.
  136. */
  137. pci_disable_device(pci_dev);
  138. pci_release_regions(pci_dev);
  139. }
  140. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  141. #ifdef CONFIG_PM
  142. int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  143. {
  144. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  145. struct rt2x00_dev *rt2x00dev = hw->priv;
  146. int retval;
  147. retval = rt2x00lib_suspend(rt2x00dev, state);
  148. if (retval)
  149. return retval;
  150. pci_save_state(pci_dev);
  151. pci_disable_device(pci_dev);
  152. return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  153. }
  154. EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
  155. int rt2x00pci_resume(struct pci_dev *pci_dev)
  156. {
  157. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  158. struct rt2x00_dev *rt2x00dev = hw->priv;
  159. if (pci_set_power_state(pci_dev, PCI_D0) ||
  160. pci_enable_device(pci_dev)) {
  161. rt2x00_err(rt2x00dev, "Failed to resume device\n");
  162. return -EIO;
  163. }
  164. pci_restore_state(pci_dev);
  165. return rt2x00lib_resume(rt2x00dev);
  166. }
  167. EXPORT_SYMBOL_GPL(rt2x00pci_resume);
  168. #endif /* CONFIG_PM */
  169. /*
  170. * rt2x00pci module information.
  171. */
  172. MODULE_AUTHOR(DRV_PROJECT);
  173. MODULE_VERSION(DRV_VERSION);
  174. MODULE_DESCRIPTION("rt2x00 pci library");
  175. MODULE_LICENSE("GPL");