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