rtl8187_dev.c 38 KB

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