dev.c 48 KB

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