rtl8187_dev.c 46 KB

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