rtl8187_dev.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. * Magic delays and register offsets below are taken from the original
  11. * r8187 driver sources. Thanks to Realtek for their support!
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/usb.h>
  19. #include <linux/delay.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/eeprom_93cx6.h>
  22. #include <net/mac80211.h>
  23. #include "rtl8187.h"
  24. #include "rtl8187_rtl8225.h"
  25. MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
  26. MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
  27. MODULE_DESCRIPTION("RTL8187/RTL8187B USB wireless driver");
  28. MODULE_LICENSE("GPL");
  29. static struct usb_device_id rtl8187_table[] __devinitdata = {
  30. /* Realtek */
  31. {USB_DEVICE(0x0bda, 0x8187), .driver_info = DEVICE_RTL8187},
  32. {USB_DEVICE(0x0bda, 0x8189), .driver_info = DEVICE_RTL8187B},
  33. {USB_DEVICE(0x0bda, 0x8197), .driver_info = DEVICE_RTL8187B},
  34. /* Netgear */
  35. {USB_DEVICE(0x0846, 0x6100), .driver_info = DEVICE_RTL8187},
  36. {USB_DEVICE(0x0846, 0x6a00), .driver_info = DEVICE_RTL8187},
  37. /* HP */
  38. {USB_DEVICE(0x03f0, 0xca02), .driver_info = DEVICE_RTL8187},
  39. /* Sitecom */
  40. {USB_DEVICE(0x0df6, 0x000d), .driver_info = DEVICE_RTL8187},
  41. {}
  42. };
  43. MODULE_DEVICE_TABLE(usb, rtl8187_table);
  44. static const struct ieee80211_rate rtl818x_rates[] = {
  45. { .bitrate = 10, .hw_value = 0, },
  46. { .bitrate = 20, .hw_value = 1, },
  47. { .bitrate = 55, .hw_value = 2, },
  48. { .bitrate = 110, .hw_value = 3, },
  49. { .bitrate = 60, .hw_value = 4, },
  50. { .bitrate = 90, .hw_value = 5, },
  51. { .bitrate = 120, .hw_value = 6, },
  52. { .bitrate = 180, .hw_value = 7, },
  53. { .bitrate = 240, .hw_value = 8, },
  54. { .bitrate = 360, .hw_value = 9, },
  55. { .bitrate = 480, .hw_value = 10, },
  56. { .bitrate = 540, .hw_value = 11, },
  57. };
  58. static const struct ieee80211_channel rtl818x_channels[] = {
  59. { .center_freq = 2412 },
  60. { .center_freq = 2417 },
  61. { .center_freq = 2422 },
  62. { .center_freq = 2427 },
  63. { .center_freq = 2432 },
  64. { .center_freq = 2437 },
  65. { .center_freq = 2442 },
  66. { .center_freq = 2447 },
  67. { .center_freq = 2452 },
  68. { .center_freq = 2457 },
  69. { .center_freq = 2462 },
  70. { .center_freq = 2467 },
  71. { .center_freq = 2472 },
  72. { .center_freq = 2484 },
  73. };
  74. static void rtl8187_iowrite_async_cb(struct urb *urb)
  75. {
  76. kfree(urb->context);
  77. usb_free_urb(urb);
  78. }
  79. static void rtl8187_iowrite_async(struct rtl8187_priv *priv, __le16 addr,
  80. void *data, u16 len)
  81. {
  82. struct usb_ctrlrequest *dr;
  83. struct urb *urb;
  84. struct rtl8187_async_write_data {
  85. u8 data[4];
  86. struct usb_ctrlrequest dr;
  87. } *buf;
  88. int rc;
  89. buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
  90. if (!buf)
  91. return;
  92. urb = usb_alloc_urb(0, GFP_ATOMIC);
  93. if (!urb) {
  94. kfree(buf);
  95. return;
  96. }
  97. dr = &buf->dr;
  98. dr->bRequestType = RTL8187_REQT_WRITE;
  99. dr->bRequest = RTL8187_REQ_SET_REG;
  100. dr->wValue = addr;
  101. dr->wIndex = 0;
  102. dr->wLength = cpu_to_le16(len);
  103. memcpy(buf, data, len);
  104. usb_fill_control_urb(urb, priv->udev, usb_sndctrlpipe(priv->udev, 0),
  105. (unsigned char *)dr, buf, len,
  106. rtl8187_iowrite_async_cb, buf);
  107. rc = usb_submit_urb(urb, GFP_ATOMIC);
  108. if (rc < 0) {
  109. kfree(buf);
  110. usb_free_urb(urb);
  111. }
  112. }
  113. static inline void rtl818x_iowrite32_async(struct rtl8187_priv *priv,
  114. __le32 *addr, u32 val)
  115. {
  116. __le32 buf = cpu_to_le32(val);
  117. rtl8187_iowrite_async(priv, cpu_to_le16((unsigned long)addr),
  118. &buf, sizeof(buf));
  119. }
  120. void rtl8187_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
  121. {
  122. struct rtl8187_priv *priv = dev->priv;
  123. data <<= 8;
  124. data |= addr | 0x80;
  125. rtl818x_iowrite8(priv, &priv->map->PHY[3], (data >> 24) & 0xFF);
  126. rtl818x_iowrite8(priv, &priv->map->PHY[2], (data >> 16) & 0xFF);
  127. rtl818x_iowrite8(priv, &priv->map->PHY[1], (data >> 8) & 0xFF);
  128. rtl818x_iowrite8(priv, &priv->map->PHY[0], data & 0xFF);
  129. msleep(1);
  130. }
  131. static void rtl8187_tx_cb(struct urb *urb)
  132. {
  133. struct sk_buff *skb = (struct sk_buff *)urb->context;
  134. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  135. struct ieee80211_hw *hw = info->driver_data[0];
  136. usb_free_urb(info->driver_data[1]);
  137. skb_pull(skb, sizeof(struct rtl8187_tx_hdr));
  138. memset(&info->status, 0, sizeof(info->status));
  139. info->flags |= IEEE80211_TX_STAT_ACK;
  140. ieee80211_tx_status_irqsafe(hw, skb);
  141. }
  142. static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  143. {
  144. struct rtl8187_priv *priv = dev->priv;
  145. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  146. struct rtl8187_tx_hdr *hdr;
  147. struct urb *urb;
  148. __le16 rts_dur = 0;
  149. u32 flags;
  150. int rc;
  151. urb = usb_alloc_urb(0, GFP_ATOMIC);
  152. if (!urb) {
  153. kfree_skb(skb);
  154. return 0;
  155. }
  156. flags = skb->len;
  157. flags |= RTL8187_TX_FLAG_NO_ENCRYPT;
  158. flags |= ieee80211_get_tx_rate(dev, info)->hw_value << 24;
  159. if (ieee80211_has_morefrags(((struct ieee80211_hdr *)skb->data)->frame_control))
  160. flags |= RTL8187_TX_FLAG_MORE_FRAG;
  161. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  162. flags |= RTL8187_TX_FLAG_RTS;
  163. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  164. rts_dur = ieee80211_rts_duration(dev, priv->vif,
  165. skb->len, info);
  166. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  167. flags |= RTL8187_TX_FLAG_CTS;
  168. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  169. }
  170. hdr = (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
  171. hdr->flags = cpu_to_le32(flags);
  172. hdr->len = 0;
  173. hdr->rts_duration = rts_dur;
  174. hdr->retry = cpu_to_le32(info->control.retry_limit << 8);
  175. info->driver_data[0] = dev;
  176. info->driver_data[1] = urb;
  177. usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, 2),
  178. hdr, skb->len, rtl8187_tx_cb, skb);
  179. rc = usb_submit_urb(urb, GFP_ATOMIC);
  180. if (rc < 0) {
  181. usb_free_urb(urb);
  182. kfree_skb(skb);
  183. }
  184. return 0;
  185. }
  186. static void rtl8187_rx_cb(struct urb *urb)
  187. {
  188. struct sk_buff *skb = (struct sk_buff *)urb->context;
  189. struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
  190. struct ieee80211_hw *dev = info->dev;
  191. struct rtl8187_priv *priv = dev->priv;
  192. struct rtl8187_rx_hdr *hdr;
  193. struct ieee80211_rx_status rx_status = { 0 };
  194. int rate, signal;
  195. u32 flags;
  196. spin_lock(&priv->rx_queue.lock);
  197. if (skb->next)
  198. __skb_unlink(skb, &priv->rx_queue);
  199. else {
  200. spin_unlock(&priv->rx_queue.lock);
  201. return;
  202. }
  203. spin_unlock(&priv->rx_queue.lock);
  204. if (unlikely(urb->status)) {
  205. usb_free_urb(urb);
  206. dev_kfree_skb_irq(skb);
  207. return;
  208. }
  209. skb_put(skb, urb->actual_length);
  210. hdr = (struct rtl8187_rx_hdr *)(skb_tail_pointer(skb) - sizeof(*hdr));
  211. flags = le32_to_cpu(hdr->flags);
  212. skb_trim(skb, flags & 0x0FFF);
  213. signal = hdr->agc >> 1;
  214. rate = (flags >> 20) & 0xF;
  215. if (rate > 3) { /* OFDM rate */
  216. if (signal > 90)
  217. signal = 90;
  218. else if (signal < 25)
  219. signal = 25;
  220. signal = 90 - signal;
  221. } else { /* CCK rate */
  222. if (signal > 95)
  223. signal = 95;
  224. else if (signal < 30)
  225. signal = 30;
  226. signal = 95 - signal;
  227. }
  228. rx_status.antenna = (hdr->signal >> 7) & 1;
  229. rx_status.qual = 64 - min(hdr->noise, (u8)64);
  230. rx_status.signal = signal;
  231. rx_status.rate_idx = rate;
  232. rx_status.freq = dev->conf.channel->center_freq;
  233. rx_status.band = dev->conf.channel->band;
  234. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  235. rx_status.flag |= RX_FLAG_TSFT;
  236. if (flags & (1 << 13))
  237. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  238. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  239. skb = dev_alloc_skb(RTL8187_MAX_RX);
  240. if (unlikely(!skb)) {
  241. usb_free_urb(urb);
  242. /* TODO check rx queue length and refill *somewhere* */
  243. return;
  244. }
  245. info = (struct rtl8187_rx_info *)skb->cb;
  246. info->urb = urb;
  247. info->dev = dev;
  248. urb->transfer_buffer = skb_tail_pointer(skb);
  249. urb->context = skb;
  250. skb_queue_tail(&priv->rx_queue, skb);
  251. usb_submit_urb(urb, GFP_ATOMIC);
  252. }
  253. static int rtl8187_init_urbs(struct ieee80211_hw *dev)
  254. {
  255. struct rtl8187_priv *priv = dev->priv;
  256. struct urb *entry;
  257. struct sk_buff *skb;
  258. struct rtl8187_rx_info *info;
  259. while (skb_queue_len(&priv->rx_queue) < 8) {
  260. skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
  261. if (!skb)
  262. break;
  263. entry = usb_alloc_urb(0, GFP_KERNEL);
  264. if (!entry) {
  265. kfree_skb(skb);
  266. break;
  267. }
  268. usb_fill_bulk_urb(entry, priv->udev,
  269. usb_rcvbulkpipe(priv->udev, 1),
  270. skb_tail_pointer(skb),
  271. RTL8187_MAX_RX, rtl8187_rx_cb, skb);
  272. info = (struct rtl8187_rx_info *)skb->cb;
  273. info->urb = entry;
  274. info->dev = dev;
  275. skb_queue_tail(&priv->rx_queue, skb);
  276. usb_submit_urb(entry, GFP_KERNEL);
  277. }
  278. return 0;
  279. }
  280. static int rtl8187_cmd_reset(struct ieee80211_hw *dev)
  281. {
  282. struct rtl8187_priv *priv = dev->priv;
  283. u8 reg;
  284. int i;
  285. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  286. reg &= (1 << 1);
  287. reg |= RTL818X_CMD_RESET;
  288. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  289. i = 10;
  290. do {
  291. msleep(2);
  292. if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
  293. RTL818X_CMD_RESET))
  294. break;
  295. } while (--i);
  296. if (!i) {
  297. printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
  298. return -ETIMEDOUT;
  299. }
  300. /* reload registers from eeprom */
  301. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  302. i = 10;
  303. do {
  304. msleep(4);
  305. if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
  306. RTL818X_EEPROM_CMD_CONFIG))
  307. break;
  308. } while (--i);
  309. if (!i) {
  310. printk(KERN_ERR "%s: eeprom reset timeout!\n",
  311. wiphy_name(dev->wiphy));
  312. return -ETIMEDOUT;
  313. }
  314. return 0;
  315. }
  316. static int rtl8187_init_hw(struct ieee80211_hw *dev)
  317. {
  318. struct rtl8187_priv *priv = dev->priv;
  319. u8 reg;
  320. int res;
  321. /* reset */
  322. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  323. RTL818X_EEPROM_CMD_CONFIG);
  324. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  325. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg |
  326. RTL818X_CONFIG3_ANAPARAM_WRITE);
  327. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  328. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  329. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg &
  330. ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  331. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  332. RTL818X_EEPROM_CMD_NORMAL);
  333. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  334. msleep(200);
  335. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  336. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  337. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  338. msleep(200);
  339. res = rtl8187_cmd_reset(dev);
  340. if (res)
  341. return res;
  342. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  343. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  344. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  345. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  346. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  347. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  348. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  349. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  350. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  351. /* setup card */
  352. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  353. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  354. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  355. rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
  356. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  357. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  358. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  359. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  360. reg &= 0x3F;
  361. reg |= 0x80;
  362. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  363. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  364. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  365. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  366. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  367. // TODO: set RESP_RATE and BRSR properly
  368. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  369. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  370. /* host_usb_init */
  371. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  372. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  373. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  374. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  375. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  376. rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
  377. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  378. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  379. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  380. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  381. msleep(100);
  382. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  383. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  384. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  385. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  386. RTL818X_EEPROM_CMD_CONFIG);
  387. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  388. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  389. RTL818X_EEPROM_CMD_NORMAL);
  390. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  391. msleep(100);
  392. priv->rf->init(dev);
  393. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  394. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  395. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  396. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  397. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  398. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  399. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  400. return 0;
  401. }
  402. static const u8 rtl8187b_reg_table[][3] = {
  403. {0xF0, 0x32, 0}, {0xF1, 0x32, 0}, {0xF2, 0x00, 0}, {0xF3, 0x00, 0},
  404. {0xF4, 0x32, 0}, {0xF5, 0x43, 0}, {0xF6, 0x00, 0}, {0xF7, 0x00, 0},
  405. {0xF8, 0x46, 0}, {0xF9, 0xA4, 0}, {0xFA, 0x00, 0}, {0xFB, 0x00, 0},
  406. {0xFC, 0x96, 0}, {0xFD, 0xA4, 0}, {0xFE, 0x00, 0}, {0xFF, 0x00, 0},
  407. {0x58, 0x4B, 1}, {0x59, 0x00, 1}, {0x5A, 0x4B, 1}, {0x5B, 0x00, 1},
  408. {0x60, 0x4B, 1}, {0x61, 0x09, 1}, {0x62, 0x4B, 1}, {0x63, 0x09, 1},
  409. {0xCE, 0x0F, 1}, {0xCF, 0x00, 1}, {0xE0, 0xFF, 1}, {0xE1, 0x0F, 1},
  410. {0xE2, 0x00, 1}, {0xF0, 0x4E, 1}, {0xF1, 0x01, 1}, {0xF2, 0x02, 1},
  411. {0xF3, 0x03, 1}, {0xF4, 0x04, 1}, {0xF5, 0x05, 1}, {0xF6, 0x06, 1},
  412. {0xF7, 0x07, 1}, {0xF8, 0x08, 1},
  413. {0x4E, 0x00, 2}, {0x0C, 0x04, 2}, {0x21, 0x61, 2}, {0x22, 0x68, 2},
  414. {0x23, 0x6F, 2}, {0x24, 0x76, 2}, {0x25, 0x7D, 2}, {0x26, 0x84, 2},
  415. {0x27, 0x8D, 2}, {0x4D, 0x08, 2}, {0x50, 0x05, 2}, {0x51, 0xF5, 2},
  416. {0x52, 0x04, 2}, {0x53, 0xA0, 2}, {0x54, 0x1F, 2}, {0x55, 0x23, 2},
  417. {0x56, 0x45, 2}, {0x57, 0x67, 2}, {0x58, 0x08, 2}, {0x59, 0x08, 2},
  418. {0x5A, 0x08, 2}, {0x5B, 0x08, 2}, {0x60, 0x08, 2}, {0x61, 0x08, 2},
  419. {0x62, 0x08, 2}, {0x63, 0x08, 2}, {0x64, 0xCF, 2}, {0x72, 0x56, 2},
  420. {0x73, 0x9A, 2},
  421. {0x34, 0xF0, 0}, {0x35, 0x0F, 0}, {0x5B, 0x40, 0}, {0x84, 0x88, 0},
  422. {0x85, 0x24, 0}, {0x88, 0x54, 0}, {0x8B, 0xB8, 0}, {0x8C, 0x07, 0},
  423. {0x8D, 0x00, 0}, {0x94, 0x1B, 0}, {0x95, 0x12, 0}, {0x96, 0x00, 0},
  424. {0x97, 0x06, 0}, {0x9D, 0x1A, 0}, {0x9F, 0x10, 0}, {0xB4, 0x22, 0},
  425. {0xBE, 0x80, 0}, {0xDB, 0x00, 0}, {0xEE, 0x00, 0}, {0x91, 0x03, 0},
  426. {0x4C, 0x00, 2}, {0x9F, 0x00, 3}, {0x8C, 0x01, 0}, {0x8D, 0x10, 0},
  427. {0x8E, 0x08, 0}, {0x8F, 0x00, 0}
  428. };
  429. static int rtl8187b_init_hw(struct ieee80211_hw *dev)
  430. {
  431. struct rtl8187_priv *priv = dev->priv;
  432. int res, i;
  433. u8 reg;
  434. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  435. RTL818X_EEPROM_CMD_CONFIG);
  436. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  437. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE | RTL818X_CONFIG3_GNT_SELECT;
  438. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  439. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, 0x727f3f52);
  440. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, 0x45090658);
  441. rtl818x_iowrite8(priv, &priv->map->ANAPARAM3, 0);
  442. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0x10);
  443. reg = rtl818x_ioread8(priv, (u8 *)0xFF62);
  444. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg & ~(1 << 5));
  445. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg | (1 << 5));
  446. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  447. reg &= ~RTL818X_CONFIG3_ANAPARAM_WRITE;
  448. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  449. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  450. RTL818X_EEPROM_CMD_NORMAL);
  451. res = rtl8187_cmd_reset(dev);
  452. if (res)
  453. return res;
  454. rtl818x_iowrite16(priv, (__le16 *)0xFF2D, 0x0FFF);
  455. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  456. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  457. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  458. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  459. reg |= RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT |
  460. RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  461. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  462. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFE0, 0x0FFF, 1);
  463. reg = rtl818x_ioread8(priv, &priv->map->RATE_FALLBACK);
  464. reg |= RTL818X_RATE_FALLBACK_ENABLE;
  465. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, reg);
  466. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  467. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  468. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFD4, 0xFFFF, 1);
  469. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  470. RTL818X_EEPROM_CMD_CONFIG);
  471. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  472. rtl818x_iowrite8(priv, &priv->map->CONFIG1, (reg & 0x3F) | 0x80);
  473. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  474. RTL818X_EEPROM_CMD_NORMAL);
  475. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  476. for (i = 0; i < ARRAY_SIZE(rtl8187b_reg_table); i++) {
  477. rtl818x_iowrite8_idx(priv,
  478. (u8 *)(uintptr_t)
  479. (rtl8187b_reg_table[i][0] | 0xFF00),
  480. rtl8187b_reg_table[i][1],
  481. rtl8187b_reg_table[i][2]);
  482. }
  483. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  484. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  485. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF0, 0, 1);
  486. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF4, 0, 1);
  487. rtl818x_iowrite8_idx(priv, (u8 *)0xFFF8, 0, 1);
  488. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x00004001);
  489. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x569A, 2);
  490. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  491. RTL818X_EEPROM_CMD_CONFIG);
  492. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  493. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE;
  494. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  495. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  496. RTL818X_EEPROM_CMD_NORMAL);
  497. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  498. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  499. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FFF);
  500. msleep(1100);
  501. priv->rf->init(dev);
  502. reg = RTL818X_CMD_TX_ENABLE | RTL818X_CMD_RX_ENABLE;
  503. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  504. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  505. rtl818x_iowrite8(priv, (u8 *)0xFE41, 0xF4);
  506. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x00);
  507. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  508. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  509. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x0F);
  510. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  511. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  512. reg = rtl818x_ioread8(priv, (u8 *)0xFFDB);
  513. rtl818x_iowrite8(priv, (u8 *)0xFFDB, reg | (1 << 2));
  514. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x59FA, 3);
  515. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF74, 0x59D2, 3);
  516. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF76, 0x59D2, 3);
  517. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF78, 0x19FA, 3);
  518. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7A, 0x19FA, 3);
  519. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7C, 0x00D0, 3);
  520. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0);
  521. rtl818x_iowrite8_idx(priv, (u8 *)0xFF80, 0x0F, 1);
  522. rtl818x_iowrite8_idx(priv, (u8 *)0xFF83, 0x03, 1);
  523. rtl818x_iowrite8(priv, (u8 *)0xFFDA, 0x10);
  524. rtl818x_iowrite8_idx(priv, (u8 *)0xFF4D, 0x08, 2);
  525. rtl818x_iowrite32(priv, &priv->map->HSSI_PARA, 0x0600321B);
  526. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFEC, 0x0800, 1);
  527. return 0;
  528. }
  529. static int rtl8187_start(struct ieee80211_hw *dev)
  530. {
  531. struct rtl8187_priv *priv = dev->priv;
  532. u32 reg;
  533. int ret;
  534. ret = (!priv->is_rtl8187b) ? rtl8187_init_hw(dev) :
  535. rtl8187b_init_hw(dev);
  536. if (ret)
  537. return ret;
  538. if (priv->is_rtl8187b) {
  539. reg = RTL818X_RX_CONF_MGMT |
  540. RTL818X_RX_CONF_DATA |
  541. RTL818X_RX_CONF_BROADCAST |
  542. RTL818X_RX_CONF_NICMAC |
  543. RTL818X_RX_CONF_BSSID |
  544. (7 << 13 /* RX FIFO threshold NONE */) |
  545. (7 << 10 /* MAX RX DMA */) |
  546. RTL818X_RX_CONF_RX_AUTORESETPHY |
  547. RTL818X_RX_CONF_ONLYERLPKT |
  548. RTL818X_RX_CONF_MULTICAST;
  549. priv->rx_conf = reg;
  550. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  551. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  552. RTL818X_TX_CONF_HW_SEQNUM |
  553. RTL818X_TX_CONF_DISREQQSIZE |
  554. (7 << 8 /* short retry limit */) |
  555. (7 << 0 /* long retry limit */) |
  556. (7 << 21 /* MAX TX DMA */));
  557. rtl8187_init_urbs(dev);
  558. return 0;
  559. }
  560. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  561. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  562. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  563. rtl8187_init_urbs(dev);
  564. reg = RTL818X_RX_CONF_ONLYERLPKT |
  565. RTL818X_RX_CONF_RX_AUTORESETPHY |
  566. RTL818X_RX_CONF_BSSID |
  567. RTL818X_RX_CONF_MGMT |
  568. RTL818X_RX_CONF_DATA |
  569. (7 << 13 /* RX FIFO threshold NONE */) |
  570. (7 << 10 /* MAX RX DMA */) |
  571. RTL818X_RX_CONF_BROADCAST |
  572. RTL818X_RX_CONF_NICMAC;
  573. priv->rx_conf = reg;
  574. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  575. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  576. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  577. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  578. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  579. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  580. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  581. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  582. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  583. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  584. reg = RTL818X_TX_CONF_CW_MIN |
  585. (7 << 21 /* MAX TX DMA */) |
  586. RTL818X_TX_CONF_NO_ICV;
  587. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  588. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  589. reg |= RTL818X_CMD_TX_ENABLE;
  590. reg |= RTL818X_CMD_RX_ENABLE;
  591. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  592. return 0;
  593. }
  594. static void rtl8187_stop(struct ieee80211_hw *dev)
  595. {
  596. struct rtl8187_priv *priv = dev->priv;
  597. struct rtl8187_rx_info *info;
  598. struct sk_buff *skb;
  599. u32 reg;
  600. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  601. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  602. reg &= ~RTL818X_CMD_TX_ENABLE;
  603. reg &= ~RTL818X_CMD_RX_ENABLE;
  604. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  605. priv->rf->stop(dev);
  606. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  607. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  608. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  609. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  610. while ((skb = skb_dequeue(&priv->rx_queue))) {
  611. info = (struct rtl8187_rx_info *)skb->cb;
  612. usb_kill_urb(info->urb);
  613. kfree_skb(skb);
  614. }
  615. return;
  616. }
  617. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  618. struct ieee80211_if_init_conf *conf)
  619. {
  620. struct rtl8187_priv *priv = dev->priv;
  621. int i;
  622. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  623. return -EOPNOTSUPP;
  624. switch (conf->type) {
  625. case IEEE80211_IF_TYPE_STA:
  626. priv->mode = conf->type;
  627. break;
  628. default:
  629. return -EOPNOTSUPP;
  630. }
  631. priv->vif = conf->vif;
  632. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  633. for (i = 0; i < ETH_ALEN; i++)
  634. rtl818x_iowrite8(priv, &priv->map->MAC[i],
  635. ((u8 *)conf->mac_addr)[i]);
  636. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  637. return 0;
  638. }
  639. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  640. struct ieee80211_if_init_conf *conf)
  641. {
  642. struct rtl8187_priv *priv = dev->priv;
  643. priv->mode = IEEE80211_IF_TYPE_MNTR;
  644. priv->vif = NULL;
  645. }
  646. static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  647. {
  648. struct rtl8187_priv *priv = dev->priv;
  649. u32 reg;
  650. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  651. /* Enable TX loopback on MAC level to avoid TX during channel
  652. * changes, as this has be seen to causes problems and the
  653. * card will stop work until next reset
  654. */
  655. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  656. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  657. msleep(10);
  658. priv->rf->set_chan(dev, conf);
  659. msleep(10);
  660. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  661. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  662. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  663. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  664. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  665. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  666. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
  667. } else {
  668. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  669. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  670. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  671. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
  672. }
  673. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  674. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  675. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  676. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  677. return 0;
  678. }
  679. static int rtl8187_config_interface(struct ieee80211_hw *dev,
  680. struct ieee80211_vif *vif,
  681. struct ieee80211_if_conf *conf)
  682. {
  683. struct rtl8187_priv *priv = dev->priv;
  684. int i;
  685. for (i = 0; i < ETH_ALEN; i++)
  686. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  687. if (is_valid_ether_addr(conf->bssid))
  688. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_INFRA);
  689. else
  690. rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_NO_LINK);
  691. return 0;
  692. }
  693. static void rtl8187_configure_filter(struct ieee80211_hw *dev,
  694. unsigned int changed_flags,
  695. unsigned int *total_flags,
  696. int mc_count, struct dev_addr_list *mclist)
  697. {
  698. struct rtl8187_priv *priv = dev->priv;
  699. if (changed_flags & FIF_FCSFAIL)
  700. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  701. if (changed_flags & FIF_CONTROL)
  702. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  703. if (changed_flags & FIF_OTHER_BSS)
  704. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  705. if (*total_flags & FIF_ALLMULTI || mc_count > 0)
  706. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  707. else
  708. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  709. *total_flags = 0;
  710. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  711. *total_flags |= FIF_FCSFAIL;
  712. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  713. *total_flags |= FIF_CONTROL;
  714. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  715. *total_flags |= FIF_OTHER_BSS;
  716. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  717. *total_flags |= FIF_ALLMULTI;
  718. rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
  719. }
  720. static const struct ieee80211_ops rtl8187_ops = {
  721. .tx = rtl8187_tx,
  722. .start = rtl8187_start,
  723. .stop = rtl8187_stop,
  724. .add_interface = rtl8187_add_interface,
  725. .remove_interface = rtl8187_remove_interface,
  726. .config = rtl8187_config,
  727. .config_interface = rtl8187_config_interface,
  728. .configure_filter = rtl8187_configure_filter,
  729. };
  730. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  731. {
  732. struct ieee80211_hw *dev = eeprom->data;
  733. struct rtl8187_priv *priv = dev->priv;
  734. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  735. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  736. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  737. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  738. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  739. }
  740. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  741. {
  742. struct ieee80211_hw *dev = eeprom->data;
  743. struct rtl8187_priv *priv = dev->priv;
  744. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  745. if (eeprom->reg_data_in)
  746. reg |= RTL818X_EEPROM_CMD_WRITE;
  747. if (eeprom->reg_data_out)
  748. reg |= RTL818X_EEPROM_CMD_READ;
  749. if (eeprom->reg_data_clock)
  750. reg |= RTL818X_EEPROM_CMD_CK;
  751. if (eeprom->reg_chip_select)
  752. reg |= RTL818X_EEPROM_CMD_CS;
  753. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  754. udelay(10);
  755. }
  756. static int __devinit rtl8187_probe(struct usb_interface *intf,
  757. const struct usb_device_id *id)
  758. {
  759. struct usb_device *udev = interface_to_usbdev(intf);
  760. struct ieee80211_hw *dev;
  761. struct rtl8187_priv *priv;
  762. struct eeprom_93cx6 eeprom;
  763. struct ieee80211_channel *channel;
  764. u16 txpwr, reg;
  765. int err, i;
  766. DECLARE_MAC_BUF(mac);
  767. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  768. if (!dev) {
  769. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  770. return -ENOMEM;
  771. }
  772. priv = dev->priv;
  773. SET_IEEE80211_DEV(dev, &intf->dev);
  774. usb_set_intfdata(intf, dev);
  775. priv->udev = udev;
  776. usb_get_dev(udev);
  777. skb_queue_head_init(&priv->rx_queue);
  778. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  779. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  780. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  781. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  782. priv->map = (struct rtl818x_csr *)0xFF00;
  783. priv->band.band = IEEE80211_BAND_2GHZ;
  784. priv->band.channels = priv->channels;
  785. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  786. priv->band.bitrates = priv->rates;
  787. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  788. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  789. priv->mode = IEEE80211_IF_TYPE_MNTR;
  790. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  791. IEEE80211_HW_RX_INCLUDES_FCS |
  792. IEEE80211_HW_SIGNAL_UNSPEC;
  793. dev->extra_tx_headroom = sizeof(struct rtl8187_tx_hdr);
  794. dev->queues = 1;
  795. dev->max_signal = 65;
  796. eeprom.data = dev;
  797. eeprom.register_read = rtl8187_eeprom_register_read;
  798. eeprom.register_write = rtl8187_eeprom_register_write;
  799. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  800. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  801. else
  802. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  803. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  804. udelay(10);
  805. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  806. (__le16 __force *)dev->wiphy->perm_addr, 3);
  807. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  808. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  809. "generated MAC address\n");
  810. random_ether_addr(dev->wiphy->perm_addr);
  811. }
  812. channel = priv->channels;
  813. for (i = 0; i < 3; i++) {
  814. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  815. &txpwr);
  816. (*channel++).hw_value = txpwr & 0xFF;
  817. (*channel++).hw_value = txpwr >> 8;
  818. }
  819. for (i = 0; i < 2; i++) {
  820. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  821. &txpwr);
  822. (*channel++).hw_value = txpwr & 0xFF;
  823. (*channel++).hw_value = txpwr >> 8;
  824. }
  825. for (i = 0; i < 2; i++) {
  826. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  827. &txpwr);
  828. (*channel++).hw_value = txpwr & 0xFF;
  829. (*channel++).hw_value = txpwr >> 8;
  830. }
  831. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  832. &priv->txpwr_base);
  833. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  834. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  835. /* 0 means asic B-cut, we should use SW 3 wire
  836. * bit-by-bit banging for radio. 1 means we can use
  837. * USB specific request to write radio registers */
  838. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  839. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  840. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  841. priv->rf = rtl8187_detect_rf(dev);
  842. err = ieee80211_register_hw(dev);
  843. if (err) {
  844. printk(KERN_ERR "rtl8187: Cannot register device\n");
  845. goto err_free_dev;
  846. }
  847. printk(KERN_INFO "%s: hwaddr %s, rtl8187 V%d + %s\n",
  848. wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
  849. priv->asic_rev, priv->rf->name);
  850. return 0;
  851. err_free_dev:
  852. ieee80211_free_hw(dev);
  853. usb_set_intfdata(intf, NULL);
  854. usb_put_dev(udev);
  855. return err;
  856. }
  857. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  858. {
  859. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  860. struct rtl8187_priv *priv;
  861. if (!dev)
  862. return;
  863. ieee80211_unregister_hw(dev);
  864. priv = dev->priv;
  865. usb_put_dev(interface_to_usbdev(intf));
  866. ieee80211_free_hw(dev);
  867. }
  868. static struct usb_driver rtl8187_driver = {
  869. .name = KBUILD_MODNAME,
  870. .id_table = rtl8187_table,
  871. .probe = rtl8187_probe,
  872. .disconnect = rtl8187_disconnect,
  873. };
  874. static int __init rtl8187_init(void)
  875. {
  876. return usb_register(&rtl8187_driver);
  877. }
  878. static void __exit rtl8187_exit(void)
  879. {
  880. usb_deregister(&rtl8187_driver);
  881. }
  882. module_init(rtl8187_init);
  883. module_exit(rtl8187_exit);