rtl8187_dev.c 35 KB

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