rtl8187_dev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 USB wireless driver");
  28. MODULE_LICENSE("GPL");
  29. static struct usb_device_id rtl8187_table[] __devinitdata = {
  30. /* Realtek */
  31. {USB_DEVICE(0x0bda, 0x8187)},
  32. /* Netgear */
  33. {USB_DEVICE(0x0846, 0x6100)},
  34. {USB_DEVICE(0x0846, 0x6a00)},
  35. {}
  36. };
  37. MODULE_DEVICE_TABLE(usb, rtl8187_table);
  38. void rtl8187_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  39. {
  40. struct rtl8187_priv *priv = dev->priv;
  41. data <<= 8;
  42. data |= addr | 0x80;
  43. rtl818x_iowrite8(priv, &priv->map->PHY[3], (data >> 24) & 0xFF);
  44. rtl818x_iowrite8(priv, &priv->map->PHY[2], (data >> 16) & 0xFF);
  45. rtl818x_iowrite8(priv, &priv->map->PHY[1], (data >> 8) & 0xFF);
  46. rtl818x_iowrite8(priv, &priv->map->PHY[0], data & 0xFF);
  47. msleep(1);
  48. }
  49. static void rtl8187_tx_cb(struct urb *urb)
  50. {
  51. struct ieee80211_tx_status status = { {0} };
  52. struct sk_buff *skb = (struct sk_buff *)urb->context;
  53. struct rtl8187_tx_info *info = (struct rtl8187_tx_info *)skb->cb;
  54. usb_free_urb(info->urb);
  55. if (info->control)
  56. memcpy(&status.control, info->control, sizeof(status.control));
  57. kfree(info->control);
  58. skb_pull(skb, sizeof(struct rtl8187_tx_hdr));
  59. status.flags |= IEEE80211_TX_STATUS_ACK;
  60. ieee80211_tx_status_irqsafe(info->dev, skb, &status);
  61. }
  62. static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb,
  63. struct ieee80211_tx_control *control)
  64. {
  65. struct rtl8187_priv *priv = dev->priv;
  66. struct rtl8187_tx_hdr *hdr;
  67. struct rtl8187_tx_info *info;
  68. struct urb *urb;
  69. u32 tmp;
  70. urb = usb_alloc_urb(0, GFP_ATOMIC);
  71. if (!urb) {
  72. kfree_skb(skb);
  73. return 0;
  74. }
  75. hdr = (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
  76. tmp = skb->len - sizeof(*hdr);
  77. tmp |= RTL8187_TX_FLAG_NO_ENCRYPT;
  78. tmp |= control->rts_cts_rate << 19;
  79. tmp |= control->tx_rate << 24;
  80. if (ieee80211_get_morefrag((struct ieee80211_hdr *)skb))
  81. tmp |= RTL8187_TX_FLAG_MORE_FRAG;
  82. if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  83. tmp |= RTL8187_TX_FLAG_RTS;
  84. hdr->rts_duration =
  85. ieee80211_rts_duration(dev, priv->if_id, skb->len, control);
  86. }
  87. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  88. tmp |= RTL8187_TX_FLAG_CTS;
  89. hdr->flags = cpu_to_le32(tmp);
  90. hdr->len = 0;
  91. tmp = control->retry_limit << 8;
  92. hdr->retry = cpu_to_le32(tmp);
  93. info = (struct rtl8187_tx_info *)skb->cb;
  94. info->control = kmemdup(control, sizeof(*control), GFP_ATOMIC);
  95. info->urb = urb;
  96. info->dev = dev;
  97. usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, 2),
  98. hdr, skb->len, rtl8187_tx_cb, skb);
  99. usb_submit_urb(urb, GFP_ATOMIC);
  100. return 0;
  101. }
  102. static void rtl8187_rx_cb(struct urb *urb)
  103. {
  104. struct sk_buff *skb = (struct sk_buff *)urb->context;
  105. struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
  106. struct ieee80211_hw *dev = info->dev;
  107. struct rtl8187_priv *priv = dev->priv;
  108. struct rtl8187_rx_hdr *hdr;
  109. struct ieee80211_rx_status rx_status = { 0 };
  110. int rate, signal;
  111. spin_lock(&priv->rx_queue.lock);
  112. if (skb->next)
  113. __skb_unlink(skb, &priv->rx_queue);
  114. else {
  115. spin_unlock(&priv->rx_queue.lock);
  116. return;
  117. }
  118. spin_unlock(&priv->rx_queue.lock);
  119. if (unlikely(urb->status)) {
  120. usb_free_urb(urb);
  121. dev_kfree_skb_irq(skb);
  122. return;
  123. }
  124. skb_put(skb, urb->actual_length);
  125. hdr = (struct rtl8187_rx_hdr *)(skb_tail_pointer(skb) - sizeof(*hdr));
  126. skb_trim(skb, le16_to_cpu(hdr->len) & 0x0FFF);
  127. signal = hdr->agc >> 1;
  128. rate = (le16_to_cpu(hdr->rate) >> 4) & 0xF;
  129. if (rate > 3) { /* OFDM rate */
  130. if (signal > 90)
  131. signal = 90;
  132. else if (signal < 25)
  133. signal = 25;
  134. signal = 90 - signal;
  135. } else { /* CCK rate */
  136. if (signal > 95)
  137. signal = 95;
  138. else if (signal < 30)
  139. signal = 30;
  140. signal = 95 - signal;
  141. }
  142. rx_status.antenna = (hdr->signal >> 7) & 1;
  143. rx_status.signal = 64 - min(hdr->noise, (u8)64);
  144. rx_status.ssi = signal;
  145. rx_status.rate = rate;
  146. rx_status.freq = dev->conf.freq;
  147. rx_status.channel = dev->conf.channel;
  148. rx_status.phymode = dev->conf.phymode;
  149. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  150. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  151. skb = dev_alloc_skb(RTL8187_MAX_RX);
  152. if (unlikely(!skb)) {
  153. usb_free_urb(urb);
  154. /* TODO check rx queue length and refill *somewhere* */
  155. return;
  156. }
  157. info = (struct rtl8187_rx_info *)skb->cb;
  158. info->urb = urb;
  159. info->dev = dev;
  160. urb->transfer_buffer = skb_tail_pointer(skb);
  161. urb->context = skb;
  162. skb_queue_tail(&priv->rx_queue, skb);
  163. usb_submit_urb(urb, GFP_ATOMIC);
  164. }
  165. static int rtl8187_init_urbs(struct ieee80211_hw *dev)
  166. {
  167. struct rtl8187_priv *priv = dev->priv;
  168. struct urb *entry;
  169. struct sk_buff *skb;
  170. struct rtl8187_rx_info *info;
  171. while (skb_queue_len(&priv->rx_queue) < 8) {
  172. skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
  173. if (!skb)
  174. break;
  175. entry = usb_alloc_urb(0, GFP_KERNEL);
  176. if (!entry) {
  177. kfree_skb(skb);
  178. break;
  179. }
  180. usb_fill_bulk_urb(entry, priv->udev,
  181. usb_rcvbulkpipe(priv->udev, 1),
  182. skb_tail_pointer(skb),
  183. RTL8187_MAX_RX, rtl8187_rx_cb, skb);
  184. info = (struct rtl8187_rx_info *)skb->cb;
  185. info->urb = entry;
  186. info->dev = dev;
  187. skb_queue_tail(&priv->rx_queue, skb);
  188. usb_submit_urb(entry, GFP_KERNEL);
  189. }
  190. return 0;
  191. }
  192. static int rtl8187_init_hw(struct ieee80211_hw *dev)
  193. {
  194. struct rtl8187_priv *priv = dev->priv;
  195. u8 reg;
  196. int i;
  197. /* reset */
  198. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  199. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  200. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  201. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  202. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  203. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  204. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  205. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  206. msleep(200);
  207. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  208. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  209. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  210. msleep(200);
  211. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  212. reg &= (1 << 1);
  213. reg |= RTL818X_CMD_RESET;
  214. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  215. i = 10;
  216. do {
  217. msleep(2);
  218. if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
  219. RTL818X_CMD_RESET))
  220. break;
  221. } while (--i);
  222. if (!i) {
  223. printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
  224. return -ETIMEDOUT;
  225. }
  226. /* reload registers from eeprom */
  227. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  228. i = 10;
  229. do {
  230. msleep(4);
  231. if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
  232. RTL818X_EEPROM_CMD_CONFIG))
  233. break;
  234. } while (--i);
  235. if (!i) {
  236. printk(KERN_ERR "%s: eeprom reset timeout!\n",
  237. wiphy_name(dev->wiphy));
  238. return -ETIMEDOUT;
  239. }
  240. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  241. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  242. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  243. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  244. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  245. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  246. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  247. /* setup card */
  248. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  249. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  250. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  251. rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
  252. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  253. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  254. for (i = 0; i < ETH_ALEN; i++)
  255. rtl818x_iowrite8(priv, &priv->map->MAC[i], priv->hwaddr[i]);
  256. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  257. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  258. reg &= 0x3F;
  259. reg |= 0x80;
  260. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  261. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  262. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  263. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  264. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  265. // TODO: set RESP_RATE and BRSR properly
  266. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  267. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  268. /* host_usb_init */
  269. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  270. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  271. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  272. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  273. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  274. rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
  275. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  276. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  277. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  278. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  279. msleep(100);
  280. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  281. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  282. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  283. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  284. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  285. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  286. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  287. msleep(100);
  288. priv->rf_init(dev);
  289. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  290. reg = rtl818x_ioread16(priv, &priv->map->PGSELECT) & 0xfffe;
  291. rtl818x_iowrite16(priv, &priv->map->PGSELECT, reg | 0x1);
  292. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  293. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  294. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  295. rtl818x_iowrite16(priv, &priv->map->PGSELECT, reg);
  296. return 0;
  297. }
  298. static void rtl8187_set_channel(struct ieee80211_hw *dev, int channel)
  299. {
  300. u32 reg;
  301. struct rtl8187_priv *priv = dev->priv;
  302. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  303. /* Enable TX loopback on MAC level to avoid TX during channel
  304. * changes, as this has be seen to causes problems and the
  305. * card will stop work until next reset
  306. */
  307. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  308. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  309. msleep(10);
  310. rtl8225_rf_set_channel(dev, channel);
  311. msleep(10);
  312. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  313. }
  314. static int rtl8187_open(struct ieee80211_hw *dev)
  315. {
  316. struct rtl8187_priv *priv = dev->priv;
  317. u32 reg;
  318. int ret;
  319. ret = rtl8187_init_hw(dev);
  320. if (ret)
  321. return ret;
  322. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  323. rtl8187_init_urbs(dev);
  324. reg = RTL818X_RX_CONF_ONLYERLPKT |
  325. RTL818X_RX_CONF_RX_AUTORESETPHY |
  326. RTL818X_RX_CONF_BSSID |
  327. RTL818X_RX_CONF_MGMT |
  328. RTL818X_RX_CONF_CTRL |
  329. RTL818X_RX_CONF_DATA |
  330. (7 << 13 /* RX FIFO threshold NONE */) |
  331. (7 << 10 /* MAX RX DMA */) |
  332. RTL818X_RX_CONF_BROADCAST |
  333. RTL818X_RX_CONF_MULTICAST |
  334. RTL818X_RX_CONF_NICMAC;
  335. if (priv->mode == IEEE80211_IF_TYPE_MNTR)
  336. reg |= RTL818X_RX_CONF_MONITOR;
  337. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  338. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  339. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  340. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  341. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  342. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  343. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  344. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  345. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  346. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  347. reg = RTL818X_TX_CONF_CW_MIN |
  348. (7 << 21 /* MAX TX DMA */) |
  349. RTL818X_TX_CONF_NO_ICV;
  350. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  351. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  352. reg |= RTL818X_CMD_TX_ENABLE;
  353. reg |= RTL818X_CMD_RX_ENABLE;
  354. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  355. return 0;
  356. }
  357. static int rtl8187_stop(struct ieee80211_hw *dev)
  358. {
  359. struct rtl8187_priv *priv = dev->priv;
  360. struct rtl8187_rx_info *info;
  361. struct sk_buff *skb;
  362. u32 reg;
  363. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  364. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  365. reg &= ~RTL818X_CMD_TX_ENABLE;
  366. reg &= ~RTL818X_CMD_RX_ENABLE;
  367. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  368. rtl8225_rf_stop(dev);
  369. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  370. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  371. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  372. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  373. while ((skb = skb_dequeue(&priv->rx_queue))) {
  374. info = (struct rtl8187_rx_info *)skb->cb;
  375. usb_kill_urb(info->urb);
  376. kfree_skb(skb);
  377. }
  378. return 0;
  379. }
  380. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  381. struct ieee80211_if_init_conf *conf)
  382. {
  383. struct rtl8187_priv *priv = dev->priv;
  384. /* NOTE: using IEEE80211_IF_TYPE_MGMT to indicate no mode selected */
  385. if (priv->mode != IEEE80211_IF_TYPE_MGMT)
  386. return -1;
  387. switch (conf->type) {
  388. case IEEE80211_IF_TYPE_STA:
  389. case IEEE80211_IF_TYPE_MNTR:
  390. priv->mode = conf->type;
  391. break;
  392. default:
  393. return -EOPNOTSUPP;
  394. }
  395. priv->hwaddr = conf->mac_addr ? conf->mac_addr : dev->wiphy->perm_addr;
  396. return 0;
  397. }
  398. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  399. struct ieee80211_if_init_conf *conf)
  400. {
  401. struct rtl8187_priv *priv = dev->priv;
  402. priv->mode = IEEE80211_IF_TYPE_MGMT;
  403. }
  404. static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  405. {
  406. struct rtl8187_priv *priv = dev->priv;
  407. rtl8187_set_channel(dev, conf->channel);
  408. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  409. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  410. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  411. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  412. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  413. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
  414. } else {
  415. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  416. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  417. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  418. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
  419. }
  420. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  421. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  422. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  423. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  424. return 0;
  425. }
  426. static int rtl8187_config_interface(struct ieee80211_hw *dev, int if_id,
  427. struct ieee80211_if_conf *conf)
  428. {
  429. struct rtl8187_priv *priv = dev->priv;
  430. int i;
  431. priv->if_id = if_id;
  432. for (i = 0; i < ETH_ALEN; i++)
  433. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  434. if (is_valid_ether_addr(conf->bssid))
  435. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_INFRA);
  436. else
  437. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_NO_LINK);
  438. return 0;
  439. }
  440. static const struct ieee80211_ops rtl8187_ops = {
  441. .tx = rtl8187_tx,
  442. .open = rtl8187_open,
  443. .stop = rtl8187_stop,
  444. .add_interface = rtl8187_add_interface,
  445. .remove_interface = rtl8187_remove_interface,
  446. .config = rtl8187_config,
  447. .config_interface = rtl8187_config_interface,
  448. };
  449. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  450. {
  451. struct ieee80211_hw *dev = eeprom->data;
  452. struct rtl8187_priv *priv = dev->priv;
  453. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  454. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  455. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  456. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  457. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  458. }
  459. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  460. {
  461. struct ieee80211_hw *dev = eeprom->data;
  462. struct rtl8187_priv *priv = dev->priv;
  463. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  464. if (eeprom->reg_data_in)
  465. reg |= RTL818X_EEPROM_CMD_WRITE;
  466. if (eeprom->reg_data_out)
  467. reg |= RTL818X_EEPROM_CMD_READ;
  468. if (eeprom->reg_data_clock)
  469. reg |= RTL818X_EEPROM_CMD_CK;
  470. if (eeprom->reg_chip_select)
  471. reg |= RTL818X_EEPROM_CMD_CS;
  472. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  473. udelay(10);
  474. }
  475. static int __devinit rtl8187_probe(struct usb_interface *intf,
  476. const struct usb_device_id *id)
  477. {
  478. struct usb_device *udev = interface_to_usbdev(intf);
  479. struct ieee80211_hw *dev;
  480. struct rtl8187_priv *priv;
  481. struct eeprom_93cx6 eeprom;
  482. struct ieee80211_channel *channel;
  483. u16 txpwr, reg;
  484. int err, i;
  485. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  486. if (!dev) {
  487. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  488. return -ENOMEM;
  489. }
  490. priv = dev->priv;
  491. SET_IEEE80211_DEV(dev, &intf->dev);
  492. usb_set_intfdata(intf, dev);
  493. priv->udev = udev;
  494. usb_get_dev(udev);
  495. skb_queue_head_init(&priv->rx_queue);
  496. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  497. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  498. priv->map = (struct rtl818x_csr *)0xFF00;
  499. priv->modes[0].mode = MODE_IEEE80211G;
  500. priv->modes[0].num_rates = ARRAY_SIZE(rtl818x_rates);
  501. priv->modes[0].rates = priv->rates;
  502. priv->modes[0].num_channels = ARRAY_SIZE(rtl818x_channels);
  503. priv->modes[0].channels = priv->channels;
  504. priv->modes[1].mode = MODE_IEEE80211B;
  505. priv->modes[1].num_rates = 4;
  506. priv->modes[1].rates = priv->rates;
  507. priv->modes[1].num_channels = ARRAY_SIZE(rtl818x_channels);
  508. priv->modes[1].channels = priv->channels;
  509. priv->mode = IEEE80211_IF_TYPE_MGMT;
  510. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  511. IEEE80211_HW_RX_INCLUDES_FCS;
  512. dev->extra_tx_headroom = sizeof(struct rtl8187_tx_hdr);
  513. dev->queues = 1;
  514. dev->max_rssi = 65;
  515. dev->max_signal = 64;
  516. for (i = 0; i < 2; i++)
  517. if ((err = ieee80211_register_hwmode(dev, &priv->modes[i])))
  518. goto err_free_dev;
  519. eeprom.data = dev;
  520. eeprom.register_read = rtl8187_eeprom_register_read;
  521. eeprom.register_write = rtl8187_eeprom_register_write;
  522. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  523. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  524. else
  525. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  526. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  527. udelay(10);
  528. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  529. (__le16 __force *)dev->wiphy->perm_addr, 3);
  530. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  531. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  532. "generated MAC address\n");
  533. random_ether_addr(dev->wiphy->perm_addr);
  534. }
  535. channel = priv->channels;
  536. for (i = 0; i < 3; i++) {
  537. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  538. &txpwr);
  539. (*channel++).val = txpwr & 0xFF;
  540. (*channel++).val = txpwr >> 8;
  541. }
  542. for (i = 0; i < 2; i++) {
  543. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  544. &txpwr);
  545. (*channel++).val = txpwr & 0xFF;
  546. (*channel++).val = txpwr >> 8;
  547. }
  548. for (i = 0; i < 2; i++) {
  549. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  550. &txpwr);
  551. (*channel++).val = txpwr & 0xFF;
  552. (*channel++).val = txpwr >> 8;
  553. }
  554. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  555. &priv->txpwr_base);
  556. reg = rtl818x_ioread16(priv, &priv->map->PGSELECT) & ~1;
  557. rtl818x_iowrite16(priv, &priv->map->PGSELECT, reg | 1);
  558. /* 0 means asic B-cut, we should use SW 3 wire
  559. * bit-by-bit banging for radio. 1 means we can use
  560. * USB specific request to write radio registers */
  561. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  562. rtl818x_iowrite16(priv, &priv->map->PGSELECT, reg);
  563. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  564. rtl8225_write(dev, 0, 0x1B7);
  565. if (rtl8225_read(dev, 8) != 0x588 || rtl8225_read(dev, 9) != 0x700)
  566. priv->rf_init = rtl8225_rf_init;
  567. else
  568. priv->rf_init = rtl8225z2_rf_init;
  569. rtl8225_write(dev, 0, 0x0B7);
  570. err = ieee80211_register_hw(dev);
  571. if (err) {
  572. printk(KERN_ERR "rtl8187: Cannot register device\n");
  573. goto err_free_dev;
  574. }
  575. printk(KERN_INFO "%s: hwaddr " MAC_FMT ", rtl8187 V%d + %s\n",
  576. wiphy_name(dev->wiphy), MAC_ARG(dev->wiphy->perm_addr),
  577. priv->asic_rev, priv->rf_init == rtl8225_rf_init ?
  578. "rtl8225" : "rtl8225z2");
  579. return 0;
  580. err_free_dev:
  581. ieee80211_free_hw(dev);
  582. usb_set_intfdata(intf, NULL);
  583. usb_put_dev(udev);
  584. return err;
  585. }
  586. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  587. {
  588. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  589. struct rtl8187_priv *priv;
  590. if (!dev)
  591. return;
  592. ieee80211_unregister_hw(dev);
  593. priv = dev->priv;
  594. usb_put_dev(interface_to_usbdev(intf));
  595. ieee80211_free_hw(dev);
  596. }
  597. static struct usb_driver rtl8187_driver = {
  598. .name = KBUILD_MODNAME,
  599. .id_table = rtl8187_table,
  600. .probe = rtl8187_probe,
  601. .disconnect = rtl8187_disconnect,
  602. };
  603. static int __init rtl8187_init(void)
  604. {
  605. return usb_register(&rtl8187_driver);
  606. }
  607. static void __exit rtl8187_exit(void)
  608. {
  609. usb_deregister(&rtl8187_driver);
  610. }
  611. module_init(rtl8187_init);
  612. module_exit(rtl8187_exit);