rtl8180_dev.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Linux device driver for RTL8180 / RTL8185
  3. *
  4. * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
  5. * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
  6. *
  7. * Based on the r8180 driver, which is:
  8. * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
  9. *
  10. * Thanks to Realtek for their support!
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/eeprom_93cx6.h>
  21. #include <net/mac80211.h>
  22. #include "rtl8180.h"
  23. #include "rtl8180_rtl8225.h"
  24. #include "rtl8180_sa2400.h"
  25. #include "rtl8180_max2820.h"
  26. #include "rtl8180_grf5101.h"
  27. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  28. MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
  29. MODULE_DESCRIPTION("RTL8180 / RTL8185 PCI wireless driver");
  30. MODULE_LICENSE("GPL");
  31. static struct pci_device_id rtl8180_table[] __devinitdata = {
  32. /* rtl8185 */
  33. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8185) },
  34. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x700f) },
  35. { PCI_DEVICE(PCI_VENDOR_ID_BELKIN, 0x701f) },
  36. /* rtl8180 */
  37. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8180) },
  38. { PCI_DEVICE(0x1799, 0x6001) },
  39. { PCI_DEVICE(0x1799, 0x6020) },
  40. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x3300) },
  41. { }
  42. };
  43. MODULE_DEVICE_TABLE(pci, rtl8180_table);
  44. static const struct ieee80211_rate rtl818x_rates[] = {
  45. { .bitrate = 10, .hw_value = 0, },
  46. { .bitrate = 20, .hw_value = 1, },
  47. { .bitrate = 55, .hw_value = 2, },
  48. { .bitrate = 110, .hw_value = 3, },
  49. { .bitrate = 60, .hw_value = 4, },
  50. { .bitrate = 90, .hw_value = 5, },
  51. { .bitrate = 120, .hw_value = 6, },
  52. { .bitrate = 180, .hw_value = 7, },
  53. { .bitrate = 240, .hw_value = 8, },
  54. { .bitrate = 360, .hw_value = 9, },
  55. { .bitrate = 480, .hw_value = 10, },
  56. { .bitrate = 540, .hw_value = 11, },
  57. };
  58. static const struct ieee80211_channel rtl818x_channels[] = {
  59. { .center_freq = 2412 },
  60. { .center_freq = 2417 },
  61. { .center_freq = 2422 },
  62. { .center_freq = 2427 },
  63. { .center_freq = 2432 },
  64. { .center_freq = 2437 },
  65. { .center_freq = 2442 },
  66. { .center_freq = 2447 },
  67. { .center_freq = 2452 },
  68. { .center_freq = 2457 },
  69. { .center_freq = 2462 },
  70. { .center_freq = 2467 },
  71. { .center_freq = 2472 },
  72. { .center_freq = 2484 },
  73. };
  74. void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  75. {
  76. struct rtl8180_priv *priv = dev->priv;
  77. int i = 10;
  78. u32 buf;
  79. buf = (data << 8) | addr;
  80. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf | 0x80);
  81. while (i--) {
  82. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->PHY[0], buf);
  83. if (rtl818x_ioread8(priv, &priv->map->PHY[2]) == (data & 0xFF))
  84. return;
  85. }
  86. }
  87. static void rtl8180_handle_rx(struct ieee80211_hw *dev)
  88. {
  89. struct rtl8180_priv *priv = dev->priv;
  90. unsigned int count = 32;
  91. while (count--) {
  92. struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
  93. struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
  94. u32 flags = le32_to_cpu(entry->flags);
  95. if (flags & RTL8180_RX_DESC_FLAG_OWN)
  96. return;
  97. if (unlikely(flags & (RTL8180_RX_DESC_FLAG_DMA_FAIL |
  98. RTL8180_RX_DESC_FLAG_FOF |
  99. RTL8180_RX_DESC_FLAG_RX_ERR)))
  100. goto done;
  101. else {
  102. u32 flags2 = le32_to_cpu(entry->flags2);
  103. struct ieee80211_rx_status rx_status = {0};
  104. struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
  105. if (unlikely(!new_skb))
  106. goto done;
  107. pci_unmap_single(priv->pdev,
  108. *((dma_addr_t *)skb->cb),
  109. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  110. skb_put(skb, flags & 0xFFF);
  111. rx_status.antenna = (flags2 >> 15) & 1;
  112. /* TODO: improve signal/rssi reporting */
  113. rx_status.qual = flags2 & 0xFF;
  114. rx_status.signal = (flags2 >> 8) & 0x7F;
  115. /* XXX: is this correct? */
  116. rx_status.rate_idx = (flags >> 20) & 0xF;
  117. rx_status.freq = dev->conf.channel->center_freq;
  118. rx_status.band = dev->conf.channel->band;
  119. rx_status.mactime = le64_to_cpu(entry->tsft);
  120. rx_status.flag |= RX_FLAG_TSFT;
  121. if (flags & RTL8180_RX_DESC_FLAG_CRC32_ERR)
  122. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  123. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  124. skb = new_skb;
  125. priv->rx_buf[priv->rx_idx] = skb;
  126. *((dma_addr_t *) skb->cb) =
  127. pci_map_single(priv->pdev, skb_tail_pointer(skb),
  128. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  129. }
  130. done:
  131. entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
  132. entry->flags = cpu_to_le32(RTL8180_RX_DESC_FLAG_OWN |
  133. MAX_RX_SIZE);
  134. if (priv->rx_idx == 31)
  135. entry->flags |= cpu_to_le32(RTL8180_RX_DESC_FLAG_EOR);
  136. priv->rx_idx = (priv->rx_idx + 1) % 32;
  137. }
  138. }
  139. static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
  140. {
  141. struct rtl8180_priv *priv = dev->priv;
  142. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  143. while (skb_queue_len(&ring->queue)) {
  144. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  145. struct sk_buff *skb;
  146. struct ieee80211_tx_info *info;
  147. u32 flags = le32_to_cpu(entry->flags);
  148. if (flags & RTL8180_TX_DESC_FLAG_OWN)
  149. return;
  150. ring->idx = (ring->idx + 1) % ring->entries;
  151. skb = __skb_dequeue(&ring->queue);
  152. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  153. skb->len, PCI_DMA_TODEVICE);
  154. info = IEEE80211_SKB_CB(skb);
  155. memset(&info->status, 0, sizeof(info->status));
  156. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  157. if (flags & RTL8180_TX_DESC_FLAG_TX_OK)
  158. info->flags |= IEEE80211_TX_STAT_ACK;
  159. else
  160. info->status.excessive_retries = 1;
  161. }
  162. info->status.retry_count = flags & 0xFF;
  163. ieee80211_tx_status_irqsafe(dev, skb);
  164. if (ring->entries - skb_queue_len(&ring->queue) == 2)
  165. ieee80211_wake_queue(dev, prio);
  166. }
  167. }
  168. static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
  169. {
  170. struct ieee80211_hw *dev = dev_id;
  171. struct rtl8180_priv *priv = dev->priv;
  172. u16 reg;
  173. spin_lock(&priv->lock);
  174. reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
  175. if (unlikely(reg == 0xFFFF)) {
  176. spin_unlock(&priv->lock);
  177. return IRQ_HANDLED;
  178. }
  179. rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
  180. if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
  181. rtl8180_handle_tx(dev, 3);
  182. if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
  183. rtl8180_handle_tx(dev, 2);
  184. if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
  185. rtl8180_handle_tx(dev, 1);
  186. if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
  187. rtl8180_handle_tx(dev, 0);
  188. if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
  189. rtl8180_handle_rx(dev);
  190. spin_unlock(&priv->lock);
  191. return IRQ_HANDLED;
  192. }
  193. static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  194. {
  195. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  196. struct rtl8180_priv *priv = dev->priv;
  197. struct rtl8180_tx_ring *ring;
  198. struct rtl8180_tx_desc *entry;
  199. unsigned long flags;
  200. unsigned int idx, prio;
  201. dma_addr_t mapping;
  202. u32 tx_flags;
  203. u16 plcp_len = 0;
  204. __le16 rts_duration = 0;
  205. prio = skb_get_queue_mapping(skb);
  206. ring = &priv->tx_ring[prio];
  207. mapping = pci_map_single(priv->pdev, skb->data,
  208. skb->len, PCI_DMA_TODEVICE);
  209. tx_flags = RTL8180_TX_DESC_FLAG_OWN | RTL8180_TX_DESC_FLAG_FS |
  210. RTL8180_TX_DESC_FLAG_LS |
  211. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  212. skb->len;
  213. if (priv->r8185)
  214. tx_flags |= RTL8180_TX_DESC_FLAG_DMA |
  215. RTL8180_TX_DESC_FLAG_NO_ENC;
  216. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  217. tx_flags |= RTL8180_TX_DESC_FLAG_RTS;
  218. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  219. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  220. tx_flags |= RTL8180_TX_DESC_FLAG_CTS;
  221. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  222. }
  223. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  224. rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
  225. info);
  226. if (!priv->r8185) {
  227. unsigned int remainder;
  228. plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
  229. (ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  230. remainder = (16 * (skb->len + 4)) %
  231. ((ieee80211_get_tx_rate(dev, info)->bitrate * 2) / 10);
  232. if (remainder > 0 && remainder <= 6)
  233. plcp_len |= 1 << 15;
  234. }
  235. spin_lock_irqsave(&priv->lock, flags);
  236. idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
  237. entry = &ring->desc[idx];
  238. entry->rts_duration = rts_duration;
  239. entry->plcp_len = cpu_to_le16(plcp_len);
  240. entry->tx_buf = cpu_to_le32(mapping);
  241. entry->frame_len = cpu_to_le32(skb->len);
  242. entry->flags2 = info->control.alt_retry_rate_idx >= 0 ?
  243. ieee80211_get_alt_retry_rate(dev, info)->bitrate << 4 : 0;
  244. entry->retry_limit = info->control.retry_limit;
  245. entry->flags = cpu_to_le32(tx_flags);
  246. __skb_queue_tail(&ring->queue, skb);
  247. if (ring->entries - skb_queue_len(&ring->queue) < 2)
  248. ieee80211_stop_queue(dev, skb_get_queue_mapping(skb));
  249. spin_unlock_irqrestore(&priv->lock, flags);
  250. rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
  251. return 0;
  252. }
  253. void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
  254. {
  255. u8 reg;
  256. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  257. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  258. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  259. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  260. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, anaparam);
  261. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  262. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  263. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  264. }
  265. static int rtl8180_init_hw(struct ieee80211_hw *dev)
  266. {
  267. struct rtl8180_priv *priv = dev->priv;
  268. u16 reg;
  269. rtl818x_iowrite8(priv, &priv->map->CMD, 0);
  270. rtl818x_ioread8(priv, &priv->map->CMD);
  271. msleep(10);
  272. /* reset */
  273. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  274. rtl818x_ioread8(priv, &priv->map->CMD);
  275. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  276. reg &= (1 << 1);
  277. reg |= RTL818X_CMD_RESET;
  278. rtl818x_iowrite8(priv, &priv->map->CMD, RTL818X_CMD_RESET);
  279. rtl818x_ioread8(priv, &priv->map->CMD);
  280. msleep(200);
  281. /* check success of reset */
  282. if (rtl818x_ioread8(priv, &priv->map->CMD) & RTL818X_CMD_RESET) {
  283. printk(KERN_ERR "%s: reset timeout!\n", wiphy_name(dev->wiphy));
  284. return -ETIMEDOUT;
  285. }
  286. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  287. rtl818x_ioread8(priv, &priv->map->CMD);
  288. msleep(200);
  289. if (rtl818x_ioread8(priv, &priv->map->CONFIG3) & (1 << 3)) {
  290. /* For cardbus */
  291. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  292. reg |= 1 << 1;
  293. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  294. reg = rtl818x_ioread16(priv, &priv->map->FEMR);
  295. reg |= (1 << 15) | (1 << 14) | (1 << 4);
  296. rtl818x_iowrite16(priv, &priv->map->FEMR, reg);
  297. }
  298. rtl818x_iowrite8(priv, &priv->map->MSR, 0);
  299. if (!priv->r8185)
  300. rtl8180_set_anaparam(priv, priv->anaparam);
  301. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  302. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  303. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  304. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  305. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  306. /* TODO: necessary? specs indicate not */
  307. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  308. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  309. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
  310. if (priv->r8185) {
  311. reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
  312. rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
  313. }
  314. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  315. /* TODO: set CONFIG5 for calibrating AGC on rtl8180 + philips radio? */
  316. /* TODO: turn off hw wep on rtl8180 */
  317. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  318. if (priv->r8185) {
  319. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  320. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  321. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  322. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  323. /* TODO: set ClkRun enable? necessary? */
  324. reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
  325. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, reg & ~(1 << 6));
  326. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  327. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  328. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
  329. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  330. } else {
  331. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
  332. rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
  333. rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
  334. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, 0x4C);
  335. }
  336. priv->rf->init(dev);
  337. if (priv->r8185)
  338. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  339. return 0;
  340. }
  341. static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
  342. {
  343. struct rtl8180_priv *priv = dev->priv;
  344. struct rtl8180_rx_desc *entry;
  345. int i;
  346. priv->rx_ring = pci_alloc_consistent(priv->pdev,
  347. sizeof(*priv->rx_ring) * 32,
  348. &priv->rx_ring_dma);
  349. if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
  350. printk(KERN_ERR "%s: Cannot allocate RX ring\n",
  351. wiphy_name(dev->wiphy));
  352. return -ENOMEM;
  353. }
  354. memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
  355. priv->rx_idx = 0;
  356. for (i = 0; i < 32; i++) {
  357. struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
  358. dma_addr_t *mapping;
  359. entry = &priv->rx_ring[i];
  360. if (!skb)
  361. return 0;
  362. priv->rx_buf[i] = skb;
  363. mapping = (dma_addr_t *)skb->cb;
  364. *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
  365. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  366. entry->rx_buf = cpu_to_le32(*mapping);
  367. entry->flags = cpu_to_le32(RTL8180_RX_DESC_FLAG_OWN |
  368. MAX_RX_SIZE);
  369. }
  370. entry->flags |= cpu_to_le32(RTL8180_RX_DESC_FLAG_EOR);
  371. return 0;
  372. }
  373. static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
  374. {
  375. struct rtl8180_priv *priv = dev->priv;
  376. int i;
  377. for (i = 0; i < 32; i++) {
  378. struct sk_buff *skb = priv->rx_buf[i];
  379. if (!skb)
  380. continue;
  381. pci_unmap_single(priv->pdev,
  382. *((dma_addr_t *)skb->cb),
  383. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  384. kfree_skb(skb);
  385. }
  386. pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
  387. priv->rx_ring, priv->rx_ring_dma);
  388. priv->rx_ring = NULL;
  389. }
  390. static int rtl8180_init_tx_ring(struct ieee80211_hw *dev,
  391. unsigned int prio, unsigned int entries)
  392. {
  393. struct rtl8180_priv *priv = dev->priv;
  394. struct rtl8180_tx_desc *ring;
  395. dma_addr_t dma;
  396. int i;
  397. ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
  398. if (!ring || (unsigned long)ring & 0xFF) {
  399. printk(KERN_ERR "%s: Cannot allocate TX ring (prio = %d)\n",
  400. wiphy_name(dev->wiphy), prio);
  401. return -ENOMEM;
  402. }
  403. memset(ring, 0, sizeof(*ring)*entries);
  404. priv->tx_ring[prio].desc = ring;
  405. priv->tx_ring[prio].dma = dma;
  406. priv->tx_ring[prio].idx = 0;
  407. priv->tx_ring[prio].entries = entries;
  408. skb_queue_head_init(&priv->tx_ring[prio].queue);
  409. for (i = 0; i < entries; i++)
  410. ring[i].next_tx_desc =
  411. cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
  412. return 0;
  413. }
  414. static void rtl8180_free_tx_ring(struct ieee80211_hw *dev, unsigned int prio)
  415. {
  416. struct rtl8180_priv *priv = dev->priv;
  417. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  418. while (skb_queue_len(&ring->queue)) {
  419. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  420. struct sk_buff *skb = __skb_dequeue(&ring->queue);
  421. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  422. skb->len, PCI_DMA_TODEVICE);
  423. kfree_skb(skb);
  424. ring->idx = (ring->idx + 1) % ring->entries;
  425. }
  426. pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
  427. ring->desc, ring->dma);
  428. ring->desc = NULL;
  429. }
  430. static int rtl8180_start(struct ieee80211_hw *dev)
  431. {
  432. struct rtl8180_priv *priv = dev->priv;
  433. int ret, i;
  434. u32 reg;
  435. ret = rtl8180_init_rx_ring(dev);
  436. if (ret)
  437. return ret;
  438. for (i = 0; i < 4; i++)
  439. if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
  440. goto err_free_rings;
  441. ret = rtl8180_init_hw(dev);
  442. if (ret)
  443. goto err_free_rings;
  444. rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
  445. rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
  446. rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
  447. rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
  448. rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
  449. ret = request_irq(priv->pdev->irq, &rtl8180_interrupt,
  450. IRQF_SHARED, KBUILD_MODNAME, dev);
  451. if (ret) {
  452. printk(KERN_ERR "%s: failed to register IRQ handler\n",
  453. wiphy_name(dev->wiphy));
  454. goto err_free_rings;
  455. }
  456. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  457. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  458. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  459. reg = RTL818X_RX_CONF_ONLYERLPKT |
  460. RTL818X_RX_CONF_RX_AUTORESETPHY |
  461. RTL818X_RX_CONF_MGMT |
  462. RTL818X_RX_CONF_DATA |
  463. (7 << 8 /* MAX RX DMA */) |
  464. RTL818X_RX_CONF_BROADCAST |
  465. RTL818X_RX_CONF_NICMAC;
  466. if (priv->r8185)
  467. reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
  468. else {
  469. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
  470. ? RTL818X_RX_CONF_CSDM1 : 0;
  471. reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE2)
  472. ? RTL818X_RX_CONF_CSDM2 : 0;
  473. }
  474. priv->rx_conf = reg;
  475. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  476. if (priv->r8185) {
  477. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  478. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  479. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  480. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  481. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  482. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  483. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  484. reg |= RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  485. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  486. /* disable early TX */
  487. rtl818x_iowrite8(priv, (u8 __iomem *)priv->map + 0xec, 0x3f);
  488. }
  489. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  490. reg |= (6 << 21 /* MAX TX DMA */) |
  491. RTL818X_TX_CONF_NO_ICV;
  492. if (priv->r8185)
  493. reg &= ~RTL818X_TX_CONF_PROBE_DTS;
  494. else
  495. reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
  496. /* different meaning, same value on both rtl8185 and rtl8180 */
  497. reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
  498. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  499. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  500. reg |= RTL818X_CMD_RX_ENABLE;
  501. reg |= RTL818X_CMD_TX_ENABLE;
  502. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  503. priv->mode = IEEE80211_IF_TYPE_MNTR;
  504. return 0;
  505. err_free_rings:
  506. rtl8180_free_rx_ring(dev);
  507. for (i = 0; i < 4; i++)
  508. if (priv->tx_ring[i].desc)
  509. rtl8180_free_tx_ring(dev, i);
  510. return ret;
  511. }
  512. static void rtl8180_stop(struct ieee80211_hw *dev)
  513. {
  514. struct rtl8180_priv *priv = dev->priv;
  515. u8 reg;
  516. int i;
  517. priv->mode = IEEE80211_IF_TYPE_INVALID;
  518. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  519. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  520. reg &= ~RTL818X_CMD_TX_ENABLE;
  521. reg &= ~RTL818X_CMD_RX_ENABLE;
  522. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  523. priv->rf->stop(dev);
  524. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  525. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  526. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  527. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  528. free_irq(priv->pdev->irq, dev);
  529. rtl8180_free_rx_ring(dev);
  530. for (i = 0; i < 4; i++)
  531. rtl8180_free_tx_ring(dev, i);
  532. }
  533. static int rtl8180_add_interface(struct ieee80211_hw *dev,
  534. struct ieee80211_if_init_conf *conf)
  535. {
  536. struct rtl8180_priv *priv = dev->priv;
  537. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  538. return -EOPNOTSUPP;
  539. switch (conf->type) {
  540. case IEEE80211_IF_TYPE_STA:
  541. priv->mode = conf->type;
  542. break;
  543. default:
  544. return -EOPNOTSUPP;
  545. }
  546. priv->vif = conf->vif;
  547. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  548. rtl818x_iowrite32(priv, (__le32 __iomem *)&priv->map->MAC[0],
  549. le32_to_cpu(*(__le32 *)conf->mac_addr));
  550. rtl818x_iowrite16(priv, (__le16 __iomem *)&priv->map->MAC[4],
  551. le16_to_cpu(*(__le16 *)(conf->mac_addr + 4)));
  552. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  553. return 0;
  554. }
  555. static void rtl8180_remove_interface(struct ieee80211_hw *dev,
  556. struct ieee80211_if_init_conf *conf)
  557. {
  558. struct rtl8180_priv *priv = dev->priv;
  559. priv->mode = IEEE80211_IF_TYPE_MNTR;
  560. priv->vif = NULL;
  561. }
  562. static int rtl8180_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  563. {
  564. struct rtl8180_priv *priv = dev->priv;
  565. priv->rf->set_chan(dev, conf);
  566. return 0;
  567. }
  568. static int rtl8180_config_interface(struct ieee80211_hw *dev,
  569. struct ieee80211_vif *vif,
  570. struct ieee80211_if_conf *conf)
  571. {
  572. struct rtl8180_priv *priv = dev->priv;
  573. int i;
  574. for (i = 0; i < ETH_ALEN; i++)
  575. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  576. if (is_valid_ether_addr(conf->bssid))
  577. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_INFRA);
  578. else
  579. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_NO_LINK);
  580. return 0;
  581. }
  582. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  583. unsigned int changed_flags,
  584. unsigned int *total_flags,
  585. int mc_count, struct dev_addr_list *mclist)
  586. {
  587. struct rtl8180_priv *priv = dev->priv;
  588. if (changed_flags & FIF_FCSFAIL)
  589. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  590. if (changed_flags & FIF_CONTROL)
  591. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  592. if (changed_flags & FIF_OTHER_BSS)
  593. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  594. if (*total_flags & FIF_ALLMULTI || mc_count > 0)
  595. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  596. else
  597. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  598. *total_flags = 0;
  599. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  600. *total_flags |= FIF_FCSFAIL;
  601. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  602. *total_flags |= FIF_CONTROL;
  603. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  604. *total_flags |= FIF_OTHER_BSS;
  605. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  606. *total_flags |= FIF_ALLMULTI;
  607. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  608. }
  609. static const struct ieee80211_ops rtl8180_ops = {
  610. .tx = rtl8180_tx,
  611. .start = rtl8180_start,
  612. .stop = rtl8180_stop,
  613. .add_interface = rtl8180_add_interface,
  614. .remove_interface = rtl8180_remove_interface,
  615. .config = rtl8180_config,
  616. .config_interface = rtl8180_config_interface,
  617. .configure_filter = rtl8180_configure_filter,
  618. };
  619. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  620. {
  621. struct ieee80211_hw *dev = eeprom->data;
  622. struct rtl8180_priv *priv = dev->priv;
  623. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  624. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  625. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  626. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  627. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  628. }
  629. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  630. {
  631. struct ieee80211_hw *dev = eeprom->data;
  632. struct rtl8180_priv *priv = dev->priv;
  633. u8 reg = 2 << 6;
  634. if (eeprom->reg_data_in)
  635. reg |= RTL818X_EEPROM_CMD_WRITE;
  636. if (eeprom->reg_data_out)
  637. reg |= RTL818X_EEPROM_CMD_READ;
  638. if (eeprom->reg_data_clock)
  639. reg |= RTL818X_EEPROM_CMD_CK;
  640. if (eeprom->reg_chip_select)
  641. reg |= RTL818X_EEPROM_CMD_CS;
  642. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  643. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  644. udelay(10);
  645. }
  646. static int __devinit rtl8180_probe(struct pci_dev *pdev,
  647. const struct pci_device_id *id)
  648. {
  649. struct ieee80211_hw *dev;
  650. struct rtl8180_priv *priv;
  651. unsigned long mem_addr, mem_len;
  652. unsigned int io_addr, io_len;
  653. int err, i;
  654. struct eeprom_93cx6 eeprom;
  655. const char *chip_name, *rf_name = NULL;
  656. u32 reg;
  657. u16 eeprom_val;
  658. DECLARE_MAC_BUF(mac);
  659. err = pci_enable_device(pdev);
  660. if (err) {
  661. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  662. pci_name(pdev));
  663. return err;
  664. }
  665. err = pci_request_regions(pdev, KBUILD_MODNAME);
  666. if (err) {
  667. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  668. pci_name(pdev));
  669. return err;
  670. }
  671. io_addr = pci_resource_start(pdev, 0);
  672. io_len = pci_resource_len(pdev, 0);
  673. mem_addr = pci_resource_start(pdev, 1);
  674. mem_len = pci_resource_len(pdev, 1);
  675. if (mem_len < sizeof(struct rtl818x_csr) ||
  676. io_len < sizeof(struct rtl818x_csr)) {
  677. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  678. pci_name(pdev));
  679. err = -ENOMEM;
  680. goto err_free_reg;
  681. }
  682. if ((err = pci_set_dma_mask(pdev, 0xFFFFFF00ULL)) ||
  683. (err = pci_set_consistent_dma_mask(pdev, 0xFFFFFF00ULL))) {
  684. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  685. pci_name(pdev));
  686. goto err_free_reg;
  687. }
  688. pci_set_master(pdev);
  689. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  690. if (!dev) {
  691. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  692. pci_name(pdev));
  693. err = -ENOMEM;
  694. goto err_free_reg;
  695. }
  696. priv = dev->priv;
  697. priv->pdev = pdev;
  698. SET_IEEE80211_DEV(dev, &pdev->dev);
  699. pci_set_drvdata(pdev, dev);
  700. priv->map = pci_iomap(pdev, 1, mem_len);
  701. if (!priv->map)
  702. priv->map = pci_iomap(pdev, 0, io_len);
  703. if (!priv->map) {
  704. printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
  705. pci_name(pdev));
  706. goto err_free_dev;
  707. }
  708. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  709. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  710. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  711. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  712. priv->band.band = IEEE80211_BAND_2GHZ;
  713. priv->band.channels = priv->channels;
  714. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  715. priv->band.bitrates = priv->rates;
  716. priv->band.n_bitrates = 4;
  717. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  718. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  719. IEEE80211_HW_RX_INCLUDES_FCS |
  720. IEEE80211_HW_SIGNAL_UNSPEC;
  721. dev->queues = 1;
  722. dev->max_signal = 65;
  723. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  724. reg &= RTL818X_TX_CONF_HWVER_MASK;
  725. switch (reg) {
  726. case RTL818X_TX_CONF_R8180_ABCD:
  727. chip_name = "RTL8180";
  728. break;
  729. case RTL818X_TX_CONF_R8180_F:
  730. chip_name = "RTL8180vF";
  731. break;
  732. case RTL818X_TX_CONF_R8185_ABC:
  733. chip_name = "RTL8185";
  734. break;
  735. case RTL818X_TX_CONF_R8185_D:
  736. chip_name = "RTL8185vD";
  737. break;
  738. default:
  739. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  740. pci_name(pdev), reg >> 25);
  741. goto err_iounmap;
  742. }
  743. priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
  744. if (priv->r8185) {
  745. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  746. pci_try_set_mwi(pdev);
  747. }
  748. eeprom.data = dev;
  749. eeprom.register_read = rtl8180_eeprom_register_read;
  750. eeprom.register_write = rtl8180_eeprom_register_write;
  751. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  752. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  753. else
  754. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  755. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
  756. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  757. udelay(10);
  758. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  759. eeprom_val &= 0xFF;
  760. switch (eeprom_val) {
  761. case 1: rf_name = "Intersil";
  762. break;
  763. case 2: rf_name = "RFMD";
  764. break;
  765. case 3: priv->rf = &sa2400_rf_ops;
  766. break;
  767. case 4: priv->rf = &max2820_rf_ops;
  768. break;
  769. case 5: priv->rf = &grf5101_rf_ops;
  770. break;
  771. case 9: priv->rf = rtl8180_detect_rf(dev);
  772. break;
  773. case 10:
  774. rf_name = "RTL8255";
  775. break;
  776. default:
  777. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  778. pci_name(pdev), eeprom_val);
  779. goto err_iounmap;
  780. }
  781. if (!priv->rf) {
  782. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  783. pci_name(pdev), rf_name);
  784. goto err_iounmap;
  785. }
  786. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  787. priv->csthreshold = eeprom_val >> 8;
  788. if (!priv->r8185) {
  789. __le32 anaparam;
  790. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  791. priv->anaparam = le32_to_cpu(anaparam);
  792. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  793. }
  794. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)dev->wiphy->perm_addr, 3);
  795. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  796. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  797. " randomly generated MAC addr\n", pci_name(pdev));
  798. random_ether_addr(dev->wiphy->perm_addr);
  799. }
  800. /* CCK TX power */
  801. for (i = 0; i < 14; i += 2) {
  802. u16 txpwr;
  803. eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
  804. priv->channels[i].hw_value = txpwr & 0xFF;
  805. priv->channels[i + 1].hw_value = txpwr >> 8;
  806. }
  807. /* OFDM TX power */
  808. if (priv->r8185) {
  809. for (i = 0; i < 14; i += 2) {
  810. u16 txpwr;
  811. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  812. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  813. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  814. }
  815. }
  816. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  817. spin_lock_init(&priv->lock);
  818. err = ieee80211_register_hw(dev);
  819. if (err) {
  820. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  821. pci_name(pdev));
  822. goto err_iounmap;
  823. }
  824. printk(KERN_INFO "%s: hwaddr %s, %s + %s\n",
  825. wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
  826. chip_name, priv->rf->name);
  827. return 0;
  828. err_iounmap:
  829. iounmap(priv->map);
  830. err_free_dev:
  831. pci_set_drvdata(pdev, NULL);
  832. ieee80211_free_hw(dev);
  833. err_free_reg:
  834. pci_release_regions(pdev);
  835. pci_disable_device(pdev);
  836. return err;
  837. }
  838. static void __devexit rtl8180_remove(struct pci_dev *pdev)
  839. {
  840. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  841. struct rtl8180_priv *priv;
  842. if (!dev)
  843. return;
  844. ieee80211_unregister_hw(dev);
  845. priv = dev->priv;
  846. pci_iounmap(pdev, priv->map);
  847. pci_release_regions(pdev);
  848. pci_disable_device(pdev);
  849. ieee80211_free_hw(dev);
  850. }
  851. #ifdef CONFIG_PM
  852. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  853. {
  854. pci_save_state(pdev);
  855. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  856. return 0;
  857. }
  858. static int rtl8180_resume(struct pci_dev *pdev)
  859. {
  860. pci_set_power_state(pdev, PCI_D0);
  861. pci_restore_state(pdev);
  862. return 0;
  863. }
  864. #endif /* CONFIG_PM */
  865. static struct pci_driver rtl8180_driver = {
  866. .name = KBUILD_MODNAME,
  867. .id_table = rtl8180_table,
  868. .probe = rtl8180_probe,
  869. .remove = __devexit_p(rtl8180_remove),
  870. #ifdef CONFIG_PM
  871. .suspend = rtl8180_suspend,
  872. .resume = rtl8180_resume,
  873. #endif /* CONFIG_PM */
  874. };
  875. static int __init rtl8180_init(void)
  876. {
  877. return pci_register_driver(&rtl8180_driver);
  878. }
  879. static void __exit rtl8180_exit(void)
  880. {
  881. pci_unregister_driver(&rtl8180_driver);
  882. }
  883. module_init(rtl8180_init);
  884. module_exit(rtl8180_exit);