dev.c 46 KB

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