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