1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
5 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6 * Linux device driver for RTL8192SU
8 * Modifications for inclusion into the Linux staging tree are
9 * Copyright(c) 2010 Larry Finger. All rights reserved.
11 * Contact information:
15 ******************************************************************************/
19 #include <linux/usb.h>
20 #include <linux/module.h>
21 #include <linux/firmware.h>
23 #include "osdep_service.h"
24 #include "drv_types.h"
25 #include "recv_osdep.h"
26 #include "xmit_osdep.h"
27 #include "rtl8712_efuse.h"
29 #include "usb_osintf.h"
31 static struct usb_interface *pintf;
33 static int r871xu_drv_init(struct usb_interface *pusb_intf,
34 const struct usb_device_id *pdid);
36 static void r871xu_dev_remove(struct usb_interface *pusb_intf);
38 static const struct usb_device_id rtl871x_usb_id_tbl[] = {
41 {USB_DEVICE(0x0BDA, 0x8171)},
42 {USB_DEVICE(0x0bda, 0x8173)},
43 {USB_DEVICE(0x0bda, 0x8712)},
44 {USB_DEVICE(0x0bda, 0x8713)},
45 {USB_DEVICE(0x0bda, 0xC512)},
47 {USB_DEVICE(0x07B8, 0x8188)},
49 {USB_DEVICE(0x0B05, 0x1786)},
50 {USB_DEVICE(0x0B05, 0x1791)}, /* 11n mode disable */
52 {USB_DEVICE(0x050D, 0x945A)},
53 /* ISY IWL - Belkin clone */
54 {USB_DEVICE(0x050D, 0x11F1)},
56 {USB_DEVICE(0x07AA, 0x0047)},
58 {USB_DEVICE(0x2001, 0x3306)},
59 {USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
61 {USB_DEVICE(0x7392, 0x7611)},
63 {USB_DEVICE(0x1740, 0x9603)},
65 {USB_DEVICE(0x0E66, 0x0016)},
67 {USB_DEVICE(0x06F8, 0xE034)},
68 {USB_DEVICE(0x06F8, 0xE032)},
70 {USB_DEVICE(0x0789, 0x0167)},
72 {USB_DEVICE(0x2019, 0xAB28)},
73 {USB_DEVICE(0x2019, 0xED16)},
75 {USB_DEVICE(0x0DF6, 0x0057)},
76 {USB_DEVICE(0x0DF6, 0x0045)},
77 {USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
78 {USB_DEVICE(0x0DF6, 0x004B)},
79 {USB_DEVICE(0x0DF6, 0x005B)},
80 {USB_DEVICE(0x0DF6, 0x005D)},
81 {USB_DEVICE(0x0DF6, 0x0063)},
83 {USB_DEVICE(0x177F, 0x0154)},
85 {USB_DEVICE(0x0BDA, 0x5077)},
87 {USB_DEVICE(0x1690, 0x0752)},
89 {USB_DEVICE(0x20F4, 0x646B)},
90 {USB_DEVICE(0x083A, 0xC512)},
91 {USB_DEVICE(0x25D4, 0x4CA1)},
92 {USB_DEVICE(0x25D4, 0x4CAB)},
96 {USB_DEVICE(0x0BDA, 0x8172)},
97 {USB_DEVICE(0x0BDA, 0x8192)},
99 {USB_DEVICE(0x0EB0, 0x9061)},
101 {USB_DEVICE(0x13D3, 0x3323)},
102 {USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
103 {USB_DEVICE(0x13D3, 0x3342)},
105 {USB_DEVICE(0x13D3, 0x3333)},
106 {USB_DEVICE(0x13D3, 0x3334)},
107 {USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
108 {USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
110 {USB_DEVICE(0x13D3, 0x3309)},
112 {USB_DEVICE(0x050D, 0x815F)},
114 {USB_DEVICE(0x07D1, 0x3302)},
115 {USB_DEVICE(0x07D1, 0x3300)},
116 {USB_DEVICE(0x07D1, 0x3303)},
118 {USB_DEVICE(0x7392, 0x7612)},
120 {USB_DEVICE(0x1740, 0x9605)},
122 {USB_DEVICE(0x06F8, 0xE031)},
124 {USB_DEVICE(0x0E66, 0x0015)},
126 {USB_DEVICE(0x13D3, 0x3306)},
128 {USB_DEVICE(0x2019, 0xED18)},
129 {USB_DEVICE(0x2019, 0x4901)},
131 {USB_DEVICE(0x0DF6, 0x0058)},
132 {USB_DEVICE(0x0DF6, 0x0049)},
133 {USB_DEVICE(0x0DF6, 0x004C)},
134 {USB_DEVICE(0x0DF6, 0x006C)},
135 {USB_DEVICE(0x0DF6, 0x0064)},
137 {USB_DEVICE(0x14b2, 0x3300)},
138 {USB_DEVICE(0x14b2, 0x3301)},
139 {USB_DEVICE(0x14B2, 0x3302)},
141 {USB_DEVICE(0x04F2, 0xAFF2)},
142 {USB_DEVICE(0x04F2, 0xAFF5)},
143 {USB_DEVICE(0x04F2, 0xAFF6)},
144 {USB_DEVICE(0x13D3, 0x3339)},
145 {USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
146 {USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
147 {USB_DEVICE(0x13D3, 0x3310)},
148 {USB_DEVICE(0x13D3, 0x3325)},
152 {USB_DEVICE(0x0BDA, 0x8174)},
154 {USB_DEVICE(0x050D, 0x845A)},
156 {USB_DEVICE(0x07AA, 0x0051)},
158 {USB_DEVICE(0x7392, 0x7622)},
160 {USB_DEVICE(0x0409, 0x02B6)},
164 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
166 static struct specific_device_id specific_device_id_tbl[] = {
167 {.idVendor = 0x0b05, .idProduct = 0x1791,
168 .flags = SPEC_DEV_ID_DISABLE_HT},
169 {.idVendor = 0x0df6, .idProduct = 0x0059,
170 .flags = SPEC_DEV_ID_DISABLE_HT},
171 {.idVendor = 0x13d3, .idProduct = 0x3306,
172 .flags = SPEC_DEV_ID_DISABLE_HT},
173 {.idVendor = 0x13D3, .idProduct = 0x3311,
174 .flags = SPEC_DEV_ID_DISABLE_HT},
175 {.idVendor = 0x13d3, .idProduct = 0x3335,
176 .flags = SPEC_DEV_ID_DISABLE_HT},
177 {.idVendor = 0x13d3, .idProduct = 0x3336,
178 .flags = SPEC_DEV_ID_DISABLE_HT},
179 {.idVendor = 0x13d3, .idProduct = 0x3340,
180 .flags = SPEC_DEV_ID_DISABLE_HT},
181 {.idVendor = 0x13d3, .idProduct = 0x3341,
182 .flags = SPEC_DEV_ID_DISABLE_HT},
187 struct usb_driver r871xu_drv;
192 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
194 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
195 struct _adapter *padapter = netdev_priv(pnetdev);
197 netdev_info(pnetdev, "Suspending...\n");
198 padapter->suspended = true;
199 rtl871x_intf_stop(padapter);
200 if (pnetdev->netdev_ops->ndo_stop)
201 pnetdev->netdev_ops->ndo_stop(pnetdev);
203 netif_device_detach(pnetdev);
207 static void rtl871x_intf_resume(struct _adapter *padapter)
209 if (padapter->dvobjpriv.inirp_init)
210 padapter->dvobjpriv.inirp_init(padapter);
213 static int r871x_resume(struct usb_interface *pusb_intf)
215 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
216 struct _adapter *padapter = netdev_priv(pnetdev);
218 netdev_info(pnetdev, "Resuming...\n");
219 netif_device_attach(pnetdev);
220 if (pnetdev->netdev_ops->ndo_open)
221 pnetdev->netdev_ops->ndo_open(pnetdev);
222 padapter->suspended = false;
223 rtl871x_intf_resume(padapter);
228 static struct drv_priv drvpriv = {
229 .r871xu_drv.name = "r8712u",
230 .r871xu_drv.id_table = rtl871x_usb_id_tbl,
231 .r871xu_drv.probe = r871xu_drv_init,
232 .r871xu_drv.disconnect = r871xu_dev_remove,
234 .r871xu_drv.suspend = r871x_suspend,
235 .r871xu_drv.resume = r871x_resume,
239 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
241 uint status = _SUCCESS;
242 struct usb_host_interface *phost_iface;
243 struct usb_interface_descriptor *piface_desc;
244 struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
245 struct usb_device *pusbd = pdvobjpriv->pusbdev;
247 pdvobjpriv->padapter = padapter;
248 padapter->eeprom_address_size = 6;
249 phost_iface = pintf->cur_altsetting;
250 piface_desc = &phost_iface->desc;
251 pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
252 if (pusbd->speed == USB_SPEED_HIGH) {
253 pdvobjpriv->ishighspeed = true;
254 dev_info(&pusbd->dev, "r8712u: USB_SPEED_HIGH with %d endpoints\n",
255 pdvobjpriv->nr_endpoint);
257 pdvobjpriv->ishighspeed = false;
258 dev_info(&pusbd->dev, "r8712u: USB_SPEED_LOW with %d endpoints\n",
259 pdvobjpriv->nr_endpoint);
261 if ((r8712_alloc_io_queue(padapter)) == _FAIL)
266 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
270 void rtl871x_intf_stop(struct _adapter *padapter)
272 /*disable_hw_interrupt*/
273 if (!padapter->surprise_removed) {
274 /*device still exists, so driver can do i/o operation
280 if (padapter->dvobjpriv.inirp_deinit)
281 padapter->dvobjpriv.inirp_deinit(padapter);
283 r8712_usb_write_port_cancel(padapter);
284 /* TODO:cancel other irps */
287 void r871x_dev_unload(struct _adapter *padapter)
291 padapter->driver_stopped = true;
294 rtl871x_intf_stop(padapter);
297 r8712_stop_drv_threads(padapter);
300 if (!padapter->surprise_removed) {
301 padapter->hw_init_completed = false;
302 rtl8712_hal_deinit(padapter);
306 if (padapter->dvobj_deinit)
307 padapter->dvobj_deinit(padapter);
308 padapter->bup = false;
312 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
313 struct _adapter *padapter)
318 int num = ARRAY_SIZE(specific_device_id_tbl);
320 for (i = 0; i < num; i++) {
321 vid = specific_device_id_tbl[i].idVendor;
322 pid = specific_device_id_tbl[i].idProduct;
323 flags = specific_device_id_tbl[i].flags;
325 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
326 (flags & SPEC_DEV_ID_DISABLE_HT)) {
327 padapter->registrypriv.ht_enable = 0;
328 padapter->registrypriv.cbw40_enable = 0;
329 padapter->registrypriv.ampdu_enable = 0;
334 static const struct device_type wlan_type = {
339 * drv_init() - a device potentially for us
341 * notes: drv_init() is called when the bus driver has located a card for us
342 * to support. We accept the new device by returning 0.
344 static int r871xu_drv_init(struct usb_interface *pusb_intf,
345 const struct usb_device_id *pdid)
348 struct _adapter *padapter = NULL;
349 struct dvobj_priv *pdvobjpriv;
350 struct net_device *pnetdev;
351 struct usb_device *udev;
353 /* In this probe function, O.S. will provide the usb interface pointer
354 * to driver. We have to increase the reference count of the usb device
355 * structure by using the usb_get_dev function.
357 udev = interface_to_usbdev(pusb_intf);
361 pnetdev = r8712_init_netdev();
364 padapter = netdev_priv(pnetdev);
365 disable_ht_for_spec_devid(pdid, padapter);
366 pdvobjpriv = &padapter->dvobjpriv;
367 pdvobjpriv->padapter = padapter;
368 padapter->dvobjpriv.pusbdev = udev;
369 padapter->pusb_intf = pusb_intf;
370 usb_set_intfdata(pusb_intf, pnetdev);
371 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
372 pnetdev->dev.type = &wlan_type;
374 padapter->dvobj_init = r8712_usb_dvobj_init;
375 padapter->dvobj_deinit = r8712_usb_dvobj_deinit;
376 padapter->halpriv.hal_bus_init = r8712_usb_hal_bus_init;
377 padapter->dvobjpriv.inirp_init = r8712_usb_inirp_init;
378 padapter->dvobjpriv.inirp_deinit = r8712_usb_inirp_deinit;
380 * initialize the dvobj_priv
383 status = padapter->dvobj_init(padapter);
384 if (status != _SUCCESS)
388 status = r8712_init_drv_sw(padapter);
391 /* step 5. read efuse/eeprom data and get mac_addr */
395 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
396 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
398 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
400 /* To check system boot selection.*/
401 dev_info(&udev->dev, "r8712u: Boot from %s: Autoload %s\n",
402 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
403 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
405 /* To check autoload success or not.*/
406 if (tmpU1b & _EEPROM_EN) {
408 /* The following operations prevent Efuse leakage by
411 tmpU1b = r8712_read8(padapter, EFUSE_TEST + 3);
412 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
414 r8712_write8(padapter, EFUSE_TEST + 3,
415 (tmpU1b & (~BIT(7))));
417 /* Retrieve Chip version.
418 * Recognize IC version by Reg0x4 BIT15.
420 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
423 padapter->registrypriv.chip_version =
426 padapter->registrypriv.chip_version =
428 switch (padapter->registrypriv.chip_version) {
434 padapter->registrypriv.chip_version =
439 for (i = 0, offset = 0; i < 128; i += 8, offset++)
440 r8712_efuse_pg_packet_read(padapter, offset,
443 if (!r8712_initmac || !mac_pton(r8712_initmac, mac)) {
444 /* Use the mac address stored in the Efuse
445 * offset = 0x12 for usb in efuse
447 ether_addr_copy(mac, &pdata[0x12]);
449 eeprom_CustomerID = pdata[0x52];
450 switch (eeprom_CustomerID) {
451 case EEPROM_CID_ALPHA:
452 padapter->eeprompriv.CustomerID =
455 case EEPROM_CID_CAMEO:
456 padapter->eeprompriv.CustomerID =
459 case EEPROM_CID_SITECOM:
460 padapter->eeprompriv.CustomerID =
463 case EEPROM_CID_COREGA:
464 padapter->eeprompriv.CustomerID =
467 case EEPROM_CID_Senao:
468 padapter->eeprompriv.CustomerID =
471 case EEPROM_CID_EDIMAX_BELKIN:
472 padapter->eeprompriv.CustomerID =
473 RT_CID_819x_Edimax_Belkin;
475 case EEPROM_CID_SERCOMM_BELKIN:
476 padapter->eeprompriv.CustomerID =
477 RT_CID_819x_Sercomm_Belkin;
479 case EEPROM_CID_WNC_COREGA:
480 padapter->eeprompriv.CustomerID =
481 RT_CID_819x_WNC_COREGA;
483 case EEPROM_CID_WHQL:
485 case EEPROM_CID_NetCore:
486 padapter->eeprompriv.CustomerID =
489 case EEPROM_CID_CAMEO1:
490 padapter->eeprompriv.CustomerID =
493 case EEPROM_CID_CLEVO:
494 padapter->eeprompriv.CustomerID =
498 padapter->eeprompriv.CustomerID =
502 dev_info(&udev->dev, "r8712u: CustomerID = 0x%.4x\n",
503 padapter->eeprompriv.CustomerID);
505 switch (padapter->eeprompriv.CustomerID) {
507 case RT_CID_819x_ALPHA:
508 case RT_CID_819x_CAMEO:
509 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
510 padapter->ledpriv.bRegUseLed = true;
512 case RT_CID_819x_Sitecom:
513 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
514 padapter->ledpriv.bRegUseLed = true;
517 case RT_CID_819x_Senao:
518 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
519 padapter->ledpriv.bRegUseLed = true;
521 case RT_CID_819x_Edimax_Belkin:
522 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
523 padapter->ledpriv.bRegUseLed = true;
525 case RT_CID_819x_Sercomm_Belkin:
526 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
527 padapter->ledpriv.bRegUseLed = true;
529 case RT_CID_819x_WNC_COREGA:
530 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
531 padapter->ledpriv.bRegUseLed = true;
534 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
535 padapter->ledpriv.bRegUseLed = false;
539 AutoloadFail = false;
541 if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
542 (mac[2] == 0xff) && (mac[3] == 0xff) &&
543 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
544 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
545 (mac[2] == 0x00) && (mac[3] == 0x00) &&
546 (mac[4] == 0x00) && (mac[5] == 0x00)) ||
556 /* Make sure the user did not select a multicast
557 * address by setting bit 1 of first octet.
561 "r8712u: MAC Address from user = %pM\n", mac);
564 "r8712u: MAC Address from efuse = %pM\n", mac);
566 eth_hw_addr_set(pnetdev, mac);
568 /* step 6. Load the firmware asynchronously */
569 if (rtl871x_load_fw(padapter))
571 spin_lock_init(&padapter->lock_rx_ff0_filter);
572 mutex_init(&padapter->mutex_start);
576 r8712_free_drv_sw(padapter);
578 padapter->dvobj_deinit(padapter);
580 free_netdev(pnetdev);
583 usb_set_intfdata(pusb_intf, NULL);
587 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
588 * => how to recognize both
590 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
592 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
593 struct usb_device *udev = interface_to_usbdev(pusb_intf);
594 struct _adapter *padapter = netdev_priv(pnetdev);
596 /* never exit with a firmware callback pending */
597 wait_for_completion(&padapter->rtl8712_fw_ready);
598 if (pnetdev->reg_state != NETREG_UNINITIALIZED)
599 unregister_netdev(pnetdev); /* will call netdev_close() */
600 usb_set_intfdata(pusb_intf, NULL);
601 release_firmware(padapter->fw);
602 if (drvpriv.drv_registered)
603 padapter->surprise_removed = true;
604 r8712_flush_rwctrl_works(padapter);
605 r8712_flush_led_works(padapter);
607 /* Stop driver mlme relation timer */
608 r8712_stop_drv_timers(padapter);
609 r871x_dev_unload(padapter);
610 r8712_free_drv_sw(padapter);
611 free_netdev(pnetdev);
613 /* decrease the reference count of the usb device structure
618 /* If we didn't unplug usb dongle and remove/insert module, driver
619 * fails on sitesurvey for the first time when device is up.
620 * Reset usb port for sitesurvey fail issue.
622 if (udev->state != USB_STATE_NOTATTACHED)
623 usb_reset_device(udev);
626 static int __init r8712u_drv_entry(void)
628 drvpriv.drv_registered = true;
629 return usb_register(&drvpriv.r871xu_drv);
632 static void __exit r8712u_drv_halt(void)
634 drvpriv.drv_registered = false;
635 usb_deregister(&drvpriv.r871xu_drv);
638 module_init(r8712u_drv_entry);
639 module_exit(r8712u_drv_halt);