rt2x00pci.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00pci
  19. Abstract: rt2x00 generic pci device routines.
  20. */
  21. #include <linux/dma-mapping.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/slab.h>
  26. #include "rt2x00.h"
  27. #include "rt2x00pci.h"
  28. /*
  29. * PCI driver handlers.
  30. */
  31. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  32. {
  33. kfree(rt2x00dev->rf);
  34. rt2x00dev->rf = NULL;
  35. kfree(rt2x00dev->eeprom);
  36. rt2x00dev->eeprom = NULL;
  37. if (rt2x00dev->csr.base) {
  38. iounmap(rt2x00dev->csr.base);
  39. rt2x00dev->csr.base = NULL;
  40. }
  41. }
  42. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  43. {
  44. struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
  45. rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
  46. if (!rt2x00dev->csr.base)
  47. goto exit;
  48. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  49. if (!rt2x00dev->eeprom)
  50. goto exit;
  51. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  52. if (!rt2x00dev->rf)
  53. goto exit;
  54. return 0;
  55. exit:
  56. rt2x00_probe_err("Failed to allocate registers\n");
  57. rt2x00pci_free_reg(rt2x00dev);
  58. return -ENOMEM;
  59. }
  60. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct rt2x00_ops *ops)
  61. {
  62. struct ieee80211_hw *hw;
  63. struct rt2x00_dev *rt2x00dev;
  64. int retval;
  65. u16 chip;
  66. retval = pci_enable_device(pci_dev);
  67. if (retval) {
  68. rt2x00_probe_err("Enable device failed\n");
  69. return retval;
  70. }
  71. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  72. if (retval) {
  73. rt2x00_probe_err("PCI request regions failed\n");
  74. goto exit_disable_device;
  75. }
  76. pci_set_master(pci_dev);
  77. if (pci_set_mwi(pci_dev))
  78. rt2x00_probe_err("MWI not available\n");
  79. if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
  80. rt2x00_probe_err("PCI DMA not supported\n");
  81. retval = -EIO;
  82. goto exit_release_regions;
  83. }
  84. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  85. if (!hw) {
  86. rt2x00_probe_err("Failed to allocate hardware\n");
  87. retval = -ENOMEM;
  88. goto exit_release_regions;
  89. }
  90. pci_set_drvdata(pci_dev, hw);
  91. rt2x00dev = hw->priv;
  92. rt2x00dev->dev = &pci_dev->dev;
  93. rt2x00dev->ops = ops;
  94. rt2x00dev->hw = hw;
  95. rt2x00dev->irq = pci_dev->irq;
  96. rt2x00dev->name = pci_name(pci_dev);
  97. if (pci_is_pcie(pci_dev))
  98. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
  99. else
  100. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
  101. retval = rt2x00pci_alloc_reg(rt2x00dev);
  102. if (retval)
  103. goto exit_free_device;
  104. /*
  105. * Because rt3290 chip use different efuse offset to read efuse data.
  106. * So before read efuse it need to indicate it is the
  107. * rt3290 or not.
  108. */
  109. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &chip);
  110. rt2x00dev->chip.rt = chip;
  111. retval = rt2x00lib_probe_dev(rt2x00dev);
  112. if (retval)
  113. goto exit_free_reg;
  114. return 0;
  115. exit_free_reg:
  116. rt2x00pci_free_reg(rt2x00dev);
  117. exit_free_device:
  118. ieee80211_free_hw(hw);
  119. exit_release_regions:
  120. pci_release_regions(pci_dev);
  121. exit_disable_device:
  122. pci_disable_device(pci_dev);
  123. pci_set_drvdata(pci_dev, NULL);
  124. return retval;
  125. }
  126. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  127. void rt2x00pci_remove(struct pci_dev *pci_dev)
  128. {
  129. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  130. struct rt2x00_dev *rt2x00dev = hw->priv;
  131. /*
  132. * Free all allocated data.
  133. */
  134. rt2x00lib_remove_dev(rt2x00dev);
  135. rt2x00pci_free_reg(rt2x00dev);
  136. ieee80211_free_hw(hw);
  137. /*
  138. * Free the PCI device data.
  139. */
  140. pci_set_drvdata(pci_dev, NULL);
  141. pci_disable_device(pci_dev);
  142. pci_release_regions(pci_dev);
  143. }
  144. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  145. #ifdef CONFIG_PM
  146. int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  147. {
  148. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  149. struct rt2x00_dev *rt2x00dev = hw->priv;
  150. int retval;
  151. retval = rt2x00lib_suspend(rt2x00dev, state);
  152. if (retval)
  153. return retval;
  154. pci_save_state(pci_dev);
  155. pci_disable_device(pci_dev);
  156. return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  157. }
  158. EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
  159. int rt2x00pci_resume(struct pci_dev *pci_dev)
  160. {
  161. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  162. struct rt2x00_dev *rt2x00dev = hw->priv;
  163. if (pci_set_power_state(pci_dev, PCI_D0) ||
  164. pci_enable_device(pci_dev)) {
  165. rt2x00_err(rt2x00dev, "Failed to resume device\n");
  166. return -EIO;
  167. }
  168. pci_restore_state(pci_dev);
  169. return rt2x00lib_resume(rt2x00dev);
  170. }
  171. EXPORT_SYMBOL_GPL(rt2x00pci_resume);
  172. #endif /* CONFIG_PM */
  173. /*
  174. * rt2x00pci module information.
  175. */
  176. MODULE_AUTHOR(DRV_PROJECT);
  177. MODULE_VERSION(DRV_VERSION);
  178. MODULE_DESCRIPTION("rt2x00 pci library");
  179. MODULE_LICENSE("GPL");