rt2x00pci.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  4. <http://rt2x00.serialmonkey.com>
  5. */
  6. /*
  7. Module: rt2x00pci
  8. Abstract: rt2x00 generic pci device routines.
  9. */
  10. #include <linux/dma-mapping.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/slab.h>
  15. #include "rt2x00.h"
  16. #include "rt2x00pci.h"
  17. /*
  18. * PCI driver handlers.
  19. */
  20. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  21. {
  22. kfree(rt2x00dev->rf);
  23. rt2x00dev->rf = NULL;
  24. kfree(rt2x00dev->eeprom);
  25. rt2x00dev->eeprom = NULL;
  26. if (rt2x00dev->csr.base) {
  27. iounmap(rt2x00dev->csr.base);
  28. rt2x00dev->csr.base = NULL;
  29. }
  30. }
  31. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  32. {
  33. struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
  34. rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
  35. if (!rt2x00dev->csr.base)
  36. goto exit;
  37. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  38. if (!rt2x00dev->eeprom)
  39. goto exit;
  40. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  41. if (!rt2x00dev->rf)
  42. goto exit;
  43. return 0;
  44. exit:
  45. rt2x00_probe_err("Failed to allocate registers\n");
  46. rt2x00pci_free_reg(rt2x00dev);
  47. return -ENOMEM;
  48. }
  49. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct rt2x00_ops *ops)
  50. {
  51. struct ieee80211_hw *hw;
  52. struct rt2x00_dev *rt2x00dev;
  53. int retval;
  54. u16 chip;
  55. retval = pci_enable_device(pci_dev);
  56. if (retval) {
  57. rt2x00_probe_err("Enable device failed\n");
  58. return retval;
  59. }
  60. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  61. if (retval) {
  62. rt2x00_probe_err("PCI request regions failed\n");
  63. goto exit_disable_device;
  64. }
  65. pci_set_master(pci_dev);
  66. if (pci_set_mwi(pci_dev))
  67. rt2x00_probe_err("MWI not available\n");
  68. if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
  69. rt2x00_probe_err("PCI DMA not supported\n");
  70. retval = -EIO;
  71. goto exit_release_regions;
  72. }
  73. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  74. if (!hw) {
  75. rt2x00_probe_err("Failed to allocate hardware\n");
  76. retval = -ENOMEM;
  77. goto exit_release_regions;
  78. }
  79. pci_set_drvdata(pci_dev, hw);
  80. rt2x00dev = hw->priv;
  81. rt2x00dev->dev = &pci_dev->dev;
  82. rt2x00dev->ops = ops;
  83. rt2x00dev->hw = hw;
  84. rt2x00dev->irq = pci_dev->irq;
  85. rt2x00dev->name = ops->name;
  86. if (pci_is_pcie(pci_dev))
  87. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCIE);
  88. else
  89. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_PCI);
  90. retval = rt2x00pci_alloc_reg(rt2x00dev);
  91. if (retval)
  92. goto exit_free_device;
  93. /*
  94. * Because rt3290 chip use different efuse offset to read efuse data.
  95. * So before read efuse it need to indicate it is the
  96. * rt3290 or not.
  97. */
  98. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &chip);
  99. rt2x00dev->chip.rt = chip;
  100. retval = rt2x00lib_probe_dev(rt2x00dev);
  101. if (retval)
  102. goto exit_free_reg;
  103. return 0;
  104. exit_free_reg:
  105. rt2x00pci_free_reg(rt2x00dev);
  106. exit_free_device:
  107. ieee80211_free_hw(hw);
  108. exit_release_regions:
  109. pci_clear_mwi(pci_dev);
  110. pci_release_regions(pci_dev);
  111. exit_disable_device:
  112. pci_disable_device(pci_dev);
  113. return retval;
  114. }
  115. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  116. void rt2x00pci_remove(struct pci_dev *pci_dev)
  117. {
  118. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  119. struct rt2x00_dev *rt2x00dev = hw->priv;
  120. /*
  121. * Free all allocated data.
  122. */
  123. rt2x00lib_remove_dev(rt2x00dev);
  124. rt2x00pci_free_reg(rt2x00dev);
  125. ieee80211_free_hw(hw);
  126. /*
  127. * Free the PCI device data.
  128. */
  129. pci_clear_mwi(pci_dev);
  130. pci_disable_device(pci_dev);
  131. pci_release_regions(pci_dev);
  132. }
  133. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  134. static int __maybe_unused rt2x00pci_suspend(struct device *dev)
  135. {
  136. struct ieee80211_hw *hw = dev_get_drvdata(dev);
  137. struct rt2x00_dev *rt2x00dev = hw->priv;
  138. return rt2x00lib_suspend(rt2x00dev);
  139. }
  140. static int __maybe_unused rt2x00pci_resume(struct device *dev)
  141. {
  142. struct ieee80211_hw *hw = dev_get_drvdata(dev);
  143. struct rt2x00_dev *rt2x00dev = hw->priv;
  144. return rt2x00lib_resume(rt2x00dev);
  145. }
  146. SIMPLE_DEV_PM_OPS(rt2x00pci_pm_ops, rt2x00pci_suspend, rt2x00pci_resume);
  147. EXPORT_SYMBOL_GPL(rt2x00pci_pm_ops);
  148. /*
  149. * rt2x00pci module information.
  150. */
  151. MODULE_AUTHOR(DRV_PROJECT);
  152. MODULE_VERSION(DRV_VERSION);
  153. MODULE_DESCRIPTION("rt2x00 pci library");
  154. MODULE_LICENSE("GPL");