rtl8180_dev.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  124. ieee80211_rx_irqsafe(dev, skb);
  125. skb = new_skb;
  126. priv->rx_buf[priv->rx_idx] = skb;
  127. *((dma_addr_t *) skb->cb) =
  128. pci_map_single(priv->pdev, skb_tail_pointer(skb),
  129. MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
  130. }
  131. done:
  132. entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
  133. entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
  134. MAX_RX_SIZE);
  135. if (priv->rx_idx == 31)
  136. entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
  137. priv->rx_idx = (priv->rx_idx + 1) % 32;
  138. }
  139. }
  140. static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
  141. {
  142. struct rtl8180_priv *priv = dev->priv;
  143. struct rtl8180_tx_ring *ring = &priv->tx_ring[prio];
  144. while (skb_queue_len(&ring->queue)) {
  145. struct rtl8180_tx_desc *entry = &ring->desc[ring->idx];
  146. struct sk_buff *skb;
  147. struct ieee80211_tx_info *info;
  148. u32 flags = le32_to_cpu(entry->flags);
  149. if (flags & RTL818X_TX_DESC_FLAG_OWN)
  150. return;
  151. ring->idx = (ring->idx + 1) % ring->entries;
  152. skb = __skb_dequeue(&ring->queue);
  153. pci_unmap_single(priv->pdev, le32_to_cpu(entry->tx_buf),
  154. skb->len, PCI_DMA_TODEVICE);
  155. info = IEEE80211_SKB_CB(skb);
  156. ieee80211_tx_info_clear_status(info);
  157. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  158. (flags & RTL818X_TX_DESC_FLAG_TX_OK))
  159. info->flags |= IEEE80211_TX_STAT_ACK;
  160. info->status.rates[0].count = (flags & 0xFF) + 1;
  161. ieee80211_tx_status_irqsafe(dev, skb);
  162. if (ring->entries - skb_queue_len(&ring->queue) == 2)
  163. ieee80211_wake_queue(dev, prio);
  164. }
  165. }
  166. static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
  167. {
  168. struct ieee80211_hw *dev = dev_id;
  169. struct rtl8180_priv *priv = dev->priv;
  170. u16 reg;
  171. spin_lock(&priv->lock);
  172. reg = rtl818x_ioread16(priv, &priv->map->INT_STATUS);
  173. if (unlikely(reg == 0xFFFF)) {
  174. spin_unlock(&priv->lock);
  175. return IRQ_HANDLED;
  176. }
  177. rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
  178. if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
  179. rtl8180_handle_tx(dev, 3);
  180. if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
  181. rtl8180_handle_tx(dev, 2);
  182. if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
  183. rtl8180_handle_tx(dev, 1);
  184. if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
  185. rtl8180_handle_tx(dev, 0);
  186. if (reg & (RTL818X_INT_RX_OK | RTL818X_INT_RX_ERR))
  187. rtl8180_handle_rx(dev);
  188. spin_unlock(&priv->lock);
  189. return IRQ_HANDLED;
  190. }
  191. static int rtl8180_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  192. {
  193. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  194. struct rtl8180_priv *priv = dev->priv;
  195. struct rtl8180_tx_ring *ring;
  196. struct rtl8180_tx_desc *entry;
  197. unsigned long flags;
  198. unsigned int idx, prio;
  199. dma_addr_t mapping;
  200. u32 tx_flags;
  201. u8 rc_flags;
  202. u16 plcp_len = 0;
  203. __le16 rts_duration = 0;
  204. prio = skb_get_queue_mapping(skb);
  205. ring = &priv->tx_ring[prio];
  206. mapping = pci_map_single(priv->pdev, skb->data,
  207. skb->len, PCI_DMA_TODEVICE);
  208. tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
  209. RTL818X_TX_DESC_FLAG_LS |
  210. (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
  211. skb->len;
  212. if (priv->r8185)
  213. tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
  214. RTL818X_TX_DESC_FLAG_NO_ENC;
  215. rc_flags = info->control.rates[0].flags;
  216. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  217. tx_flags |= RTL818X_TX_DESC_FLAG_RTS;
  218. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  219. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  220. tx_flags |= RTL818X_TX_DESC_FLAG_CTS;
  221. tx_flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  222. }
  223. if (rc_flags & IEEE80211_TX_RC_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 <= 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.rates[1].idx >= 0 ?
  243. ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
  244. entry->retry_limit = info->control.rates[0].count;
  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(RTL818X_RX_DESC_FLAG_OWN |
  368. MAX_RX_SIZE);
  369. }
  370. entry->flags |= cpu_to_le32(RTL818X_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 = NL80211_IFTYPE_MONITOR;
  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 = NL80211_IFTYPE_UNSPECIFIED;
  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 != NL80211_IFTYPE_MONITOR)
  538. return -EOPNOTSUPP;
  539. switch (conf->type) {
  540. case NL80211_IFTYPE_STATION:
  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 = NL80211_IFTYPE_MONITOR;
  560. priv->vif = NULL;
  561. }
  562. static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
  563. {
  564. struct rtl8180_priv *priv = dev->priv;
  565. struct ieee80211_conf *conf = &dev->conf;
  566. priv->rf->set_chan(dev, conf);
  567. return 0;
  568. }
  569. static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
  570. struct ieee80211_vif *vif,
  571. struct ieee80211_bss_conf *info,
  572. u32 changed)
  573. {
  574. struct rtl8180_priv *priv = dev->priv;
  575. int i;
  576. if (changed & BSS_CHANGED_BSSID) {
  577. for (i = 0; i < ETH_ALEN; i++)
  578. rtl818x_iowrite8(priv, &priv->map->BSSID[i],
  579. info->bssid[i]);
  580. if (is_valid_ether_addr(info->bssid))
  581. rtl818x_iowrite8(priv, &priv->map->MSR,
  582. RTL818X_MSR_INFRA);
  583. else
  584. rtl818x_iowrite8(priv, &priv->map->MSR,
  585. RTL818X_MSR_NO_LINK);
  586. }
  587. if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
  588. priv->rf->conf_erp(dev, info);
  589. }
  590. static u64 rtl8180_prepare_multicast(struct ieee80211_hw *dev, int mc_count,
  591. struct dev_addr_list *mc_list)
  592. {
  593. return mc_count;
  594. }
  595. static void rtl8180_configure_filter(struct ieee80211_hw *dev,
  596. unsigned int changed_flags,
  597. unsigned int *total_flags,
  598. u64 multicast)
  599. {
  600. struct rtl8180_priv *priv = dev->priv;
  601. if (changed_flags & FIF_FCSFAIL)
  602. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  603. if (changed_flags & FIF_CONTROL)
  604. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  605. if (changed_flags & FIF_OTHER_BSS)
  606. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  607. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  608. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  609. else
  610. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  611. *total_flags = 0;
  612. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  613. *total_flags |= FIF_FCSFAIL;
  614. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  615. *total_flags |= FIF_CONTROL;
  616. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  617. *total_flags |= FIF_OTHER_BSS;
  618. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  619. *total_flags |= FIF_ALLMULTI;
  620. rtl818x_iowrite32(priv, &priv->map->RX_CONF, priv->rx_conf);
  621. }
  622. static const struct ieee80211_ops rtl8180_ops = {
  623. .tx = rtl8180_tx,
  624. .start = rtl8180_start,
  625. .stop = rtl8180_stop,
  626. .add_interface = rtl8180_add_interface,
  627. .remove_interface = rtl8180_remove_interface,
  628. .config = rtl8180_config,
  629. .bss_info_changed = rtl8180_bss_info_changed,
  630. .prepare_multicast = rtl8180_prepare_multicast,
  631. .configure_filter = rtl8180_configure_filter,
  632. };
  633. static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  634. {
  635. struct ieee80211_hw *dev = eeprom->data;
  636. struct rtl8180_priv *priv = dev->priv;
  637. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  638. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  639. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  640. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  641. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  642. }
  643. static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  644. {
  645. struct ieee80211_hw *dev = eeprom->data;
  646. struct rtl8180_priv *priv = dev->priv;
  647. u8 reg = 2 << 6;
  648. if (eeprom->reg_data_in)
  649. reg |= RTL818X_EEPROM_CMD_WRITE;
  650. if (eeprom->reg_data_out)
  651. reg |= RTL818X_EEPROM_CMD_READ;
  652. if (eeprom->reg_data_clock)
  653. reg |= RTL818X_EEPROM_CMD_CK;
  654. if (eeprom->reg_chip_select)
  655. reg |= RTL818X_EEPROM_CMD_CS;
  656. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  657. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  658. udelay(10);
  659. }
  660. static int __devinit rtl8180_probe(struct pci_dev *pdev,
  661. const struct pci_device_id *id)
  662. {
  663. struct ieee80211_hw *dev;
  664. struct rtl8180_priv *priv;
  665. unsigned long mem_addr, mem_len;
  666. unsigned int io_addr, io_len;
  667. int err, i;
  668. struct eeprom_93cx6 eeprom;
  669. const char *chip_name, *rf_name = NULL;
  670. u32 reg;
  671. u16 eeprom_val;
  672. err = pci_enable_device(pdev);
  673. if (err) {
  674. printk(KERN_ERR "%s (rtl8180): Cannot enable new PCI device\n",
  675. pci_name(pdev));
  676. return err;
  677. }
  678. err = pci_request_regions(pdev, KBUILD_MODNAME);
  679. if (err) {
  680. printk(KERN_ERR "%s (rtl8180): Cannot obtain PCI resources\n",
  681. pci_name(pdev));
  682. return err;
  683. }
  684. io_addr = pci_resource_start(pdev, 0);
  685. io_len = pci_resource_len(pdev, 0);
  686. mem_addr = pci_resource_start(pdev, 1);
  687. mem_len = pci_resource_len(pdev, 1);
  688. if (mem_len < sizeof(struct rtl818x_csr) ||
  689. io_len < sizeof(struct rtl818x_csr)) {
  690. printk(KERN_ERR "%s (rtl8180): Too short PCI resources\n",
  691. pci_name(pdev));
  692. err = -ENOMEM;
  693. goto err_free_reg;
  694. }
  695. if ((err = pci_set_dma_mask(pdev, 0xFFFFFF00ULL)) ||
  696. (err = pci_set_consistent_dma_mask(pdev, 0xFFFFFF00ULL))) {
  697. printk(KERN_ERR "%s (rtl8180): No suitable DMA available\n",
  698. pci_name(pdev));
  699. goto err_free_reg;
  700. }
  701. pci_set_master(pdev);
  702. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8180_ops);
  703. if (!dev) {
  704. printk(KERN_ERR "%s (rtl8180): ieee80211 alloc failed\n",
  705. pci_name(pdev));
  706. err = -ENOMEM;
  707. goto err_free_reg;
  708. }
  709. priv = dev->priv;
  710. priv->pdev = pdev;
  711. dev->max_rates = 2;
  712. SET_IEEE80211_DEV(dev, &pdev->dev);
  713. pci_set_drvdata(pdev, dev);
  714. priv->map = pci_iomap(pdev, 1, mem_len);
  715. if (!priv->map)
  716. priv->map = pci_iomap(pdev, 0, io_len);
  717. if (!priv->map) {
  718. printk(KERN_ERR "%s (rtl8180): Cannot map device memory\n",
  719. pci_name(pdev));
  720. goto err_free_dev;
  721. }
  722. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  723. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  724. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  725. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  726. priv->band.band = IEEE80211_BAND_2GHZ;
  727. priv->band.channels = priv->channels;
  728. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  729. priv->band.bitrates = priv->rates;
  730. priv->band.n_bitrates = 4;
  731. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  732. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  733. IEEE80211_HW_RX_INCLUDES_FCS |
  734. IEEE80211_HW_SIGNAL_UNSPEC;
  735. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  736. dev->queues = 1;
  737. dev->max_signal = 65;
  738. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  739. reg &= RTL818X_TX_CONF_HWVER_MASK;
  740. switch (reg) {
  741. case RTL818X_TX_CONF_R8180_ABCD:
  742. chip_name = "RTL8180";
  743. break;
  744. case RTL818X_TX_CONF_R8180_F:
  745. chip_name = "RTL8180vF";
  746. break;
  747. case RTL818X_TX_CONF_R8185_ABC:
  748. chip_name = "RTL8185";
  749. break;
  750. case RTL818X_TX_CONF_R8185_D:
  751. chip_name = "RTL8185vD";
  752. break;
  753. default:
  754. printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
  755. pci_name(pdev), reg >> 25);
  756. goto err_iounmap;
  757. }
  758. priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
  759. if (priv->r8185) {
  760. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  761. pci_try_set_mwi(pdev);
  762. }
  763. eeprom.data = dev;
  764. eeprom.register_read = rtl8180_eeprom_register_read;
  765. eeprom.register_write = rtl8180_eeprom_register_write;
  766. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  767. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  768. else
  769. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  770. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
  771. rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  772. udelay(10);
  773. eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
  774. eeprom_val &= 0xFF;
  775. switch (eeprom_val) {
  776. case 1: rf_name = "Intersil";
  777. break;
  778. case 2: rf_name = "RFMD";
  779. break;
  780. case 3: priv->rf = &sa2400_rf_ops;
  781. break;
  782. case 4: priv->rf = &max2820_rf_ops;
  783. break;
  784. case 5: priv->rf = &grf5101_rf_ops;
  785. break;
  786. case 9: priv->rf = rtl8180_detect_rf(dev);
  787. break;
  788. case 10:
  789. rf_name = "RTL8255";
  790. break;
  791. default:
  792. printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
  793. pci_name(pdev), eeprom_val);
  794. goto err_iounmap;
  795. }
  796. if (!priv->rf) {
  797. printk(KERN_ERR "%s (rtl8180): %s RF frontend not supported!\n",
  798. pci_name(pdev), rf_name);
  799. goto err_iounmap;
  800. }
  801. eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
  802. priv->csthreshold = eeprom_val >> 8;
  803. if (!priv->r8185) {
  804. __le32 anaparam;
  805. eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
  806. priv->anaparam = le32_to_cpu(anaparam);
  807. eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
  808. }
  809. eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)dev->wiphy->perm_addr, 3);
  810. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  811. printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
  812. " randomly generated MAC addr\n", pci_name(pdev));
  813. random_ether_addr(dev->wiphy->perm_addr);
  814. }
  815. /* CCK TX power */
  816. for (i = 0; i < 14; i += 2) {
  817. u16 txpwr;
  818. eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
  819. priv->channels[i].hw_value = txpwr & 0xFF;
  820. priv->channels[i + 1].hw_value = txpwr >> 8;
  821. }
  822. /* OFDM TX power */
  823. if (priv->r8185) {
  824. for (i = 0; i < 14; i += 2) {
  825. u16 txpwr;
  826. eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
  827. priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
  828. priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
  829. }
  830. }
  831. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  832. spin_lock_init(&priv->lock);
  833. err = ieee80211_register_hw(dev);
  834. if (err) {
  835. printk(KERN_ERR "%s (rtl8180): Cannot register device\n",
  836. pci_name(pdev));
  837. goto err_iounmap;
  838. }
  839. printk(KERN_INFO "%s: hwaddr %pM, %s + %s\n",
  840. wiphy_name(dev->wiphy), dev->wiphy->perm_addr,
  841. chip_name, priv->rf->name);
  842. return 0;
  843. err_iounmap:
  844. iounmap(priv->map);
  845. err_free_dev:
  846. pci_set_drvdata(pdev, NULL);
  847. ieee80211_free_hw(dev);
  848. err_free_reg:
  849. pci_release_regions(pdev);
  850. pci_disable_device(pdev);
  851. return err;
  852. }
  853. static void __devexit rtl8180_remove(struct pci_dev *pdev)
  854. {
  855. struct ieee80211_hw *dev = pci_get_drvdata(pdev);
  856. struct rtl8180_priv *priv;
  857. if (!dev)
  858. return;
  859. ieee80211_unregister_hw(dev);
  860. priv = dev->priv;
  861. pci_iounmap(pdev, priv->map);
  862. pci_release_regions(pdev);
  863. pci_disable_device(pdev);
  864. ieee80211_free_hw(dev);
  865. }
  866. #ifdef CONFIG_PM
  867. static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
  868. {
  869. pci_save_state(pdev);
  870. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  871. return 0;
  872. }
  873. static int rtl8180_resume(struct pci_dev *pdev)
  874. {
  875. pci_set_power_state(pdev, PCI_D0);
  876. pci_restore_state(pdev);
  877. return 0;
  878. }
  879. #endif /* CONFIG_PM */
  880. static struct pci_driver rtl8180_driver = {
  881. .name = KBUILD_MODNAME,
  882. .id_table = rtl8180_table,
  883. .probe = rtl8180_probe,
  884. .remove = __devexit_p(rtl8180_remove),
  885. #ifdef CONFIG_PM
  886. .suspend = rtl8180_suspend,
  887. .resume = rtl8180_resume,
  888. #endif /* CONFIG_PM */
  889. };
  890. static int __init rtl8180_init(void)
  891. {
  892. return pci_register_driver(&rtl8180_driver);
  893. }
  894. static void __exit rtl8180_exit(void)
  895. {
  896. pci_unregister_driver(&rtl8180_driver);
  897. }
  898. module_init(rtl8180_init);
  899. module_exit(rtl8180_exit);