rtl8187_dev.c 37 KB

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