1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>
25 * Larry Finger <Larry.Finger@lwfinger.net>
27 ******************************************************************************/
31 #include <linux/usb.h>
32 #include <linux/module.h>
33 #include <linux/firmware.h>
35 #include "osdep_service.h"
36 #include "drv_types.h"
37 #include "recv_osdep.h"
38 #include "xmit_osdep.h"
39 #include "rtl8712_efuse.h"
41 #include "usb_osintf.h"
43 static struct usb_interface *pintf;
45 static int r871xu_drv_init(struct usb_interface *pusb_intf,
46 const struct usb_device_id *pdid);
48 static void r871xu_dev_remove(struct usb_interface *pusb_intf);
50 static struct usb_device_id rtl871x_usb_id_tbl[] = {
54 {USB_DEVICE(0x0BDA, 0x8171)},
55 {USB_DEVICE(0x0bda, 0x8173)},
56 {USB_DEVICE(0x0bda, 0x8712)},
57 {USB_DEVICE(0x0bda, 0x8713)},
58 {USB_DEVICE(0x0bda, 0xC512)},
60 {USB_DEVICE(0x07B8, 0x8188)},
62 {USB_DEVICE(0x0B05, 0x1786)},
63 {USB_DEVICE(0x0B05, 0x1791)}, /* 11n mode disable */
65 {USB_DEVICE(0x050D, 0x945A)},
67 {USB_DEVICE(0x07AA, 0x0047)},
69 {USB_DEVICE(0x2001, 0x3306)},
70 {USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
72 {USB_DEVICE(0x7392, 0x7611)},
74 {USB_DEVICE(0x1740, 0x9603)},
76 {USB_DEVICE(0x0E66, 0x0016)},
78 {USB_DEVICE(0x06F8, 0xE034)},
79 {USB_DEVICE(0x06F8, 0xE032)},
81 {USB_DEVICE(0x0789, 0x0167)},
83 {USB_DEVICE(0x2019, 0xAB28)},
84 {USB_DEVICE(0x2019, 0xED16)},
86 {USB_DEVICE(0x0DF6, 0x0057)},
87 {USB_DEVICE(0x0DF6, 0x0045)},
88 {USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
89 {USB_DEVICE(0x0DF6, 0x004B)},
90 {USB_DEVICE(0x0DF6, 0x005B)},
91 {USB_DEVICE(0x0DF6, 0x005D)},
92 {USB_DEVICE(0x0DF6, 0x0063)},
94 {USB_DEVICE(0x177F, 0x0154)},
96 {USB_DEVICE(0x0BDA, 0x5077)},
98 {USB_DEVICE(0x1690, 0x0752)},
100 {USB_DEVICE(0x20F4, 0x646B)},
101 {USB_DEVICE(0x083A, 0xC512)},
102 {USB_DEVICE(0x25D4, 0x4CA1)},
103 {USB_DEVICE(0x25D4, 0x4CAB)},
107 {USB_DEVICE(0x0BDA, 0x8172)},
108 {USB_DEVICE(0x0BDA, 0x8192)},
110 {USB_DEVICE(0x0EB0, 0x9061)},
112 {USB_DEVICE(0x13D3, 0x3323)},
113 {USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
114 {USB_DEVICE(0x13D3, 0x3342)},
116 {USB_DEVICE(0x13D3, 0x3333)},
117 {USB_DEVICE(0x13D3, 0x3334)},
118 {USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
119 {USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
121 {USB_DEVICE(0x13D3, 0x3309)},
123 {USB_DEVICE(0x050D, 0x815F)},
125 {USB_DEVICE(0x07D1, 0x3302)},
126 {USB_DEVICE(0x07D1, 0x3300)},
127 {USB_DEVICE(0x07D1, 0x3303)},
129 {USB_DEVICE(0x7392, 0x7612)},
131 {USB_DEVICE(0x1740, 0x9605)},
133 {USB_DEVICE(0x06F8, 0xE031)},
135 {USB_DEVICE(0x0E66, 0x0015)},
137 {USB_DEVICE(0x13D3, 0x3306)},
139 {USB_DEVICE(0x2019, 0xED18)},
140 {USB_DEVICE(0x2019, 0x4901)},
142 {USB_DEVICE(0x0DF6, 0x0058)},
143 {USB_DEVICE(0x0DF6, 0x0049)},
144 {USB_DEVICE(0x0DF6, 0x004C)},
145 {USB_DEVICE(0x0DF6, 0x0064)},
147 {USB_DEVICE(0x14b2, 0x3300)},
148 {USB_DEVICE(0x14b2, 0x3301)},
149 {USB_DEVICE(0x14B2, 0x3302)},
151 {USB_DEVICE(0x04F2, 0xAFF2)},
152 {USB_DEVICE(0x04F2, 0xAFF5)},
153 {USB_DEVICE(0x04F2, 0xAFF6)},
154 {USB_DEVICE(0x13D3, 0x3339)},
155 {USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
156 {USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
157 {USB_DEVICE(0x13D3, 0x3310)},
158 {USB_DEVICE(0x13D3, 0x3325)},
162 {USB_DEVICE(0x0BDA, 0x8174)},
164 {USB_DEVICE(0x050D, 0x845A)},
166 {USB_DEVICE(0x07AA, 0x0051)},
168 {USB_DEVICE(0x7392, 0x7622)},
170 {USB_DEVICE(0x0409, 0x02B6)},
174 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
176 static struct specific_device_id specific_device_id_tbl[] = {
177 {.idVendor = 0x0b05, .idProduct = 0x1791,
178 .flags = SPEC_DEV_ID_DISABLE_HT},
179 {.idVendor = 0x0df6, .idProduct = 0x0059,
180 .flags = SPEC_DEV_ID_DISABLE_HT},
181 {.idVendor = 0x13d3, .idProduct = 0x3306,
182 .flags = SPEC_DEV_ID_DISABLE_HT},
183 {.idVendor = 0x13D3, .idProduct = 0x3311,
184 .flags = SPEC_DEV_ID_DISABLE_HT},
185 {.idVendor = 0x13d3, .idProduct = 0x3335,
186 .flags = SPEC_DEV_ID_DISABLE_HT},
187 {.idVendor = 0x13d3, .idProduct = 0x3336,
188 .flags = SPEC_DEV_ID_DISABLE_HT},
189 {.idVendor = 0x13d3, .idProduct = 0x3340,
190 .flags = SPEC_DEV_ID_DISABLE_HT},
191 {.idVendor = 0x13d3, .idProduct = 0x3341,
192 .flags = SPEC_DEV_ID_DISABLE_HT},
197 struct usb_driver r871xu_drv;
202 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
204 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
206 printk(KERN_INFO "r8712: suspending...\n");
207 if (!pnetdev || !netif_running(pnetdev)) {
208 printk(KERN_INFO "r8712: unable to suspend\n");
211 if (pnetdev->netdev_ops->ndo_stop)
212 pnetdev->netdev_ops->ndo_stop(pnetdev);
214 netif_device_detach(pnetdev);
218 static int r871x_resume(struct usb_interface *pusb_intf)
220 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
222 printk(KERN_INFO "r8712: resuming...\n");
223 if (!pnetdev || !netif_running(pnetdev)) {
224 printk(KERN_INFO "r8712: unable to resume\n");
227 netif_device_attach(pnetdev);
228 if (pnetdev->netdev_ops->ndo_open)
229 pnetdev->netdev_ops->ndo_open(pnetdev);
233 static int r871x_reset_resume(struct usb_interface *pusb_intf)
241 static struct drv_priv drvpriv = {
242 .r871xu_drv.name = "r8712u",
243 .r871xu_drv.id_table = rtl871x_usb_id_tbl,
244 .r871xu_drv.probe = r871xu_drv_init,
245 .r871xu_drv.disconnect = r871xu_dev_remove,
247 .r871xu_drv.suspend = r871x_suspend,
248 .r871xu_drv.resume = r871x_resume,
249 .r871xu_drv.reset_resume = r871x_reset_resume,
253 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
255 uint status = _SUCCESS;
256 struct usb_device_descriptor *pdev_desc;
257 struct usb_host_config *phost_conf;
258 struct usb_config_descriptor *pconf_desc;
259 struct usb_host_interface *phost_iface;
260 struct usb_interface_descriptor *piface_desc;
261 struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
262 struct usb_device *pusbd = pdvobjpriv->pusbdev;
264 pdvobjpriv->padapter = padapter;
265 padapter->EepromAddressSize = 6;
266 pdev_desc = &pusbd->descriptor;
267 phost_conf = pusbd->actconfig;
268 pconf_desc = &phost_conf->desc;
269 phost_iface = &pintf->altsetting[0];
270 piface_desc = &phost_iface->desc;
271 pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
272 if (pusbd->speed == USB_SPEED_HIGH) {
273 pdvobjpriv->ishighspeed = true;
274 printk(KERN_INFO "r8712u: USB_SPEED_HIGH with %d endpoints\n",
275 pdvobjpriv->nr_endpoint);
277 pdvobjpriv->ishighspeed = false;
278 printk(KERN_INFO "r8712u: USB_SPEED_LOW with %d endpoints\n",
279 pdvobjpriv->nr_endpoint);
281 if ((r8712_alloc_io_queue(padapter)) == _FAIL)
286 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
290 void rtl871x_intf_stop(struct _adapter *padapter)
292 /*disable_hw_interrupt*/
293 if (padapter->bSurpriseRemoved == false) {
294 /*device still exists, so driver can do i/o operation
299 if (padapter->dvobjpriv.inirp_deinit != NULL)
300 padapter->dvobjpriv.inirp_deinit(padapter);
302 r8712_usb_write_port_cancel(padapter);
303 /* TODO:cancel other irps */
306 void r871x_dev_unload(struct _adapter *padapter)
308 if (padapter->bup == true) {
310 padapter->bDriverStopped = true;
313 rtl871x_intf_stop(padapter);
316 r8712_stop_drv_threads(padapter);
319 if (padapter->bSurpriseRemoved == false) {
320 padapter->hw_init_completed = false;
321 rtl8712_hal_deinit(padapter);
325 if (padapter->dvobj_deinit)
326 padapter->dvobj_deinit(padapter);
327 padapter->bup = false;
331 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
332 struct _adapter *padapter)
337 int num = sizeof(specific_device_id_tbl) /
338 sizeof(struct specific_device_id);
340 for (i = 0; i < num; i++) {
341 vid = specific_device_id_tbl[i].idVendor;
342 pid = specific_device_id_tbl[i].idProduct;
343 flags = specific_device_id_tbl[i].flags;
345 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
346 (flags&SPEC_DEV_ID_DISABLE_HT)) {
347 padapter->registrypriv.ht_enable = 0;
348 padapter->registrypriv.cbw40_enable = 0;
349 padapter->registrypriv.ampdu_enable = 0;
354 static u8 key_2char2num(u8 hch, u8 lch)
356 return (hex_to_bin(hch) << 4) | hex_to_bin(lch);
360 * drv_init() - a device potentially for us
362 * notes: drv_init() is called when the bus driver has located a card for us
363 * to support. We accept the new device by returning 0.
365 static int r871xu_drv_init(struct usb_interface *pusb_intf,
366 const struct usb_device_id *pdid)
369 struct _adapter *padapter = NULL;
370 struct dvobj_priv *pdvobjpriv;
371 struct net_device *pnetdev;
372 struct usb_device *udev;
374 printk(KERN_INFO "r8712u: Staging version\n");
375 /* In this probe function, O.S. will provide the usb interface pointer
376 * to driver. We have to increase the reference count of the usb device
377 * structure by using the usb_get_dev function.
379 udev = interface_to_usbdev(pusb_intf);
383 pnetdev = r8712_init_netdev();
386 padapter = netdev_priv(pnetdev);
387 disable_ht_for_spec_devid(pdid, padapter);
388 pdvobjpriv = &padapter->dvobjpriv;
389 pdvobjpriv->padapter = padapter;
390 padapter->dvobjpriv.pusbdev = udev;
391 padapter->pusb_intf = pusb_intf;
392 usb_set_intfdata(pusb_intf, pnetdev);
393 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
395 padapter->dvobj_init = &r8712_usb_dvobj_init;
396 padapter->dvobj_deinit = &r8712_usb_dvobj_deinit;
397 padapter->halpriv.hal_bus_init = &r8712_usb_hal_bus_init;
398 padapter->dvobjpriv.inirp_init = &r8712_usb_inirp_init;
399 padapter->dvobjpriv.inirp_deinit = &r8712_usb_inirp_deinit;
401 * initialize the dvobj_priv
403 if (padapter->dvobj_init == NULL)
406 status = padapter->dvobj_init(padapter);
407 if (status != _SUCCESS)
411 status = r8712_init_drv_sw(padapter);
414 /* step 5. read efuse/eeprom data and get mac_addr */
418 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
419 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
421 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
423 /* To check system boot selection.*/
424 printk(KERN_INFO "r8712u: Boot from %s: Autoload %s\n",
425 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
426 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
428 /* To check autoload success or not.*/
429 if (tmpU1b & _EEPROM_EN) {
431 /* The following operations prevent Efuse leakage by
434 tmpU1b = r8712_read8(padapter, EFUSE_TEST+3);
435 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
437 r8712_write8(padapter, EFUSE_TEST + 3,
438 (tmpU1b & (~BIT(7))));
440 /* Retrieve Chip version.
441 * Recognize IC version by Reg0x4 BIT15.
443 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
446 padapter->registrypriv.chip_version =
449 padapter->registrypriv.chip_version =
451 switch (padapter->registrypriv.chip_version) {
457 padapter->registrypriv.chip_version =
462 for (i = 0, offset = 0; i < 128; i += 8, offset++)
463 r8712_efuse_pg_packet_read(padapter, offset,
467 /* Users specify the mac address */
470 for (jj = 0, kk = 0; jj < ETH_ALEN;
473 key_2char2num(r8712_initmac[kk],
474 r8712_initmac[kk + 1]);
476 /* Use the mac address stored in the Efuse
477 * offset = 0x12 for usb in efuse
479 memcpy(mac, &pdata[0x12], ETH_ALEN);
481 eeprom_CustomerID = pdata[0x52];
482 switch (eeprom_CustomerID) {
483 case EEPROM_CID_ALPHA:
484 padapter->eeprompriv.CustomerID =
487 case EEPROM_CID_CAMEO:
488 padapter->eeprompriv.CustomerID =
491 case EEPROM_CID_SITECOM:
492 padapter->eeprompriv.CustomerID =
495 case EEPROM_CID_COREGA:
496 padapter->eeprompriv.CustomerID =
499 case EEPROM_CID_Senao:
500 padapter->eeprompriv.CustomerID =
503 case EEPROM_CID_EDIMAX_BELKIN:
504 padapter->eeprompriv.CustomerID =
505 RT_CID_819x_Edimax_Belkin;
507 case EEPROM_CID_SERCOMM_BELKIN:
508 padapter->eeprompriv.CustomerID =
509 RT_CID_819x_Sercomm_Belkin;
511 case EEPROM_CID_WNC_COREGA:
512 padapter->eeprompriv.CustomerID =
513 RT_CID_819x_WNC_COREGA;
515 case EEPROM_CID_WHQL:
517 case EEPROM_CID_NetCore:
518 padapter->eeprompriv.CustomerID =
521 case EEPROM_CID_CAMEO1:
522 padapter->eeprompriv.CustomerID =
525 case EEPROM_CID_CLEVO:
526 padapter->eeprompriv.CustomerID =
530 padapter->eeprompriv.CustomerID =
534 printk(KERN_INFO "r8712u: CustomerID = 0x%.4x\n",
535 padapter->eeprompriv.CustomerID);
537 switch (padapter->eeprompriv.CustomerID) {
539 case RT_CID_819x_ALPHA:
540 case RT_CID_819x_CAMEO:
541 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
542 padapter->ledpriv.bRegUseLed = true;
544 case RT_CID_819x_Sitecom:
545 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
546 padapter->ledpriv.bRegUseLed = true;
549 case RT_CID_819x_Senao:
550 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
551 padapter->ledpriv.bRegUseLed = true;
553 case RT_CID_819x_Edimax_Belkin:
554 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
555 padapter->ledpriv.bRegUseLed = true;
557 case RT_CID_819x_Sercomm_Belkin:
558 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
559 padapter->ledpriv.bRegUseLed = true;
561 case RT_CID_819x_WNC_COREGA:
562 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
563 padapter->ledpriv.bRegUseLed = true;
566 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
567 padapter->ledpriv.bRegUseLed = false;
571 AutoloadFail = false;
572 if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
573 (mac[2] == 0xff) && (mac[3] == 0xff) &&
574 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
575 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
576 (mac[2] == 0x00) && (mac[3] == 0x00) &&
577 (mac[4] == 0x00) && (mac[5] == 0x00)) ||
578 (AutoloadFail == false)) {
587 /* Make sure the user did not select a multicast
588 * address by setting bit 1 of first octet.
591 printk(KERN_INFO "r8712u: MAC Address from user = "
594 printk(KERN_INFO "r8712u: MAC Address from efuse = "
596 memcpy(pnetdev->dev_addr, mac, ETH_ALEN);
598 /* step 6. Load the firmware asynchronously */
599 if (rtl871x_load_fw(padapter))
601 spin_lock_init(&padapter->lockRxFF0Filter);
602 mutex_init(&padapter->mutex_start);
606 usb_set_intfdata(pusb_intf, NULL);
607 if (padapter->dvobj_deinit != NULL)
608 padapter->dvobj_deinit(padapter);
610 free_netdev(pnetdev);
614 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
615 * => how to recognize both */
616 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
618 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
619 struct _adapter *padapter = netdev_priv(pnetdev);
620 struct usb_device *udev = interface_to_usbdev(pusb_intf);
622 usb_set_intfdata(pusb_intf, NULL);
623 if (padapter->fw_found)
624 release_firmware(padapter->fw);
625 /* never exit with a firmware callback pending */
626 wait_for_completion(&padapter->rtl8712_fw_ready);
627 if (drvpriv.drv_registered == true)
628 padapter->bSurpriseRemoved = true;
629 if (pnetdev != NULL) {
630 /* will call netdev_close() */
631 unregister_netdev(pnetdev);
633 flush_scheduled_work();
635 /*Stop driver mlme relation timer */
636 if (padapter->fw_found)
637 r8712_stop_drv_timers(padapter);
638 r871x_dev_unload(padapter);
639 r8712_free_drv_sw(padapter);
640 usb_set_intfdata(pusb_intf, NULL);
641 /* decrease the reference count of the usb device structure
644 /* If we didn't unplug usb dongle and remove/insert module, driver
645 * fails on sitesurvey for the first time when device is up.
646 * Reset usb port for sitesurvey fail issue. */
647 if (udev->state != USB_STATE_NOTATTACHED)
648 usb_reset_device(udev);
652 static int __init r8712u_drv_entry(void)
654 drvpriv.drv_registered = true;
655 return usb_register(&drvpriv.r871xu_drv);
658 static void __exit r8712u_drv_halt(void)
660 drvpriv.drv_registered = false;
661 usb_deregister(&drvpriv.r871xu_drv);
662 printk(KERN_INFO "r8712u: Driver unloaded\n");
665 module_init(r8712u_drv_entry);
666 module_exit(r8712u_drv_halt);