rt2x00pci.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  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 "rt2x00.h"
  26. #include "rt2x00pci.h"
  27. /*
  28. * Register access.
  29. */
  30. int rt2x00pci_regbusy_read(struct rt2x00_dev *rt2x00dev,
  31. const unsigned int offset,
  32. const struct rt2x00_field32 field,
  33. u32 *reg)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  37. rt2x00pci_register_read(rt2x00dev, offset, reg);
  38. if (!rt2x00_get_field32(*reg, field))
  39. return 1;
  40. udelay(REGISTER_BUSY_DELAY);
  41. }
  42. ERROR(rt2x00dev, "Indirect register access failed: "
  43. "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
  44. *reg = ~0;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(rt2x00pci_regbusy_read);
  48. /*
  49. * TX data handlers.
  50. */
  51. int rt2x00pci_write_tx_data(struct queue_entry *entry)
  52. {
  53. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  54. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  55. struct skb_frame_desc *skbdesc;
  56. /*
  57. * This should not happen, we already checked the entry
  58. * was ours. When the hardware disagrees there has been
  59. * a queue corruption!
  60. */
  61. if (unlikely(rt2x00dev->ops->lib->get_entry_state(entry))) {
  62. ERROR(rt2x00dev,
  63. "Corrupt queue %d, accessing entry which is not ours.\n"
  64. "Please file bug report to %s.\n",
  65. entry->queue->qid, DRV_PROJECT);
  66. return -EINVAL;
  67. }
  68. /*
  69. * Fill in skb descriptor
  70. */
  71. skbdesc = get_skb_frame_desc(entry->skb);
  72. skbdesc->desc = entry_priv->desc;
  73. skbdesc->desc_len = entry->queue->desc_size;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL_GPL(rt2x00pci_write_tx_data);
  77. /*
  78. * TX/RX data handlers.
  79. */
  80. void rt2x00pci_rxdone(struct rt2x00_dev *rt2x00dev)
  81. {
  82. struct data_queue *queue = rt2x00dev->rx;
  83. struct queue_entry *entry;
  84. struct queue_entry_priv_pci *entry_priv;
  85. struct skb_frame_desc *skbdesc;
  86. while (1) {
  87. entry = rt2x00queue_get_entry(queue, Q_INDEX);
  88. entry_priv = entry->priv_data;
  89. if (rt2x00dev->ops->lib->get_entry_state(entry))
  90. break;
  91. /*
  92. * Fill in desc fields of the skb descriptor
  93. */
  94. skbdesc = get_skb_frame_desc(entry->skb);
  95. skbdesc->desc = entry_priv->desc;
  96. skbdesc->desc_len = entry->queue->desc_size;
  97. /*
  98. * Send the frame to rt2x00lib for further processing.
  99. */
  100. rt2x00lib_rxdone(rt2x00dev, entry);
  101. }
  102. }
  103. EXPORT_SYMBOL_GPL(rt2x00pci_rxdone);
  104. /*
  105. * Device initialization handlers.
  106. */
  107. static int rt2x00pci_alloc_queue_dma(struct rt2x00_dev *rt2x00dev,
  108. struct data_queue *queue)
  109. {
  110. struct queue_entry_priv_pci *entry_priv;
  111. void *addr;
  112. dma_addr_t dma;
  113. unsigned int i;
  114. /*
  115. * Allocate DMA memory for descriptor and buffer.
  116. */
  117. addr = dma_alloc_coherent(rt2x00dev->dev,
  118. queue->limit * queue->desc_size,
  119. &dma, GFP_KERNEL | GFP_DMA);
  120. if (!addr)
  121. return -ENOMEM;
  122. memset(addr, 0, queue->limit * queue->desc_size);
  123. /*
  124. * Initialize all queue entries to contain valid addresses.
  125. */
  126. for (i = 0; i < queue->limit; i++) {
  127. entry_priv = queue->entries[i].priv_data;
  128. entry_priv->desc = addr + i * queue->desc_size;
  129. entry_priv->desc_dma = dma + i * queue->desc_size;
  130. }
  131. return 0;
  132. }
  133. static void rt2x00pci_free_queue_dma(struct rt2x00_dev *rt2x00dev,
  134. struct data_queue *queue)
  135. {
  136. struct queue_entry_priv_pci *entry_priv =
  137. queue->entries[0].priv_data;
  138. if (entry_priv->desc)
  139. dma_free_coherent(rt2x00dev->dev,
  140. queue->limit * queue->desc_size,
  141. entry_priv->desc, entry_priv->desc_dma);
  142. entry_priv->desc = NULL;
  143. }
  144. int rt2x00pci_initialize(struct rt2x00_dev *rt2x00dev)
  145. {
  146. struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
  147. struct data_queue *queue;
  148. int status;
  149. /*
  150. * Allocate DMA
  151. */
  152. queue_for_each(rt2x00dev, queue) {
  153. status = rt2x00pci_alloc_queue_dma(rt2x00dev, queue);
  154. if (status)
  155. goto exit;
  156. }
  157. /*
  158. * Register interrupt handler.
  159. */
  160. status = request_irq(pci_dev->irq, rt2x00dev->ops->lib->irq_handler,
  161. IRQF_SHARED, pci_name(pci_dev), rt2x00dev);
  162. if (status) {
  163. ERROR(rt2x00dev, "IRQ %d allocation failed (error %d).\n",
  164. pci_dev->irq, status);
  165. goto exit;
  166. }
  167. return 0;
  168. exit:
  169. queue_for_each(rt2x00dev, queue)
  170. rt2x00pci_free_queue_dma(rt2x00dev, queue);
  171. return status;
  172. }
  173. EXPORT_SYMBOL_GPL(rt2x00pci_initialize);
  174. void rt2x00pci_uninitialize(struct rt2x00_dev *rt2x00dev)
  175. {
  176. struct data_queue *queue;
  177. /*
  178. * Free irq line.
  179. */
  180. free_irq(to_pci_dev(rt2x00dev->dev)->irq, rt2x00dev);
  181. /*
  182. * Free DMA
  183. */
  184. queue_for_each(rt2x00dev, queue)
  185. rt2x00pci_free_queue_dma(rt2x00dev, queue);
  186. }
  187. EXPORT_SYMBOL_GPL(rt2x00pci_uninitialize);
  188. /*
  189. * PCI driver handlers.
  190. */
  191. static void rt2x00pci_free_reg(struct rt2x00_dev *rt2x00dev)
  192. {
  193. kfree(rt2x00dev->rf);
  194. rt2x00dev->rf = NULL;
  195. kfree(rt2x00dev->eeprom);
  196. rt2x00dev->eeprom = NULL;
  197. if (rt2x00dev->csr.base) {
  198. iounmap(rt2x00dev->csr.base);
  199. rt2x00dev->csr.base = NULL;
  200. }
  201. }
  202. static int rt2x00pci_alloc_reg(struct rt2x00_dev *rt2x00dev)
  203. {
  204. struct pci_dev *pci_dev = to_pci_dev(rt2x00dev->dev);
  205. rt2x00dev->csr.base = pci_ioremap_bar(pci_dev, 0);
  206. if (!rt2x00dev->csr.base)
  207. goto exit;
  208. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  209. if (!rt2x00dev->eeprom)
  210. goto exit;
  211. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  212. if (!rt2x00dev->rf)
  213. goto exit;
  214. return 0;
  215. exit:
  216. ERROR_PROBE("Failed to allocate registers.\n");
  217. rt2x00pci_free_reg(rt2x00dev);
  218. return -ENOMEM;
  219. }
  220. int rt2x00pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  221. {
  222. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_data;
  223. struct ieee80211_hw *hw;
  224. struct rt2x00_dev *rt2x00dev;
  225. int retval;
  226. retval = pci_request_regions(pci_dev, pci_name(pci_dev));
  227. if (retval) {
  228. ERROR_PROBE("PCI request regions failed.\n");
  229. return retval;
  230. }
  231. retval = pci_enable_device(pci_dev);
  232. if (retval) {
  233. ERROR_PROBE("Enable device failed.\n");
  234. goto exit_release_regions;
  235. }
  236. pci_set_master(pci_dev);
  237. if (pci_set_mwi(pci_dev))
  238. ERROR_PROBE("MWI not available.\n");
  239. if (dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32))) {
  240. ERROR_PROBE("PCI DMA not supported.\n");
  241. retval = -EIO;
  242. goto exit_disable_device;
  243. }
  244. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  245. if (!hw) {
  246. ERROR_PROBE("Failed to allocate hardware.\n");
  247. retval = -ENOMEM;
  248. goto exit_disable_device;
  249. }
  250. pci_set_drvdata(pci_dev, hw);
  251. rt2x00dev = hw->priv;
  252. rt2x00dev->dev = &pci_dev->dev;
  253. rt2x00dev->ops = ops;
  254. rt2x00dev->hw = hw;
  255. retval = rt2x00pci_alloc_reg(rt2x00dev);
  256. if (retval)
  257. goto exit_free_device;
  258. retval = rt2x00lib_probe_dev(rt2x00dev);
  259. if (retval)
  260. goto exit_free_reg;
  261. return 0;
  262. exit_free_reg:
  263. rt2x00pci_free_reg(rt2x00dev);
  264. exit_free_device:
  265. ieee80211_free_hw(hw);
  266. exit_disable_device:
  267. if (retval != -EBUSY)
  268. pci_disable_device(pci_dev);
  269. exit_release_regions:
  270. pci_release_regions(pci_dev);
  271. pci_set_drvdata(pci_dev, NULL);
  272. return retval;
  273. }
  274. EXPORT_SYMBOL_GPL(rt2x00pci_probe);
  275. void rt2x00pci_remove(struct pci_dev *pci_dev)
  276. {
  277. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  278. struct rt2x00_dev *rt2x00dev = hw->priv;
  279. /*
  280. * Free all allocated data.
  281. */
  282. rt2x00lib_remove_dev(rt2x00dev);
  283. rt2x00pci_free_reg(rt2x00dev);
  284. ieee80211_free_hw(hw);
  285. /*
  286. * Free the PCI device data.
  287. */
  288. pci_set_drvdata(pci_dev, NULL);
  289. pci_disable_device(pci_dev);
  290. pci_release_regions(pci_dev);
  291. }
  292. EXPORT_SYMBOL_GPL(rt2x00pci_remove);
  293. #ifdef CONFIG_PM
  294. int rt2x00pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  295. {
  296. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  297. struct rt2x00_dev *rt2x00dev = hw->priv;
  298. int retval;
  299. retval = rt2x00lib_suspend(rt2x00dev, state);
  300. if (retval)
  301. return retval;
  302. rt2x00pci_free_reg(rt2x00dev);
  303. pci_save_state(pci_dev);
  304. pci_disable_device(pci_dev);
  305. return pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  306. }
  307. EXPORT_SYMBOL_GPL(rt2x00pci_suspend);
  308. int rt2x00pci_resume(struct pci_dev *pci_dev)
  309. {
  310. struct ieee80211_hw *hw = pci_get_drvdata(pci_dev);
  311. struct rt2x00_dev *rt2x00dev = hw->priv;
  312. int retval;
  313. if (pci_set_power_state(pci_dev, PCI_D0) ||
  314. pci_enable_device(pci_dev) ||
  315. pci_restore_state(pci_dev)) {
  316. ERROR(rt2x00dev, "Failed to resume device.\n");
  317. return -EIO;
  318. }
  319. retval = rt2x00pci_alloc_reg(rt2x00dev);
  320. if (retval)
  321. return retval;
  322. retval = rt2x00lib_resume(rt2x00dev);
  323. if (retval)
  324. goto exit_free_reg;
  325. return 0;
  326. exit_free_reg:
  327. rt2x00pci_free_reg(rt2x00dev);
  328. return retval;
  329. }
  330. EXPORT_SYMBOL_GPL(rt2x00pci_resume);
  331. #endif /* CONFIG_PM */
  332. /*
  333. * rt2x00pci module information.
  334. */
  335. MODULE_AUTHOR(DRV_PROJECT);
  336. MODULE_VERSION(DRV_VERSION);
  337. MODULE_DESCRIPTION("rt2x00 pci library");
  338. MODULE_LICENSE("GPL");