rtl8180_dev.c 30 KB

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