blob: c03834d5cb0b531a164d58992d06655181376ed9 [file] [log] [blame]
Michael Wu605bebe2007-05-14 01:41:02 -04001/*
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 *
John W. Linville0aec00a2007-06-12 22:11:42 -040010 * Magic delays and register offsets below are taken from the original
11 * r8187 driver sources. Thanks to Realtek for their support!
Michael Wu605bebe2007-05-14 01:41:02 -040012 *
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
28MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
29MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
30MODULE_DESCRIPTION("RTL8187 USB wireless driver");
31MODULE_LICENSE("GPL");
32
33static 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)},
Michael Wuc3cf60a2007-10-04 00:04:07 -040039 /* HP */
40 {USB_DEVICE(0x03f0, 0xca02)},
Matthias Mueller99345502007-12-02 17:17:51 -050041 /* Sitecom */
42 {USB_DEVICE(0x0df6, 0x000d)},
Michael Wu605bebe2007-05-14 01:41:02 -040043 {}
44};
45
46MODULE_DEVICE_TABLE(usb, rtl8187_table);
47
Johannes Berg8318d782008-01-24 19:38:38 +010048static 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
63static 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
Johannes Berg4150c572007-09-17 01:29:23 -040080static void rtl8187_iowrite_async_cb(struct urb *urb)
81{
82 kfree(urb->context);
83 usb_free_urb(urb);
84}
85
86static 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
122static 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
Michael Wu605bebe2007-05-14 01:41:02 -0400131void 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
146static void rtl8187_tx_cb(struct urb *urb)
147{
Johannes Berg1955fd02008-02-20 12:05:59 +0100148 struct ieee80211_tx_status status;
Michael Wu605bebe2007-05-14 01:41:02 -0400149 struct sk_buff *skb = (struct sk_buff *)urb->context;
150 struct rtl8187_tx_info *info = (struct rtl8187_tx_info *)skb->cb;
151
Johannes Berg1955fd02008-02-20 12:05:59 +0100152 memset(&status, 0, sizeof(status));
153
Michael Wu605bebe2007-05-14 01:41:02 -0400154 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
163static 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;
Michael Wu98798f42007-10-10 17:28:59 -0400170 __le16 rts_dur = 0;
171 u32 flags;
Michael Wu605bebe2007-05-14 01:41:02 -0400172
173 urb = usb_alloc_urb(0, GFP_ATOMIC);
174 if (!urb) {
175 kfree_skb(skb);
176 return 0;
177 }
178
Michael Wu98798f42007-10-10 17:28:59 -0400179 flags = skb->len;
180 flags |= RTL8187_TX_FLAG_NO_ENCRYPT;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100181
182 BUG_ON(!control->tx_rate);
183
Johannes Berg8318d782008-01-24 19:38:38 +0100184 flags |= control->tx_rate->hw_value << 24;
Michael Wu98798f42007-10-10 17:28:59 -0400185 if (ieee80211_get_morefrag((struct ieee80211_hdr *)skb->data))
186 flags |= RTL8187_TX_FLAG_MORE_FRAG;
Michael Wu605bebe2007-05-14 01:41:02 -0400187 if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100188 BUG_ON(!control->rts_cts_rate);
Michael Wu98798f42007-10-10 17:28:59 -0400189 flags |= RTL8187_TX_FLAG_RTS;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100190 flags |= control->rts_cts_rate->hw_value << 19;
Johannes Berg32bfd352007-12-19 01:31:26 +0100191 rts_dur = ieee80211_rts_duration(dev, priv->vif,
192 skb->len, control);
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100193 } else if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
194 BUG_ON(!control->rts_cts_rate);
Michael Wu98798f42007-10-10 17:28:59 -0400195 flags |= RTL8187_TX_FLAG_CTS;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100196 flags |= control->rts_cts_rate->hw_value << 19;
197 }
Michael Wu98798f42007-10-10 17:28:59 -0400198
199 hdr = (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
200 hdr->flags = cpu_to_le32(flags);
Michael Wu605bebe2007-05-14 01:41:02 -0400201 hdr->len = 0;
Michael Wu98798f42007-10-10 17:28:59 -0400202 hdr->rts_duration = rts_dur;
203 hdr->retry = cpu_to_le32(control->retry_limit << 8);
Michael Wu605bebe2007-05-14 01:41:02 -0400204
205 info = (struct rtl8187_tx_info *)skb->cb;
206 info->control = kmemdup(control, sizeof(*control), GFP_ATOMIC);
207 info->urb = urb;
208 info->dev = dev;
209 usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, 2),
210 hdr, skb->len, rtl8187_tx_cb, skb);
211 usb_submit_urb(urb, GFP_ATOMIC);
212
213 return 0;
214}
215
216static 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 rtl8187_rx_hdr *hdr;
223 struct ieee80211_rx_status rx_status = { 0 };
224 int rate, signal;
Johannes Berg4150c572007-09-17 01:29:23 -0400225 u32 flags;
Michael Wu605bebe2007-05-14 01:41:02 -0400226
227 spin_lock(&priv->rx_queue.lock);
228 if (skb->next)
229 __skb_unlink(skb, &priv->rx_queue);
230 else {
231 spin_unlock(&priv->rx_queue.lock);
232 return;
233 }
234 spin_unlock(&priv->rx_queue.lock);
235
236 if (unlikely(urb->status)) {
237 usb_free_urb(urb);
238 dev_kfree_skb_irq(skb);
239 return;
240 }
241
242 skb_put(skb, urb->actual_length);
243 hdr = (struct rtl8187_rx_hdr *)(skb_tail_pointer(skb) - sizeof(*hdr));
Johannes Berg4150c572007-09-17 01:29:23 -0400244 flags = le32_to_cpu(hdr->flags);
245 skb_trim(skb, flags & 0x0FFF);
Michael Wu605bebe2007-05-14 01:41:02 -0400246
247 signal = hdr->agc >> 1;
Johannes Berg4150c572007-09-17 01:29:23 -0400248 rate = (flags >> 20) & 0xF;
Michael Wu605bebe2007-05-14 01:41:02 -0400249 if (rate > 3) { /* OFDM rate */
250 if (signal > 90)
251 signal = 90;
252 else if (signal < 25)
253 signal = 25;
254 signal = 90 - signal;
255 } else { /* CCK rate */
256 if (signal > 95)
257 signal = 95;
258 else if (signal < 30)
259 signal = 30;
260 signal = 95 - signal;
261 }
262
263 rx_status.antenna = (hdr->signal >> 7) & 1;
264 rx_status.signal = 64 - min(hdr->noise, (u8)64);
265 rx_status.ssi = signal;
Johannes Berg8318d782008-01-24 19:38:38 +0100266 rx_status.rate_idx = rate;
267 rx_status.freq = dev->conf.channel->center_freq;
268 rx_status.band = dev->conf.channel->band;
Michael Wu605bebe2007-05-14 01:41:02 -0400269 rx_status.mactime = le64_to_cpu(hdr->mac_time);
Johannes Berg03bffc12007-12-04 20:33:40 +0100270 rx_status.flag |= RX_FLAG_TSFT;
Johannes Berg4150c572007-09-17 01:29:23 -0400271 if (flags & (1 << 13))
272 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
Michael Wu605bebe2007-05-14 01:41:02 -0400273 ieee80211_rx_irqsafe(dev, skb, &rx_status);
274
275 skb = dev_alloc_skb(RTL8187_MAX_RX);
276 if (unlikely(!skb)) {
277 usb_free_urb(urb);
278 /* TODO check rx queue length and refill *somewhere* */
279 return;
280 }
281
282 info = (struct rtl8187_rx_info *)skb->cb;
283 info->urb = urb;
284 info->dev = dev;
285 urb->transfer_buffer = skb_tail_pointer(skb);
286 urb->context = skb;
287 skb_queue_tail(&priv->rx_queue, skb);
288
289 usb_submit_urb(urb, GFP_ATOMIC);
290}
291
292static int rtl8187_init_urbs(struct ieee80211_hw *dev)
293{
294 struct rtl8187_priv *priv = dev->priv;
295 struct urb *entry;
296 struct sk_buff *skb;
297 struct rtl8187_rx_info *info;
298
299 while (skb_queue_len(&priv->rx_queue) < 8) {
300 skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
301 if (!skb)
302 break;
303 entry = usb_alloc_urb(0, GFP_KERNEL);
304 if (!entry) {
305 kfree_skb(skb);
306 break;
307 }
308 usb_fill_bulk_urb(entry, priv->udev,
309 usb_rcvbulkpipe(priv->udev, 1),
310 skb_tail_pointer(skb),
311 RTL8187_MAX_RX, rtl8187_rx_cb, skb);
312 info = (struct rtl8187_rx_info *)skb->cb;
313 info->urb = entry;
314 info->dev = dev;
315 skb_queue_tail(&priv->rx_queue, skb);
316 usb_submit_urb(entry, GFP_KERNEL);
317 }
318
319 return 0;
320}
321
322static int rtl8187_init_hw(struct ieee80211_hw *dev)
323{
324 struct rtl8187_priv *priv = dev->priv;
325 u8 reg;
326 int i;
327
328 /* reset */
329 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
330 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
331 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
332 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
333 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
334 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
335 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
336
337 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
338
339 msleep(200);
340 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
341 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
342 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
343 msleep(200);
344
345 reg = rtl818x_ioread8(priv, &priv->map->CMD);
346 reg &= (1 << 1);
347 reg |= RTL818X_CMD_RESET;
348 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
349
350 i = 10;
351 do {
352 msleep(2);
353 if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
354 RTL818X_CMD_RESET))
355 break;
356 } while (--i);
357
358 if (!i) {
359 printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
360 return -ETIMEDOUT;
361 }
362
363 /* reload registers from eeprom */
364 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
365
366 i = 10;
367 do {
368 msleep(4);
369 if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
370 RTL818X_EEPROM_CMD_CONFIG))
371 break;
372 } while (--i);
373
374 if (!i) {
375 printk(KERN_ERR "%s: eeprom reset timeout!\n",
376 wiphy_name(dev->wiphy));
377 return -ETIMEDOUT;
378 }
379
380 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
381 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
382 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
383 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
384 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
385 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
386 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
387
388 /* setup card */
389 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
390 rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
391
392 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
393 rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
394 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
395
396 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
Michael Wu605bebe2007-05-14 01:41:02 -0400397
398 rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
399 reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
400 reg &= 0x3F;
401 reg |= 0x80;
402 rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
403
404 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
405
406 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
407 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
408 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
409
410 // TODO: set RESP_RATE and BRSR properly
411 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
412 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
413
414 /* host_usb_init */
415 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
416 rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
417 reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
418 rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
419 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
420 rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
421 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
422 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
423 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
424 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
425 msleep(100);
426
427 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
428 rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
429 rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
430 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
431 rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
432 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
433 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
434 msleep(100);
435
Michael Wuf6532112007-10-14 14:43:16 -0400436 priv->rf->init(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400437
438 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
Michael Wuf6532112007-10-14 14:43:16 -0400439 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
440 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
Michael Wu605bebe2007-05-14 01:41:02 -0400441 rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
442 rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
443 rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
Michael Wuf6532112007-10-14 14:43:16 -0400444 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400445
446 return 0;
447}
448
Johannes Berg4150c572007-09-17 01:29:23 -0400449static int rtl8187_start(struct ieee80211_hw *dev)
Michael Wu605bebe2007-05-14 01:41:02 -0400450{
451 struct rtl8187_priv *priv = dev->priv;
452 u32 reg;
453 int ret;
454
455 ret = rtl8187_init_hw(dev);
456 if (ret)
457 return ret;
458
459 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
460
Michael Wu2fe14262007-10-20 20:05:31 -0400461 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
462 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
463
Michael Wu605bebe2007-05-14 01:41:02 -0400464 rtl8187_init_urbs(dev);
465
466 reg = RTL818X_RX_CONF_ONLYERLPKT |
467 RTL818X_RX_CONF_RX_AUTORESETPHY |
468 RTL818X_RX_CONF_BSSID |
469 RTL818X_RX_CONF_MGMT |
Michael Wu605bebe2007-05-14 01:41:02 -0400470 RTL818X_RX_CONF_DATA |
471 (7 << 13 /* RX FIFO threshold NONE */) |
472 (7 << 10 /* MAX RX DMA */) |
473 RTL818X_RX_CONF_BROADCAST |
Michael Wu605bebe2007-05-14 01:41:02 -0400474 RTL818X_RX_CONF_NICMAC;
Michael Wu605bebe2007-05-14 01:41:02 -0400475
Johannes Berg4150c572007-09-17 01:29:23 -0400476 priv->rx_conf = reg;
Michael Wu605bebe2007-05-14 01:41:02 -0400477 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
478
479 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
480 reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
481 reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
482 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
483
484 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
485 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
486 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
487 reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
488 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
489
490 reg = RTL818X_TX_CONF_CW_MIN |
491 (7 << 21 /* MAX TX DMA */) |
492 RTL818X_TX_CONF_NO_ICV;
493 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
494
495 reg = rtl818x_ioread8(priv, &priv->map->CMD);
496 reg |= RTL818X_CMD_TX_ENABLE;
497 reg |= RTL818X_CMD_RX_ENABLE;
498 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
499
500 return 0;
501}
502
Johannes Berg4150c572007-09-17 01:29:23 -0400503static void rtl8187_stop(struct ieee80211_hw *dev)
Michael Wu605bebe2007-05-14 01:41:02 -0400504{
505 struct rtl8187_priv *priv = dev->priv;
506 struct rtl8187_rx_info *info;
507 struct sk_buff *skb;
508 u32 reg;
509
510 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
511
512 reg = rtl818x_ioread8(priv, &priv->map->CMD);
513 reg &= ~RTL818X_CMD_TX_ENABLE;
514 reg &= ~RTL818X_CMD_RX_ENABLE;
515 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
516
Michael Wuf6532112007-10-14 14:43:16 -0400517 priv->rf->stop(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400518
519 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
520 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
521 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
522 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
523
524 while ((skb = skb_dequeue(&priv->rx_queue))) {
525 info = (struct rtl8187_rx_info *)skb->cb;
526 usb_kill_urb(info->urb);
527 kfree_skb(skb);
528 }
Johannes Berg4150c572007-09-17 01:29:23 -0400529 return;
Michael Wu605bebe2007-05-14 01:41:02 -0400530}
531
532static int rtl8187_add_interface(struct ieee80211_hw *dev,
533 struct ieee80211_if_init_conf *conf)
534{
535 struct rtl8187_priv *priv = dev->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400536 int i;
Michael Wu605bebe2007-05-14 01:41:02 -0400537
Johannes Berg4150c572007-09-17 01:29:23 -0400538 if (priv->mode != IEEE80211_IF_TYPE_MNTR)
539 return -EOPNOTSUPP;
Michael Wu605bebe2007-05-14 01:41:02 -0400540
541 switch (conf->type) {
542 case IEEE80211_IF_TYPE_STA:
Michael Wu605bebe2007-05-14 01:41:02 -0400543 priv->mode = conf->type;
544 break;
545 default:
546 return -EOPNOTSUPP;
547 }
548
Johannes Berg4150c572007-09-17 01:29:23 -0400549 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
550 for (i = 0; i < ETH_ALEN; i++)
551 rtl818x_iowrite8(priv, &priv->map->MAC[i],
552 ((u8 *)conf->mac_addr)[i]);
553 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Michael Wu605bebe2007-05-14 01:41:02 -0400554
555 return 0;
556}
557
558static void rtl8187_remove_interface(struct ieee80211_hw *dev,
559 struct ieee80211_if_init_conf *conf)
560{
561 struct rtl8187_priv *priv = dev->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400562 priv->mode = IEEE80211_IF_TYPE_MNTR;
Michael Wu605bebe2007-05-14 01:41:02 -0400563}
564
565static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
566{
567 struct rtl8187_priv *priv = dev->priv;
Michael Wuf6532112007-10-14 14:43:16 -0400568 u32 reg;
569
570 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
571 /* Enable TX loopback on MAC level to avoid TX during channel
572 * changes, as this has be seen to causes problems and the
573 * card will stop work until next reset
574 */
575 rtl818x_iowrite32(priv, &priv->map->TX_CONF,
576 reg | RTL818X_TX_CONF_LOOPBACK_MAC);
577 msleep(10);
578 priv->rf->set_chan(dev, conf);
579 msleep(10);
580 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400581
582 rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
583
584 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
585 rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
586 rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
587 rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
588 rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
589 } else {
590 rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
591 rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
592 rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
593 rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
594 }
595
596 rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
597 rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
598 rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
599 rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
600 return 0;
601}
602
Johannes Berg32bfd352007-12-19 01:31:26 +0100603static int rtl8187_config_interface(struct ieee80211_hw *dev,
604 struct ieee80211_vif *vif,
Michael Wu605bebe2007-05-14 01:41:02 -0400605 struct ieee80211_if_conf *conf)
606{
607 struct rtl8187_priv *priv = dev->priv;
608 int i;
609
Michael Wu605bebe2007-05-14 01:41:02 -0400610 for (i = 0; i < ETH_ALEN; i++)
611 rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
612
613 if (is_valid_ether_addr(conf->bssid))
614 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_INFRA);
615 else
616 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_NO_LINK);
617
618 return 0;
619}
620
Johannes Berg4150c572007-09-17 01:29:23 -0400621static void rtl8187_configure_filter(struct ieee80211_hw *dev,
622 unsigned int changed_flags,
623 unsigned int *total_flags,
Michael Wu2fe14262007-10-20 20:05:31 -0400624 int mc_count, struct dev_addr_list *mclist)
Johannes Berg4150c572007-09-17 01:29:23 -0400625{
626 struct rtl8187_priv *priv = dev->priv;
627
Johannes Berg4150c572007-09-17 01:29:23 -0400628 if (changed_flags & FIF_FCSFAIL)
629 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
630 if (changed_flags & FIF_CONTROL)
631 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
632 if (changed_flags & FIF_OTHER_BSS)
633 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Michael Wu2fe14262007-10-20 20:05:31 -0400634 if (*total_flags & FIF_ALLMULTI || mc_count > 0)
Johannes Berg4150c572007-09-17 01:29:23 -0400635 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
Michael Wu2fe14262007-10-20 20:05:31 -0400636 else
637 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
Johannes Berg4150c572007-09-17 01:29:23 -0400638
Michael Wu2fe14262007-10-20 20:05:31 -0400639 *total_flags = 0;
640
Johannes Berg4150c572007-09-17 01:29:23 -0400641 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
642 *total_flags |= FIF_FCSFAIL;
643 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
644 *total_flags |= FIF_CONTROL;
645 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
646 *total_flags |= FIF_OTHER_BSS;
Michael Wu2fe14262007-10-20 20:05:31 -0400647 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
648 *total_flags |= FIF_ALLMULTI;
Johannes Berg4150c572007-09-17 01:29:23 -0400649
650 rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
651}
652
Michael Wu605bebe2007-05-14 01:41:02 -0400653static const struct ieee80211_ops rtl8187_ops = {
654 .tx = rtl8187_tx,
Johannes Berg4150c572007-09-17 01:29:23 -0400655 .start = rtl8187_start,
Michael Wu605bebe2007-05-14 01:41:02 -0400656 .stop = rtl8187_stop,
657 .add_interface = rtl8187_add_interface,
658 .remove_interface = rtl8187_remove_interface,
659 .config = rtl8187_config,
660 .config_interface = rtl8187_config_interface,
Johannes Berg4150c572007-09-17 01:29:23 -0400661 .configure_filter = rtl8187_configure_filter,
Michael Wu605bebe2007-05-14 01:41:02 -0400662};
663
664static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
665{
666 struct ieee80211_hw *dev = eeprom->data;
667 struct rtl8187_priv *priv = dev->priv;
668 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
669
670 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
671 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
672 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
673 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
674}
675
676static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
677{
678 struct ieee80211_hw *dev = eeprom->data;
679 struct rtl8187_priv *priv = dev->priv;
680 u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
681
682 if (eeprom->reg_data_in)
683 reg |= RTL818X_EEPROM_CMD_WRITE;
684 if (eeprom->reg_data_out)
685 reg |= RTL818X_EEPROM_CMD_READ;
686 if (eeprom->reg_data_clock)
687 reg |= RTL818X_EEPROM_CMD_CK;
688 if (eeprom->reg_chip_select)
689 reg |= RTL818X_EEPROM_CMD_CS;
690
691 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
692 udelay(10);
693}
694
695static int __devinit rtl8187_probe(struct usb_interface *intf,
696 const struct usb_device_id *id)
697{
698 struct usb_device *udev = interface_to_usbdev(intf);
699 struct ieee80211_hw *dev;
700 struct rtl8187_priv *priv;
701 struct eeprom_93cx6 eeprom;
702 struct ieee80211_channel *channel;
703 u16 txpwr, reg;
704 int err, i;
Joe Perches0795af52007-10-03 17:59:30 -0700705 DECLARE_MAC_BUF(mac);
Michael Wu605bebe2007-05-14 01:41:02 -0400706
707 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
708 if (!dev) {
709 printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
710 return -ENOMEM;
711 }
712
713 priv = dev->priv;
714
715 SET_IEEE80211_DEV(dev, &intf->dev);
716 usb_set_intfdata(intf, dev);
717 priv->udev = udev;
718
719 usb_get_dev(udev);
720
721 skb_queue_head_init(&priv->rx_queue);
Johannes Berg8318d782008-01-24 19:38:38 +0100722
723 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
724 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
725
Michael Wu605bebe2007-05-14 01:41:02 -0400726 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
727 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
728 priv->map = (struct rtl818x_csr *)0xFF00;
Johannes Berg8318d782008-01-24 19:38:38 +0100729
730 priv->band.band = IEEE80211_BAND_2GHZ;
731 priv->band.channels = priv->channels;
732 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
733 priv->band.bitrates = priv->rates;
734 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
735 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
736
737
Johannes Berg4150c572007-09-17 01:29:23 -0400738 priv->mode = IEEE80211_IF_TYPE_MNTR;
Michael Wu605bebe2007-05-14 01:41:02 -0400739 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Johannes Berg7848ba72007-09-14 11:10:25 -0400740 IEEE80211_HW_RX_INCLUDES_FCS;
Michael Wu605bebe2007-05-14 01:41:02 -0400741 dev->extra_tx_headroom = sizeof(struct rtl8187_tx_hdr);
742 dev->queues = 1;
743 dev->max_rssi = 65;
744 dev->max_signal = 64;
745
Michael Wu605bebe2007-05-14 01:41:02 -0400746 eeprom.data = dev;
747 eeprom.register_read = rtl8187_eeprom_register_read;
748 eeprom.register_write = rtl8187_eeprom_register_write;
749 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
750 eeprom.width = PCI_EEPROM_WIDTH_93C66;
751 else
752 eeprom.width = PCI_EEPROM_WIDTH_93C46;
753
754 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
755 udelay(10);
756
757 eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
758 (__le16 __force *)dev->wiphy->perm_addr, 3);
759 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
760 printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
761 "generated MAC address\n");
762 random_ether_addr(dev->wiphy->perm_addr);
763 }
764
765 channel = priv->channels;
766 for (i = 0; i < 3; i++) {
767 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
768 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100769 (*channel++).hw_value = txpwr & 0xFF;
770 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400771 }
772 for (i = 0; i < 2; i++) {
773 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
774 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100775 (*channel++).hw_value = txpwr & 0xFF;
776 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400777 }
778 for (i = 0; i < 2; i++) {
779 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6 + i,
780 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100781 (*channel++).hw_value = txpwr & 0xFF;
782 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400783 }
784
785 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
786 &priv->txpwr_base);
787
Michael Wuf6532112007-10-14 14:43:16 -0400788 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
789 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
Michael Wu605bebe2007-05-14 01:41:02 -0400790 /* 0 means asic B-cut, we should use SW 3 wire
791 * bit-by-bit banging for radio. 1 means we can use
792 * USB specific request to write radio registers */
793 priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
Michael Wuf6532112007-10-14 14:43:16 -0400794 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400795 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
796
Michael Wuf6532112007-10-14 14:43:16 -0400797 priv->rf = rtl8187_detect_rf(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400798
799 err = ieee80211_register_hw(dev);
800 if (err) {
801 printk(KERN_ERR "rtl8187: Cannot register device\n");
802 goto err_free_dev;
803 }
804
Joe Perches0795af52007-10-03 17:59:30 -0700805 printk(KERN_INFO "%s: hwaddr %s, rtl8187 V%d + %s\n",
806 wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
Michael Wuf6532112007-10-14 14:43:16 -0400807 priv->asic_rev, priv->rf->name);
Michael Wu605bebe2007-05-14 01:41:02 -0400808
809 return 0;
810
811 err_free_dev:
812 ieee80211_free_hw(dev);
813 usb_set_intfdata(intf, NULL);
814 usb_put_dev(udev);
815 return err;
816}
817
818static void __devexit rtl8187_disconnect(struct usb_interface *intf)
819{
820 struct ieee80211_hw *dev = usb_get_intfdata(intf);
821 struct rtl8187_priv *priv;
822
823 if (!dev)
824 return;
825
826 ieee80211_unregister_hw(dev);
827
828 priv = dev->priv;
829 usb_put_dev(interface_to_usbdev(intf));
830 ieee80211_free_hw(dev);
831}
832
833static struct usb_driver rtl8187_driver = {
834 .name = KBUILD_MODNAME,
835 .id_table = rtl8187_table,
836 .probe = rtl8187_probe,
837 .disconnect = rtl8187_disconnect,
838};
839
840static int __init rtl8187_init(void)
841{
842 return usb_register(&rtl8187_driver);
843}
844
845static void __exit rtl8187_exit(void)
846{
847 usb_deregister(&rtl8187_driver);
848}
849
850module_init(rtl8187_init);
851module_exit(rtl8187_exit);