rt2x00pci.c 8.8 KB

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