rtl8187_dev.c 46 KB

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