rtl8187_dev.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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, RTL8225_ANAPARAM_ON);
  378. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  379. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg &
  380. ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  381. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  382. RTL818X_EEPROM_CMD_NORMAL);
  383. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  384. msleep(200);
  385. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  386. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  387. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  388. msleep(200);
  389. res = rtl8187_cmd_reset(dev);
  390. if (res)
  391. return res;
  392. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  393. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  394. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  395. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  396. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  397. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  398. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  399. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  400. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  401. /* setup card */
  402. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  403. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  404. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  405. rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
  406. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  407. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  408. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  409. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  410. reg &= 0x3F;
  411. reg |= 0x80;
  412. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  413. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  414. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  415. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  416. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  417. // TODO: set RESP_RATE and BRSR properly
  418. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  419. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  420. /* host_usb_init */
  421. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  422. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  423. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  424. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  425. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  426. rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
  427. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  428. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  429. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  430. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  431. msleep(100);
  432. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  433. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  434. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  435. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  436. RTL818X_EEPROM_CMD_CONFIG);
  437. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  438. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  439. RTL818X_EEPROM_CMD_NORMAL);
  440. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  441. msleep(100);
  442. priv->rf->init(dev);
  443. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  444. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  445. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  446. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  447. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  448. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  449. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  450. return 0;
  451. }
  452. static const u8 rtl8187b_reg_table[][3] = {
  453. {0xF0, 0x32, 0}, {0xF1, 0x32, 0}, {0xF2, 0x00, 0}, {0xF3, 0x00, 0},
  454. {0xF4, 0x32, 0}, {0xF5, 0x43, 0}, {0xF6, 0x00, 0}, {0xF7, 0x00, 0},
  455. {0xF8, 0x46, 0}, {0xF9, 0xA4, 0}, {0xFA, 0x00, 0}, {0xFB, 0x00, 0},
  456. {0xFC, 0x96, 0}, {0xFD, 0xA4, 0}, {0xFE, 0x00, 0}, {0xFF, 0x00, 0},
  457. {0x58, 0x4B, 1}, {0x59, 0x00, 1}, {0x5A, 0x4B, 1}, {0x5B, 0x00, 1},
  458. {0x60, 0x4B, 1}, {0x61, 0x09, 1}, {0x62, 0x4B, 1}, {0x63, 0x09, 1},
  459. {0xCE, 0x0F, 1}, {0xCF, 0x00, 1}, {0xE0, 0xFF, 1}, {0xE1, 0x0F, 1},
  460. {0xE2, 0x00, 1}, {0xF0, 0x4E, 1}, {0xF1, 0x01, 1}, {0xF2, 0x02, 1},
  461. {0xF3, 0x03, 1}, {0xF4, 0x04, 1}, {0xF5, 0x05, 1}, {0xF6, 0x06, 1},
  462. {0xF7, 0x07, 1}, {0xF8, 0x08, 1},
  463. {0x4E, 0x00, 2}, {0x0C, 0x04, 2}, {0x21, 0x61, 2}, {0x22, 0x68, 2},
  464. {0x23, 0x6F, 2}, {0x24, 0x76, 2}, {0x25, 0x7D, 2}, {0x26, 0x84, 2},
  465. {0x27, 0x8D, 2}, {0x4D, 0x08, 2}, {0x50, 0x05, 2}, {0x51, 0xF5, 2},
  466. {0x52, 0x04, 2}, {0x53, 0xA0, 2}, {0x54, 0x1F, 2}, {0x55, 0x23, 2},
  467. {0x56, 0x45, 2}, {0x57, 0x67, 2}, {0x58, 0x08, 2}, {0x59, 0x08, 2},
  468. {0x5A, 0x08, 2}, {0x5B, 0x08, 2}, {0x60, 0x08, 2}, {0x61, 0x08, 2},
  469. {0x62, 0x08, 2}, {0x63, 0x08, 2}, {0x64, 0xCF, 2}, {0x72, 0x56, 2},
  470. {0x73, 0x9A, 2},
  471. {0x34, 0xF0, 0}, {0x35, 0x0F, 0}, {0x5B, 0x40, 0}, {0x84, 0x88, 0},
  472. {0x85, 0x24, 0}, {0x88, 0x54, 0}, {0x8B, 0xB8, 0}, {0x8C, 0x07, 0},
  473. {0x8D, 0x00, 0}, {0x94, 0x1B, 0}, {0x95, 0x12, 0}, {0x96, 0x00, 0},
  474. {0x97, 0x06, 0}, {0x9D, 0x1A, 0}, {0x9F, 0x10, 0}, {0xB4, 0x22, 0},
  475. {0xBE, 0x80, 0}, {0xDB, 0x00, 0}, {0xEE, 0x00, 0}, {0x91, 0x03, 0},
  476. {0x4C, 0x00, 2}, {0x9F, 0x00, 3}, {0x8C, 0x01, 0}, {0x8D, 0x10, 0},
  477. {0x8E, 0x08, 0}, {0x8F, 0x00, 0}
  478. };
  479. static int rtl8187b_init_hw(struct ieee80211_hw *dev)
  480. {
  481. struct rtl8187_priv *priv = dev->priv;
  482. int res, i;
  483. u8 reg;
  484. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  485. RTL818X_EEPROM_CMD_CONFIG);
  486. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  487. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE | RTL818X_CONFIG3_GNT_SELECT;
  488. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  489. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, 0x727f3f52);
  490. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, 0x45090658);
  491. rtl818x_iowrite8(priv, &priv->map->ANAPARAM3, 0);
  492. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0x10);
  493. reg = rtl818x_ioread8(priv, (u8 *)0xFF62);
  494. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg & ~(1 << 5));
  495. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg | (1 << 5));
  496. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  497. reg &= ~RTL818X_CONFIG3_ANAPARAM_WRITE;
  498. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  499. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  500. RTL818X_EEPROM_CMD_NORMAL);
  501. res = rtl8187_cmd_reset(dev);
  502. if (res)
  503. return res;
  504. rtl818x_iowrite16(priv, (__le16 *)0xFF2D, 0x0FFF);
  505. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  506. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  507. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  508. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  509. reg |= RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT |
  510. RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  511. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  512. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFE0, 0x0FFF, 1);
  513. reg = rtl818x_ioread8(priv, &priv->map->RATE_FALLBACK);
  514. reg |= RTL818X_RATE_FALLBACK_ENABLE;
  515. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, reg);
  516. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  517. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  518. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFD4, 0xFFFF, 1);
  519. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  520. RTL818X_EEPROM_CMD_CONFIG);
  521. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  522. rtl818x_iowrite8(priv, &priv->map->CONFIG1, (reg & 0x3F) | 0x80);
  523. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  524. RTL818X_EEPROM_CMD_NORMAL);
  525. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  526. for (i = 0; i < ARRAY_SIZE(rtl8187b_reg_table); i++) {
  527. rtl818x_iowrite8_idx(priv,
  528. (u8 *)(uintptr_t)
  529. (rtl8187b_reg_table[i][0] | 0xFF00),
  530. rtl8187b_reg_table[i][1],
  531. rtl8187b_reg_table[i][2]);
  532. }
  533. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  534. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  535. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF0, 0, 1);
  536. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF4, 0, 1);
  537. rtl818x_iowrite8_idx(priv, (u8 *)0xFFF8, 0, 1);
  538. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x00004001);
  539. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x569A, 2);
  540. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  541. RTL818X_EEPROM_CMD_CONFIG);
  542. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  543. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE;
  544. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  545. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  546. RTL818X_EEPROM_CMD_NORMAL);
  547. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  548. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  549. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FFF);
  550. msleep(1100);
  551. priv->rf->init(dev);
  552. reg = RTL818X_CMD_TX_ENABLE | RTL818X_CMD_RX_ENABLE;
  553. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  554. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  555. rtl818x_iowrite8(priv, (u8 *)0xFE41, 0xF4);
  556. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x00);
  557. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  558. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  559. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x0F);
  560. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  561. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  562. reg = rtl818x_ioread8(priv, (u8 *)0xFFDB);
  563. rtl818x_iowrite8(priv, (u8 *)0xFFDB, reg | (1 << 2));
  564. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x59FA, 3);
  565. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF74, 0x59D2, 3);
  566. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF76, 0x59D2, 3);
  567. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF78, 0x19FA, 3);
  568. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7A, 0x19FA, 3);
  569. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7C, 0x00D0, 3);
  570. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0);
  571. rtl818x_iowrite8_idx(priv, (u8 *)0xFF80, 0x0F, 1);
  572. rtl818x_iowrite8_idx(priv, (u8 *)0xFF83, 0x03, 1);
  573. rtl818x_iowrite8(priv, (u8 *)0xFFDA, 0x10);
  574. rtl818x_iowrite8_idx(priv, (u8 *)0xFF4D, 0x08, 2);
  575. rtl818x_iowrite32(priv, &priv->map->HSSI_PARA, 0x0600321B);
  576. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFEC, 0x0800, 1);
  577. return 0;
  578. }
  579. static int rtl8187_start(struct ieee80211_hw *dev)
  580. {
  581. struct rtl8187_priv *priv = dev->priv;
  582. u32 reg;
  583. int ret;
  584. ret = (!priv->is_rtl8187b) ? rtl8187_init_hw(dev) :
  585. rtl8187b_init_hw(dev);
  586. if (ret)
  587. return ret;
  588. if (priv->is_rtl8187b) {
  589. reg = RTL818X_RX_CONF_MGMT |
  590. RTL818X_RX_CONF_DATA |
  591. RTL818X_RX_CONF_BROADCAST |
  592. RTL818X_RX_CONF_NICMAC |
  593. RTL818X_RX_CONF_BSSID |
  594. (7 << 13 /* RX FIFO threshold NONE */) |
  595. (7 << 10 /* MAX RX DMA */) |
  596. RTL818X_RX_CONF_RX_AUTORESETPHY |
  597. RTL818X_RX_CONF_ONLYERLPKT |
  598. RTL818X_RX_CONF_MULTICAST;
  599. priv->rx_conf = reg;
  600. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  601. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  602. RTL818X_TX_CONF_HW_SEQNUM |
  603. RTL818X_TX_CONF_DISREQQSIZE |
  604. (7 << 8 /* short retry limit */) |
  605. (7 << 0 /* long retry limit */) |
  606. (7 << 21 /* MAX TX DMA */));
  607. rtl8187_init_urbs(dev);
  608. return 0;
  609. }
  610. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  611. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  612. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  613. rtl8187_init_urbs(dev);
  614. reg = RTL818X_RX_CONF_ONLYERLPKT |
  615. RTL818X_RX_CONF_RX_AUTORESETPHY |
  616. RTL818X_RX_CONF_BSSID |
  617. RTL818X_RX_CONF_MGMT |
  618. RTL818X_RX_CONF_DATA |
  619. (7 << 13 /* RX FIFO threshold NONE */) |
  620. (7 << 10 /* MAX RX DMA */) |
  621. RTL818X_RX_CONF_BROADCAST |
  622. RTL818X_RX_CONF_NICMAC;
  623. priv->rx_conf = reg;
  624. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  625. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  626. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  627. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  628. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  629. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  630. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  631. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  632. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  633. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  634. reg = RTL818X_TX_CONF_CW_MIN |
  635. (7 << 21 /* MAX TX DMA */) |
  636. RTL818X_TX_CONF_NO_ICV;
  637. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  638. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  639. reg |= RTL818X_CMD_TX_ENABLE;
  640. reg |= RTL818X_CMD_RX_ENABLE;
  641. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  642. return 0;
  643. }
  644. static void rtl8187_stop(struct ieee80211_hw *dev)
  645. {
  646. struct rtl8187_priv *priv = dev->priv;
  647. struct rtl8187_rx_info *info;
  648. struct sk_buff *skb;
  649. u32 reg;
  650. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  651. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  652. reg &= ~RTL818X_CMD_TX_ENABLE;
  653. reg &= ~RTL818X_CMD_RX_ENABLE;
  654. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  655. priv->rf->stop(dev);
  656. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  657. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  658. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  659. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  660. while ((skb = skb_dequeue(&priv->rx_queue))) {
  661. info = (struct rtl8187_rx_info *)skb->cb;
  662. usb_kill_urb(info->urb);
  663. kfree_skb(skb);
  664. }
  665. return;
  666. }
  667. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  668. struct ieee80211_if_init_conf *conf)
  669. {
  670. struct rtl8187_priv *priv = dev->priv;
  671. int i;
  672. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  673. return -EOPNOTSUPP;
  674. switch (conf->type) {
  675. case IEEE80211_IF_TYPE_STA:
  676. priv->mode = conf->type;
  677. break;
  678. default:
  679. return -EOPNOTSUPP;
  680. }
  681. priv->vif = conf->vif;
  682. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  683. for (i = 0; i < ETH_ALEN; i++)
  684. rtl818x_iowrite8(priv, &priv->map->MAC[i],
  685. ((u8 *)conf->mac_addr)[i]);
  686. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  687. return 0;
  688. }
  689. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  690. struct ieee80211_if_init_conf *conf)
  691. {
  692. struct rtl8187_priv *priv = dev->priv;
  693. priv->mode = IEEE80211_IF_TYPE_MNTR;
  694. priv->vif = NULL;
  695. }
  696. static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  697. {
  698. struct rtl8187_priv *priv = dev->priv;
  699. u32 reg;
  700. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  701. /* Enable TX loopback on MAC level to avoid TX during channel
  702. * changes, as this has be seen to causes problems and the
  703. * card will stop work until next reset
  704. */
  705. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  706. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  707. msleep(10);
  708. priv->rf->set_chan(dev, conf);
  709. msleep(10);
  710. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  711. if (!priv->is_rtl8187b) {
  712. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  713. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  714. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  715. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  716. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  717. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
  718. } else {
  719. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  720. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  721. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  722. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
  723. }
  724. }
  725. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  726. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  727. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  728. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  729. return 0;
  730. }
  731. static int rtl8187_config_interface(struct ieee80211_hw *dev,
  732. struct ieee80211_vif *vif,
  733. struct ieee80211_if_conf *conf)
  734. {
  735. struct rtl8187_priv *priv = dev->priv;
  736. int i;
  737. u8 reg;
  738. for (i = 0; i < ETH_ALEN; i++)
  739. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  740. if (is_valid_ether_addr(conf->bssid)) {
  741. reg = RTL818X_MSR_INFRA;
  742. if (priv->is_rtl8187b)
  743. reg |= RTL818X_MSR_ENEDCA;
  744. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  745. } else {
  746. reg = RTL818X_MSR_NO_LINK;
  747. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  748. }
  749. return 0;
  750. }
  751. static void rtl8187_configure_filter(struct ieee80211_hw *dev,
  752. unsigned int changed_flags,
  753. unsigned int *total_flags,
  754. int mc_count, struct dev_addr_list *mclist)
  755. {
  756. struct rtl8187_priv *priv = dev->priv;
  757. if (changed_flags & FIF_FCSFAIL)
  758. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  759. if (changed_flags & FIF_CONTROL)
  760. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  761. if (changed_flags & FIF_OTHER_BSS)
  762. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  763. if (*total_flags & FIF_ALLMULTI || mc_count > 0)
  764. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  765. else
  766. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  767. *total_flags = 0;
  768. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  769. *total_flags |= FIF_FCSFAIL;
  770. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  771. *total_flags |= FIF_CONTROL;
  772. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  773. *total_flags |= FIF_OTHER_BSS;
  774. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  775. *total_flags |= FIF_ALLMULTI;
  776. rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
  777. }
  778. static const struct ieee80211_ops rtl8187_ops = {
  779. .tx = rtl8187_tx,
  780. .start = rtl8187_start,
  781. .stop = rtl8187_stop,
  782. .add_interface = rtl8187_add_interface,
  783. .remove_interface = rtl8187_remove_interface,
  784. .config = rtl8187_config,
  785. .config_interface = rtl8187_config_interface,
  786. .configure_filter = rtl8187_configure_filter,
  787. };
  788. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  789. {
  790. struct ieee80211_hw *dev = eeprom->data;
  791. struct rtl8187_priv *priv = dev->priv;
  792. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  793. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  794. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  795. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  796. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  797. }
  798. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  799. {
  800. struct ieee80211_hw *dev = eeprom->data;
  801. struct rtl8187_priv *priv = dev->priv;
  802. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  803. if (eeprom->reg_data_in)
  804. reg |= RTL818X_EEPROM_CMD_WRITE;
  805. if (eeprom->reg_data_out)
  806. reg |= RTL818X_EEPROM_CMD_READ;
  807. if (eeprom->reg_data_clock)
  808. reg |= RTL818X_EEPROM_CMD_CK;
  809. if (eeprom->reg_chip_select)
  810. reg |= RTL818X_EEPROM_CMD_CS;
  811. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  812. udelay(10);
  813. }
  814. static int __devinit rtl8187_probe(struct usb_interface *intf,
  815. const struct usb_device_id *id)
  816. {
  817. struct usb_device *udev = interface_to_usbdev(intf);
  818. struct ieee80211_hw *dev;
  819. struct rtl8187_priv *priv;
  820. struct eeprom_93cx6 eeprom;
  821. struct ieee80211_channel *channel;
  822. const char *chip_name;
  823. u16 txpwr, reg;
  824. int err, i;
  825. DECLARE_MAC_BUF(mac);
  826. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  827. if (!dev) {
  828. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  829. return -ENOMEM;
  830. }
  831. priv = dev->priv;
  832. priv->is_rtl8187b = (id->driver_info == DEVICE_RTL8187B) ||
  833. !memcmp(udev->product, "RTL8187B", 8);
  834. SET_IEEE80211_DEV(dev, &intf->dev);
  835. usb_set_intfdata(intf, dev);
  836. priv->udev = udev;
  837. usb_get_dev(udev);
  838. skb_queue_head_init(&priv->rx_queue);
  839. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  840. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  841. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  842. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  843. priv->map = (struct rtl818x_csr *)0xFF00;
  844. priv->band.band = IEEE80211_BAND_2GHZ;
  845. priv->band.channels = priv->channels;
  846. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  847. priv->band.bitrates = priv->rates;
  848. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  849. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  850. priv->mode = IEEE80211_IF_TYPE_MNTR;
  851. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  852. IEEE80211_HW_RX_INCLUDES_FCS |
  853. IEEE80211_HW_SIGNAL_UNSPEC;
  854. dev->extra_tx_headroom = (!priv->is_rtl8187b) ?
  855. sizeof(struct rtl8187_tx_hdr) :
  856. sizeof(struct rtl8187b_tx_hdr);
  857. if (!priv->is_rtl8187b)
  858. dev->queues = 1;
  859. else
  860. dev->queues = 4;
  861. dev->max_signal = 65;
  862. eeprom.data = dev;
  863. eeprom.register_read = rtl8187_eeprom_register_read;
  864. eeprom.register_write = rtl8187_eeprom_register_write;
  865. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  866. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  867. else
  868. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  869. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  870. udelay(10);
  871. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  872. (__le16 __force *)dev->wiphy->perm_addr, 3);
  873. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  874. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  875. "generated MAC address\n");
  876. random_ether_addr(dev->wiphy->perm_addr);
  877. }
  878. channel = priv->channels;
  879. for (i = 0; i < 3; i++) {
  880. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  881. &txpwr);
  882. (*channel++).hw_value = txpwr & 0xFF;
  883. (*channel++).hw_value = txpwr >> 8;
  884. }
  885. for (i = 0; i < 2; i++) {
  886. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  887. &txpwr);
  888. (*channel++).hw_value = txpwr & 0xFF;
  889. (*channel++).hw_value = txpwr >> 8;
  890. }
  891. if (!priv->is_rtl8187b) {
  892. for (i = 0; i < 2; i++) {
  893. eeprom_93cx6_read(&eeprom,
  894. RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  895. &txpwr);
  896. (*channel++).hw_value = txpwr & 0xFF;
  897. (*channel++).hw_value = txpwr >> 8;
  898. }
  899. } else {
  900. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
  901. &txpwr);
  902. (*channel++).hw_value = txpwr & 0xFF;
  903. eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
  904. (*channel++).hw_value = txpwr & 0xFF;
  905. eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
  906. (*channel++).hw_value = txpwr & 0xFF;
  907. (*channel++).hw_value = txpwr >> 8;
  908. }
  909. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  910. &priv->txpwr_base);
  911. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  912. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  913. /* 0 means asic B-cut, we should use SW 3 wire
  914. * bit-by-bit banging for radio. 1 means we can use
  915. * USB specific request to write radio registers */
  916. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  917. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  918. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  919. if (!priv->is_rtl8187b) {
  920. u32 reg32;
  921. reg32 = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  922. reg32 &= RTL818X_TX_CONF_HWVER_MASK;
  923. switch (reg32) {
  924. case RTL818X_TX_CONF_R8187vD_1:
  925. case RTL818X_TX_CONF_R8187vD_2:
  926. chip_name = "RTL8187vD";
  927. break;
  928. default:
  929. chip_name = "RTL8187vB (default)";
  930. }
  931. } else {
  932. printk(KERN_WARNING "rtl8187: 8187B chip detected. Support "
  933. "is EXPERIMENTAL, and could damage your\n"
  934. " hardware, use at your own risk\n");
  935. /*
  936. * Force USB request to write radio registers for 8187B, Realtek
  937. * only uses it in their sources
  938. */
  939. /*if (priv->asic_rev == 0) {
  940. printk(KERN_WARNING "rtl8187: Forcing use of USB "
  941. "requests to write to radio registers\n");
  942. priv->asic_rev = 1;
  943. }*/
  944. switch (rtl818x_ioread8(priv, (u8 *)0xFFE1)) {
  945. case RTL818X_R8187B_B:
  946. chip_name = "RTL8187BvB";
  947. priv->hw_rev = RTL8187BvB;
  948. break;
  949. case RTL818X_R8187B_D:
  950. chip_name = "RTL8187BvD";
  951. priv->hw_rev = RTL8187BvD;
  952. break;
  953. case RTL818X_R8187B_E:
  954. chip_name = "RTL8187BvE";
  955. priv->hw_rev = RTL8187BvE;
  956. break;
  957. default:
  958. chip_name = "RTL8187BvB (default)";
  959. priv->hw_rev = RTL8187BvB;
  960. }
  961. }
  962. priv->rf = rtl8187_detect_rf(dev);
  963. err = ieee80211_register_hw(dev);
  964. if (err) {
  965. printk(KERN_ERR "rtl8187: Cannot register device\n");
  966. goto err_free_dev;
  967. }
  968. printk(KERN_INFO "%s: hwaddr %s, %s V%d + %s\n",
  969. wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
  970. chip_name, priv->asic_rev, priv->rf->name);
  971. return 0;
  972. err_free_dev:
  973. ieee80211_free_hw(dev);
  974. usb_set_intfdata(intf, NULL);
  975. usb_put_dev(udev);
  976. return err;
  977. }
  978. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  979. {
  980. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  981. struct rtl8187_priv *priv;
  982. if (!dev)
  983. return;
  984. ieee80211_unregister_hw(dev);
  985. priv = dev->priv;
  986. usb_put_dev(interface_to_usbdev(intf));
  987. ieee80211_free_hw(dev);
  988. }
  989. static struct usb_driver rtl8187_driver = {
  990. .name = KBUILD_MODNAME,
  991. .id_table = rtl8187_table,
  992. .probe = rtl8187_probe,
  993. .disconnect = rtl8187_disconnect,
  994. };
  995. static int __init rtl8187_init(void)
  996. {
  997. return usb_register(&rtl8187_driver);
  998. }
  999. static void __exit rtl8187_exit(void)
  1000. {
  1001. usb_deregister(&rtl8187_driver);
  1002. }
  1003. module_init(rtl8187_init);
  1004. module_exit(rtl8187_exit);