rtl8187_dev.c 47 KB

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