rtl8187_dev.c 23 KB

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