dev.c 46 KB

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