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"
40 #include "usb_vendor_req.h"
42 #include "usb_osintf.h"
44 #define DRVER "v7_0.20100831"
46 static struct usb_interface *pintf;
48 static int r871xu_drv_init(struct usb_interface *pusb_intf,
49 const struct usb_device_id *pdid);
51 static void r871xu_dev_remove(struct usb_interface *pusb_intf);
53 static struct usb_device_id rtl871x_usb_id_tbl[] = {
57 {USB_DEVICE(0x0BDA, 0x8171)},
58 {USB_DEVICE(0x0bda, 0x8173)},
59 {USB_DEVICE(0x0bda, 0x8712)},
60 {USB_DEVICE(0x0bda, 0x8713)},
61 {USB_DEVICE(0x0bda, 0xC512)},
63 {USB_DEVICE(0x07B8, 0x8188)},
65 {USB_DEVICE(0x0B05, 0x1786)},
66 {USB_DEVICE(0x0B05, 0x1791)}, /* 11n mode disable */
68 {USB_DEVICE(0x050D, 0x945A)},
69 /* ISY IWL - Belkin clone */
70 {USB_DEVICE(0x050D, 0x11F1)},
72 {USB_DEVICE(0x07AA, 0x0047)},
74 {USB_DEVICE(0x2001, 0x3306)},
75 {USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
77 {USB_DEVICE(0x7392, 0x7611)},
79 {USB_DEVICE(0x1740, 0x9603)},
81 {USB_DEVICE(0x0E66, 0x0016)},
83 {USB_DEVICE(0x06F8, 0xE034)},
84 {USB_DEVICE(0x06F8, 0xE032)},
86 {USB_DEVICE(0x0789, 0x0167)},
88 {USB_DEVICE(0x2019, 0xAB28)},
89 {USB_DEVICE(0x2019, 0xED16)},
91 {USB_DEVICE(0x0DF6, 0x0057)},
92 {USB_DEVICE(0x0DF6, 0x0045)},
93 {USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
94 {USB_DEVICE(0x0DF6, 0x004B)},
95 {USB_DEVICE(0x0DF6, 0x005B)},
96 {USB_DEVICE(0x0DF6, 0x005D)},
97 {USB_DEVICE(0x0DF6, 0x0063)},
99 {USB_DEVICE(0x177F, 0x0154)},
101 {USB_DEVICE(0x0BDA, 0x5077)},
103 {USB_DEVICE(0x1690, 0x0752)},
105 {USB_DEVICE(0x20F4, 0x646B)},
106 {USB_DEVICE(0x083A, 0xC512)},
107 {USB_DEVICE(0x25D4, 0x4CA1)},
108 {USB_DEVICE(0x25D4, 0x4CAB)},
112 {USB_DEVICE(0x0BDA, 0x8172)},
114 {USB_DEVICE(0x0EB0, 0x9061)},
116 {USB_DEVICE(0x0BDA, 0x8172)},
117 {USB_DEVICE(0x13D3, 0x3323)},
118 {USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
119 {USB_DEVICE(0x13D3, 0x3342)},
121 {USB_DEVICE(0x13D3, 0x3333)},
122 {USB_DEVICE(0x13D3, 0x3334)},
123 {USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
124 {USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
126 {USB_DEVICE(0x13D3, 0x3309)},
128 {USB_DEVICE(0x050D, 0x815F)},
130 {USB_DEVICE(0x07D1, 0x3302)},
131 {USB_DEVICE(0x07D1, 0x3300)},
132 {USB_DEVICE(0x07D1, 0x3303)},
134 {USB_DEVICE(0x7392, 0x7612)},
136 {USB_DEVICE(0x1740, 0x9605)},
138 {USB_DEVICE(0x06F8, 0xE031)},
140 {USB_DEVICE(0x0E66, 0x0015)},
142 {USB_DEVICE(0x13D3, 0x3306)},
144 {USB_DEVICE(0x2019, 0xED18)},
145 {USB_DEVICE(0x2019, 0x4901)},
147 {USB_DEVICE(0x0DF6, 0x0058)},
148 {USB_DEVICE(0x0DF6, 0x0049)},
149 {USB_DEVICE(0x0DF6, 0x004C)},
150 {USB_DEVICE(0x0DF6, 0x006C)},
151 {USB_DEVICE(0x0DF6, 0x0064)},
153 {USB_DEVICE(0x14b2, 0x3300)},
154 {USB_DEVICE(0x14b2, 0x3301)},
155 {USB_DEVICE(0x14B2, 0x3302)},
157 {USB_DEVICE(0x04F2, 0xAFF2)},
158 {USB_DEVICE(0x04F2, 0xAFF5)},
159 {USB_DEVICE(0x04F2, 0xAFF6)},
160 {USB_DEVICE(0x13D3, 0x3339)},
161 {USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
162 {USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
163 {USB_DEVICE(0x13D3, 0x3310)},
164 {USB_DEVICE(0x13D3, 0x3325)},
168 {USB_DEVICE(0x0BDA, 0x8174)},
169 {USB_DEVICE(0x0BDA, 0x8174)},
171 {USB_DEVICE(0x050D, 0x845A)},
173 {USB_DEVICE(0x07AA, 0x0051)},
175 {USB_DEVICE(0x7392, 0x7622)},
177 {USB_DEVICE(0x0409, 0x02B6)},
181 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
183 static struct specific_device_id specific_device_id_tbl[] = {
184 {.idVendor = 0x0b05, .idProduct = 0x1791,
185 .flags = SPEC_DEV_ID_DISABLE_HT},
186 {.idVendor = 0x0df6, .idProduct = 0x0059,
187 .flags = SPEC_DEV_ID_DISABLE_HT},
188 {.idVendor = 0x13d3, .idProduct = 0x3306,
189 .flags = SPEC_DEV_ID_DISABLE_HT},
190 {.idVendor = 0x13D3, .idProduct = 0x3311,
191 .flags = SPEC_DEV_ID_DISABLE_HT},
192 {.idVendor = 0x13d3, .idProduct = 0x3335,
193 .flags = SPEC_DEV_ID_DISABLE_HT},
194 {.idVendor = 0x13d3, .idProduct = 0x3336,
195 .flags = SPEC_DEV_ID_DISABLE_HT},
196 {.idVendor = 0x13d3, .idProduct = 0x3340,
197 .flags = SPEC_DEV_ID_DISABLE_HT},
198 {.idVendor = 0x13d3, .idProduct = 0x3341,
199 .flags = SPEC_DEV_ID_DISABLE_HT},
204 struct usb_driver r871xu_drv;
209 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
211 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
213 printk(KERN_INFO "r8712: suspending...\n");
214 if (!pnetdev || !netif_running(pnetdev)) {
215 printk(KERN_INFO "r8712: unable to suspend\n");
218 if (pnetdev->netdev_ops->ndo_stop)
219 pnetdev->netdev_ops->ndo_stop(pnetdev);
221 netif_device_detach(pnetdev);
225 static int r871x_resume(struct usb_interface *pusb_intf)
227 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
229 printk(KERN_INFO "r8712: resuming...\n");
230 if (!pnetdev || !netif_running(pnetdev)) {
231 printk(KERN_INFO "r8712: unable to resume\n");
234 netif_device_attach(pnetdev);
235 if (pnetdev->netdev_ops->ndo_open)
236 pnetdev->netdev_ops->ndo_open(pnetdev);
240 static int r871x_reset_resume(struct usb_interface *pusb_intf)
248 static struct drv_priv drvpriv = {
249 .r871xu_drv.name = "r8712u",
250 .r871xu_drv.id_table = rtl871x_usb_id_tbl,
251 .r871xu_drv.probe = r871xu_drv_init,
252 .r871xu_drv.disconnect = r871xu_dev_remove,
254 .r871xu_drv.suspend = r871x_suspend,
255 .r871xu_drv.resume = r871x_resume,
256 .r871xu_drv.reset_resume = r871x_reset_resume,
260 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
262 uint status = _SUCCESS;
263 struct usb_device_descriptor *pdev_desc;
264 struct usb_host_config *phost_conf;
265 struct usb_config_descriptor *pconf_desc;
266 struct usb_host_interface *phost_iface;
267 struct usb_interface_descriptor *piface_desc;
268 struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
269 struct usb_device *pusbd = pdvobjpriv->pusbdev;
271 pdvobjpriv->padapter = padapter;
272 padapter->EepromAddressSize = 6;
273 pdev_desc = &pusbd->descriptor;
274 phost_conf = pusbd->actconfig;
275 pconf_desc = &phost_conf->desc;
276 phost_iface = &pintf->altsetting[0];
277 piface_desc = &phost_iface->desc;
278 pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
279 if (pusbd->speed == USB_SPEED_HIGH) {
280 pdvobjpriv->ishighspeed = true;
281 printk(KERN_INFO "r8712u: USB_SPEED_HIGH with %d endpoints\n",
282 pdvobjpriv->nr_endpoint);
284 pdvobjpriv->ishighspeed = false;
285 printk(KERN_INFO "r8712u: USB_SPEED_LOW with %d endpoints\n",
286 pdvobjpriv->nr_endpoint);
288 if ((r8712_alloc_io_queue(padapter)) == _FAIL)
290 sema_init(&(padapter->dvobjpriv.usb_suspend_sema), 0);
294 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
298 void rtl871x_intf_stop(struct _adapter *padapter)
300 /*disable_hw_interrupt*/
301 if (padapter->bSurpriseRemoved == false) {
302 /*device still exists, so driver can do i/o operation
307 if (padapter->dvobjpriv.inirp_deinit != NULL)
308 padapter->dvobjpriv.inirp_deinit(padapter);
310 r8712_usb_write_port_cancel(padapter);
311 /* TODO:cancel other irps */
314 void r871x_dev_unload(struct _adapter *padapter)
316 if (padapter->bup == true) {
318 padapter->bDriverStopped = true;
321 rtl871x_intf_stop(padapter);
324 r8712_stop_drv_threads(padapter);
327 if (padapter->bSurpriseRemoved == false) {
328 padapter->hw_init_completed = false;
329 rtl8712_hal_deinit(padapter);
333 if (padapter->dvobj_deinit)
334 padapter->dvobj_deinit(padapter);
335 padapter->bup = false;
339 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
340 struct _adapter *padapter)
345 int num = sizeof(specific_device_id_tbl) /
346 sizeof(struct specific_device_id);
348 for (i = 0; i < num; i++) {
349 vid = specific_device_id_tbl[i].idVendor;
350 pid = specific_device_id_tbl[i].idProduct;
351 flags = specific_device_id_tbl[i].flags;
353 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
354 (flags&SPEC_DEV_ID_DISABLE_HT)) {
355 padapter->registrypriv.ht_enable = 0;
356 padapter->registrypriv.cbw40_enable = 0;
357 padapter->registrypriv.ampdu_enable = 0;
362 static u8 key_2char2num(u8 hch, u8 lch)
364 return (hex_to_bin(hch) << 4) | hex_to_bin(lch);
367 static const struct device_type wlan_type = {
372 * drv_init() - a device potentially for us
374 * notes: drv_init() is called when the bus driver has located a card for us
375 * to support. We accept the new device by returning 0.
377 static int r871xu_drv_init(struct usb_interface *pusb_intf,
378 const struct usb_device_id *pdid)
381 struct _adapter *padapter = NULL;
382 struct dvobj_priv *pdvobjpriv;
383 struct net_device *pnetdev;
384 struct usb_device *udev;
386 printk(KERN_INFO "r8712u: DriverVersion: %s\n", DRVER);
387 /* In this probe function, O.S. will provide the usb interface pointer
388 * to driver. We have to increase the reference count of the usb device
389 * structure by using the usb_get_dev function.
391 udev = interface_to_usbdev(pusb_intf);
395 pnetdev = r8712_init_netdev();
398 padapter = netdev_priv(pnetdev);
399 disable_ht_for_spec_devid(pdid, padapter);
400 pdvobjpriv = &padapter->dvobjpriv;
401 pdvobjpriv->padapter = padapter;
402 padapter->dvobjpriv.pusbdev = udev;
403 padapter->pusb_intf = pusb_intf;
404 usb_set_intfdata(pusb_intf, pnetdev);
405 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
406 pnetdev->dev.type = &wlan_type;
408 padapter->dvobj_init = &r8712_usb_dvobj_init;
409 padapter->dvobj_deinit = &r8712_usb_dvobj_deinit;
410 padapter->halpriv.hal_bus_init = &r8712_usb_hal_bus_init;
411 padapter->dvobjpriv.inirp_init = &r8712_usb_inirp_init;
412 padapter->dvobjpriv.inirp_deinit = &r8712_usb_inirp_deinit;
414 * initialize the dvobj_priv
416 if (padapter->dvobj_init == NULL)
419 status = padapter->dvobj_init(padapter);
420 if (status != _SUCCESS)
424 status = r8712_init_drv_sw(padapter);
427 /* step 5. read efuse/eeprom data and get mac_addr */
431 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
432 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
434 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
436 /* To check system boot selection.*/
437 printk(KERN_INFO "r8712u: Boot from %s: Autoload %s\n",
438 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
439 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
441 /* To check autoload success or not.*/
442 if (tmpU1b & _EEPROM_EN) {
444 /* The following operations prevent Efuse leakage by
447 tmpU1b = r8712_read8(padapter, EFUSE_TEST+3);
448 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
450 r8712_write8(padapter, EFUSE_TEST + 3,
451 (tmpU1b & (~BIT(7))));
453 /* Retrieve Chip version.
454 * Recognize IC version by Reg0x4 BIT15.
456 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
459 padapter->registrypriv.chip_version =
462 padapter->registrypriv.chip_version =
464 switch (padapter->registrypriv.chip_version) {
470 padapter->registrypriv.chip_version =
475 for (i = 0, offset = 0; i < 128; i += 8, offset++)
476 r8712_efuse_pg_packet_read(padapter, offset,
480 /* Users specify the mac address */
483 for (jj = 0, kk = 0; jj < ETH_ALEN;
486 key_2char2num(r8712_initmac[kk],
487 r8712_initmac[kk + 1]);
489 /* Use the mac address stored in the Efuse
490 * offset = 0x12 for usb in efuse
492 memcpy(mac, &pdata[0x12], ETH_ALEN);
494 eeprom_CustomerID = pdata[0x52];
495 switch (eeprom_CustomerID) {
496 case EEPROM_CID_ALPHA:
497 padapter->eeprompriv.CustomerID =
500 case EEPROM_CID_CAMEO:
501 padapter->eeprompriv.CustomerID =
504 case EEPROM_CID_SITECOM:
505 padapter->eeprompriv.CustomerID =
508 case EEPROM_CID_COREGA:
509 padapter->eeprompriv.CustomerID =
512 case EEPROM_CID_Senao:
513 padapter->eeprompriv.CustomerID =
516 case EEPROM_CID_EDIMAX_BELKIN:
517 padapter->eeprompriv.CustomerID =
518 RT_CID_819x_Edimax_Belkin;
520 case EEPROM_CID_SERCOMM_BELKIN:
521 padapter->eeprompriv.CustomerID =
522 RT_CID_819x_Sercomm_Belkin;
524 case EEPROM_CID_WNC_COREGA:
525 padapter->eeprompriv.CustomerID =
526 RT_CID_819x_WNC_COREGA;
528 case EEPROM_CID_WHQL:
530 case EEPROM_CID_NetCore:
531 padapter->eeprompriv.CustomerID =
534 case EEPROM_CID_CAMEO1:
535 padapter->eeprompriv.CustomerID =
538 case EEPROM_CID_CLEVO:
539 padapter->eeprompriv.CustomerID =
543 padapter->eeprompriv.CustomerID =
547 printk(KERN_INFO "r8712u: CustomerID = 0x%.4x\n",
548 padapter->eeprompriv.CustomerID);
550 switch (padapter->eeprompriv.CustomerID) {
552 case RT_CID_819x_ALPHA:
553 case RT_CID_819x_CAMEO:
554 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
555 padapter->ledpriv.bRegUseLed = true;
557 case RT_CID_819x_Sitecom:
558 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
559 padapter->ledpriv.bRegUseLed = true;
562 case RT_CID_819x_Senao:
563 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
564 padapter->ledpriv.bRegUseLed = true;
566 case RT_CID_819x_Edimax_Belkin:
567 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
568 padapter->ledpriv.bRegUseLed = true;
570 case RT_CID_819x_Sercomm_Belkin:
571 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
572 padapter->ledpriv.bRegUseLed = true;
574 case RT_CID_819x_WNC_COREGA:
575 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
576 padapter->ledpriv.bRegUseLed = true;
579 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
580 padapter->ledpriv.bRegUseLed = false;
584 AutoloadFail = false;
585 if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
586 (mac[2] == 0xff) && (mac[3] == 0xff) &&
587 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
588 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
589 (mac[2] == 0x00) && (mac[3] == 0x00) &&
590 (mac[4] == 0x00) && (mac[5] == 0x00)) ||
591 (AutoloadFail == false)) {
600 /* Make sure the user did not select a multicast
601 * address by setting bit 1 of first octet.
604 printk(KERN_INFO "r8712u: MAC Address from user = "
607 printk(KERN_INFO "r8712u: MAC Address from efuse = "
609 memcpy(pnetdev->dev_addr, mac, ETH_ALEN);
611 /* step 6. Load the firmware asynchronously */
612 if (rtl871x_load_fw(padapter))
614 spin_lock_init(&padapter->lockRxFF0Filter);
615 mutex_init(&padapter->mutex_start);
619 usb_set_intfdata(pusb_intf, NULL);
620 if (padapter->dvobj_deinit != NULL)
621 padapter->dvobj_deinit(padapter);
623 free_netdev(pnetdev);
627 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
628 * => how to recognize both */
629 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
631 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
632 struct _adapter *padapter = netdev_priv(pnetdev);
633 struct usb_device *udev = interface_to_usbdev(pusb_intf);
635 if (padapter->fw_found)
636 release_firmware(padapter->fw);
637 /* never exit with a firmware callback pending */
638 wait_for_completion(&padapter->rtl8712_fw_ready);
639 usb_set_intfdata(pusb_intf, NULL);
641 if (drvpriv.drv_registered == true)
642 padapter->bSurpriseRemoved = true;
643 if (pnetdev != NULL) {
644 /* will call netdev_close() */
645 unregister_netdev(pnetdev);
647 flush_scheduled_work();
649 /*Stop driver mlme relation timer */
650 if (padapter->fw_found)
651 r8712_stop_drv_timers(padapter);
652 r871x_dev_unload(padapter);
653 r8712_free_drv_sw(padapter);
655 usb_set_intfdata(pusb_intf, NULL);
656 /* decrease the reference count of the usb device structure
659 /* If we didn't unplug usb dongle and remove/insert modlue, driver
660 * fails on sitesurvey for the first time when device is up.
661 * Reset usb port for sitesurvey fail issue. */
662 if (udev->state != USB_STATE_NOTATTACHED)
663 usb_reset_device(udev);
667 static int __init r8712u_drv_entry(void)
669 drvpriv.drv_registered = true;
670 return usb_register(&drvpriv.r871xu_drv);
673 static void __exit r8712u_drv_halt(void)
675 drvpriv.drv_registered = false;
676 usb_deregister(&drvpriv.r871xu_drv);
677 printk(KERN_INFO "r8712u: Driver unloaded\n");
680 module_init(r8712u_drv_entry);
681 module_exit(r8712u_drv_halt);