blob: 9223ada5f00ed2d73ca633793c2b9e646033019a [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;
Oliver Neukumea8ee242008-05-15 21:49:16 +020095 int rc;
Johannes Berg4150c572007-09-17 01:29:23 -040096
97 buf = kmalloc(sizeof(*buf), GFP_ATOMIC);
98 if (!buf)
99 return;
100
101 urb = usb_alloc_urb(0, GFP_ATOMIC);
102 if (!urb) {
103 kfree(buf);
104 return;
105 }
106
107 dr = &buf->dr;
108
109 dr->bRequestType = RTL8187_REQT_WRITE;
110 dr->bRequest = RTL8187_REQ_SET_REG;
111 dr->wValue = addr;
112 dr->wIndex = 0;
113 dr->wLength = cpu_to_le16(len);
114
115 memcpy(buf, data, len);
116
117 usb_fill_control_urb(urb, priv->udev, usb_sndctrlpipe(priv->udev, 0),
118 (unsigned char *)dr, buf, len,
119 rtl8187_iowrite_async_cb, buf);
Oliver Neukumea8ee242008-05-15 21:49:16 +0200120 rc = usb_submit_urb(urb, GFP_ATOMIC);
121 if (rc < 0) {
122 kfree(buf);
123 usb_free_urb(urb);
124 }
Johannes Berg4150c572007-09-17 01:29:23 -0400125}
126
127static inline void rtl818x_iowrite32_async(struct rtl8187_priv *priv,
128 __le32 *addr, u32 val)
129{
130 __le32 buf = cpu_to_le32(val);
131
132 rtl8187_iowrite_async(priv, cpu_to_le16((unsigned long)addr),
133 &buf, sizeof(buf));
134}
135
Michael Wu605bebe2007-05-14 01:41:02 -0400136void rtl8187_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
137{
138 struct rtl8187_priv *priv = dev->priv;
139
140 data <<= 8;
141 data |= addr | 0x80;
142
143 rtl818x_iowrite8(priv, &priv->map->PHY[3], (data >> 24) & 0xFF);
144 rtl818x_iowrite8(priv, &priv->map->PHY[2], (data >> 16) & 0xFF);
145 rtl818x_iowrite8(priv, &priv->map->PHY[1], (data >> 8) & 0xFF);
146 rtl818x_iowrite8(priv, &priv->map->PHY[0], data & 0xFF);
147
148 msleep(1);
149}
150
151static void rtl8187_tx_cb(struct urb *urb)
152{
Johannes Berg1955fd02008-02-20 12:05:59 +0100153 struct ieee80211_tx_status status;
Michael Wu605bebe2007-05-14 01:41:02 -0400154 struct sk_buff *skb = (struct sk_buff *)urb->context;
155 struct rtl8187_tx_info *info = (struct rtl8187_tx_info *)skb->cb;
156
Johannes Berg1955fd02008-02-20 12:05:59 +0100157 memset(&status, 0, sizeof(status));
158
Michael Wu605bebe2007-05-14 01:41:02 -0400159 usb_free_urb(info->urb);
160 if (info->control)
161 memcpy(&status.control, info->control, sizeof(status.control));
162 kfree(info->control);
163 skb_pull(skb, sizeof(struct rtl8187_tx_hdr));
164 status.flags |= IEEE80211_TX_STATUS_ACK;
165 ieee80211_tx_status_irqsafe(info->dev, skb, &status);
166}
167
168static int rtl8187_tx(struct ieee80211_hw *dev, struct sk_buff *skb,
169 struct ieee80211_tx_control *control)
170{
171 struct rtl8187_priv *priv = dev->priv;
172 struct rtl8187_tx_hdr *hdr;
173 struct rtl8187_tx_info *info;
174 struct urb *urb;
Michael Wu98798f42007-10-10 17:28:59 -0400175 __le16 rts_dur = 0;
176 u32 flags;
Oliver Neukumea8ee242008-05-15 21:49:16 +0200177 int rc;
Michael Wu605bebe2007-05-14 01:41:02 -0400178
179 urb = usb_alloc_urb(0, GFP_ATOMIC);
180 if (!urb) {
181 kfree_skb(skb);
182 return 0;
183 }
184
Michael Wu98798f42007-10-10 17:28:59 -0400185 flags = skb->len;
186 flags |= RTL8187_TX_FLAG_NO_ENCRYPT;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100187
188 BUG_ON(!control->tx_rate);
189
Johannes Berg8318d782008-01-24 19:38:38 +0100190 flags |= control->tx_rate->hw_value << 24;
Michael Wu98798f42007-10-10 17:28:59 -0400191 if (ieee80211_get_morefrag((struct ieee80211_hdr *)skb->data))
192 flags |= RTL8187_TX_FLAG_MORE_FRAG;
Michael Wu605bebe2007-05-14 01:41:02 -0400193 if (control->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100194 BUG_ON(!control->rts_cts_rate);
Michael Wu98798f42007-10-10 17:28:59 -0400195 flags |= RTL8187_TX_FLAG_RTS;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100196 flags |= control->rts_cts_rate->hw_value << 19;
Johannes Berg32bfd352007-12-19 01:31:26 +0100197 rts_dur = ieee80211_rts_duration(dev, priv->vif,
198 skb->len, control);
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100199 } else if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
200 BUG_ON(!control->rts_cts_rate);
Michael Wu98798f42007-10-10 17:28:59 -0400201 flags |= RTL8187_TX_FLAG_CTS;
Johannes Bergaa68cbf2008-02-18 14:20:30 +0100202 flags |= control->rts_cts_rate->hw_value << 19;
203 }
Michael Wu98798f42007-10-10 17:28:59 -0400204
205 hdr = (struct rtl8187_tx_hdr *)skb_push(skb, sizeof(*hdr));
206 hdr->flags = cpu_to_le32(flags);
Michael Wu605bebe2007-05-14 01:41:02 -0400207 hdr->len = 0;
Michael Wu98798f42007-10-10 17:28:59 -0400208 hdr->rts_duration = rts_dur;
209 hdr->retry = cpu_to_le32(control->retry_limit << 8);
Michael Wu605bebe2007-05-14 01:41:02 -0400210
211 info = (struct rtl8187_tx_info *)skb->cb;
212 info->control = kmemdup(control, sizeof(*control), GFP_ATOMIC);
213 info->urb = urb;
214 info->dev = dev;
215 usb_fill_bulk_urb(urb, priv->udev, usb_sndbulkpipe(priv->udev, 2),
216 hdr, skb->len, rtl8187_tx_cb, skb);
Oliver Neukumea8ee242008-05-15 21:49:16 +0200217 rc = usb_submit_urb(urb, GFP_ATOMIC);
218 if (rc < 0) {
219 usb_free_urb(urb);
220 kfree_skb(skb);
221 }
Michael Wu605bebe2007-05-14 01:41:02 -0400222
223 return 0;
224}
225
226static void rtl8187_rx_cb(struct urb *urb)
227{
228 struct sk_buff *skb = (struct sk_buff *)urb->context;
229 struct rtl8187_rx_info *info = (struct rtl8187_rx_info *)skb->cb;
230 struct ieee80211_hw *dev = info->dev;
231 struct rtl8187_priv *priv = dev->priv;
232 struct rtl8187_rx_hdr *hdr;
233 struct ieee80211_rx_status rx_status = { 0 };
234 int rate, signal;
Johannes Berg4150c572007-09-17 01:29:23 -0400235 u32 flags;
Michael Wu605bebe2007-05-14 01:41:02 -0400236
237 spin_lock(&priv->rx_queue.lock);
238 if (skb->next)
239 __skb_unlink(skb, &priv->rx_queue);
240 else {
241 spin_unlock(&priv->rx_queue.lock);
242 return;
243 }
244 spin_unlock(&priv->rx_queue.lock);
245
246 if (unlikely(urb->status)) {
247 usb_free_urb(urb);
248 dev_kfree_skb_irq(skb);
249 return;
250 }
251
252 skb_put(skb, urb->actual_length);
253 hdr = (struct rtl8187_rx_hdr *)(skb_tail_pointer(skb) - sizeof(*hdr));
Johannes Berg4150c572007-09-17 01:29:23 -0400254 flags = le32_to_cpu(hdr->flags);
255 skb_trim(skb, flags & 0x0FFF);
Michael Wu605bebe2007-05-14 01:41:02 -0400256
257 signal = hdr->agc >> 1;
Johannes Berg4150c572007-09-17 01:29:23 -0400258 rate = (flags >> 20) & 0xF;
Michael Wu605bebe2007-05-14 01:41:02 -0400259 if (rate > 3) { /* OFDM rate */
260 if (signal > 90)
261 signal = 90;
262 else if (signal < 25)
263 signal = 25;
264 signal = 90 - signal;
265 } else { /* CCK rate */
266 if (signal > 95)
267 signal = 95;
268 else if (signal < 30)
269 signal = 30;
270 signal = 95 - signal;
271 }
272
273 rx_status.antenna = (hdr->signal >> 7) & 1;
274 rx_status.signal = 64 - min(hdr->noise, (u8)64);
275 rx_status.ssi = signal;
Johannes Berg8318d782008-01-24 19:38:38 +0100276 rx_status.rate_idx = rate;
277 rx_status.freq = dev->conf.channel->center_freq;
278 rx_status.band = dev->conf.channel->band;
Michael Wu605bebe2007-05-14 01:41:02 -0400279 rx_status.mactime = le64_to_cpu(hdr->mac_time);
Johannes Berg03bffc12007-12-04 20:33:40 +0100280 rx_status.flag |= RX_FLAG_TSFT;
Johannes Berg4150c572007-09-17 01:29:23 -0400281 if (flags & (1 << 13))
282 rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
Michael Wu605bebe2007-05-14 01:41:02 -0400283 ieee80211_rx_irqsafe(dev, skb, &rx_status);
284
285 skb = dev_alloc_skb(RTL8187_MAX_RX);
286 if (unlikely(!skb)) {
287 usb_free_urb(urb);
288 /* TODO check rx queue length and refill *somewhere* */
289 return;
290 }
291
292 info = (struct rtl8187_rx_info *)skb->cb;
293 info->urb = urb;
294 info->dev = dev;
295 urb->transfer_buffer = skb_tail_pointer(skb);
296 urb->context = skb;
297 skb_queue_tail(&priv->rx_queue, skb);
298
299 usb_submit_urb(urb, GFP_ATOMIC);
300}
301
302static int rtl8187_init_urbs(struct ieee80211_hw *dev)
303{
304 struct rtl8187_priv *priv = dev->priv;
305 struct urb *entry;
306 struct sk_buff *skb;
307 struct rtl8187_rx_info *info;
308
309 while (skb_queue_len(&priv->rx_queue) < 8) {
310 skb = __dev_alloc_skb(RTL8187_MAX_RX, GFP_KERNEL);
311 if (!skb)
312 break;
313 entry = usb_alloc_urb(0, GFP_KERNEL);
314 if (!entry) {
315 kfree_skb(skb);
316 break;
317 }
318 usb_fill_bulk_urb(entry, priv->udev,
319 usb_rcvbulkpipe(priv->udev, 1),
320 skb_tail_pointer(skb),
321 RTL8187_MAX_RX, rtl8187_rx_cb, skb);
322 info = (struct rtl8187_rx_info *)skb->cb;
323 info->urb = entry;
324 info->dev = dev;
325 skb_queue_tail(&priv->rx_queue, skb);
326 usb_submit_urb(entry, GFP_KERNEL);
327 }
328
329 return 0;
330}
331
332static int rtl8187_init_hw(struct ieee80211_hw *dev)
333{
334 struct rtl8187_priv *priv = dev->priv;
335 u8 reg;
336 int i;
337
338 /* reset */
339 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
340 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
341 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
342 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
343 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
344 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
345 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
346
347 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
348
349 msleep(200);
350 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x10);
351 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x11);
352 rtl818x_iowrite8(priv, (u8 *)0xFE18, 0x00);
353 msleep(200);
354
355 reg = rtl818x_ioread8(priv, &priv->map->CMD);
356 reg &= (1 << 1);
357 reg |= RTL818X_CMD_RESET;
358 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
359
360 i = 10;
361 do {
362 msleep(2);
363 if (!(rtl818x_ioread8(priv, &priv->map->CMD) &
364 RTL818X_CMD_RESET))
365 break;
366 } while (--i);
367
368 if (!i) {
369 printk(KERN_ERR "%s: Reset timeout!\n", wiphy_name(dev->wiphy));
370 return -ETIMEDOUT;
371 }
372
373 /* reload registers from eeprom */
374 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_LOAD);
375
376 i = 10;
377 do {
378 msleep(4);
379 if (!(rtl818x_ioread8(priv, &priv->map->EEPROM_CMD) &
380 RTL818X_EEPROM_CMD_CONFIG))
381 break;
382 } while (--i);
383
384 if (!i) {
385 printk(KERN_ERR "%s: eeprom reset timeout!\n",
386 wiphy_name(dev->wiphy));
387 return -ETIMEDOUT;
388 }
389
390 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
391 reg = rtl818x_ioread8(priv, &priv->map->CONFIG3);
392 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | RTL818X_CONFIG3_ANAPARAM_WRITE);
393 rtl818x_iowrite32(priv, &priv->map->ANAPARAM, RTL8225_ANAPARAM_ON);
394 rtl818x_iowrite32(priv, &priv->map->ANAPARAM2, RTL8225_ANAPARAM2_ON);
395 rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg & ~RTL818X_CONFIG3_ANAPARAM_WRITE);
396 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
397
398 /* setup card */
399 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
400 rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
401
402 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
403 rtl818x_iowrite8(priv, &priv->map->GPIO, 1);
404 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
405
406 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
Michael Wu605bebe2007-05-14 01:41:02 -0400407
408 rtl818x_iowrite16(priv, (__le16 *)0xFFF4, 0xFFFF);
409 reg = rtl818x_ioread8(priv, &priv->map->CONFIG1);
410 reg &= 0x3F;
411 reg |= 0x80;
412 rtl818x_iowrite8(priv, &priv->map->CONFIG1, reg);
413
414 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
415
416 rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
417 rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
418 rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
419
420 // TODO: set RESP_RATE and BRSR properly
421 rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
422 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
423
424 /* host_usb_init */
425 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0);
426 rtl818x_iowrite8(priv, &priv->map->GPIO, 0);
427 reg = rtl818x_ioread8(priv, (u8 *)0xFE53);
428 rtl818x_iowrite8(priv, (u8 *)0xFE53, reg | (1 << 7));
429 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, (4 << 8));
430 rtl818x_iowrite8(priv, &priv->map->GPIO, 0x20);
431 rtl818x_iowrite8(priv, &priv->map->GP_ENABLE, 0);
432 rtl818x_iowrite16(priv, &priv->map->RFPinsOutput, 0x80);
433 rtl818x_iowrite16(priv, &priv->map->RFPinsSelect, 0x80);
434 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x80);
435 msleep(100);
436
437 rtl818x_iowrite32(priv, &priv->map->RF_TIMING, 0x000a8008);
438 rtl818x_iowrite16(priv, &priv->map->BRSR, 0xFFFF);
439 rtl818x_iowrite32(priv, &priv->map->RF_PARA, 0x00100044);
440 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
441 rtl818x_iowrite8(priv, &priv->map->CONFIG3, 0x44);
442 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
443 rtl818x_iowrite16(priv, &priv->map->RFPinsEnable, 0x1FF7);
444 msleep(100);
445
Michael Wuf6532112007-10-14 14:43:16 -0400446 priv->rf->init(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400447
448 rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
Michael Wuf6532112007-10-14 14:43:16 -0400449 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
450 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
Michael Wu605bebe2007-05-14 01:41:02 -0400451 rtl818x_iowrite16(priv, (__le16 *)0xFFFE, 0x10);
452 rtl818x_iowrite8(priv, &priv->map->TALLY_SEL, 0x80);
453 rtl818x_iowrite8(priv, (u8 *)0xFFFF, 0x60);
Michael Wuf6532112007-10-14 14:43:16 -0400454 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400455
456 return 0;
457}
458
Johannes Berg4150c572007-09-17 01:29:23 -0400459static int rtl8187_start(struct ieee80211_hw *dev)
Michael Wu605bebe2007-05-14 01:41:02 -0400460{
461 struct rtl8187_priv *priv = dev->priv;
462 u32 reg;
463 int ret;
464
465 ret = rtl8187_init_hw(dev);
466 if (ret)
467 return ret;
468
469 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
470
Michael Wu2fe14262007-10-20 20:05:31 -0400471 rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
472 rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
473
Michael Wu605bebe2007-05-14 01:41:02 -0400474 rtl8187_init_urbs(dev);
475
476 reg = RTL818X_RX_CONF_ONLYERLPKT |
477 RTL818X_RX_CONF_RX_AUTORESETPHY |
478 RTL818X_RX_CONF_BSSID |
479 RTL818X_RX_CONF_MGMT |
Michael Wu605bebe2007-05-14 01:41:02 -0400480 RTL818X_RX_CONF_DATA |
481 (7 << 13 /* RX FIFO threshold NONE */) |
482 (7 << 10 /* MAX RX DMA */) |
483 RTL818X_RX_CONF_BROADCAST |
Michael Wu605bebe2007-05-14 01:41:02 -0400484 RTL818X_RX_CONF_NICMAC;
Michael Wu605bebe2007-05-14 01:41:02 -0400485
Johannes Berg4150c572007-09-17 01:29:23 -0400486 priv->rx_conf = reg;
Michael Wu605bebe2007-05-14 01:41:02 -0400487 rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
488
489 reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
490 reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
491 reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
492 rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
493
494 reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
495 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
496 reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
497 reg &= ~RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
498 rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
499
500 reg = RTL818X_TX_CONF_CW_MIN |
501 (7 << 21 /* MAX TX DMA */) |
502 RTL818X_TX_CONF_NO_ICV;
503 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
504
505 reg = rtl818x_ioread8(priv, &priv->map->CMD);
506 reg |= RTL818X_CMD_TX_ENABLE;
507 reg |= RTL818X_CMD_RX_ENABLE;
508 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
509
510 return 0;
511}
512
Johannes Berg4150c572007-09-17 01:29:23 -0400513static void rtl8187_stop(struct ieee80211_hw *dev)
Michael Wu605bebe2007-05-14 01:41:02 -0400514{
515 struct rtl8187_priv *priv = dev->priv;
516 struct rtl8187_rx_info *info;
517 struct sk_buff *skb;
518 u32 reg;
519
520 rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
521
522 reg = rtl818x_ioread8(priv, &priv->map->CMD);
523 reg &= ~RTL818X_CMD_TX_ENABLE;
524 reg &= ~RTL818X_CMD_RX_ENABLE;
525 rtl818x_iowrite8(priv, &priv->map->CMD, reg);
526
Michael Wuf6532112007-10-14 14:43:16 -0400527 priv->rf->stop(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400528
529 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
530 reg = rtl818x_ioread8(priv, &priv->map->CONFIG4);
531 rtl818x_iowrite8(priv, &priv->map->CONFIG4, reg | RTL818X_CONFIG4_VCOOFF);
532 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
533
534 while ((skb = skb_dequeue(&priv->rx_queue))) {
535 info = (struct rtl8187_rx_info *)skb->cb;
536 usb_kill_urb(info->urb);
537 kfree_skb(skb);
538 }
Johannes Berg4150c572007-09-17 01:29:23 -0400539 return;
Michael Wu605bebe2007-05-14 01:41:02 -0400540}
541
542static int rtl8187_add_interface(struct ieee80211_hw *dev,
543 struct ieee80211_if_init_conf *conf)
544{
545 struct rtl8187_priv *priv = dev->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400546 int i;
Michael Wu605bebe2007-05-14 01:41:02 -0400547
Johannes Berg4150c572007-09-17 01:29:23 -0400548 if (priv->mode != IEEE80211_IF_TYPE_MNTR)
549 return -EOPNOTSUPP;
Michael Wu605bebe2007-05-14 01:41:02 -0400550
551 switch (conf->type) {
552 case IEEE80211_IF_TYPE_STA:
Michael Wu605bebe2007-05-14 01:41:02 -0400553 priv->mode = conf->type;
554 break;
555 default:
556 return -EOPNOTSUPP;
557 }
558
Herton Ronaldo Krzesinskiaa979a62008-04-09 16:38:31 -0300559 priv->vif = conf->vif;
560
Johannes Berg4150c572007-09-17 01:29:23 -0400561 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
562 for (i = 0; i < ETH_ALEN; i++)
563 rtl818x_iowrite8(priv, &priv->map->MAC[i],
564 ((u8 *)conf->mac_addr)[i]);
565 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
Michael Wu605bebe2007-05-14 01:41:02 -0400566
567 return 0;
568}
569
570static void rtl8187_remove_interface(struct ieee80211_hw *dev,
571 struct ieee80211_if_init_conf *conf)
572{
573 struct rtl8187_priv *priv = dev->priv;
Johannes Berg4150c572007-09-17 01:29:23 -0400574 priv->mode = IEEE80211_IF_TYPE_MNTR;
Herton Ronaldo Krzesinskiaa979a62008-04-09 16:38:31 -0300575 priv->vif = NULL;
Michael Wu605bebe2007-05-14 01:41:02 -0400576}
577
578static int rtl8187_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
579{
580 struct rtl8187_priv *priv = dev->priv;
Michael Wuf6532112007-10-14 14:43:16 -0400581 u32 reg;
582
583 reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
584 /* Enable TX loopback on MAC level to avoid TX during channel
585 * changes, as this has be seen to causes problems and the
586 * card will stop work until next reset
587 */
588 rtl818x_iowrite32(priv, &priv->map->TX_CONF,
589 reg | RTL818X_TX_CONF_LOOPBACK_MAC);
590 msleep(10);
591 priv->rf->set_chan(dev, conf);
592 msleep(10);
593 rtl818x_iowrite32(priv, &priv->map->TX_CONF, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400594
595 rtl818x_iowrite8(priv, &priv->map->SIFS, 0x22);
596
597 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME) {
598 rtl818x_iowrite8(priv, &priv->map->SLOT, 0x9);
599 rtl818x_iowrite8(priv, &priv->map->DIFS, 0x14);
600 rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x14);
601 rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0x73);
602 } else {
603 rtl818x_iowrite8(priv, &priv->map->SLOT, 0x14);
604 rtl818x_iowrite8(priv, &priv->map->DIFS, 0x24);
605 rtl818x_iowrite8(priv, &priv->map->EIFS, 91 - 0x24);
606 rtl818x_iowrite8(priv, &priv->map->CW_VAL, 0xa5);
607 }
608
609 rtl818x_iowrite16(priv, &priv->map->ATIM_WND, 2);
610 rtl818x_iowrite16(priv, &priv->map->ATIMTR_INTERVAL, 100);
611 rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL, 100);
612 rtl818x_iowrite16(priv, &priv->map->BEACON_INTERVAL_TIME, 100);
613 return 0;
614}
615
Johannes Berg32bfd352007-12-19 01:31:26 +0100616static int rtl8187_config_interface(struct ieee80211_hw *dev,
617 struct ieee80211_vif *vif,
Michael Wu605bebe2007-05-14 01:41:02 -0400618 struct ieee80211_if_conf *conf)
619{
620 struct rtl8187_priv *priv = dev->priv;
621 int i;
622
Michael Wu605bebe2007-05-14 01:41:02 -0400623 for (i = 0; i < ETH_ALEN; i++)
624 rtl818x_iowrite8(priv, &priv->map->BSSID[i], conf->bssid[i]);
625
626 if (is_valid_ether_addr(conf->bssid))
627 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_INFRA);
628 else
629 rtl818x_iowrite8(priv, &priv->map->MSR, RTL818X_MSR_NO_LINK);
630
631 return 0;
632}
633
Johannes Berg4150c572007-09-17 01:29:23 -0400634static void rtl8187_configure_filter(struct ieee80211_hw *dev,
635 unsigned int changed_flags,
636 unsigned int *total_flags,
Michael Wu2fe14262007-10-20 20:05:31 -0400637 int mc_count, struct dev_addr_list *mclist)
Johannes Berg4150c572007-09-17 01:29:23 -0400638{
639 struct rtl8187_priv *priv = dev->priv;
640
Johannes Berg4150c572007-09-17 01:29:23 -0400641 if (changed_flags & FIF_FCSFAIL)
642 priv->rx_conf ^= RTL818X_RX_CONF_FCS;
643 if (changed_flags & FIF_CONTROL)
644 priv->rx_conf ^= RTL818X_RX_CONF_CTRL;
645 if (changed_flags & FIF_OTHER_BSS)
646 priv->rx_conf ^= RTL818X_RX_CONF_MONITOR;
Michael Wu2fe14262007-10-20 20:05:31 -0400647 if (*total_flags & FIF_ALLMULTI || mc_count > 0)
Johannes Berg4150c572007-09-17 01:29:23 -0400648 priv->rx_conf |= RTL818X_RX_CONF_MULTICAST;
Michael Wu2fe14262007-10-20 20:05:31 -0400649 else
650 priv->rx_conf &= ~RTL818X_RX_CONF_MULTICAST;
Johannes Berg4150c572007-09-17 01:29:23 -0400651
Michael Wu2fe14262007-10-20 20:05:31 -0400652 *total_flags = 0;
653
Johannes Berg4150c572007-09-17 01:29:23 -0400654 if (priv->rx_conf & RTL818X_RX_CONF_FCS)
655 *total_flags |= FIF_FCSFAIL;
656 if (priv->rx_conf & RTL818X_RX_CONF_CTRL)
657 *total_flags |= FIF_CONTROL;
658 if (priv->rx_conf & RTL818X_RX_CONF_MONITOR)
659 *total_flags |= FIF_OTHER_BSS;
Michael Wu2fe14262007-10-20 20:05:31 -0400660 if (priv->rx_conf & RTL818X_RX_CONF_MULTICAST)
661 *total_flags |= FIF_ALLMULTI;
Johannes Berg4150c572007-09-17 01:29:23 -0400662
663 rtl818x_iowrite32_async(priv, &priv->map->RX_CONF, priv->rx_conf);
664}
665
Michael Wu605bebe2007-05-14 01:41:02 -0400666static const struct ieee80211_ops rtl8187_ops = {
667 .tx = rtl8187_tx,
Johannes Berg4150c572007-09-17 01:29:23 -0400668 .start = rtl8187_start,
Michael Wu605bebe2007-05-14 01:41:02 -0400669 .stop = rtl8187_stop,
670 .add_interface = rtl8187_add_interface,
671 .remove_interface = rtl8187_remove_interface,
672 .config = rtl8187_config,
673 .config_interface = rtl8187_config_interface,
Johannes Berg4150c572007-09-17 01:29:23 -0400674 .configure_filter = rtl8187_configure_filter,
Michael Wu605bebe2007-05-14 01:41:02 -0400675};
676
677static void rtl8187_eeprom_register_read(struct eeprom_93cx6 *eeprom)
678{
679 struct ieee80211_hw *dev = eeprom->data;
680 struct rtl8187_priv *priv = dev->priv;
681 u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
682
683 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
684 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
685 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
686 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
687}
688
689static void rtl8187_eeprom_register_write(struct eeprom_93cx6 *eeprom)
690{
691 struct ieee80211_hw *dev = eeprom->data;
692 struct rtl8187_priv *priv = dev->priv;
693 u8 reg = RTL818X_EEPROM_CMD_PROGRAM;
694
695 if (eeprom->reg_data_in)
696 reg |= RTL818X_EEPROM_CMD_WRITE;
697 if (eeprom->reg_data_out)
698 reg |= RTL818X_EEPROM_CMD_READ;
699 if (eeprom->reg_data_clock)
700 reg |= RTL818X_EEPROM_CMD_CK;
701 if (eeprom->reg_chip_select)
702 reg |= RTL818X_EEPROM_CMD_CS;
703
704 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, reg);
705 udelay(10);
706}
707
708static int __devinit rtl8187_probe(struct usb_interface *intf,
709 const struct usb_device_id *id)
710{
711 struct usb_device *udev = interface_to_usbdev(intf);
712 struct ieee80211_hw *dev;
713 struct rtl8187_priv *priv;
714 struct eeprom_93cx6 eeprom;
715 struct ieee80211_channel *channel;
716 u16 txpwr, reg;
717 int err, i;
Joe Perches0795af52007-10-03 17:59:30 -0700718 DECLARE_MAC_BUF(mac);
Michael Wu605bebe2007-05-14 01:41:02 -0400719
720 dev = ieee80211_alloc_hw(sizeof(*priv), &rtl8187_ops);
721 if (!dev) {
722 printk(KERN_ERR "rtl8187: ieee80211 alloc failed\n");
723 return -ENOMEM;
724 }
725
726 priv = dev->priv;
727
728 SET_IEEE80211_DEV(dev, &intf->dev);
729 usb_set_intfdata(intf, dev);
730 priv->udev = udev;
731
732 usb_get_dev(udev);
733
734 skb_queue_head_init(&priv->rx_queue);
Johannes Berg8318d782008-01-24 19:38:38 +0100735
736 BUILD_BUG_ON(sizeof(priv->channels) != sizeof(rtl818x_channels));
737 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(rtl818x_rates));
738
Michael Wu605bebe2007-05-14 01:41:02 -0400739 memcpy(priv->channels, rtl818x_channels, sizeof(rtl818x_channels));
740 memcpy(priv->rates, rtl818x_rates, sizeof(rtl818x_rates));
741 priv->map = (struct rtl818x_csr *)0xFF00;
Johannes Berg8318d782008-01-24 19:38:38 +0100742
743 priv->band.band = IEEE80211_BAND_2GHZ;
744 priv->band.channels = priv->channels;
745 priv->band.n_channels = ARRAY_SIZE(rtl818x_channels);
746 priv->band.bitrates = priv->rates;
747 priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
748 dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
749
750
Johannes Berg4150c572007-09-17 01:29:23 -0400751 priv->mode = IEEE80211_IF_TYPE_MNTR;
Michael Wu605bebe2007-05-14 01:41:02 -0400752 dev->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Johannes Berg7848ba72007-09-14 11:10:25 -0400753 IEEE80211_HW_RX_INCLUDES_FCS;
Michael Wu605bebe2007-05-14 01:41:02 -0400754 dev->extra_tx_headroom = sizeof(struct rtl8187_tx_hdr);
755 dev->queues = 1;
756 dev->max_rssi = 65;
757 dev->max_signal = 64;
758
Michael Wu605bebe2007-05-14 01:41:02 -0400759 eeprom.data = dev;
760 eeprom.register_read = rtl8187_eeprom_register_read;
761 eeprom.register_write = rtl8187_eeprom_register_write;
762 if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
763 eeprom.width = PCI_EEPROM_WIDTH_93C66;
764 else
765 eeprom.width = PCI_EEPROM_WIDTH_93C46;
766
767 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
768 udelay(10);
769
770 eeprom_93cx6_multiread(&eeprom, RTL8187_EEPROM_MAC_ADDR,
771 (__le16 __force *)dev->wiphy->perm_addr, 3);
772 if (!is_valid_ether_addr(dev->wiphy->perm_addr)) {
773 printk(KERN_WARNING "rtl8187: Invalid hwaddr! Using randomly "
774 "generated MAC address\n");
775 random_ether_addr(dev->wiphy->perm_addr);
776 }
777
778 channel = priv->channels;
779 for (i = 0; i < 3; i++) {
780 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
781 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100782 (*channel++).hw_value = txpwr & 0xFF;
783 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400784 }
785 for (i = 0; i < 2; i++) {
786 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
787 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100788 (*channel++).hw_value = txpwr & 0xFF;
789 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400790 }
791 for (i = 0; i < 2; i++) {
792 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6 + i,
793 &txpwr);
Johannes Berg8318d782008-01-24 19:38:38 +0100794 (*channel++).hw_value = txpwr & 0xFF;
795 (*channel++).hw_value = txpwr >> 8;
Michael Wu605bebe2007-05-14 01:41:02 -0400796 }
797
798 eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_BASE,
799 &priv->txpwr_base);
800
Michael Wuf6532112007-10-14 14:43:16 -0400801 reg = rtl818x_ioread8(priv, &priv->map->PGSELECT) & ~1;
802 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg | 1);
Michael Wu605bebe2007-05-14 01:41:02 -0400803 /* 0 means asic B-cut, we should use SW 3 wire
804 * bit-by-bit banging for radio. 1 means we can use
805 * USB specific request to write radio registers */
806 priv->asic_rev = rtl818x_ioread8(priv, (u8 *)0xFFFE) & 0x3;
Michael Wuf6532112007-10-14 14:43:16 -0400807 rtl818x_iowrite8(priv, &priv->map->PGSELECT, reg);
Michael Wu605bebe2007-05-14 01:41:02 -0400808 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
809
Michael Wuf6532112007-10-14 14:43:16 -0400810 priv->rf = rtl8187_detect_rf(dev);
Michael Wu605bebe2007-05-14 01:41:02 -0400811
812 err = ieee80211_register_hw(dev);
813 if (err) {
814 printk(KERN_ERR "rtl8187: Cannot register device\n");
815 goto err_free_dev;
816 }
817
Joe Perches0795af52007-10-03 17:59:30 -0700818 printk(KERN_INFO "%s: hwaddr %s, rtl8187 V%d + %s\n",
819 wiphy_name(dev->wiphy), print_mac(mac, dev->wiphy->perm_addr),
Michael Wuf6532112007-10-14 14:43:16 -0400820 priv->asic_rev, priv->rf->name);
Michael Wu605bebe2007-05-14 01:41:02 -0400821
822 return 0;
823
824 err_free_dev:
825 ieee80211_free_hw(dev);
826 usb_set_intfdata(intf, NULL);
827 usb_put_dev(udev);
828 return err;
829}
830
831static void __devexit rtl8187_disconnect(struct usb_interface *intf)
832{
833 struct ieee80211_hw *dev = usb_get_intfdata(intf);
834 struct rtl8187_priv *priv;
835
836 if (!dev)
837 return;
838
839 ieee80211_unregister_hw(dev);
840
841 priv = dev->priv;
842 usb_put_dev(interface_to_usbdev(intf));
843 ieee80211_free_hw(dev);
844}
845
846static struct usb_driver rtl8187_driver = {
847 .name = KBUILD_MODNAME,
848 .id_table = rtl8187_table,
849 .probe = rtl8187_probe,
850 .disconnect = rtl8187_disconnect,
851};
852
853static int __init rtl8187_init(void)
854{
855 return usb_register(&rtl8187_driver);
856}
857
858static void __exit rtl8187_exit(void)
859{
860 usb_deregister(&rtl8187_driver);
861}
862
863module_init(rtl8187_init);
864module_exit(rtl8187_exit);