rtl8187_dev.c 39 KB

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