rtl8187_dev.c 37 KB

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