rtl8187_dev.c 45 KB

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