dev.c 32 KB

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