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