rtl8187_dev.c 38 KB

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