rtl8187_dev.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. struct rtl8187_priv *priv = hw->priv;
  137. usb_free_urb(info->driver_data[1]);
  138. skb_pull(skb, priv->is_rtl8187b ? sizeof(struct rtl8187b_tx_hdr) :
  139. sizeof(struct rtl8187_tx_hdr));
  140. memset(&info->status, 0, sizeof(info->status));
  141. info->flags |= IEEE80211_TX_STAT_ACK;
  142. ieee80211_tx_status_irqsafe(hw, skb);
  143. }
  144. static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  145. {
  146. struct rtl8187_priv *priv = dev->priv;
  147. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  148. unsigned int ep;
  149. void *buf;
  150. struct urb *urb;
  151. __le16 rts_dur = 0;
  152. u32 flags;
  153. int rc;
  154. urb = usb_alloc_urb(0, GFP_ATOMIC);
  155. if (!urb) {
  156. kfree_skb(skb);
  157. return 0;
  158. }
  159. flags = skb->len;
  160. flags |= RTL8187_TX_FLAG_NO_ENCRYPT;
  161. flags |= ieee80211_get_tx_rate(dev, info)->hw_value << 24;
  162. if (ieee80211_has_morefrags(((struct ieee80211_hdr *)skb->data)->frame_control))
  163. flags |= RTL8187_TX_FLAG_MORE_FRAG;
  164. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  165. flags |= RTL8187_TX_FLAG_RTS;
  166. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  167. rts_dur = ieee80211_rts_duration(dev, priv->vif,
  168. skb->len, info);
  169. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  170. flags |= RTL8187_TX_FLAG_CTS;
  171. flags |= ieee80211_get_rts_cts_rate(dev, info)->hw_value << 19;
  172. }
  173. if (!priv->is_rtl8187b) {
  174. struct rtl8187_tx_hdr *hdr =
  175. (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
  176. hdr->flags = cpu_to_le32(flags);
  177. hdr->len = 0;
  178. hdr->rts_duration = rts_dur;
  179. hdr->retry = cpu_to_le32(info->control.retry_limit << 8);
  180. buf = hdr;
  181. ep = 2;
  182. } else {
  183. /* fc needs to be calculated before skb_push() */
  184. unsigned int epmap[4] = { 6, 7, 5, 4 };
  185. struct ieee80211_hdr *tx_hdr =
  186. (struct ieee80211_hdr *)(skb->data);
  187. u16 fc = le16_to_cpu(tx_hdr->frame_control);
  188. struct rtl8187b_tx_hdr *hdr =
  189. (struct rtl8187b_tx_hdr *)skb_push(skb, sizeof(*hdr));
  190. struct ieee80211_rate *txrate =
  191. ieee80211_get_tx_rate(dev, info);
  192. memset(hdr, 0, sizeof(*hdr));
  193. hdr->flags = cpu_to_le32(flags);
  194. hdr->rts_duration = rts_dur;
  195. hdr->retry = cpu_to_le32(info->control.retry_limit << 8);
  196. hdr->tx_duration =
  197. ieee80211_generic_frame_duration(dev, priv->vif,
  198. skb->len, txrate);
  199. buf = hdr;
  200. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  201. ep = 12;
  202. else
  203. ep = epmap[skb_get_queue_mapping(skb)];
  204. }
  205. info->driver_data[0] = dev;
  206. info->driver_data[1] = urb;
  207. usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, ep),
  208. buf, skb->len, rtl8187_tx_cb, skb);
  209. rc = usb_submit_urb(urb, GFP_ATOMIC);
  210. if (rc < 0) {
  211. usb_free_urb(urb);
  212. kfree_skb(skb);
  213. }
  214. return 0;
  215. }
  216. static void rtl8187_rx_cb(struct urb *urb)
  217. {
  218. struct sk_buff *skb = (struct sk_buff *)urb->context;
  219. struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
  220. struct ieee80211_hw *dev = info->dev;
  221. struct rtl8187_priv *priv = dev->priv;
  222. struct ieee80211_rx_status rx_status = { 0 };
  223. int rate, signal;
  224. u32 flags;
  225. spin_lock(&priv->rx_queue.lock);
  226. if (skb->next)
  227. __skb_unlink(skb, &priv->rx_queue);
  228. else {
  229. spin_unlock(&priv->rx_queue.lock);
  230. return;
  231. }
  232. spin_unlock(&priv->rx_queue.lock);
  233. if (unlikely(urb->status)) {
  234. usb_free_urb(urb);
  235. dev_kfree_skb_irq(skb);
  236. return;
  237. }
  238. skb_put(skb, urb->actual_length);
  239. if (!priv->is_rtl8187b) {
  240. struct rtl8187_rx_hdr *hdr =
  241. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  242. flags = le32_to_cpu(hdr->flags);
  243. signal = hdr->signal & 0x7f;
  244. rx_status.antenna = (hdr->signal >> 7) & 1;
  245. rx_status.signal = signal;
  246. rx_status.noise = hdr->noise;
  247. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  248. priv->signal = signal;
  249. priv->quality = signal;
  250. priv->noise = hdr->noise;
  251. } else {
  252. struct rtl8187b_rx_hdr *hdr =
  253. (typeof(hdr))(skb_tail_pointer(skb) - sizeof(*hdr));
  254. flags = le32_to_cpu(hdr->flags);
  255. signal = hdr->agc >> 1;
  256. rx_status.antenna = (hdr->signal >> 7) & 1;
  257. rx_status.signal = 64 - min(hdr->noise, (u8)64);
  258. rx_status.noise = hdr->noise;
  259. rx_status.mactime = le64_to_cpu(hdr->mac_time);
  260. priv->signal = hdr->signal;
  261. priv->quality = hdr->agc >> 1;
  262. priv->noise = hdr->noise;
  263. }
  264. skb_trim(skb, flags & 0x0FFF);
  265. rate = (flags >> 20) & 0xF;
  266. if (rate > 3) { /* OFDM rate */
  267. if (signal > 90)
  268. signal = 90;
  269. else if (signal < 25)
  270. signal = 25;
  271. signal = 90 - signal;
  272. } else { /* CCK rate */
  273. if (signal > 95)
  274. signal = 95;
  275. else if (signal < 30)
  276. signal = 30;
  277. signal = 95 - signal;
  278. }
  279. rx_status.qual = priv->quality;
  280. rx_status.signal = signal;
  281. rx_status.rate_idx = rate;
  282. rx_status.freq = dev->conf.channel->center_freq;
  283. rx_status.band = dev->conf.channel->band;
  284. rx_status.flag |= RX_FLAG_TSFT;
  285. if (flags & (1 << 13))
  286. rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
  287. ieee80211_rx_irqsafe(dev, skb, &rx_status);
  288. skb = dev_alloc_skb(RTL8187_MAX_RX);
  289. if (unlikely(!skb)) {
  290. usb_free_urb(urb);
  291. /* TODO check rx queue length and refill *somewhere* */
  292. return;
  293. }
  294. info = (struct rtl8187_rx_info *)skb->cb;
  295. info->urb = urb;
  296. info->dev = dev;
  297. urb->transfer_buffer = skb_tail_pointer(skb);
  298. urb->context = skb;
  299. skb_queue_tail(&priv->rx_queue, skb);
  300. usb_submit_urb(urb, GFP_ATOMIC);
  301. }
  302. static int rtl8187_init_urbs(struct ieee80211_hw *dev)
  303. {
  304. struct rtl8187_priv *priv = dev->priv;
  305. struct urb *entry;
  306. struct sk_buff *skb;
  307. struct rtl8187_rx_info *info;
  308. while (skb_queue_len(&priv->rx_queue) < 8) {
  309. skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
  310. if (!skb)
  311. break;
  312. entry = usb_alloc_urb(0, GFP_KERNEL);
  313. if (!entry) {
  314. kfree_skb(skb);
  315. break;
  316. }
  317. usb_fill_bulk_urb(entry, priv->udev,
  318. usb_rcvbulkpipe(priv->udev,
  319. priv->is_rtl8187b ? 3 : 1),
  320. skb_tail_pointer(skb),
  321. RTL8187_MAX_RX, rtl8187_rx_cb, skb);
  322. info = (struct rtl8187_rx_info *)skb->cb;
  323. info->urb = entry;
  324. info->dev = dev;
  325. skb_queue_tail(&priv->rx_queue, skb);
  326. usb_submit_urb(entry, GFP_KERNEL);
  327. }
  328. return 0;
  329. }
  330. static int rtl8187_cmd_reset(struct ieee80211_hw *dev)
  331. {
  332. struct rtl8187_priv *priv = dev->priv;
  333. u8 reg;
  334. int i;
  335. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  336. reg &= (1 << 1);
  337. reg |= RTL818X_CMD_RESET;
  338. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  339. i = 10;
  340. do {
  341. msleep(2);
  342. if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
  343. RTL818X_CMD_RESET))
  344. break;
  345. } while (--i);
  346. if (!i) {
  347. printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
  348. return -ETIMEDOUT;
  349. }
  350. /* reload registers from eeprom */
  351. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
  352. i = 10;
  353. do {
  354. msleep(4);
  355. if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
  356. RTL818X_EEPROM_CMD_CONFIG))
  357. break;
  358. } while (--i);
  359. if (!i) {
  360. printk(KERN_ERR "%s: eeprom reset timeout!\n",
  361. wiphy_name(dev->wiphy));
  362. return -ETIMEDOUT;
  363. }
  364. return 0;
  365. }
  366. static int rtl8187_init_hw(struct ieee80211_hw *dev)
  367. {
  368. struct rtl8187_priv *priv = dev->priv;
  369. u8 reg;
  370. int res;
  371. /* reset */
  372. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  373. RTL818X_EEPROM_CMD_CONFIG);
  374. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  375. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg |
  376. RTL818X_CONFIG3_ANAPARAM_WRITE);
  377. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  378. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  379. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg &
  380. ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  381. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  382. RTL818X_EEPROM_CMD_NORMAL);
  383. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  384. msleep(200);
  385. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
  386. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
  387. rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
  388. msleep(200);
  389. res = rtl8187_cmd_reset(dev);
  390. if (res)
  391. return res;
  392. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  393. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  394. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  395. reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
  396. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
  397. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
  398. rtl818x_iowrite8(priv, &priv->map->CONFIG3,
  399. reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
  400. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  401. /* setup card */
  402. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  403. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  404. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  405. rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
  406. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  407. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  408. rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
  409. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  410. reg &= 0x3F;
  411. reg |= 0x80;
  412. rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
  413. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  414. rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
  415. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  416. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
  417. // TODO: set RESP_RATE and BRSR properly
  418. rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
  419. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  420. /* host_usb_init */
  421. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
  422. rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
  423. reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
  424. rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
  425. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
  426. rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
  427. rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
  428. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
  429. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
  430. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
  431. msleep(100);
  432. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
  433. rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
  434. rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
  435. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  436. RTL818X_EEPROM_CMD_CONFIG);
  437. rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
  438. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  439. RTL818X_EEPROM_CMD_NORMAL);
  440. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
  441. msleep(100);
  442. priv->rf->init(dev);
  443. rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
  444. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  445. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  446. rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
  447. rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
  448. rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
  449. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  450. return 0;
  451. }
  452. static const u8 rtl8187b_reg_table[][3] = {
  453. {0xF0, 0x32, 0}, {0xF1, 0x32, 0}, {0xF2, 0x00, 0}, {0xF3, 0x00, 0},
  454. {0xF4, 0x32, 0}, {0xF5, 0x43, 0}, {0xF6, 0x00, 0}, {0xF7, 0x00, 0},
  455. {0xF8, 0x46, 0}, {0xF9, 0xA4, 0}, {0xFA, 0x00, 0}, {0xFB, 0x00, 0},
  456. {0xFC, 0x96, 0}, {0xFD, 0xA4, 0}, {0xFE, 0x00, 0}, {0xFF, 0x00, 0},
  457. {0x58, 0x4B, 1}, {0x59, 0x00, 1}, {0x5A, 0x4B, 1}, {0x5B, 0x00, 1},
  458. {0x60, 0x4B, 1}, {0x61, 0x09, 1}, {0x62, 0x4B, 1}, {0x63, 0x09, 1},
  459. {0xCE, 0x0F, 1}, {0xCF, 0x00, 1}, {0xE0, 0xFF, 1}, {0xE1, 0x0F, 1},
  460. {0xE2, 0x00, 1}, {0xF0, 0x4E, 1}, {0xF1, 0x01, 1}, {0xF2, 0x02, 1},
  461. {0xF3, 0x03, 1}, {0xF4, 0x04, 1}, {0xF5, 0x05, 1}, {0xF6, 0x06, 1},
  462. {0xF7, 0x07, 1}, {0xF8, 0x08, 1},
  463. {0x4E, 0x00, 2}, {0x0C, 0x04, 2}, {0x21, 0x61, 2}, {0x22, 0x68, 2},
  464. {0x23, 0x6F, 2}, {0x24, 0x76, 2}, {0x25, 0x7D, 2}, {0x26, 0x84, 2},
  465. {0x27, 0x8D, 2}, {0x4D, 0x08, 2}, {0x50, 0x05, 2}, {0x51, 0xF5, 2},
  466. {0x52, 0x04, 2}, {0x53, 0xA0, 2}, {0x54, 0x1F, 2}, {0x55, 0x23, 2},
  467. {0x56, 0x45, 2}, {0x57, 0x67, 2}, {0x58, 0x08, 2}, {0x59, 0x08, 2},
  468. {0x5A, 0x08, 2}, {0x5B, 0x08, 2}, {0x60, 0x08, 2}, {0x61, 0x08, 2},
  469. {0x62, 0x08, 2}, {0x63, 0x08, 2}, {0x64, 0xCF, 2}, {0x72, 0x56, 2},
  470. {0x73, 0x9A, 2},
  471. {0x34, 0xF0, 0}, {0x35, 0x0F, 0}, {0x5B, 0x40, 0}, {0x84, 0x88, 0},
  472. {0x85, 0x24, 0}, {0x88, 0x54, 0}, {0x8B, 0xB8, 0}, {0x8C, 0x07, 0},
  473. {0x8D, 0x00, 0}, {0x94, 0x1B, 0}, {0x95, 0x12, 0}, {0x96, 0x00, 0},
  474. {0x97, 0x06, 0}, {0x9D, 0x1A, 0}, {0x9F, 0x10, 0}, {0xB4, 0x22, 0},
  475. {0xBE, 0x80, 0}, {0xDB, 0x00, 0}, {0xEE, 0x00, 0}, {0x91, 0x03, 0},
  476. {0x4C, 0x00, 2}, {0x9F, 0x00, 3}, {0x8C, 0x01, 0}, {0x8D, 0x10, 0},
  477. {0x8E, 0x08, 0}, {0x8F, 0x00, 0}
  478. };
  479. static int rtl8187b_init_hw(struct ieee80211_hw *dev)
  480. {
  481. struct rtl8187_priv *priv = dev->priv;
  482. int res, i;
  483. u8 reg;
  484. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  485. RTL818X_EEPROM_CMD_CONFIG);
  486. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  487. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE | RTL818X_CONFIG3_GNT_SELECT;
  488. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  489. rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, 0x727f3f52);
  490. rtl818x_iowrite32(priv, &priv->map->ANAPARAM, 0x45090658);
  491. rtl818x_iowrite8(priv, &priv->map->ANAPARAM3, 0);
  492. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0x10);
  493. reg = rtl818x_ioread8(priv, (u8 *)0xFF62);
  494. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg & ~(1 << 5));
  495. rtl818x_iowrite8(priv, (u8 *)0xFF62, reg | (1 << 5));
  496. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  497. reg &= ~RTL818X_CONFIG3_ANAPARAM_WRITE;
  498. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  499. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  500. RTL818X_EEPROM_CMD_NORMAL);
  501. res = rtl8187_cmd_reset(dev);
  502. if (res)
  503. return res;
  504. rtl818x_iowrite16(priv, (__le16 *)0xFF2D, 0x0FFF);
  505. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  506. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  507. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  508. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  509. reg |= RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT |
  510. RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  511. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  512. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFE0, 0x0FFF, 1);
  513. reg = rtl818x_ioread8(priv, &priv->map->RATE_FALLBACK);
  514. reg |= RTL818X_RATE_FALLBACK_ENABLE;
  515. rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, reg);
  516. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  517. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  518. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFD4, 0xFFFF, 1);
  519. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  520. RTL818X_EEPROM_CMD_CONFIG);
  521. reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
  522. rtl818x_iowrite8(priv, &priv->map->CONFIG1, (reg & 0x3F) | 0x80);
  523. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  524. RTL818X_EEPROM_CMD_NORMAL);
  525. rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
  526. for (i = 0; i < ARRAY_SIZE(rtl8187b_reg_table); i++) {
  527. rtl818x_iowrite8_idx(priv,
  528. (u8 *)(uintptr_t)
  529. (rtl8187b_reg_table[i][0] | 0xFF00),
  530. rtl8187b_reg_table[i][1],
  531. rtl8187b_reg_table[i][2]);
  532. }
  533. rtl818x_iowrite16(priv, &priv->map->TID_AC_MAP, 0xFA50);
  534. rtl818x_iowrite16(priv, &priv->map->INT_MIG, 0);
  535. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF0, 0, 1);
  536. rtl818x_iowrite32_idx(priv, (__le32 *)0xFFF4, 0, 1);
  537. rtl818x_iowrite8_idx(priv, (u8 *)0xFFF8, 0, 1);
  538. rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x00004001);
  539. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x569A, 2);
  540. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  541. RTL818X_EEPROM_CMD_CONFIG);
  542. reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
  543. reg |= RTL818X_CONFIG3_ANAPARAM_WRITE;
  544. rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg);
  545. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
  546. RTL818X_EEPROM_CMD_NORMAL);
  547. rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x0480);
  548. rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x2488);
  549. rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FFF);
  550. msleep(1100);
  551. priv->rf->init(dev);
  552. reg = RTL818X_CMD_TX_ENABLE | RTL818X_CMD_RX_ENABLE;
  553. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  554. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  555. rtl818x_iowrite8(priv, (u8 *)0xFE41, 0xF4);
  556. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x00);
  557. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  558. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  559. rtl818x_iowrite8(priv, (u8 *)0xFE40, 0x0F);
  560. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x00);
  561. rtl818x_iowrite8(priv, (u8 *)0xFE42, 0x01);
  562. reg = rtl818x_ioread8(priv, (u8 *)0xFFDB);
  563. rtl818x_iowrite8(priv, (u8 *)0xFFDB, reg | (1 << 2));
  564. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF72, 0x59FA, 3);
  565. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF74, 0x59D2, 3);
  566. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF76, 0x59D2, 3);
  567. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF78, 0x19FA, 3);
  568. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7A, 0x19FA, 3);
  569. rtl818x_iowrite16_idx(priv, (__le16 *)0xFF7C, 0x00D0, 3);
  570. rtl818x_iowrite8(priv, (u8 *)0xFF61, 0);
  571. rtl818x_iowrite8_idx(priv, (u8 *)0xFF80, 0x0F, 1);
  572. rtl818x_iowrite8_idx(priv, (u8 *)0xFF83, 0x03, 1);
  573. rtl818x_iowrite8(priv, (u8 *)0xFFDA, 0x10);
  574. rtl818x_iowrite8_idx(priv, (u8 *)0xFF4D, 0x08, 2);
  575. rtl818x_iowrite32(priv, &priv->map->HSSI_PARA, 0x0600321B);
  576. rtl818x_iowrite16_idx(priv, (__le16 *)0xFFEC, 0x0800, 1);
  577. return 0;
  578. }
  579. static int rtl8187_start(struct ieee80211_hw *dev)
  580. {
  581. struct rtl8187_priv *priv = dev->priv;
  582. u32 reg;
  583. int ret;
  584. ret = (!priv->is_rtl8187b) ? rtl8187_init_hw(dev) :
  585. rtl8187b_init_hw(dev);
  586. if (ret)
  587. return ret;
  588. if (priv->is_rtl8187b) {
  589. reg = RTL818X_RX_CONF_MGMT |
  590. RTL818X_RX_CONF_DATA |
  591. RTL818X_RX_CONF_BROADCAST |
  592. RTL818X_RX_CONF_NICMAC |
  593. RTL818X_RX_CONF_BSSID |
  594. (7 << 13 /* RX FIFO threshold NONE */) |
  595. (7 << 10 /* MAX RX DMA */) |
  596. RTL818X_RX_CONF_RX_AUTORESETPHY |
  597. RTL818X_RX_CONF_ONLYERLPKT |
  598. RTL818X_RX_CONF_MULTICAST;
  599. priv->rx_conf = reg;
  600. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  601. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  602. RTL818X_TX_CONF_HW_SEQNUM |
  603. RTL818X_TX_CONF_DISREQQSIZE |
  604. (7 << 8 /* short retry limit */) |
  605. (7 << 0 /* long retry limit */) |
  606. (7 << 21 /* MAX TX DMA */));
  607. rtl8187_init_urbs(dev);
  608. return 0;
  609. }
  610. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
  611. rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
  612. rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
  613. rtl8187_init_urbs(dev);
  614. reg = RTL818X_RX_CONF_ONLYERLPKT |
  615. RTL818X_RX_CONF_RX_AUTORESETPHY |
  616. RTL818X_RX_CONF_BSSID |
  617. RTL818X_RX_CONF_MGMT |
  618. RTL818X_RX_CONF_DATA |
  619. (7 << 13 /* RX FIFO threshold NONE */) |
  620. (7 << 10 /* MAX RX DMA */) |
  621. RTL818X_RX_CONF_BROADCAST |
  622. RTL818X_RX_CONF_NICMAC;
  623. priv->rx_conf = reg;
  624. rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
  625. reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
  626. reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
  627. reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
  628. rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
  629. reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
  630. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
  631. reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
  632. reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
  633. rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
  634. reg = RTL818X_TX_CONF_CW_MIN |
  635. (7 << 21 /* MAX TX DMA */) |
  636. RTL818X_TX_CONF_NO_ICV;
  637. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  638. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  639. reg |= RTL818X_CMD_TX_ENABLE;
  640. reg |= RTL818X_CMD_RX_ENABLE;
  641. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  642. return 0;
  643. }
  644. static void rtl8187_stop(struct ieee80211_hw *dev)
  645. {
  646. struct rtl8187_priv *priv = dev->priv;
  647. struct rtl8187_rx_info *info;
  648. struct sk_buff *skb;
  649. u32 reg;
  650. rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
  651. reg = rtl818x_ioread8(priv, &priv->map->CMD);
  652. reg &= ~RTL818X_CMD_TX_ENABLE;
  653. reg &= ~RTL818X_CMD_RX_ENABLE;
  654. rtl818x_iowrite8(priv, &priv->map->CMD, reg);
  655. priv->rf->stop(dev);
  656. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  657. reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
  658. rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
  659. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  660. while ((skb = skb_dequeue(&priv->rx_queue))) {
  661. info = (struct rtl8187_rx_info *)skb->cb;
  662. usb_kill_urb(info->urb);
  663. kfree_skb(skb);
  664. }
  665. return;
  666. }
  667. static int rtl8187_add_interface(struct ieee80211_hw *dev,
  668. struct ieee80211_if_init_conf *conf)
  669. {
  670. struct rtl8187_priv *priv = dev->priv;
  671. int i;
  672. if (priv->mode != IEEE80211_IF_TYPE_MNTR)
  673. return -EOPNOTSUPP;
  674. switch (conf->type) {
  675. case IEEE80211_IF_TYPE_STA:
  676. priv->mode = conf->type;
  677. break;
  678. default:
  679. return -EOPNOTSUPP;
  680. }
  681. priv->vif = conf->vif;
  682. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  683. for (i = 0; i < ETH_ALEN; i++)
  684. rtl818x_iowrite8(priv, &priv->map->MAC[i],
  685. ((u8 *)conf->mac_addr)[i]);
  686. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  687. return 0;
  688. }
  689. static void rtl8187_remove_interface(struct ieee80211_hw *dev,
  690. struct ieee80211_if_init_conf *conf)
  691. {
  692. struct rtl8187_priv *priv = dev->priv;
  693. priv->mode = IEEE80211_IF_TYPE_MNTR;
  694. priv->vif = NULL;
  695. }
  696. static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
  697. {
  698. struct rtl8187_priv *priv = dev->priv;
  699. u32 reg;
  700. reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  701. /* Enable TX loopback on MAC level to avoid TX during channel
  702. * changes, as this has be seen to causes problems and the
  703. * card will stop work until next reset
  704. */
  705. rtl818x_iowrite32(priv, &priv->map->TX_CONF,
  706. reg | RTL818X_TX_CONF_LOOPBACK_MAC);
  707. msleep(10);
  708. priv->rf->set_chan(dev, conf);
  709. msleep(10);
  710. rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
  711. if (!priv->is_rtl8187b) {
  712. rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
  713. if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
  714. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
  715. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
  716. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
  717. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
  718. } else {
  719. rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
  720. rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
  721. rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
  722. rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
  723. }
  724. }
  725. rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
  726. rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
  727. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
  728. rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
  729. return 0;
  730. }
  731. static int rtl8187_config_interface(struct ieee80211_hw *dev,
  732. struct ieee80211_vif *vif,
  733. struct ieee80211_if_conf *conf)
  734. {
  735. struct rtl8187_priv *priv = dev->priv;
  736. int i;
  737. u8 reg;
  738. for (i = 0; i < ETH_ALEN; i++)
  739. rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
  740. if (is_valid_ether_addr(conf->bssid)) {
  741. reg = RTL818X_MSR_INFRA;
  742. if (priv->is_rtl8187b)
  743. reg |= RTL818X_MSR_ENEDCA;
  744. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  745. } else {
  746. reg = RTL818X_MSR_NO_LINK;
  747. rtl818x_iowrite8(priv, &priv->map->MSR, reg);
  748. }
  749. return 0;
  750. }
  751. static void rtl8187_configure_filter(struct ieee80211_hw *dev,
  752. unsigned int changed_flags,
  753. unsigned int *total_flags,
  754. int mc_count, struct dev_addr_list *mclist)
  755. {
  756. struct rtl8187_priv *priv = dev->priv;
  757. if (changed_flags & FIF_FCSFAIL)
  758. priv->rx_conf ^= RTL818X_RX_CONF_FCS;
  759. if (changed_flags & FIF_CONTROL)
  760. priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
  761. if (changed_flags & FIF_OTHER_BSS)
  762. priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
  763. if (*total_flags & FIF_ALLMULTI || mc_count > 0)
  764. priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
  765. else
  766. priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
  767. *total_flags = 0;
  768. if (priv->rx_conf & RTL818X_RX_CONF_FCS)
  769. *total_flags |= FIF_FCSFAIL;
  770. if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
  771. *total_flags |= FIF_CONTROL;
  772. if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
  773. *total_flags |= FIF_OTHER_BSS;
  774. if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
  775. *total_flags |= FIF_ALLMULTI;
  776. rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
  777. }
  778. static const struct ieee80211_ops rtl8187_ops = {
  779. .tx = rtl8187_tx,
  780. .start = rtl8187_start,
  781. .stop = rtl8187_stop,
  782. .add_interface = rtl8187_add_interface,
  783. .remove_interface = rtl8187_remove_interface,
  784. .config = rtl8187_config,
  785. .config_interface = rtl8187_config_interface,
  786. .configure_filter = rtl8187_configure_filter,
  787. };
  788. static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  789. {
  790. struct ieee80211_hw *dev = eeprom->data;
  791. struct rtl8187_priv *priv = dev->priv;
  792. u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
  793. eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
  794. eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
  795. eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
  796. eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
  797. }
  798. static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  799. {
  800. struct ieee80211_hw *dev = eeprom->data;
  801. struct rtl8187_priv *priv = dev->priv;
  802. u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
  803. if (eeprom->reg_data_in)
  804. reg |= RTL818X_EEPROM_CMD_WRITE;
  805. if (eeprom->reg_data_out)
  806. reg |= RTL818X_EEPROM_CMD_READ;
  807. if (eeprom->reg_data_clock)
  808. reg |= RTL818X_EEPROM_CMD_CK;
  809. if (eeprom->reg_chip_select)
  810. reg |= RTL818X_EEPROM_CMD_CS;
  811. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
  812. udelay(10);
  813. }
  814. static int __devinit rtl8187_probe(struct usb_interface *intf,
  815. const struct usb_device_id *id)
  816. {
  817. struct usb_device *udev = interface_to_usbdev(intf);
  818. struct ieee80211_hw *dev;
  819. struct rtl8187_priv *priv;
  820. struct eeprom_93cx6 eeprom;
  821. struct ieee80211_channel *channel;
  822. const char *chip_name;
  823. u16 txpwr, reg;
  824. int err, i;
  825. DECLARE_MAC_BUF(mac);
  826. dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
  827. if (!dev) {
  828. printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
  829. return -ENOMEM;
  830. }
  831. priv = dev->priv;
  832. priv->is_rtl8187b = (id->driver_info == DEVICE_RTL8187B);
  833. SET_IEEE80211_DEV(dev, &intf->dev);
  834. usb_set_intfdata(intf, dev);
  835. priv->udev = udev;
  836. usb_get_dev(udev);
  837. skb_queue_head_init(&priv->rx_queue);
  838. BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
  839. BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
  840. memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
  841. memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
  842. priv->map = (struct rtl818x_csr *)0xFF00;
  843. priv->band.band = IEEE80211_BAND_2GHZ;
  844. priv->band.channels = priv->channels;
  845. priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
  846. priv->band.bitrates = priv->rates;
  847. priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
  848. dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
  849. priv->mode = IEEE80211_IF_TYPE_MNTR;
  850. dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  851. IEEE80211_HW_RX_INCLUDES_FCS |
  852. IEEE80211_HW_SIGNAL_UNSPEC;
  853. dev->max_signal = 65;
  854. eeprom.data = dev;
  855. eeprom.register_read = rtl8187_eeprom_register_read;
  856. eeprom.register_write = rtl8187_eeprom_register_write;
  857. if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
  858. eeprom.width = PCI_EEPROM_WIDTH_93C66;
  859. else
  860. eeprom.width = PCI_EEPROM_WIDTH_93C46;
  861. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
  862. udelay(10);
  863. eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
  864. (__le16 __force *)dev->wiphy->perm_addr, 3);
  865. if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
  866. printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
  867. "generated MAC address\n");
  868. random_ether_addr(dev->wiphy->perm_addr);
  869. }
  870. channel = priv->channels;
  871. for (i = 0; i < 3; i++) {
  872. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
  873. &txpwr);
  874. (*channel++).hw_value = txpwr & 0xFF;
  875. (*channel++).hw_value = txpwr >> 8;
  876. }
  877. for (i = 0; i < 2; i++) {
  878. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
  879. &txpwr);
  880. (*channel++).hw_value = txpwr & 0xFF;
  881. (*channel++).hw_value = txpwr >> 8;
  882. }
  883. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
  884. &priv->txpwr_base);
  885. reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
  886. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
  887. /* 0 means asic B-cut, we should use SW 3 wire
  888. * bit-by-bit banging for radio. 1 means we can use
  889. * USB specific request to write radio registers */
  890. priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
  891. rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
  892. rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
  893. if (!priv->is_rtl8187b) {
  894. u32 reg32;
  895. reg32 = rtl818x_ioread32(priv, &priv->map->TX_CONF);
  896. reg32 &= RTL818X_TX_CONF_HWVER_MASK;
  897. switch (reg32) {
  898. case RTL818X_TX_CONF_R8187vD_B:
  899. /* Some RTL8187B devices have a USB ID of 0x8187
  900. * detect them here */
  901. chip_name = "RTL8187BvB(early)";
  902. priv->is_rtl8187b = 1;
  903. priv->hw_rev = RTL8187BvB;
  904. break;
  905. case RTL818X_TX_CONF_R8187vD:
  906. chip_name = "RTL8187vD";
  907. break;
  908. default:
  909. chip_name = "RTL8187vB (default)";
  910. }
  911. } else {
  912. /*
  913. * Force USB request to write radio registers for 8187B, Realtek
  914. * only uses it in their sources
  915. */
  916. /*if (priv->asic_rev == 0) {
  917. printk(KERN_WARNING "rtl8187: Forcing use of USB "
  918. "requests to write to radio registers\n");
  919. priv->asic_rev = 1;
  920. }*/
  921. switch (rtl818x_ioread8(priv, (u8 *)0xFFE1)) {
  922. case RTL818X_R8187B_B:
  923. chip_name = "RTL8187BvB";
  924. priv->hw_rev = RTL8187BvB;
  925. break;
  926. case RTL818X_R8187B_D:
  927. chip_name = "RTL8187BvD";
  928. priv->hw_rev = RTL8187BvD;
  929. break;
  930. case RTL818X_R8187B_E:
  931. chip_name = "RTL8187BvE";
  932. priv->hw_rev = RTL8187BvE;
  933. break;
  934. default:
  935. chip_name = "RTL8187BvB (default)";
  936. priv->hw_rev = RTL8187BvB;
  937. }
  938. }
  939. if (!priv->is_rtl8187b) {
  940. for (i = 0; i < 2; i++) {
  941. eeprom_93cx6_read(&eeprom,
  942. RTL8187_EEPROM_TXPWR_CHAN_6 + i,
  943. &txpwr);
  944. (*channel++).hw_value = txpwr & 0xFF;
  945. (*channel++).hw_value = txpwr >> 8;
  946. }
  947. } else {
  948. eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
  949. &txpwr);
  950. (*channel++).hw_value = txpwr & 0xFF;
  951. eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
  952. (*channel++).hw_value = txpwr & 0xFF;
  953. eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
  954. (*channel++).hw_value = txpwr & 0xFF;
  955. (*channel++).hw_value = txpwr >> 8;
  956. }
  957. if (priv->is_rtl8187b)
  958. printk(KERN_WARNING "rtl8187: 8187B chip detected. Support "
  959. "is EXPERIMENTAL, and could damage your\n"
  960. " hardware, use at your own risk\n");
  961. if ((id->driver_info == DEVICE_RTL8187) && priv->is_rtl8187b)
  962. printk(KERN_INFO "rtl8187: inconsistency between id with OEM"
  963. " info!\n");
  964. priv->rf = rtl8187_detect_rf(dev);
  965. dev->extra_tx_headroom = (!priv->is_rtl8187b) ?
  966. sizeof(struct rtl8187_tx_hdr) :
  967. sizeof(struct rtl8187b_tx_hdr);
  968. if (!priv->is_rtl8187b)
  969. dev->queues = 1;
  970. else
  971. dev->queues = 4;
  972. err = ieee80211_register_hw(dev);
  973. if (err) {
  974. printk(KERN_ERR "rtl8187: Cannot register device\n");
  975. goto err_free_dev;
  976. }
  977. printk(KERN_INFO "%s: hwaddr %s, %s V%d + %s\n",
  978. wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
  979. chip_name, priv->asic_rev, priv->rf->name);
  980. return 0;
  981. err_free_dev:
  982. ieee80211_free_hw(dev);
  983. usb_set_intfdata(intf, NULL);
  984. usb_put_dev(udev);
  985. return err;
  986. }
  987. static void __devexit rtl8187_disconnect(struct usb_interface *intf)
  988. {
  989. struct ieee80211_hw *dev = usb_get_intfdata(intf);
  990. struct rtl8187_priv *priv;
  991. if (!dev)
  992. return;
  993. ieee80211_unregister_hw(dev);
  994. priv = dev->priv;
  995. usb_put_dev(interface_to_usbdev(intf));
  996. ieee80211_free_hw(dev);
  997. }
  998. static struct usb_driver rtl8187_driver = {
  999. .name = KBUILD_MODNAME,
  1000. .id_table = rtl8187_table,
  1001. .probe = rtl8187_probe,
  1002. .disconnect = rtl8187_disconnect,
  1003. };
  1004. static int __init rtl8187_init(void)
  1005. {
  1006. return usb_register(&rtl8187_driver);
  1007. }
  1008. static void __exit rtl8187_exit(void)
  1009. {
  1010. usb_deregister(&rtl8187_driver);
  1011. }
  1012. module_init(rtl8187_init);
  1013. module_exit(rtl8187_exit);