rtl8187_dev.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. * The driver was extended to the RTL8187B in 2008 by:
  11. * Herton Ronaldo Krzesinski <herton@mandriva.com.br>
  12. * Hin-Tak Leung <htl10@users.sourceforge.net>
  13. * Larry Finger <Larry.Finger@lwfinger.net>
  14. *
  15. * Magic delays and register offsets below are taken from the original
  16. * r8187 driver sources. Thanks to Realtek for their support!
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/usb.h>
  24. #include <linux/delay.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/eeprom_93cx6.h>
  27. #include <net/mac80211.h>
  28. #include "rtl8187.h"
  29. #include "rtl8187_rtl8225.h"
  30. #ifdef CONFIG_RTL8187_LEDS
  31. #include "rtl8187_leds.h"
  32. #endif
  33. #include "rtl8187_rfkill.h"
  34. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  35. MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
  36. MODULE_AUTHOR("Herton Ronaldo Krzesinski <herton@mandriva.com.br>");
  37. MODULE_AUTHOR("Hin-Tak Leung <htl10@users.sourceforge.net>");
  38. MODULE_AUTHOR("Larry Finger <Larry.Finger@lwfinger.net>");
  39. MODULE_DESCRIPTION("RTL8187/RTL8187B USB wireless driver");
  40. MODULE_LICENSE("GPL");
  41. static struct usb_device_id rtl8187_table[] __devinitdata = {
  42. /* Asus */
  43. {USB_DEVICE(0x0b05, 0x171d), .driver_info = DEVICE_RTL8187},
  44. /* Belkin */
  45. {USB_DEVICE(0x050d, 0x705e), .driver_info = DEVICE_RTL8187B},
  46. /* Realtek */
  47. {USB_DEVICE(0x0bda, 0x8187), .driver_info = DEVICE_RTL8187},
  48. {USB_DEVICE(0x0bda, 0x8189), .driver_info = DEVICE_RTL8187B},
  49. {USB_DEVICE(0x0bda, 0x8197), .driver_info = DEVICE_RTL8187B},
  50. {USB_DEVICE(0x0bda, 0x8198), .driver_info = DEVICE_RTL8187B},
  51. /* Surecom */
  52. {USB_DEVICE(0x0769, 0x11F2), .driver_info = DEVICE_RTL8187},
  53. /* Logitech */
  54. {USB_DEVICE(0x0789, 0x010C), .driver_info = DEVICE_RTL8187},
  55. /* Netgear */
  56. {USB_DEVICE(0x0846, 0x6100), .driver_info = DEVICE_RTL8187},
  57. {USB_DEVICE(0x0846, 0x6a00), .driver_info = DEVICE_RTL8187},
  58. {USB_DEVICE(0x0846, 0x4260), .driver_info = DEVICE_RTL8187B},
  59. /* HP */
  60. {USB_DEVICE(0x03f0, 0xca02), .driver_info = DEVICE_RTL8187},
  61. /* Sitecom */
  62. {USB_DEVICE(0x0df6, 0x000d), .driver_info = DEVICE_RTL8187},
  63. {USB_DEVICE(0x0df6, 0x0028), .driver_info = DEVICE_RTL8187B},
  64. /* Sphairon Access Systems GmbH */
  65. {USB_DEVICE(0x114B, 0x0150), .driver_info = DEVICE_RTL8187},
  66. /* Dick Smith Electronics */
  67. {USB_DEVICE(0x1371, 0x9401), .driver_info = DEVICE_RTL8187},
  68. /* Abocom */
  69. {USB_DEVICE(0x13d1, 0xabe6), .driver_info = DEVICE_RTL8187},
  70. /* Qcom */
  71. {USB_DEVICE(0x18E8, 0x6232), .driver_info = DEVICE_RTL8187},
  72. /* AirLive */
  73. {USB_DEVICE(0x1b75, 0x8187), .driver_info = DEVICE_RTL8187},
  74. /* Linksys */
  75. {USB_DEVICE(0x1737, 0x0073), .driver_info = DEVICE_RTL8187B},
  76. {}
  77. };
  78. MODULE_DEVICE_TABLE(usb, rtl8187_table);
  79. static const struct ieee80211_rate rtl818x_rates[] = {
  80. { .bitrate = 10, .hw_value = 0, },
  81. { .bitrate = 20, .hw_value = 1, },
  82. { .bitrate = 55, .hw_value = 2, },
  83. { .bitrate = 110, .hw_value = 3, },
  84. { .bitrate = 60, .hw_value = 4, },
  85. { .bitrate = 90, .hw_value = 5, },
  86. { .bitrate = 120, .hw_value = 6, },
  87. { .bitrate = 180, .hw_value = 7, },
  88. { .bitrate = 240, .hw_value = 8, },
  89. { .bitrate = 360, .hw_value = 9, },
  90. { .bitrate = 480, .hw_value = 10, },
  91. { .bitrate = 540, .hw_value = 11, },
  92. };
  93. static const struct ieee80211_channel rtl818x_channels[] = {
  94. { .center_freq = 2412 },
  95. { .center_freq = 2417 },
  96. { .center_freq = 2422 },
  97. { .center_freq = 2427 },
  98. { .center_freq = 2432 },
  99. { .center_freq = 2437 },
  100. { .center_freq = 2442 },
  101. { .center_freq = 2447 },
  102. { .center_freq = 2452 },
  103. { .center_freq = 2457 },
  104. { .center_freq = 2462 },
  105. { .center_freq = 2467 },
  106. { .center_freq = 2472 },
  107. { .center_freq = 2484 },
  108. };
  109. static void rtl8187_iowrite_async_cb(struct urb *urb)
  110. {
  111. kfree(urb->context);
  112. }
  113. static void rtl8187_iowrite_async(struct rtl8187_priv *priv, __le16 addr,
  114. void *data, u16 len)
  115. {
  116. struct usb_ctrlrequest *dr;
  117. struct urb *urb;
  118. struct rtl8187_async_write_data {
  119. u8 data[4];
  120. struct usb_ctrlrequest dr;
  121. } *buf;
  122. int rc;
  123. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  124. if (!buf)
  125. return;
  126. urb = usb_alloc_urb(0, GFP_ATOMIC);
  127. if (!urb) {
  128. kfree(buf);
  129. return;
  130. }
  131. dr = &buf->dr;
  132. dr->bRequestType = RTL8187_REQT_WRITE;
  133. dr->bRequest = RTL8187_REQ_SET_REG;
  134. dr->wValue = addr;
  135. dr->wIndex = 0;
  136. dr->wLength = cpu_to_le16(len);
  137. memcpy(buf, data, len);
  138. usb_fill_control_urb(urb, priv->udev, usb_sndctrlpipe(priv->udev, 0),
  139. (unsigned char *)dr, buf, len,
  140. rtl8187_iowrite_async_cb, buf);
  141. usb_anchor_urb(urb, &priv->anchored);
  142. rc = usb_submit_urb(urb, GFP_ATOMIC);
  143. if (rc < 0) {
  144. kfree(buf);
  145. usb_unanchor_urb(urb);
  146. }
  147. usb_free_urb(urb);
  148. }
  149. static inline void rtl818x_iowrite32_async(struct rtl8187_priv *priv,
  150. __le32 *addr, u32 val)
  151. {
  152. __le32 buf = cpu_to_le32(val);
  153. rtl8187_iowrite_async(priv, cpu_to_le16((unsigned long)addr),
  154. &buf, sizeof(buf));
  155. }
  156. void rtl8187_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  157. {
  158. struct rtl8187_priv *priv = dev->priv;
  159. data <<= 8;
  160. data |= addr | 0x80;
  161. rtl818x_iowrite8(priv, &priv->map->PHY[3], (data >> 24) & 0xFF);
  162. rtl818x_iowrite8(priv, &priv->map->PHY[2], (data >> 16) & 0xFF);
  163. rtl818x_iowrite8(priv, &priv->map->PHY[1], (data >> 8) & 0xFF);
  164. rtl818x_iowrite8(priv, &priv->map->PHY[0], data & 0xFF);
  165. }
  166. static void rtl8187_tx_cb(struct urb *urb)
  167. {
  168. struct sk_buff *skb = (struct sk_buff *)urb->context;
  169. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  170. struct ieee80211_hw *hw = info->rate_driver_data[0];
  171. struct rtl8187_priv *priv = hw->priv;
  172. skb_pull(skb, priv->is_rtl8187b ? sizeof(struct rtl8187b_tx_hdr) :
  173. sizeof(struct rtl8187_tx_hdr));
  174. ieee80211_tx_info_clear_status(info);
  175. if (!(urb->status) && !(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  176. if (priv->is_rtl8187b) {
  177. skb_queue_tail(&priv->b_tx_status.queue, skb);
  178. /* queue is "full", discard last items */
  179. while (skb_queue_len(&priv->b_tx_status.queue) > 5) {
  180. struct sk_buff *old_skb;
  181. dev_dbg(&priv->udev->dev,
  182. "transmit status queue full\n");
  183. old_skb = skb_dequeue(&priv->b_tx_status.queue);
  184. ieee80211_tx_status_irqsafe(hw, old_skb);
  185. }
  186. return;
  187. } else {
  188. info->flags |= IEEE80211_TX_STAT_ACK;
  189. }
  190. }
  191. if (priv->is_rtl8187b)
  192. ieee80211_tx_status_irqsafe(hw, skb);
  193. else {
  194. /* Retry information for the RTI8187 is only available by
  195. * reading a register in the device. We are in interrupt mode
  196. * here, thus queue the skb and finish on a work queue. */
  197. skb_queue_tail(&priv->b_tx_status.queue, skb);
  198. ieee80211_queue_delayed_work(hw, &priv->work, 0);
  199. }
  200. }
  201. static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  202. {
  203. struct rtl8187_priv *priv = dev->priv;
  204. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  205. unsigned int ep;
  206. void *buf;
  207. struct urb *urb;
  208. __le16 rts_dur = 0;
  209. u32 flags;
  210. int rc;
  211. urb = usb_alloc_urb(0, GFP_ATOMIC);
  212. if (!urb) {
  213. kfree_skb(skb);
  214. return NETDEV_TX_OK;
  215. }
  216. flags = skb->len;
  217. flags |= RTL818X_TX_DESC_FLAG_NO_ENC;
  218. flags |= ieee80211_get_tx_rate(dev, info)->hw_value << 24;
  219. if (ieee80211_has_morefrags(((struct ieee80211_hdr *)skb->data)->frame_control))
  220. flags |= RTL818X_TX_DESC_FLAG_MOREFRAG;
  221. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  222. flags |= RTL818X_TX_DESC_FLAG_RTS;
  223. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  224. rts_dur = ieee80211_rts_duration(dev, priv->vif,
  225. skb->len, info);
  226. } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  227. flags |= RTL818X_TX_DESC_FLAG_CTS;
  228. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  229. }
  230. if (!priv->is_rtl8187b) {
  231. struct rtl8187_tx_hdr *hdr =
  232. (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
  233. hdr->flags = cpu_to_le32(flags);
  234. hdr->len = 0;
  235. hdr->rts_duration = rts_dur;
  236. hdr->retry = cpu_to_le32((info->control.rates[0].count - 1) << 8);
  237. buf = hdr;
  238. ep = 2;
  239. } else {
  240. /* fc needs to be calculated before skb_push() */
  241. unsigned int epmap[4] = { 6, 7, 5, 4 };
  242. struct ieee80211_hdr *tx_hdr =
  243. (struct ieee80211_hdr *)(skb->data);
  244. u16 fc = le16_to_cpu(tx_hdr->frame_control);
  245. struct rtl8187b_tx_hdr *hdr =
  246. (struct rtl8187b_tx_hdr *)skb_push(skb, sizeof(*hdr));
  247. struct ieee80211_rate *txrate =
  248. ieee80211_get_tx_rate(dev, info);
  249. memset(hdr, 0, sizeof(*hdr));
  250. hdr->flags = cpu_to_le32(flags);
  251. hdr->rts_duration = rts_dur;
  252. hdr->retry = cpu_to_le32((info->control.rates[0].count - 1) << 8);
  253. hdr->tx_duration =
  254. ieee80211_generic_frame_duration(dev, priv->vif,
  255. skb->len, txrate);
  256. buf = hdr;
  257. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  258. ep = 12;
  259. else
  260. ep = epmap[skb_get_queue_mapping(skb)];
  261. }
  262. info->rate_driver_data[0] = dev;
  263. info->rate_driver_data[1] = urb;
  264. usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, ep),
  265. buf, skb->len, rtl8187_tx_cb, skb);
  266. urb->transfer_flags |= URB_ZERO_PACKET;
  267. usb_anchor_urb(urb, &priv->anchored);
  268. rc = usb_submit_urb(urb, GFP_ATOMIC);
  269. if (rc < 0) {
  270. usb_unanchor_urb(urb);
  271. kfree_skb(skb);
  272. }
  273. usb_free_urb(urb);
  274. return NETDEV_TX_OK;
  275. }
  276. static void rtl8187_rx_cb(struct urb *urb)
  277. {
  278. struct sk_buff *skb = (struct sk_buff *)urb->context;
  279. struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
  280. struct ieee80211_hw *dev = info->dev;
  281. struct rtl8187_priv *priv = dev->priv;
  282. struct ieee80211_rx_status rx_status = { 0 };
  283. int rate, signal;
  284. u32 flags;
  285. unsigned long f;
  286. spin_lock_irqsave(&priv->rx_queue.lock, f);
  287. __skb_unlink(skb, &priv->rx_queue);
  288. spin_unlock_irqrestore(&priv->rx_queue.lock, f);
  289. skb_put(skb, urb->actual_length);
  290. if (unlikely(urb->status)) {
  291. dev_kfree_skb_irq(skb);
  292. return;
  293. }
  294. if (!priv->is_rtl8187b) {
  295. struct rtl8187_rx_hdr *hdr =
  296. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  297. flags = le32_to_cpu(hdr->flags);
  298. /* As with the RTL8187B below, the AGC is used to calculate
  299. * signal strength. In this case, the scaling
  300. * constants are derived from the output of p54usb.
  301. */
  302. signal = -4 - ((27 * hdr->agc) >> 6);
  303. rx_status.antenna = (hdr->signal >> 7) & 1;
  304. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  305. } else {
  306. struct rtl8187b_rx_hdr *hdr =
  307. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  308. /* The Realtek datasheet for the RTL8187B shows that the RX
  309. * header contains the following quantities: signal quality,
  310. * RSSI, AGC, the received power in dB, and the measured SNR.
  311. * In testing, none of these quantities show qualitative
  312. * agreement with AP signal strength, except for the AGC,
  313. * which is inversely proportional to the strength of the
  314. * signal. In the following, the signal strength
  315. * is derived from the AGC. The arbitrary scaling constants
  316. * are chosen to make the results close to the values obtained
  317. * for a BCM4312 using b43 as the driver. The noise is ignored
  318. * for now.
  319. */
  320. flags = le32_to_cpu(hdr->flags);
  321. signal = 14 - hdr->agc / 2;
  322. rx_status.antenna = (hdr->rssi >> 7) & 1;
  323. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  324. }
  325. rx_status.signal = signal;
  326. priv->signal = signal;
  327. rate = (flags >> 20) & 0xF;
  328. skb_trim(skb, flags & 0x0FFF);
  329. rx_status.rate_idx = rate;
  330. rx_status.freq = dev->conf.channel->center_freq;
  331. rx_status.band = dev->conf.channel->band;
  332. rx_status.flag |= RX_FLAG_TSFT;
  333. if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
  334. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  335. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  336. ieee80211_rx_irqsafe(dev, skb);
  337. skb = dev_alloc_skb(RTL8187_MAX_RX);
  338. if (unlikely(!skb)) {
  339. /* TODO check rx queue length and refill *somewhere* */
  340. return;
  341. }
  342. info = (struct rtl8187_rx_info *)skb->cb;
  343. info->urb = urb;
  344. info->dev = dev;
  345. urb->transfer_buffer = skb_tail_pointer(skb);
  346. urb->context = skb;
  347. skb_queue_tail(&priv->rx_queue, skb);
  348. usb_anchor_urb(urb, &priv->anchored);
  349. if (usb_submit_urb(urb, GFP_ATOMIC)) {
  350. usb_unanchor_urb(urb);
  351. skb_unlink(skb, &priv->rx_queue);
  352. dev_kfree_skb_irq(skb);
  353. }
  354. }
  355. static int rtl8187_init_urbs(struct ieee80211_hw *dev)
  356. {
  357. struct rtl8187_priv *priv = dev->priv;
  358. struct urb *entry = NULL;
  359. struct sk_buff *skb;
  360. struct rtl8187_rx_info *info;
  361. int ret = 0;
  362. while (skb_queue_len(&priv->rx_queue) < 16) {
  363. skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
  364. if (!skb) {
  365. ret = -ENOMEM;
  366. goto err;
  367. }
  368. entry = usb_alloc_urb(0, GFP_KERNEL);
  369. if (!entry) {
  370. ret = -ENOMEM;
  371. goto err;
  372. }
  373. usb_fill_bulk_urb(entry, priv->udev,
  374. usb_rcvbulkpipe(priv->udev,
  375. priv->is_rtl8187b ? 3 : 1),
  376. skb_tail_pointer(skb),
  377. RTL8187_MAX_RX, rtl8187_rx_cb, skb);
  378. info = (struct rtl8187_rx_info *)skb->cb;
  379. info->urb = entry;
  380. info->dev = dev;
  381. skb_queue_tail(&priv->rx_queue, skb);
  382. usb_anchor_urb(entry, &priv->anchored);
  383. ret = usb_submit_urb(entry, GFP_KERNEL);
  384. if (ret) {
  385. skb_unlink(skb, &priv->rx_queue);
  386. usb_unanchor_urb(entry);
  387. goto err;
  388. }
  389. usb_free_urb(entry);
  390. }
  391. return ret;
  392. err:
  393. usb_free_urb(entry);
  394. kfree_skb(skb);
  395. usb_kill_anchored_urbs(&priv->anchored);
  396. return ret;
  397. }
  398. static void rtl8187b_status_cb(struct urb *urb)
  399. {
  400. struct ieee80211_hw *hw = (struct ieee80211_hw *)urb->context;
  401. struct rtl8187_priv *priv = hw->priv;
  402. u64 val;
  403. unsigned int cmd_type;
  404. if (unlikely(urb->status))
  405. return;
  406. /*
  407. * Read from status buffer:
  408. *
  409. * bits [30:31] = cmd type:
  410. * - 0 indicates tx beacon interrupt
  411. * - 1 indicates tx close descriptor
  412. *
  413. * In the case of tx beacon interrupt:
  414. * [0:9] = Last Beacon CW
  415. * [10:29] = reserved
  416. * [30:31] = 00b
  417. * [32:63] = Last Beacon TSF
  418. *
  419. * If it's tx close descriptor:
  420. * [0:7] = Packet Retry Count
  421. * [8:14] = RTS Retry Count
  422. * [15] = TOK
  423. * [16:27] = Sequence No
  424. * [28] = LS
  425. * [29] = FS
  426. * [30:31] = 01b
  427. * [32:47] = unused (reserved?)
  428. * [48:63] = MAC Used Time
  429. */
  430. val = le64_to_cpu(priv->b_tx_status.buf);
  431. cmd_type = (val >> 30) & 0x3;
  432. if (cmd_type == 1) {
  433. unsigned int pkt_rc, seq_no;
  434. bool tok;
  435. struct sk_buff *skb;
  436. struct ieee80211_hdr *ieee80211hdr;
  437. unsigned long flags;
  438. pkt_rc = val & 0xFF;
  439. tok = val & (1 << 15);
  440. seq_no = (val >> 16) & 0xFFF;
  441. spin_lock_irqsave(&priv->b_tx_status.queue.lock, flags);
  442. skb_queue_reverse_walk(&priv->b_tx_status.queue, skb) {
  443. ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  444. /*
  445. * While testing, it was discovered that the seq_no
  446. * doesn't actually contains the sequence number.
  447. * Instead of returning just the 12 bits of sequence
  448. * number, hardware is returning entire sequence control
  449. * (fragment number plus sequence number) in a 12 bit
  450. * only field overflowing after some time. As a
  451. * workaround, just consider the lower bits, and expect
  452. * it's unlikely we wrongly ack some sent data
  453. */
  454. if ((le16_to_cpu(ieee80211hdr->seq_ctrl)
  455. & 0xFFF) == seq_no)
  456. break;
  457. }
  458. if (skb != (struct sk_buff *) &priv->b_tx_status.queue) {
  459. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  460. __skb_unlink(skb, &priv->b_tx_status.queue);
  461. if (tok)
  462. info->flags |= IEEE80211_TX_STAT_ACK;
  463. info->status.rates[0].count = pkt_rc + 1;
  464. ieee80211_tx_status_irqsafe(hw, skb);
  465. }
  466. spin_unlock_irqrestore(&priv->b_tx_status.queue.lock, flags);
  467. }
  468. usb_anchor_urb(urb, &priv->anchored);
  469. if (usb_submit_urb(urb, GFP_ATOMIC))
  470. usb_unanchor_urb(urb);
  471. }
  472. static int rtl8187b_init_status_urb(struct ieee80211_hw *dev)
  473. {
  474. struct rtl8187_priv *priv = dev->priv;
  475. struct urb *entry;
  476. int ret = 0;
  477. entry = usb_alloc_urb(0, GFP_KERNEL);
  478. if (!entry)
  479. return -ENOMEM;
  480. usb_fill_bulk_urb(entry, priv->udev, usb_rcvbulkpipe(priv->udev, 9),
  481. &priv->b_tx_status.buf, sizeof(priv->b_tx_status.buf),
  482. rtl8187b_status_cb, dev);
  483. usb_anchor_urb(entry, &priv->anchored);
  484. ret = usb_submit_urb(entry, GFP_KERNEL);
  485. if (ret)
  486. usb_unanchor_urb(entry);
  487. usb_free_urb(entry);
  488. return ret;
  489. }
  490. static int rtl8187_cmd_reset(struct ieee80211_hw *dev)
  491. {
  492. struct rtl8187_priv *priv = dev->priv;
  493. u8 reg;
  494. int i;
  495. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  496. reg &= (1 << 1);
  497. reg |= RTL818X_CMD_RESET;
  498. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  499. i = 10;
  500. do {
  501. msleep(2);
  502. if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
  503. RTL818X_CMD_RESET))
  504. break;
  505. } while (--i);
  506. if (!i) {
  507. printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
  508. return -ETIMEDOUT;
  509. }
  510. /* reload registers from eeprom */
  511. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  512. i = 10;
  513. do {
  514. msleep(4);
  515. if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
  516. RTL818X_EEPROM_CMD_CONFIG))
  517. break;
  518. } while (--i);
  519. if (!i) {
  520. printk(KERN_ERR "%s: eeprom reset timeout!\n",
  521. wiphy_name(dev->wiphy));
  522. return -ETIMEDOUT;
  523. }
  524. return 0;
  525. }
  526. static int rtl8187_init_hw(struct ieee80211_hw *dev)
  527. {
  528. struct rtl8187_priv *priv = dev->priv;
  529. u8 reg;
  530. int res;
  531. /* reset */
  532. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  533. RTL818X_EEPROM_CMD_CONFIG);
  534. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  535. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg |
  536. RTL818X_CONFIG3_ANAPARAM_WRITE);
  537. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  538. RTL8187_RTL8225_ANAPARAM_ON);
  539. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  540. RTL8187_RTL8225_ANAPARAM2_ON);
  541. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg &
  542. ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  543. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  544. RTL818X_EEPROM_CMD_NORMAL);
  545. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  546. msleep(200);
  547. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  548. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  549. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  550. msleep(200);
  551. res = rtl8187_cmd_reset(dev);
  552. if (res)
  553. return res;
  554. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  555. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  556. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  557. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  558. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  559. RTL8187_RTL8225_ANAPARAM_ON);
  560. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  561. RTL8187_RTL8225_ANAPARAM2_ON);
  562. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  563. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  564. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  565. /* setup card */
  566. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  567. rtl818x_iowrite8(priv, &priv->map->GPIO0, 0);
  568. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  569. rtl818x_iowrite8(priv, &priv->map->GPIO0, 1);
  570. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  571. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  572. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  573. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  574. reg &= 0x3F;
  575. reg |= 0x80;
  576. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  577. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  578. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  579. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  580. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0);
  581. // TODO: set RESP_RATE and BRSR properly
  582. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  583. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  584. /* host_usb_init */
  585. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  586. rtl818x_iowrite8(priv, &priv->map->GPIO0, 0);
  587. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  588. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  589. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  590. rtl818x_iowrite8(priv, &priv->map->GPIO0, 0x20);
  591. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  592. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  593. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  594. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  595. msleep(100);
  596. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  597. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  598. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  599. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  600. RTL818X_EEPROM_CMD_CONFIG);
  601. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  602. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  603. RTL818X_EEPROM_CMD_NORMAL);
  604. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  605. msleep(100);
  606. priv->rf->init(dev);
  607. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  608. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  609. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  610. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  611. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  612. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  613. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  614. return 0;
  615. }
  616. static const u8 rtl8187b_reg_table[][3] = {
  617. {0xF0, 0x32, 0}, {0xF1, 0x32, 0}, {0xF2, 0x00, 0}, {0xF3, 0x00, 0},
  618. {0xF4, 0x32, 0}, {0xF5, 0x43, 0}, {0xF6, 0x00, 0}, {0xF7, 0x00, 0},
  619. {0xF8, 0x46, 0}, {0xF9, 0xA4, 0}, {0xFA, 0x00, 0}, {0xFB, 0x00, 0},
  620. {0xFC, 0x96, 0}, {0xFD, 0xA4, 0}, {0xFE, 0x00, 0}, {0xFF, 0x00, 0},
  621. {0x58, 0x4B, 1}, {0x59, 0x00, 1}, {0x5A, 0x4B, 1}, {0x5B, 0x00, 1},
  622. {0x60, 0x4B, 1}, {0x61, 0x09, 1}, {0x62, 0x4B, 1}, {0x63, 0x09, 1},
  623. {0xCE, 0x0F, 1}, {0xCF, 0x00, 1}, {0xE0, 0xFF, 1}, {0xE1, 0x0F, 1},
  624. {0xE2, 0x00, 1}, {0xF0, 0x4E, 1}, {0xF1, 0x01, 1}, {0xF2, 0x02, 1},
  625. {0xF3, 0x03, 1}, {0xF4, 0x04, 1}, {0xF5, 0x05, 1}, {0xF6, 0x06, 1},
  626. {0xF7, 0x07, 1}, {0xF8, 0x08, 1},
  627. {0x4E, 0x00, 2}, {0x0C, 0x04, 2}, {0x21, 0x61, 2}, {0x22, 0x68, 2},
  628. {0x23, 0x6F, 2}, {0x24, 0x76, 2}, {0x25, 0x7D, 2}, {0x26, 0x84, 2},
  629. {0x27, 0x8D, 2}, {0x4D, 0x08, 2}, {0x50, 0x05, 2}, {0x51, 0xF5, 2},
  630. {0x52, 0x04, 2}, {0x53, 0xA0, 2}, {0x54, 0x1F, 2}, {0x55, 0x23, 2},
  631. {0x56, 0x45, 2}, {0x57, 0x67, 2}, {0x58, 0x08, 2}, {0x59, 0x08, 2},
  632. {0x5A, 0x08, 2}, {0x5B, 0x08, 2}, {0x60, 0x08, 2}, {0x61, 0x08, 2},
  633. {0x62, 0x08, 2}, {0x63, 0x08, 2}, {0x64, 0xCF, 2}, {0x72, 0x56, 2},
  634. {0x73, 0x9A, 2},
  635. {0x34, 0xF0, 0}, {0x35, 0x0F, 0}, {0x5B, 0x40, 0}, {0x84, 0x88, 0},
  636. {0x85, 0x24, 0}, {0x88, 0x54, 0}, {0x8B, 0xB8, 0}, {0x8C, 0x07, 0},
  637. {0x8D, 0x00, 0}, {0x94, 0x1B, 0}, {0x95, 0x12, 0}, {0x96, 0x00, 0},
  638. {0x97, 0x06, 0}, {0x9D, 0x1A, 0}, {0x9F, 0x10, 0}, {0xB4, 0x22, 0},
  639. {0xBE, 0x80, 0}, {0xDB, 0x00, 0}, {0xEE, 0x00, 0}, {0x4C, 0x00, 2},
  640. {0x9F, 0x00, 3}, {0x8C, 0x01, 0}, {0x8D, 0x10, 0}, {0x8E, 0x08, 0},
  641. {0x8F, 0x00, 0}
  642. };
  643. static int rtl8187b_init_hw(struct ieee80211_hw *dev)
  644. {
  645. struct rtl8187_priv *priv = dev->priv;
  646. int res, i;
  647. u8 reg;
  648. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  649. RTL818X_EEPROM_CMD_CONFIG);
  650. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  651. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE | RTL818X_CONFIG3_GNT_SELECT;
  652. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  653. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2,
  654. RTL8187B_RTL8225_ANAPARAM2_ON);
  655. rtl818x_iowrite32(priv, &priv->map->ANAPARAM,
  656. RTL8187B_RTL8225_ANAPARAM_ON);
  657. rtl818x_iowrite8(priv, &priv->map->ANAPARAM3,
  658. RTL8187B_RTL8225_ANAPARAM3_ON);
  659. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0x10);
  660. reg = rtl818x_ioread8(priv, (u8 *)0xFF62);
  661. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg & ~(1 << 5));
  662. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg | (1 << 5));
  663. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  664. reg &= ~RTL818X_CONFIG3_ANAPARAM_WRITE;
  665. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  666. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  667. RTL818X_EEPROM_CMD_NORMAL);
  668. res = rtl8187_cmd_reset(dev);
  669. if (res)
  670. return res;
  671. rtl818x_iowrite16(priv, (__le16 *)0xFF2D, 0x0FFF);
  672. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  673. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  674. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  675. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  676. reg |= RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT |
  677. RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  678. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  679. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFE0, 0x0FFF, 1);
  680. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  681. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  682. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFD4, 0xFFFF, 1);
  683. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  684. RTL818X_EEPROM_CMD_CONFIG);
  685. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  686. rtl818x_iowrite8(priv, &priv->map->CONFIG1, (reg & 0x3F) | 0x80);
  687. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  688. RTL818X_EEPROM_CMD_NORMAL);
  689. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  690. for (i = 0; i < ARRAY_SIZE(rtl8187b_reg_table); i++) {
  691. rtl818x_iowrite8_idx(priv,
  692. (u8 *)(uintptr_t)
  693. (rtl8187b_reg_table[i][0] | 0xFF00),
  694. rtl8187b_reg_table[i][1],
  695. rtl8187b_reg_table[i][2]);
  696. }
  697. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  698. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  699. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF0, 0, 1);
  700. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF4, 0, 1);
  701. rtl818x_iowrite8_idx(priv, (u8 *)0xFFF8, 0, 1);
  702. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x00004001);
  703. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x569A, 2);
  704. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  705. RTL818X_EEPROM_CMD_CONFIG);
  706. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  707. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE;
  708. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  709. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  710. RTL818X_EEPROM_CMD_NORMAL);
  711. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  712. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  713. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FFF);
  714. msleep(100);
  715. priv->rf->init(dev);
  716. reg = RTL818X_CMD_TX_ENABLE | RTL818X_CMD_RX_ENABLE;
  717. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  718. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  719. rtl818x_iowrite8(priv, (u8 *)0xFE41, 0xF4);
  720. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x00);
  721. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  722. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  723. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x0F);
  724. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  725. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  726. reg = rtl818x_ioread8(priv, (u8 *)0xFFDB);
  727. rtl818x_iowrite8(priv, (u8 *)0xFFDB, reg | (1 << 2));
  728. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x59FA, 3);
  729. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF74, 0x59D2, 3);
  730. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF76, 0x59D2, 3);
  731. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF78, 0x19FA, 3);
  732. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7A, 0x19FA, 3);
  733. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7C, 0x00D0, 3);
  734. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0);
  735. rtl818x_iowrite8_idx(priv, (u8 *)0xFF80, 0x0F, 1);
  736. rtl818x_iowrite8_idx(priv, (u8 *)0xFF83, 0x03, 1);
  737. rtl818x_iowrite8(priv, (u8 *)0xFFDA, 0x10);
  738. rtl818x_iowrite8_idx(priv, (u8 *)0xFF4D, 0x08, 2);
  739. rtl818x_iowrite32(priv, &priv->map->HSSI_PARA, 0x0600321B);
  740. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFEC, 0x0800, 1);
  741. priv->slot_time = 0x9;
  742. priv->aifsn[0] = 2; /* AIFSN[AC_VO] */
  743. priv->aifsn[1] = 2; /* AIFSN[AC_VI] */
  744. priv->aifsn[2] = 7; /* AIFSN[AC_BK] */
  745. priv->aifsn[3] = 3; /* AIFSN[AC_BE] */
  746. rtl818x_iowrite8(priv, &priv->map->ACM_CONTROL, 0);
  747. /* ENEDCA flag must always be set, transmit issues? */
  748. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_ENEDCA);
  749. return 0;
  750. }
  751. static void rtl8187_work(struct work_struct *work)
  752. {
  753. /* The RTL8187 returns the retry count through register 0xFFFA. In
  754. * addition, it appears to be a cumulative retry count, not the
  755. * value for the current TX packet. When multiple TX entries are
  756. * queued, the retry count will be valid for the last one in the queue.
  757. * The "error" should not matter for purposes of rate setting. */
  758. struct rtl8187_priv *priv = container_of(work, struct rtl8187_priv,
  759. work.work);
  760. struct ieee80211_tx_info *info;
  761. struct ieee80211_hw *dev = priv->dev;
  762. static u16 retry;
  763. u16 tmp;
  764. mutex_lock(&priv->conf_mutex);
  765. tmp = rtl818x_ioread16(priv, (__le16 *)0xFFFA);
  766. while (skb_queue_len(&priv->b_tx_status.queue) > 0) {
  767. struct sk_buff *old_skb;
  768. old_skb = skb_dequeue(&priv->b_tx_status.queue);
  769. info = IEEE80211_SKB_CB(old_skb);
  770. info->status.rates[0].count = tmp - retry + 1;
  771. ieee80211_tx_status_irqsafe(dev, old_skb);
  772. }
  773. retry = tmp;
  774. mutex_unlock(&priv->conf_mutex);
  775. }
  776. static int rtl8187_start(struct ieee80211_hw *dev)
  777. {
  778. struct rtl8187_priv *priv = dev->priv;
  779. u32 reg;
  780. int ret;
  781. mutex_lock(&priv->conf_mutex);
  782. ret = (!priv->is_rtl8187b) ? rtl8187_init_hw(dev) :
  783. rtl8187b_init_hw(dev);
  784. if (ret)
  785. goto rtl8187_start_exit;
  786. init_usb_anchor(&priv->anchored);
  787. priv->dev = dev;
  788. if (priv->is_rtl8187b) {
  789. reg = RTL818X_RX_CONF_MGMT |
  790. RTL818X_RX_CONF_DATA |
  791. RTL818X_RX_CONF_BROADCAST |
  792. RTL818X_RX_CONF_NICMAC |
  793. RTL818X_RX_CONF_BSSID |
  794. (7 << 13 /* RX FIFO threshold NONE */) |
  795. (7 << 10 /* MAX RX DMA */) |
  796. RTL818X_RX_CONF_RX_AUTORESETPHY |
  797. RTL818X_RX_CONF_ONLYERLPKT |
  798. RTL818X_RX_CONF_MULTICAST;
  799. priv->rx_conf = reg;
  800. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  801. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  802. RTL818X_TX_CONF_HW_SEQNUM |
  803. RTL818X_TX_CONF_DISREQQSIZE |
  804. (7 << 8 /* short retry limit */) |
  805. (7 << 0 /* long retry limit */) |
  806. (7 << 21 /* MAX TX DMA */));
  807. rtl8187_init_urbs(dev);
  808. rtl8187b_init_status_urb(dev);
  809. goto rtl8187_start_exit;
  810. }
  811. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  812. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  813. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  814. rtl8187_init_urbs(dev);
  815. reg = RTL818X_RX_CONF_ONLYERLPKT |
  816. RTL818X_RX_CONF_RX_AUTORESETPHY |
  817. RTL818X_RX_CONF_BSSID |
  818. RTL818X_RX_CONF_MGMT |
  819. RTL818X_RX_CONF_DATA |
  820. (7 << 13 /* RX FIFO threshold NONE */) |
  821. (7 << 10 /* MAX RX DMA */) |
  822. RTL818X_RX_CONF_BROADCAST |
  823. RTL818X_RX_CONF_NICMAC;
  824. priv->rx_conf = reg;
  825. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  826. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  827. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  828. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  829. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  830. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  831. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  832. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  833. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  834. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  835. reg = RTL818X_TX_CONF_CW_MIN |
  836. (7 << 21 /* MAX TX DMA */) |
  837. RTL818X_TX_CONF_NO_ICV;
  838. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  839. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  840. reg |= RTL818X_CMD_TX_ENABLE;
  841. reg |= RTL818X_CMD_RX_ENABLE;
  842. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  843. INIT_DELAYED_WORK(&priv->work, rtl8187_work);
  844. rtl8187_start_exit:
  845. mutex_unlock(&priv->conf_mutex);
  846. return ret;
  847. }
  848. static void rtl8187_stop(struct ieee80211_hw *dev)
  849. {
  850. struct rtl8187_priv *priv = dev->priv;
  851. struct sk_buff *skb;
  852. u32 reg;
  853. mutex_lock(&priv->conf_mutex);
  854. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  855. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  856. reg &= ~RTL818X_CMD_TX_ENABLE;
  857. reg &= ~RTL818X_CMD_RX_ENABLE;
  858. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  859. priv->rf->stop(dev);
  860. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  861. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  862. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  863. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  864. while ((skb = skb_dequeue(&priv->b_tx_status.queue)))
  865. dev_kfree_skb_any(skb);
  866. usb_kill_anchored_urbs(&priv->anchored);
  867. mutex_unlock(&priv->conf_mutex);
  868. if (!priv->is_rtl8187b)
  869. cancel_delayed_work_sync(&priv->work);
  870. }
  871. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  872. struct ieee80211_if_init_conf *conf)
  873. {
  874. struct rtl8187_priv *priv = dev->priv;
  875. int i;
  876. int ret = -EOPNOTSUPP;
  877. mutex_lock(&priv->conf_mutex);
  878. if (priv->mode != NL80211_IFTYPE_MONITOR)
  879. goto exit;
  880. switch (conf->type) {
  881. case NL80211_IFTYPE_STATION:
  882. priv->mode = conf->type;
  883. break;
  884. default:
  885. goto exit;
  886. }
  887. ret = 0;
  888. priv->vif = conf->vif;
  889. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  890. for (i = 0; i < ETH_ALEN; i++)
  891. rtl818x_iowrite8(priv, &priv->map->MAC[i],
  892. ((u8 *)conf->mac_addr)[i]);
  893. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  894. exit:
  895. mutex_unlock(&priv->conf_mutex);
  896. return ret;
  897. }
  898. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  899. struct ieee80211_if_init_conf *conf)
  900. {
  901. struct rtl8187_priv *priv = dev->priv;
  902. mutex_lock(&priv->conf_mutex);
  903. priv->mode = NL80211_IFTYPE_MONITOR;
  904. priv->vif = NULL;
  905. mutex_unlock(&priv->conf_mutex);
  906. }
  907. static int rtl8187_config(struct ieee80211_hw *dev, u32 changed)
  908. {
  909. struct rtl8187_priv *priv = dev->priv;
  910. struct ieee80211_conf *conf = &dev->conf;
  911. u32 reg;
  912. mutex_lock(&priv->conf_mutex);
  913. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  914. /* Enable TX loopback on MAC level to avoid TX during channel
  915. * changes, as this has be seen to causes problems and the
  916. * card will stop work until next reset
  917. */
  918. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  919. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  920. priv->rf->set_chan(dev, conf);
  921. msleep(10);
  922. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  923. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  924. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  925. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  926. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  927. mutex_unlock(&priv->conf_mutex);
  928. return 0;
  929. }
  930. /*
  931. * With 8187B, AC_*_PARAM clashes with FEMR definition in struct rtl818x_csr for
  932. * example. Thus we have to use raw values for AC_*_PARAM register addresses.
  933. */
  934. static __le32 *rtl8187b_ac_addr[4] = {
  935. (__le32 *) 0xFFF0, /* AC_VO */
  936. (__le32 *) 0xFFF4, /* AC_VI */
  937. (__le32 *) 0xFFFC, /* AC_BK */
  938. (__le32 *) 0xFFF8, /* AC_BE */
  939. };
  940. #define SIFS_TIME 0xa
  941. static void rtl8187_conf_erp(struct rtl8187_priv *priv, bool use_short_slot,
  942. bool use_short_preamble)
  943. {
  944. if (priv->is_rtl8187b) {
  945. u8 difs, eifs;
  946. u16 ack_timeout;
  947. int queue;
  948. if (use_short_slot) {
  949. priv->slot_time = 0x9;
  950. difs = 0x1c;
  951. eifs = 0x53;
  952. } else {
  953. priv->slot_time = 0x14;
  954. difs = 0x32;
  955. eifs = 0x5b;
  956. }
  957. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  958. rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
  959. rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
  960. /*
  961. * BRSR+1 on 8187B is in fact EIFS register
  962. * Value in units of 4 us
  963. */
  964. rtl818x_iowrite8(priv, (u8 *)&priv->map->BRSR + 1, eifs);
  965. /*
  966. * For 8187B, CARRIER_SENSE_COUNTER is in fact ack timeout
  967. * register. In units of 4 us like eifs register
  968. * ack_timeout = ack duration + plcp + difs + preamble
  969. */
  970. ack_timeout = 112 + 48 + difs;
  971. if (use_short_preamble)
  972. ack_timeout += 72;
  973. else
  974. ack_timeout += 144;
  975. rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER,
  976. DIV_ROUND_UP(ack_timeout, 4));
  977. for (queue = 0; queue < 4; queue++)
  978. rtl818x_iowrite8(priv, (u8 *) rtl8187b_ac_addr[queue],
  979. priv->aifsn[queue] * priv->slot_time +
  980. SIFS_TIME);
  981. } else {
  982. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  983. if (use_short_slot) {
  984. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  985. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  986. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  987. } else {
  988. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  989. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  990. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  991. }
  992. }
  993. }
  994. static void rtl8187_bss_info_changed(struct ieee80211_hw *dev,
  995. struct ieee80211_vif *vif,
  996. struct ieee80211_bss_conf *info,
  997. u32 changed)
  998. {
  999. struct rtl8187_priv *priv = dev->priv;
  1000. int i;
  1001. u8 reg;
  1002. if (changed & BSS_CHANGED_BSSID) {
  1003. mutex_lock(&priv->conf_mutex);
  1004. for (i = 0; i < ETH_ALEN; i++)
  1005. rtl818x_iowrite8(priv, &priv->map->BSSID[i],
  1006. info->bssid[i]);
  1007. if (priv->is_rtl8187b)
  1008. reg = RTL818X_MSR_ENEDCA;
  1009. else
  1010. reg = 0;
  1011. if (is_valid_ether_addr(info->bssid)) {
  1012. reg |= RTL818X_MSR_INFRA;
  1013. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  1014. } else {
  1015. reg |= RTL818X_MSR_NO_LINK;
  1016. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  1017. }
  1018. mutex_unlock(&priv->conf_mutex);
  1019. }
  1020. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE))
  1021. rtl8187_conf_erp(priv, info->use_short_slot,
  1022. info->use_short_preamble);
  1023. }
  1024. static u64 rtl8187_prepare_multicast(struct ieee80211_hw *dev,
  1025. int mc_count, struct dev_addr_list *mc_list)
  1026. {
  1027. return mc_count;
  1028. }
  1029. static void rtl8187_configure_filter(struct ieee80211_hw *dev,
  1030. unsigned int changed_flags,
  1031. unsigned int *total_flags,
  1032. u64 multicast)
  1033. {
  1034. struct rtl8187_priv *priv = dev->priv;
  1035. if (changed_flags & FIF_FCSFAIL)
  1036. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  1037. if (changed_flags & FIF_CONTROL)
  1038. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  1039. if (changed_flags & FIF_OTHER_BSS)
  1040. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  1041. if (*total_flags & FIF_ALLMULTI || multicast > 0)
  1042. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  1043. else
  1044. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  1045. *total_flags = 0;
  1046. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  1047. *total_flags |= FIF_FCSFAIL;
  1048. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  1049. *total_flags |= FIF_CONTROL;
  1050. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  1051. *total_flags |= FIF_OTHER_BSS;
  1052. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  1053. *total_flags |= FIF_ALLMULTI;
  1054. rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
  1055. }
  1056. static int rtl8187_conf_tx(struct ieee80211_hw *dev, u16 queue,
  1057. const struct ieee80211_tx_queue_params *params)
  1058. {
  1059. struct rtl8187_priv *priv = dev->priv;
  1060. u8 cw_min, cw_max;
  1061. if (queue > 3)
  1062. return -EINVAL;
  1063. cw_min = fls(params->cw_min);
  1064. cw_max = fls(params->cw_max);
  1065. if (priv->is_rtl8187b) {
  1066. priv->aifsn[queue] = params->aifs;
  1067. /*
  1068. * This is the structure of AC_*_PARAM registers in 8187B:
  1069. * - TXOP limit field, bit offset = 16
  1070. * - ECWmax, bit offset = 12
  1071. * - ECWmin, bit offset = 8
  1072. * - AIFS, bit offset = 0
  1073. */
  1074. rtl818x_iowrite32(priv, rtl8187b_ac_addr[queue],
  1075. (params->txop << 16) | (cw_max << 12) |
  1076. (cw_min << 8) | (params->aifs *
  1077. priv->slot_time + SIFS_TIME));
  1078. } else {
  1079. if (queue != 0)
  1080. return -EINVAL;
  1081. rtl818x_iowrite8(priv, &priv->map->CW_VAL,
  1082. cw_min | (cw_max << 4));
  1083. }
  1084. return 0;
  1085. }
  1086. static const struct ieee80211_ops rtl8187_ops = {
  1087. .tx = rtl8187_tx,
  1088. .start = rtl8187_start,
  1089. .stop = rtl8187_stop,
  1090. .add_interface = rtl8187_add_interface,
  1091. .remove_interface = rtl8187_remove_interface,
  1092. .config = rtl8187_config,
  1093. .bss_info_changed = rtl8187_bss_info_changed,
  1094. .prepare_multicast = rtl8187_prepare_multicast,
  1095. .configure_filter = rtl8187_configure_filter,
  1096. .conf_tx = rtl8187_conf_tx,
  1097. .rfkill_poll = rtl8187_rfkill_poll
  1098. };
  1099. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  1100. {
  1101. struct ieee80211_hw *dev = eeprom->data;
  1102. struct rtl8187_priv *priv = dev->priv;
  1103. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  1104. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  1105. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  1106. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  1107. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  1108. }
  1109. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  1110. {
  1111. struct ieee80211_hw *dev = eeprom->data;
  1112. struct rtl8187_priv *priv = dev->priv;
  1113. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  1114. if (eeprom->reg_data_in)
  1115. reg |= RTL818X_EEPROM_CMD_WRITE;
  1116. if (eeprom->reg_data_out)
  1117. reg |= RTL818X_EEPROM_CMD_READ;
  1118. if (eeprom->reg_data_clock)
  1119. reg |= RTL818X_EEPROM_CMD_CK;
  1120. if (eeprom->reg_chip_select)
  1121. reg |= RTL818X_EEPROM_CMD_CS;
  1122. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  1123. udelay(10);
  1124. }
  1125. static int __devinit rtl8187_probe(struct usb_interface *intf,
  1126. const struct usb_device_id *id)
  1127. {
  1128. struct usb_device *udev = interface_to_usbdev(intf);
  1129. struct ieee80211_hw *dev;
  1130. struct rtl8187_priv *priv;
  1131. struct eeprom_93cx6 eeprom;
  1132. struct ieee80211_channel *channel;
  1133. const char *chip_name;
  1134. u16 txpwr, reg;
  1135. u16 product_id = le16_to_cpu(udev->descriptor.idProduct);
  1136. int err, i;
  1137. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  1138. if (!dev) {
  1139. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  1140. return -ENOMEM;
  1141. }
  1142. priv = dev->priv;
  1143. priv->is_rtl8187b = (id->driver_info == DEVICE_RTL8187B);
  1144. /* allocate "DMA aware" buffer for register accesses */
  1145. priv->io_dmabuf = kmalloc(sizeof(*priv->io_dmabuf), GFP_KERNEL);
  1146. if (!priv->io_dmabuf) {
  1147. err = -ENOMEM;
  1148. goto err_free_dev;
  1149. }
  1150. mutex_init(&priv->io_mutex);
  1151. SET_IEEE80211_DEV(dev, &intf->dev);
  1152. usb_set_intfdata(intf, dev);
  1153. priv->udev = udev;
  1154. usb_get_dev(udev);
  1155. skb_queue_head_init(&priv->rx_queue);
  1156. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  1157. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  1158. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  1159. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  1160. priv->map = (struct rtl818x_csr *)0xFF00;
  1161. priv->band.band = IEEE80211_BAND_2GHZ;
  1162. priv->band.channels = priv->channels;
  1163. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  1164. priv->band.bitrates = priv->rates;
  1165. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  1166. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  1167. priv->mode = NL80211_IFTYPE_MONITOR;
  1168. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1169. IEEE80211_HW_SIGNAL_DBM |
  1170. IEEE80211_HW_RX_INCLUDES_FCS;
  1171. eeprom.data = dev;
  1172. eeprom.register_read = rtl8187_eeprom_register_read;
  1173. eeprom.register_write = rtl8187_eeprom_register_write;
  1174. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  1175. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  1176. else
  1177. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1178. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  1179. udelay(10);
  1180. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  1181. (__le16 __force *)dev->wiphy->perm_addr, 3);
  1182. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  1183. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  1184. "generated MAC address\n");
  1185. random_ether_addr(dev->wiphy->perm_addr);
  1186. }
  1187. channel = priv->channels;
  1188. for (i = 0; i < 3; i++) {
  1189. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  1190. &txpwr);
  1191. (*channel++).hw_value = txpwr & 0xFF;
  1192. (*channel++).hw_value = txpwr >> 8;
  1193. }
  1194. for (i = 0; i < 2; i++) {
  1195. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  1196. &txpwr);
  1197. (*channel++).hw_value = txpwr & 0xFF;
  1198. (*channel++).hw_value = txpwr >> 8;
  1199. }
  1200. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  1201. &priv->txpwr_base);
  1202. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  1203. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  1204. /* 0 means asic B-cut, we should use SW 3 wire
  1205. * bit-by-bit banging for radio. 1 means we can use
  1206. * USB specific request to write radio registers */
  1207. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  1208. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  1209. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  1210. if (!priv->is_rtl8187b) {
  1211. u32 reg32;
  1212. reg32 = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  1213. reg32 &= RTL818X_TX_CONF_HWVER_MASK;
  1214. switch (reg32) {
  1215. case RTL818X_TX_CONF_R8187vD_B:
  1216. /* Some RTL8187B devices have a USB ID of 0x8187
  1217. * detect them here */
  1218. chip_name = "RTL8187BvB(early)";
  1219. priv->is_rtl8187b = 1;
  1220. priv->hw_rev = RTL8187BvB;
  1221. break;
  1222. case RTL818X_TX_CONF_R8187vD:
  1223. chip_name = "RTL8187vD";
  1224. break;
  1225. default:
  1226. chip_name = "RTL8187vB (default)";
  1227. }
  1228. } else {
  1229. /*
  1230. * Force USB request to write radio registers for 8187B, Realtek
  1231. * only uses it in their sources
  1232. */
  1233. /*if (priv->asic_rev == 0) {
  1234. printk(KERN_WARNING "rtl8187: Forcing use of USB "
  1235. "requests to write to radio registers\n");
  1236. priv->asic_rev = 1;
  1237. }*/
  1238. switch (rtl818x_ioread8(priv, (u8 *)0xFFE1)) {
  1239. case RTL818X_R8187B_B:
  1240. chip_name = "RTL8187BvB";
  1241. priv->hw_rev = RTL8187BvB;
  1242. break;
  1243. case RTL818X_R8187B_D:
  1244. chip_name = "RTL8187BvD";
  1245. priv->hw_rev = RTL8187BvD;
  1246. break;
  1247. case RTL818X_R8187B_E:
  1248. chip_name = "RTL8187BvE";
  1249. priv->hw_rev = RTL8187BvE;
  1250. break;
  1251. default:
  1252. chip_name = "RTL8187BvB (default)";
  1253. priv->hw_rev = RTL8187BvB;
  1254. }
  1255. }
  1256. if (!priv->is_rtl8187b) {
  1257. for (i = 0; i < 2; i++) {
  1258. eeprom_93cx6_read(&eeprom,
  1259. RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  1260. &txpwr);
  1261. (*channel++).hw_value = txpwr & 0xFF;
  1262. (*channel++).hw_value = txpwr >> 8;
  1263. }
  1264. } else {
  1265. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
  1266. &txpwr);
  1267. (*channel++).hw_value = txpwr & 0xFF;
  1268. eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
  1269. (*channel++).hw_value = txpwr & 0xFF;
  1270. eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
  1271. (*channel++).hw_value = txpwr & 0xFF;
  1272. (*channel++).hw_value = txpwr >> 8;
  1273. }
  1274. /* Handle the differing rfkill GPIO bit in different models */
  1275. priv->rfkill_mask = RFKILL_MASK_8187_89_97;
  1276. if (product_id == 0x8197 || product_id == 0x8198) {
  1277. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_SELECT_GPIO, &reg);
  1278. if (reg & 0xFF00)
  1279. priv->rfkill_mask = RFKILL_MASK_8198;
  1280. }
  1281. /*
  1282. * XXX: Once this driver supports anything that requires
  1283. * beacons it must implement IEEE80211_TX_CTL_ASSIGN_SEQ.
  1284. */
  1285. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1286. if ((id->driver_info == DEVICE_RTL8187) && priv->is_rtl8187b)
  1287. printk(KERN_INFO "rtl8187: inconsistency between id with OEM"
  1288. " info!\n");
  1289. priv->rf = rtl8187_detect_rf(dev);
  1290. dev->extra_tx_headroom = (!priv->is_rtl8187b) ?
  1291. sizeof(struct rtl8187_tx_hdr) :
  1292. sizeof(struct rtl8187b_tx_hdr);
  1293. if (!priv->is_rtl8187b)
  1294. dev->queues = 1;
  1295. else
  1296. dev->queues = 4;
  1297. err = ieee80211_register_hw(dev);
  1298. if (err) {
  1299. printk(KERN_ERR "rtl8187: Cannot register device\n");
  1300. goto err_free_dmabuf;
  1301. }
  1302. mutex_init(&priv->conf_mutex);
  1303. skb_queue_head_init(&priv->b_tx_status.queue);
  1304. printk(KERN_INFO "%s: hwaddr %pM, %s V%d + %s, rfkill mask %d\n",
  1305. wiphy_name(dev->wiphy), dev->wiphy->perm_addr,
  1306. chip_name, priv->asic_rev, priv->rf->name, priv->rfkill_mask);
  1307. #ifdef CONFIG_RTL8187_LEDS
  1308. eeprom_93cx6_read(&eeprom, 0x3F, &reg);
  1309. reg &= 0xFF;
  1310. rtl8187_leds_init(dev, reg);
  1311. #endif
  1312. rtl8187_rfkill_init(dev);
  1313. return 0;
  1314. err_free_dmabuf:
  1315. kfree(priv->io_dmabuf);
  1316. err_free_dev:
  1317. ieee80211_free_hw(dev);
  1318. usb_set_intfdata(intf, NULL);
  1319. usb_put_dev(udev);
  1320. return err;
  1321. }
  1322. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  1323. {
  1324. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  1325. struct rtl8187_priv *priv;
  1326. if (!dev)
  1327. return;
  1328. #ifdef CONFIG_RTL8187_LEDS
  1329. rtl8187_leds_exit(dev);
  1330. #endif
  1331. rtl8187_rfkill_exit(dev);
  1332. ieee80211_unregister_hw(dev);
  1333. priv = dev->priv;
  1334. usb_reset_device(priv->udev);
  1335. usb_put_dev(interface_to_usbdev(intf));
  1336. kfree(priv->io_dmabuf);
  1337. ieee80211_free_hw(dev);
  1338. }
  1339. static struct usb_driver rtl8187_driver = {
  1340. .name = KBUILD_MODNAME,
  1341. .id_table = rtl8187_table,
  1342. .probe = rtl8187_probe,
  1343. .disconnect = __devexit_p(rtl8187_disconnect),
  1344. };
  1345. static int __init rtl8187_init(void)
  1346. {
  1347. return usb_register(&rtl8187_driver);
  1348. }
  1349. static void __exit rtl8187_exit(void)
  1350. {
  1351. usb_deregister(&rtl8187_driver);
  1352. }
  1353. module_init(rtl8187_init);
  1354. module_exit(rtl8187_exit);