rtl8187_dev.c 46 KB

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