rtl8187_dev.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * Linux device driver for RTL8187
  3. *
  4. * Copyright 2007 Michael Wu <flamingice@sourmilk.net>
  5. * Copyright 2007 Andrea Merello <andreamrl@tiscali.it>
  6. *
  7. * Based on the r8187 driver, which is:
  8. * Copyright 2005 Andrea Merello <andreamrl@tiscali.it>, et al.
  9. *
  10. * Magic delays and register offsets below are taken from the original
  11. * r8187 driver sources. Thanks to Realtek for their support!
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/usb.h>
  19. #include <linux/delay.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/eeprom_93cx6.h>
  22. #include <net/mac80211.h>
  23. #include "rtl8187.h"
  24. #include "rtl8187_rtl8225.h"
  25. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  26. MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
  27. MODULE_DESCRIPTION("RTL8187/RTL8187B USB wireless driver");
  28. MODULE_LICENSE("GPL");
  29. static struct usb_device_id rtl8187_table[] __devinitdata = {
  30. /* Asus */
  31. {USB_DEVICE(0x0b05, 0x171d), .driver_info = DEVICE_RTL8187},
  32. /* Realtek */
  33. {USB_DEVICE(0x0bda, 0x8187), .driver_info = DEVICE_RTL8187},
  34. {USB_DEVICE(0x0bda, 0x8189), .driver_info = DEVICE_RTL8187B},
  35. {USB_DEVICE(0x0bda, 0x8197), .driver_info = DEVICE_RTL8187B},
  36. /* Netgear */
  37. {USB_DEVICE(0x0846, 0x6100), .driver_info = DEVICE_RTL8187},
  38. {USB_DEVICE(0x0846, 0x6a00), .driver_info = DEVICE_RTL8187},
  39. {USB_DEVICE(0x0846, 0x4260), .driver_info = DEVICE_RTL8187B},
  40. /* HP */
  41. {USB_DEVICE(0x03f0, 0xca02), .driver_info = DEVICE_RTL8187},
  42. /* Sitecom */
  43. {USB_DEVICE(0x0df6, 0x000d), .driver_info = DEVICE_RTL8187},
  44. {}
  45. };
  46. MODULE_DEVICE_TABLE(usb, rtl8187_table);
  47. static const struct ieee80211_rate rtl818x_rates[] = {
  48. { .bitrate = 10, .hw_value = 0, },
  49. { .bitrate = 20, .hw_value = 1, },
  50. { .bitrate = 55, .hw_value = 2, },
  51. { .bitrate = 110, .hw_value = 3, },
  52. { .bitrate = 60, .hw_value = 4, },
  53. { .bitrate = 90, .hw_value = 5, },
  54. { .bitrate = 120, .hw_value = 6, },
  55. { .bitrate = 180, .hw_value = 7, },
  56. { .bitrate = 240, .hw_value = 8, },
  57. { .bitrate = 360, .hw_value = 9, },
  58. { .bitrate = 480, .hw_value = 10, },
  59. { .bitrate = 540, .hw_value = 11, },
  60. };
  61. static const struct ieee80211_channel rtl818x_channels[] = {
  62. { .center_freq = 2412 },
  63. { .center_freq = 2417 },
  64. { .center_freq = 2422 },
  65. { .center_freq = 2427 },
  66. { .center_freq = 2432 },
  67. { .center_freq = 2437 },
  68. { .center_freq = 2442 },
  69. { .center_freq = 2447 },
  70. { .center_freq = 2452 },
  71. { .center_freq = 2457 },
  72. { .center_freq = 2462 },
  73. { .center_freq = 2467 },
  74. { .center_freq = 2472 },
  75. { .center_freq = 2484 },
  76. };
  77. static void rtl8187_iowrite_async_cb(struct urb *urb)
  78. {
  79. kfree(urb->context);
  80. usb_free_urb(urb);
  81. }
  82. static void rtl8187_iowrite_async(struct rtl8187_priv *priv, __le16 addr,
  83. void *data, u16 len)
  84. {
  85. struct usb_ctrlrequest *dr;
  86. struct urb *urb;
  87. struct rtl8187_async_write_data {
  88. u8 data[4];
  89. struct usb_ctrlrequest dr;
  90. } *buf;
  91. int rc;
  92. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  93. if (!buf)
  94. return;
  95. urb = usb_alloc_urb(0, GFP_ATOMIC);
  96. if (!urb) {
  97. kfree(buf);
  98. return;
  99. }
  100. dr = &buf->dr;
  101. dr->bRequestType = RTL8187_REQT_WRITE;
  102. dr->bRequest = RTL8187_REQ_SET_REG;
  103. dr->wValue = addr;
  104. dr->wIndex = 0;
  105. dr->wLength = cpu_to_le16(len);
  106. memcpy(buf, data, len);
  107. usb_fill_control_urb(urb, priv->udev, usb_sndctrlpipe(priv->udev, 0),
  108. (unsigned char *)dr, buf, len,
  109. rtl8187_iowrite_async_cb, buf);
  110. rc = usb_submit_urb(urb, GFP_ATOMIC);
  111. if (rc < 0) {
  112. kfree(buf);
  113. usb_free_urb(urb);
  114. }
  115. }
  116. static inline void rtl818x_iowrite32_async(struct rtl8187_priv *priv,
  117. __le32 *addr, u32 val)
  118. {
  119. __le32 buf = cpu_to_le32(val);
  120. rtl8187_iowrite_async(priv, cpu_to_le16((unsigned long)addr),
  121. &buf, sizeof(buf));
  122. }
  123. void rtl8187_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  124. {
  125. struct rtl8187_priv *priv = dev->priv;
  126. data <<= 8;
  127. data |= addr | 0x80;
  128. rtl818x_iowrite8(priv, &priv->map->PHY[3], (data >> 24) & 0xFF);
  129. rtl818x_iowrite8(priv, &priv->map->PHY[2], (data >> 16) & 0xFF);
  130. rtl818x_iowrite8(priv, &priv->map->PHY[1], (data >> 8) & 0xFF);
  131. rtl818x_iowrite8(priv, &priv->map->PHY[0], data & 0xFF);
  132. msleep(1);
  133. }
  134. static void rtl8187_tx_cb(struct urb *urb)
  135. {
  136. struct sk_buff *skb = (struct sk_buff *)urb->context;
  137. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  138. struct ieee80211_hw *hw = info->driver_data[0];
  139. struct rtl8187_priv *priv = hw->priv;
  140. usb_free_urb(info->driver_data[1]);
  141. skb_pull(skb, priv->is_rtl8187b ? sizeof(struct rtl8187b_tx_hdr) :
  142. sizeof(struct rtl8187_tx_hdr));
  143. memset(&info->status, 0, sizeof(info->status));
  144. info->flags |= IEEE80211_TX_STAT_ACK;
  145. ieee80211_tx_status_irqsafe(hw, skb);
  146. }
  147. static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  148. {
  149. struct rtl8187_priv *priv = dev->priv;
  150. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  151. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  152. unsigned int ep;
  153. void *buf;
  154. struct urb *urb;
  155. __le16 rts_dur = 0;
  156. u32 flags;
  157. int rc;
  158. urb = usb_alloc_urb(0, GFP_ATOMIC);
  159. if (!urb) {
  160. kfree_skb(skb);
  161. return 0;
  162. }
  163. flags = skb->len;
  164. flags |= RTL818X_TX_DESC_FLAG_NO_ENC;
  165. flags |= ieee80211_get_tx_rate(dev, info)->hw_value << 24;
  166. if (ieee80211_has_morefrags(((struct ieee80211_hdr *)skb->data)->frame_control))
  167. flags |= RTL818X_TX_DESC_FLAG_MOREFRAG;
  168. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  169. flags |= RTL818X_TX_DESC_FLAG_RTS;
  170. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  171. rts_dur = ieee80211_rts_duration(dev, priv->vif,
  172. skb->len, info);
  173. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  174. flags |= RTL818X_TX_DESC_FLAG_CTS;
  175. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  176. }
  177. if (!priv->is_rtl8187b) {
  178. struct rtl8187_tx_hdr *hdr =
  179. (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
  180. hdr->flags = cpu_to_le32(flags);
  181. hdr->len = 0;
  182. hdr->rts_duration = rts_dur;
  183. hdr->retry = cpu_to_le32(info->control.retry_limit << 8);
  184. buf = hdr;
  185. ep = 2;
  186. } else {
  187. /* fc needs to be calculated before skb_push() */
  188. unsigned int epmap[4] = { 6, 7, 5, 4 };
  189. struct ieee80211_hdr *tx_hdr =
  190. (struct ieee80211_hdr *)(skb->data);
  191. u16 fc = le16_to_cpu(tx_hdr->frame_control);
  192. struct rtl8187b_tx_hdr *hdr =
  193. (struct rtl8187b_tx_hdr *)skb_push(skb, sizeof(*hdr));
  194. struct ieee80211_rate *txrate =
  195. ieee80211_get_tx_rate(dev, info);
  196. memset(hdr, 0, sizeof(*hdr));
  197. hdr->flags = cpu_to_le32(flags);
  198. hdr->rts_duration = rts_dur;
  199. hdr->retry = cpu_to_le32(info->control.retry_limit << 8);
  200. hdr->tx_duration =
  201. ieee80211_generic_frame_duration(dev, priv->vif,
  202. skb->len, txrate);
  203. buf = hdr;
  204. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  205. ep = 12;
  206. else
  207. ep = epmap[skb_get_queue_mapping(skb)];
  208. }
  209. /* FIXME: The sequence that follows is needed for this driver to
  210. * work with mac80211 since "mac80211: fix TX sequence numbers".
  211. * As with the temporary code in rt2x00, changes will be needed
  212. * to get proper sequence numbers on beacons. In addition, this
  213. * patch places the sequence number in the hardware state, which
  214. * limits us to a single virtual state.
  215. */
  216. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  217. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  218. priv->seqno += 0x10;
  219. ieee80211hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  220. ieee80211hdr->seq_ctrl |= cpu_to_le16(priv->seqno);
  221. }
  222. info->driver_data[0] = dev;
  223. info->driver_data[1] = urb;
  224. usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, ep),
  225. buf, skb->len, rtl8187_tx_cb, skb);
  226. rc = usb_submit_urb(urb, GFP_ATOMIC);
  227. if (rc < 0) {
  228. usb_free_urb(urb);
  229. kfree_skb(skb);
  230. }
  231. return 0;
  232. }
  233. static void rtl8187_rx_cb(struct urb *urb)
  234. {
  235. struct sk_buff *skb = (struct sk_buff *)urb->context;
  236. struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
  237. struct ieee80211_hw *dev = info->dev;
  238. struct rtl8187_priv *priv = dev->priv;
  239. struct ieee80211_rx_status rx_status = { 0 };
  240. int rate, signal;
  241. u32 flags;
  242. u32 quality;
  243. spin_lock(&priv->rx_queue.lock);
  244. if (skb->next)
  245. __skb_unlink(skb, &priv->rx_queue);
  246. else {
  247. spin_unlock(&priv->rx_queue.lock);
  248. return;
  249. }
  250. spin_unlock(&priv->rx_queue.lock);
  251. if (unlikely(urb->status)) {
  252. usb_free_urb(urb);
  253. dev_kfree_skb_irq(skb);
  254. return;
  255. }
  256. skb_put(skb, urb->actual_length);
  257. if (!priv->is_rtl8187b) {
  258. struct rtl8187_rx_hdr *hdr =
  259. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  260. flags = le32_to_cpu(hdr->flags);
  261. signal = hdr->signal & 0x7f;
  262. rx_status.antenna = (hdr->signal >> 7) & 1;
  263. rx_status.noise = hdr->noise;
  264. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  265. priv->quality = signal;
  266. rx_status.qual = priv->quality;
  267. priv->noise = hdr->noise;
  268. rate = (flags >> 20) & 0xF;
  269. if (rate > 3) { /* OFDM rate */
  270. if (signal > 90)
  271. signal = 90;
  272. else if (signal < 25)
  273. signal = 25;
  274. signal = 90 - signal;
  275. } else { /* CCK rate */
  276. if (signal > 95)
  277. signal = 95;
  278. else if (signal < 30)
  279. signal = 30;
  280. signal = 95 - signal;
  281. }
  282. rx_status.signal = signal;
  283. priv->signal = signal;
  284. } else {
  285. struct rtl8187b_rx_hdr *hdr =
  286. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  287. /* The Realtek datasheet for the RTL8187B shows that the RX
  288. * header contains the following quantities: signal quality,
  289. * RSSI, AGC, the received power in dB, and the measured SNR.
  290. * In testing, none of these quantities show qualitative
  291. * agreement with AP signal strength, except for the AGC,
  292. * which is inversely proportional to the strength of the
  293. * signal. In the following, the quality and signal strength
  294. * are derived from the AGC. The arbitrary scaling constants
  295. * are chosen to make the results close to the values obtained
  296. * for a BCM4312 using b43 as the driver. The noise is ignored
  297. * for now.
  298. */
  299. flags = le32_to_cpu(hdr->flags);
  300. quality = 170 - hdr->agc;
  301. if (quality > 100)
  302. quality = 100;
  303. signal = 14 - hdr->agc / 2;
  304. rx_status.qual = quality;
  305. priv->quality = quality;
  306. rx_status.signal = signal;
  307. priv->signal = signal;
  308. rx_status.antenna = (hdr->rssi >> 7) & 1;
  309. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  310. rate = (flags >> 20) & 0xF;
  311. }
  312. skb_trim(skb, flags & 0x0FFF);
  313. rx_status.rate_idx = rate;
  314. rx_status.freq = dev->conf.channel->center_freq;
  315. rx_status.band = dev->conf.channel->band;
  316. rx_status.flag |= RX_FLAG_TSFT;
  317. if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
  318. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  319. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  320. skb = dev_alloc_skb(RTL8187_MAX_RX);
  321. if (unlikely(!skb)) {
  322. usb_free_urb(urb);
  323. /* TODO check rx queue length and refill *somewhere* */
  324. return;
  325. }
  326. info = (struct rtl8187_rx_info *)skb->cb;
  327. info->urb = urb;
  328. info->dev = dev;
  329. urb->transfer_buffer = skb_tail_pointer(skb);
  330. urb->context = skb;
  331. skb_queue_tail(&priv->rx_queue, skb);
  332. usb_submit_urb(urb, GFP_ATOMIC);
  333. }
  334. static int rtl8187_init_urbs(struct ieee80211_hw *dev)
  335. {
  336. struct rtl8187_priv *priv = dev->priv;
  337. struct urb *entry;
  338. struct sk_buff *skb;
  339. struct rtl8187_rx_info *info;
  340. while (skb_queue_len(&priv->rx_queue) < 8) {
  341. skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
  342. if (!skb)
  343. break;
  344. entry = usb_alloc_urb(0, GFP_KERNEL);
  345. if (!entry) {
  346. kfree_skb(skb);
  347. break;
  348. }
  349. usb_fill_bulk_urb(entry, priv->udev,
  350. usb_rcvbulkpipe(priv->udev,
  351. priv->is_rtl8187b ? 3 : 1),
  352. skb_tail_pointer(skb),
  353. RTL8187_MAX_RX, rtl8187_rx_cb, skb);
  354. info = (struct rtl8187_rx_info *)skb->cb;
  355. info->urb = entry;
  356. info->dev = dev;
  357. skb_queue_tail(&priv->rx_queue, skb);
  358. usb_submit_urb(entry, GFP_KERNEL);
  359. }
  360. return 0;
  361. }
  362. static int rtl8187_cmd_reset(struct ieee80211_hw *dev)
  363. {
  364. struct rtl8187_priv *priv = dev->priv;
  365. u8 reg;
  366. int i;
  367. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  368. reg &= (1 << 1);
  369. reg |= RTL818X_CMD_RESET;
  370. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  371. i = 10;
  372. do {
  373. msleep(2);
  374. if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
  375. RTL818X_CMD_RESET))
  376. break;
  377. } while (--i);
  378. if (!i) {
  379. printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
  380. return -ETIMEDOUT;
  381. }
  382. /* reload registers from eeprom */
  383. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  384. i = 10;
  385. do {
  386. msleep(4);
  387. if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
  388. RTL818X_EEPROM_CMD_CONFIG))
  389. break;
  390. } while (--i);
  391. if (!i) {
  392. printk(KERN_ERR "%s: eeprom reset timeout!\n",
  393. wiphy_name(dev->wiphy));
  394. return -ETIMEDOUT;
  395. }
  396. return 0;
  397. }
  398. static int rtl8187_init_hw(struct ieee80211_hw *dev)
  399. {
  400. struct rtl8187_priv *priv = dev->priv;
  401. u8 reg;
  402. int res;
  403. /* reset */
  404. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  405. RTL818X_EEPROM_CMD_CONFIG);
  406. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  407. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg |
  408. RTL818X_CONFIG3_ANAPARAM_WRITE);
  409. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  410. RTL8187_RTL8225_ANAPARAM_ON);
  411. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  412. RTL8187_RTL8225_ANAPARAM2_ON);
  413. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg &
  414. ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  415. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  416. RTL818X_EEPROM_CMD_NORMAL);
  417. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  418. msleep(200);
  419. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  420. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  421. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  422. msleep(200);
  423. res = rtl8187_cmd_reset(dev);
  424. if (res)
  425. return res;
  426. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  427. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  428. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  429. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  430. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  431. RTL8187_RTL8225_ANAPARAM_ON);
  432. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  433. RTL8187_RTL8225_ANAPARAM2_ON);
  434. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  435. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  436. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  437. /* setup card */
  438. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  439. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  440. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  441. rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
  442. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  443. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  444. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  445. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  446. reg &= 0x3F;
  447. reg |= 0x80;
  448. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  449. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  450. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  451. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  452. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  453. // TODO: set RESP_RATE and BRSR properly
  454. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  455. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  456. /* host_usb_init */
  457. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  458. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  459. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  460. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  461. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  462. rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
  463. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  464. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  465. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  466. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  467. msleep(100);
  468. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  469. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  470. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  471. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  472. RTL818X_EEPROM_CMD_CONFIG);
  473. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  474. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  475. RTL818X_EEPROM_CMD_NORMAL);
  476. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  477. msleep(100);
  478. priv->rf->init(dev);
  479. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  480. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  481. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  482. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  483. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  484. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  485. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  486. return 0;
  487. }
  488. static const u8 rtl8187b_reg_table[][3] = {
  489. {0xF0, 0x32, 0}, {0xF1, 0x32, 0}, {0xF2, 0x00, 0}, {0xF3, 0x00, 0},
  490. {0xF4, 0x32, 0}, {0xF5, 0x43, 0}, {0xF6, 0x00, 0}, {0xF7, 0x00, 0},
  491. {0xF8, 0x46, 0}, {0xF9, 0xA4, 0}, {0xFA, 0x00, 0}, {0xFB, 0x00, 0},
  492. {0xFC, 0x96, 0}, {0xFD, 0xA4, 0}, {0xFE, 0x00, 0}, {0xFF, 0x00, 0},
  493. {0x58, 0x4B, 1}, {0x59, 0x00, 1}, {0x5A, 0x4B, 1}, {0x5B, 0x00, 1},
  494. {0x60, 0x4B, 1}, {0x61, 0x09, 1}, {0x62, 0x4B, 1}, {0x63, 0x09, 1},
  495. {0xCE, 0x0F, 1}, {0xCF, 0x00, 1}, {0xE0, 0xFF, 1}, {0xE1, 0x0F, 1},
  496. {0xE2, 0x00, 1}, {0xF0, 0x4E, 1}, {0xF1, 0x01, 1}, {0xF2, 0x02, 1},
  497. {0xF3, 0x03, 1}, {0xF4, 0x04, 1}, {0xF5, 0x05, 1}, {0xF6, 0x06, 1},
  498. {0xF7, 0x07, 1}, {0xF8, 0x08, 1},
  499. {0x4E, 0x00, 2}, {0x0C, 0x04, 2}, {0x21, 0x61, 2}, {0x22, 0x68, 2},
  500. {0x23, 0x6F, 2}, {0x24, 0x76, 2}, {0x25, 0x7D, 2}, {0x26, 0x84, 2},
  501. {0x27, 0x8D, 2}, {0x4D, 0x08, 2}, {0x50, 0x05, 2}, {0x51, 0xF5, 2},
  502. {0x52, 0x04, 2}, {0x53, 0xA0, 2}, {0x54, 0x1F, 2}, {0x55, 0x23, 2},
  503. {0x56, 0x45, 2}, {0x57, 0x67, 2}, {0x58, 0x08, 2}, {0x59, 0x08, 2},
  504. {0x5A, 0x08, 2}, {0x5B, 0x08, 2}, {0x60, 0x08, 2}, {0x61, 0x08, 2},
  505. {0x62, 0x08, 2}, {0x63, 0x08, 2}, {0x64, 0xCF, 2}, {0x72, 0x56, 2},
  506. {0x73, 0x9A, 2},
  507. {0x34, 0xF0, 0}, {0x35, 0x0F, 0}, {0x5B, 0x40, 0}, {0x84, 0x88, 0},
  508. {0x85, 0x24, 0}, {0x88, 0x54, 0}, {0x8B, 0xB8, 0}, {0x8C, 0x07, 0},
  509. {0x8D, 0x00, 0}, {0x94, 0x1B, 0}, {0x95, 0x12, 0}, {0x96, 0x00, 0},
  510. {0x97, 0x06, 0}, {0x9D, 0x1A, 0}, {0x9F, 0x10, 0}, {0xB4, 0x22, 0},
  511. {0xBE, 0x80, 0}, {0xDB, 0x00, 0}, {0xEE, 0x00, 0}, {0x91, 0x03, 0},
  512. {0x4C, 0x00, 2}, {0x9F, 0x00, 3}, {0x8C, 0x01, 0}, {0x8D, 0x10, 0},
  513. {0x8E, 0x08, 0}, {0x8F, 0x00, 0}
  514. };
  515. static int rtl8187b_init_hw(struct ieee80211_hw *dev)
  516. {
  517. struct rtl8187_priv *priv = dev->priv;
  518. int res, i;
  519. u8 reg;
  520. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  521. RTL818X_EEPROM_CMD_CONFIG);
  522. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  523. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE | RTL818X_CONFIG3_GNT_SELECT;
  524. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  525. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  526. RTL8187B_RTL8225_ANAPARAM2_ON);
  527. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  528. RTL8187B_RTL8225_ANAPARAM_ON);
  529. rtl818x_iowrite8(priv, &priv->map->ANAPARAM3,
  530. RTL8187B_RTL8225_ANAPARAM3_ON);
  531. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0x10);
  532. reg = rtl818x_ioread8(priv, (u8 *)0xFF62);
  533. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg & ~(1 << 5));
  534. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg | (1 << 5));
  535. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  536. reg &= ~RTL818X_CONFIG3_ANAPARAM_WRITE;
  537. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  538. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  539. RTL818X_EEPROM_CMD_NORMAL);
  540. res = rtl8187_cmd_reset(dev);
  541. if (res)
  542. return res;
  543. rtl818x_iowrite16(priv, (__le16 *)0xFF2D, 0x0FFF);
  544. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  545. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  546. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  547. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  548. reg |= RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT |
  549. RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  550. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  551. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFE0, 0x0FFF, 1);
  552. reg = rtl818x_ioread8(priv, &priv->map->RATE_FALLBACK);
  553. reg |= RTL818X_RATE_FALLBACK_ENABLE;
  554. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, reg);
  555. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  556. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  557. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFD4, 0xFFFF, 1);
  558. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  559. RTL818X_EEPROM_CMD_CONFIG);
  560. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  561. rtl818x_iowrite8(priv, &priv->map->CONFIG1, (reg & 0x3F) | 0x80);
  562. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  563. RTL818X_EEPROM_CMD_NORMAL);
  564. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  565. for (i = 0; i < ARRAY_SIZE(rtl8187b_reg_table); i++) {
  566. rtl818x_iowrite8_idx(priv,
  567. (u8 *)(uintptr_t)
  568. (rtl8187b_reg_table[i][0] | 0xFF00),
  569. rtl8187b_reg_table[i][1],
  570. rtl8187b_reg_table[i][2]);
  571. }
  572. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  573. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  574. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF0, 0, 1);
  575. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF4, 0, 1);
  576. rtl818x_iowrite8_idx(priv, (u8 *)0xFFF8, 0, 1);
  577. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x00004001);
  578. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x569A, 2);
  579. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  580. RTL818X_EEPROM_CMD_CONFIG);
  581. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  582. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE;
  583. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  584. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  585. RTL818X_EEPROM_CMD_NORMAL);
  586. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  587. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  588. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FFF);
  589. msleep(1100);
  590. priv->rf->init(dev);
  591. reg = RTL818X_CMD_TX_ENABLE | RTL818X_CMD_RX_ENABLE;
  592. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  593. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  594. rtl818x_iowrite8(priv, (u8 *)0xFE41, 0xF4);
  595. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x00);
  596. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  597. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  598. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x0F);
  599. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  600. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  601. reg = rtl818x_ioread8(priv, (u8 *)0xFFDB);
  602. rtl818x_iowrite8(priv, (u8 *)0xFFDB, reg | (1 << 2));
  603. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x59FA, 3);
  604. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF74, 0x59D2, 3);
  605. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF76, 0x59D2, 3);
  606. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF78, 0x19FA, 3);
  607. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7A, 0x19FA, 3);
  608. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7C, 0x00D0, 3);
  609. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0);
  610. rtl818x_iowrite8_idx(priv, (u8 *)0xFF80, 0x0F, 1);
  611. rtl818x_iowrite8_idx(priv, (u8 *)0xFF83, 0x03, 1);
  612. rtl818x_iowrite8(priv, (u8 *)0xFFDA, 0x10);
  613. rtl818x_iowrite8_idx(priv, (u8 *)0xFF4D, 0x08, 2);
  614. rtl818x_iowrite32(priv, &priv->map->HSSI_PARA, 0x0600321B);
  615. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFEC, 0x0800, 1);
  616. return 0;
  617. }
  618. static int rtl8187_start(struct ieee80211_hw *dev)
  619. {
  620. struct rtl8187_priv *priv = dev->priv;
  621. u32 reg;
  622. int ret;
  623. ret = (!priv->is_rtl8187b) ? rtl8187_init_hw(dev) :
  624. rtl8187b_init_hw(dev);
  625. if (ret)
  626. return ret;
  627. mutex_lock(&priv->conf_mutex);
  628. if (priv->is_rtl8187b) {
  629. reg = RTL818X_RX_CONF_MGMT |
  630. RTL818X_RX_CONF_DATA |
  631. RTL818X_RX_CONF_BROADCAST |
  632. RTL818X_RX_CONF_NICMAC |
  633. RTL818X_RX_CONF_BSSID |
  634. (7 << 13 /* RX FIFO threshold NONE */) |
  635. (7 << 10 /* MAX RX DMA */) |
  636. RTL818X_RX_CONF_RX_AUTORESETPHY |
  637. RTL818X_RX_CONF_ONLYERLPKT |
  638. RTL818X_RX_CONF_MULTICAST;
  639. priv->rx_conf = reg;
  640. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  641. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  642. RTL818X_TX_CONF_HW_SEQNUM |
  643. RTL818X_TX_CONF_DISREQQSIZE |
  644. (7 << 8 /* short retry limit */) |
  645. (7 << 0 /* long retry limit */) |
  646. (7 << 21 /* MAX TX DMA */));
  647. rtl8187_init_urbs(dev);
  648. mutex_unlock(&priv->conf_mutex);
  649. return 0;
  650. }
  651. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  652. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  653. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  654. rtl8187_init_urbs(dev);
  655. reg = RTL818X_RX_CONF_ONLYERLPKT |
  656. RTL818X_RX_CONF_RX_AUTORESETPHY |
  657. RTL818X_RX_CONF_BSSID |
  658. RTL818X_RX_CONF_MGMT |
  659. RTL818X_RX_CONF_DATA |
  660. (7 << 13 /* RX FIFO threshold NONE */) |
  661. (7 << 10 /* MAX RX DMA */) |
  662. RTL818X_RX_CONF_BROADCAST |
  663. RTL818X_RX_CONF_NICMAC;
  664. priv->rx_conf = reg;
  665. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  666. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  667. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  668. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  669. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  670. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  671. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  672. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  673. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  674. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  675. reg = RTL818X_TX_CONF_CW_MIN |
  676. (7 << 21 /* MAX TX DMA */) |
  677. RTL818X_TX_CONF_NO_ICV;
  678. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  679. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  680. reg |= RTL818X_CMD_TX_ENABLE;
  681. reg |= RTL818X_CMD_RX_ENABLE;
  682. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  683. mutex_unlock(&priv->conf_mutex);
  684. return 0;
  685. }
  686. static void rtl8187_stop(struct ieee80211_hw *dev)
  687. {
  688. struct rtl8187_priv *priv = dev->priv;
  689. struct rtl8187_rx_info *info;
  690. struct sk_buff *skb;
  691. u32 reg;
  692. mutex_lock(&priv->conf_mutex);
  693. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  694. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  695. reg &= ~RTL818X_CMD_TX_ENABLE;
  696. reg &= ~RTL818X_CMD_RX_ENABLE;
  697. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  698. priv->rf->stop(dev);
  699. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  700. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  701. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  702. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  703. while ((skb = skb_dequeue(&priv->rx_queue))) {
  704. info = (struct rtl8187_rx_info *)skb->cb;
  705. usb_kill_urb(info->urb);
  706. kfree_skb(skb);
  707. }
  708. mutex_unlock(&priv->conf_mutex);
  709. }
  710. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  711. struct ieee80211_if_init_conf *conf)
  712. {
  713. struct rtl8187_priv *priv = dev->priv;
  714. int i;
  715. if (priv->mode != NL80211_IFTYPE_MONITOR)
  716. return -EOPNOTSUPP;
  717. switch (conf->type) {
  718. case NL80211_IFTYPE_STATION:
  719. priv->mode = conf->type;
  720. break;
  721. default:
  722. return -EOPNOTSUPP;
  723. }
  724. mutex_lock(&priv->conf_mutex);
  725. priv->vif = conf->vif;
  726. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  727. for (i = 0; i < ETH_ALEN; i++)
  728. rtl818x_iowrite8(priv, &priv->map->MAC[i],
  729. ((u8 *)conf->mac_addr)[i]);
  730. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  731. mutex_unlock(&priv->conf_mutex);
  732. return 0;
  733. }
  734. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  735. struct ieee80211_if_init_conf *conf)
  736. {
  737. struct rtl8187_priv *priv = dev->priv;
  738. mutex_lock(&priv->conf_mutex);
  739. priv->mode = NL80211_IFTYPE_MONITOR;
  740. priv->vif = NULL;
  741. mutex_unlock(&priv->conf_mutex);
  742. }
  743. static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  744. {
  745. struct rtl8187_priv *priv = dev->priv;
  746. u32 reg;
  747. mutex_lock(&priv->conf_mutex);
  748. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  749. /* Enable TX loopback on MAC level to avoid TX during channel
  750. * changes, as this has be seen to causes problems and the
  751. * card will stop work until next reset
  752. */
  753. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  754. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  755. msleep(10);
  756. priv->rf->set_chan(dev, conf);
  757. msleep(10);
  758. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  759. if (!priv->is_rtl8187b) {
  760. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  761. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  762. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  763. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  764. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  765. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
  766. } else {
  767. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  768. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  769. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  770. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
  771. }
  772. }
  773. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  774. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  775. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  776. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  777. mutex_unlock(&priv->conf_mutex);
  778. return 0;
  779. }
  780. static int rtl8187_config_interface(struct ieee80211_hw *dev,
  781. struct ieee80211_vif *vif,
  782. struct ieee80211_if_conf *conf)
  783. {
  784. struct rtl8187_priv *priv = dev->priv;
  785. int i;
  786. u8 reg;
  787. mutex_lock(&priv->conf_mutex);
  788. for (i = 0; i < ETH_ALEN; i++)
  789. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  790. if (is_valid_ether_addr(conf->bssid)) {
  791. reg = RTL818X_MSR_INFRA;
  792. if (priv->is_rtl8187b)
  793. reg |= RTL818X_MSR_ENEDCA;
  794. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  795. } else {
  796. reg = RTL818X_MSR_NO_LINK;
  797. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  798. }
  799. mutex_unlock(&priv->conf_mutex);
  800. return 0;
  801. }
  802. static void rtl8187_configure_filter(struct ieee80211_hw *dev,
  803. unsigned int changed_flags,
  804. unsigned int *total_flags,
  805. int mc_count, struct dev_addr_list *mclist)
  806. {
  807. struct rtl8187_priv *priv = dev->priv;
  808. if (changed_flags & FIF_FCSFAIL)
  809. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  810. if (changed_flags & FIF_CONTROL)
  811. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  812. if (changed_flags & FIF_OTHER_BSS)
  813. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  814. if (*total_flags & FIF_ALLMULTI || mc_count > 0)
  815. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  816. else
  817. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  818. *total_flags = 0;
  819. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  820. *total_flags |= FIF_FCSFAIL;
  821. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  822. *total_flags |= FIF_CONTROL;
  823. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  824. *total_flags |= FIF_OTHER_BSS;
  825. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  826. *total_flags |= FIF_ALLMULTI;
  827. rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
  828. }
  829. static const struct ieee80211_ops rtl8187_ops = {
  830. .tx = rtl8187_tx,
  831. .start = rtl8187_start,
  832. .stop = rtl8187_stop,
  833. .add_interface = rtl8187_add_interface,
  834. .remove_interface = rtl8187_remove_interface,
  835. .config = rtl8187_config,
  836. .config_interface = rtl8187_config_interface,
  837. .configure_filter = rtl8187_configure_filter,
  838. };
  839. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  840. {
  841. struct ieee80211_hw *dev = eeprom->data;
  842. struct rtl8187_priv *priv = dev->priv;
  843. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  844. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  845. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  846. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  847. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  848. }
  849. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  850. {
  851. struct ieee80211_hw *dev = eeprom->data;
  852. struct rtl8187_priv *priv = dev->priv;
  853. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  854. if (eeprom->reg_data_in)
  855. reg |= RTL818X_EEPROM_CMD_WRITE;
  856. if (eeprom->reg_data_out)
  857. reg |= RTL818X_EEPROM_CMD_READ;
  858. if (eeprom->reg_data_clock)
  859. reg |= RTL818X_EEPROM_CMD_CK;
  860. if (eeprom->reg_chip_select)
  861. reg |= RTL818X_EEPROM_CMD_CS;
  862. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  863. udelay(10);
  864. }
  865. static int __devinit rtl8187_probe(struct usb_interface *intf,
  866. const struct usb_device_id *id)
  867. {
  868. struct usb_device *udev = interface_to_usbdev(intf);
  869. struct ieee80211_hw *dev;
  870. struct rtl8187_priv *priv;
  871. struct eeprom_93cx6 eeprom;
  872. struct ieee80211_channel *channel;
  873. const char *chip_name;
  874. u16 txpwr, reg;
  875. int err, i;
  876. DECLARE_MAC_BUF(mac);
  877. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  878. if (!dev) {
  879. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  880. return -ENOMEM;
  881. }
  882. priv = dev->priv;
  883. priv->is_rtl8187b = (id->driver_info == DEVICE_RTL8187B);
  884. SET_IEEE80211_DEV(dev, &intf->dev);
  885. usb_set_intfdata(intf, dev);
  886. priv->udev = udev;
  887. usb_get_dev(udev);
  888. skb_queue_head_init(&priv->rx_queue);
  889. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  890. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  891. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  892. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  893. priv->map = (struct rtl818x_csr *)0xFF00;
  894. priv->band.band = IEEE80211_BAND_2GHZ;
  895. priv->band.channels = priv->channels;
  896. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  897. priv->band.bitrates = priv->rates;
  898. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  899. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  900. priv->mode = NL80211_IFTYPE_MONITOR;
  901. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  902. IEEE80211_HW_RX_INCLUDES_FCS;
  903. eeprom.data = dev;
  904. eeprom.register_read = rtl8187_eeprom_register_read;
  905. eeprom.register_write = rtl8187_eeprom_register_write;
  906. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  907. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  908. else
  909. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  910. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  911. udelay(10);
  912. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  913. (__le16 __force *)dev->wiphy->perm_addr, 3);
  914. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  915. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  916. "generated MAC address\n");
  917. random_ether_addr(dev->wiphy->perm_addr);
  918. }
  919. channel = priv->channels;
  920. for (i = 0; i < 3; i++) {
  921. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  922. &txpwr);
  923. (*channel++).hw_value = txpwr & 0xFF;
  924. (*channel++).hw_value = txpwr >> 8;
  925. }
  926. for (i = 0; i < 2; i++) {
  927. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  928. &txpwr);
  929. (*channel++).hw_value = txpwr & 0xFF;
  930. (*channel++).hw_value = txpwr >> 8;
  931. }
  932. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  933. &priv->txpwr_base);
  934. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  935. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  936. /* 0 means asic B-cut, we should use SW 3 wire
  937. * bit-by-bit banging for radio. 1 means we can use
  938. * USB specific request to write radio registers */
  939. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  940. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  941. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  942. if (!priv->is_rtl8187b) {
  943. u32 reg32;
  944. reg32 = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  945. reg32 &= RTL818X_TX_CONF_HWVER_MASK;
  946. switch (reg32) {
  947. case RTL818X_TX_CONF_R8187vD_B:
  948. /* Some RTL8187B devices have a USB ID of 0x8187
  949. * detect them here */
  950. chip_name = "RTL8187BvB(early)";
  951. priv->is_rtl8187b = 1;
  952. priv->hw_rev = RTL8187BvB;
  953. break;
  954. case RTL818X_TX_CONF_R8187vD:
  955. chip_name = "RTL8187vD";
  956. break;
  957. default:
  958. chip_name = "RTL8187vB (default)";
  959. }
  960. } else {
  961. /*
  962. * Force USB request to write radio registers for 8187B, Realtek
  963. * only uses it in their sources
  964. */
  965. /*if (priv->asic_rev == 0) {
  966. printk(KERN_WARNING "rtl8187: Forcing use of USB "
  967. "requests to write to radio registers\n");
  968. priv->asic_rev = 1;
  969. }*/
  970. switch (rtl818x_ioread8(priv, (u8 *)0xFFE1)) {
  971. case RTL818X_R8187B_B:
  972. chip_name = "RTL8187BvB";
  973. priv->hw_rev = RTL8187BvB;
  974. break;
  975. case RTL818X_R8187B_D:
  976. chip_name = "RTL8187BvD";
  977. priv->hw_rev = RTL8187BvD;
  978. break;
  979. case RTL818X_R8187B_E:
  980. chip_name = "RTL8187BvE";
  981. priv->hw_rev = RTL8187BvE;
  982. break;
  983. default:
  984. chip_name = "RTL8187BvB (default)";
  985. priv->hw_rev = RTL8187BvB;
  986. }
  987. }
  988. if (!priv->is_rtl8187b) {
  989. for (i = 0; i < 2; i++) {
  990. eeprom_93cx6_read(&eeprom,
  991. RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  992. &txpwr);
  993. (*channel++).hw_value = txpwr & 0xFF;
  994. (*channel++).hw_value = txpwr >> 8;
  995. }
  996. } else {
  997. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
  998. &txpwr);
  999. (*channel++).hw_value = txpwr & 0xFF;
  1000. eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
  1001. (*channel++).hw_value = txpwr & 0xFF;
  1002. eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
  1003. (*channel++).hw_value = txpwr & 0xFF;
  1004. (*channel++).hw_value = txpwr >> 8;
  1005. }
  1006. if (priv->is_rtl8187b) {
  1007. printk(KERN_WARNING "rtl8187: 8187B chip detected. Support "
  1008. "is EXPERIMENTAL, and could damage your\n"
  1009. " hardware, use at your own risk\n");
  1010. dev->flags |= IEEE80211_HW_SIGNAL_DBM;
  1011. } else {
  1012. dev->flags |= IEEE80211_HW_SIGNAL_UNSPEC;
  1013. dev->max_signal = 65;
  1014. }
  1015. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1016. if ((id->driver_info == DEVICE_RTL8187) && priv->is_rtl8187b)
  1017. printk(KERN_INFO "rtl8187: inconsistency between id with OEM"
  1018. " info!\n");
  1019. priv->rf = rtl8187_detect_rf(dev);
  1020. dev->extra_tx_headroom = (!priv->is_rtl8187b) ?
  1021. sizeof(struct rtl8187_tx_hdr) :
  1022. sizeof(struct rtl8187b_tx_hdr);
  1023. if (!priv->is_rtl8187b)
  1024. dev->queues = 1;
  1025. else
  1026. dev->queues = 4;
  1027. err = ieee80211_register_hw(dev);
  1028. if (err) {
  1029. printk(KERN_ERR "rtl8187: Cannot register device\n");
  1030. goto err_free_dev;
  1031. }
  1032. mutex_init(&priv->conf_mutex);
  1033. printk(KERN_INFO "%s: hwaddr %s, %s V%d + %s\n",
  1034. wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
  1035. chip_name, priv->asic_rev, priv->rf->name);
  1036. return 0;
  1037. err_free_dev:
  1038. ieee80211_free_hw(dev);
  1039. usb_set_intfdata(intf, NULL);
  1040. usb_put_dev(udev);
  1041. return err;
  1042. }
  1043. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  1044. {
  1045. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  1046. struct rtl8187_priv *priv;
  1047. if (!dev)
  1048. return;
  1049. ieee80211_unregister_hw(dev);
  1050. priv = dev->priv;
  1051. usb_put_dev(interface_to_usbdev(intf));
  1052. ieee80211_free_hw(dev);
  1053. }
  1054. static struct usb_driver rtl8187_driver = {
  1055. .name = KBUILD_MODNAME,
  1056. .id_table = rtl8187_table,
  1057. .probe = rtl8187_probe,
  1058. .disconnect = __devexit_p(rtl8187_disconnect),
  1059. };
  1060. static int __init rtl8187_init(void)
  1061. {
  1062. return usb_register(&rtl8187_driver);
  1063. }
  1064. static void __exit rtl8187_exit(void)
  1065. {
  1066. usb_deregister(&rtl8187_driver);
  1067. }
  1068. module_init(rtl8187_init);
  1069. module_exit(rtl8187_exit);