rtl8180_dev.c 30 KB

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