rtl8187_dev.c 23 KB

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