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)
268 r8712_free_io_queue(padapter);
271 void rtl871x_intf_stop(struct _adapter *padapter)
273 /*disable_hw_interrupt*/
274 if (!padapter->surprise_removed) {
275 /*device still exists, so driver can do i/o operation
281 if (padapter->dvobjpriv.inirp_deinit)
282 padapter->dvobjpriv.inirp_deinit(padapter);
284 r8712_usb_write_port_cancel(padapter);
285 /* TODO:cancel other irps */
288 void r871x_dev_unload(struct _adapter *padapter)
292 padapter->driver_stopped = true;
295 rtl871x_intf_stop(padapter);
298 r8712_stop_drv_threads(padapter);
301 if (!padapter->surprise_removed) {
302 padapter->hw_init_completed = false;
303 rtl8712_hal_deinit(padapter);
306 padapter->bup = false;
310 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
311 struct _adapter *padapter)
316 int num = ARRAY_SIZE(specific_device_id_tbl);
318 for (i = 0; i < num; i++) {
319 vid = specific_device_id_tbl[i].idVendor;
320 pid = specific_device_id_tbl[i].idProduct;
321 flags = specific_device_id_tbl[i].flags;
323 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
324 (flags & SPEC_DEV_ID_DISABLE_HT)) {
325 padapter->registrypriv.ht_enable = 0;
326 padapter->registrypriv.cbw40_enable = 0;
327 padapter->registrypriv.ampdu_enable = 0;
332 static const struct device_type wlan_type = {
337 * drv_init() - a device potentially for us
339 * notes: drv_init() is called when the bus driver has located a card for us
340 * to support. We accept the new device by returning 0.
342 static int r871xu_drv_init(struct usb_interface *pusb_intf,
343 const struct usb_device_id *pdid)
346 struct _adapter *padapter = NULL;
347 struct dvobj_priv *pdvobjpriv;
348 struct net_device *pnetdev;
349 struct usb_device *udev;
351 /* In this probe function, O.S. will provide the usb interface pointer
352 * to driver. We have to increase the reference count of the usb device
353 * structure by using the usb_get_dev function.
355 udev = interface_to_usbdev(pusb_intf);
359 pnetdev = r8712_init_netdev();
362 padapter = netdev_priv(pnetdev);
363 disable_ht_for_spec_devid(pdid, padapter);
364 pdvobjpriv = &padapter->dvobjpriv;
365 pdvobjpriv->padapter = padapter;
366 padapter->dvobjpriv.pusbdev = udev;
367 padapter->pusb_intf = pusb_intf;
368 usb_set_intfdata(pusb_intf, pnetdev);
369 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
370 pnetdev->dev.type = &wlan_type;
372 padapter->dvobj_init = r8712_usb_dvobj_init;
373 padapter->dvobj_deinit = r8712_usb_dvobj_deinit;
374 padapter->halpriv.hal_bus_init = r8712_usb_hal_bus_init;
375 padapter->dvobjpriv.inirp_init = r8712_usb_inirp_init;
376 padapter->dvobjpriv.inirp_deinit = r8712_usb_inirp_deinit;
378 * initialize the dvobj_priv
381 status = padapter->dvobj_init(padapter);
382 if (status != _SUCCESS)
386 status = r8712_init_drv_sw(padapter);
389 /* step 5. read efuse/eeprom data and get mac_addr */
393 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
394 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
396 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
398 /* To check system boot selection.*/
399 dev_info(&udev->dev, "r8712u: Boot from %s: Autoload %s\n",
400 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
401 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
403 /* To check autoload success or not.*/
404 if (tmpU1b & _EEPROM_EN) {
406 /* The following operations prevent Efuse leakage by
409 tmpU1b = r8712_read8(padapter, EFUSE_TEST + 3);
410 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
412 r8712_write8(padapter, EFUSE_TEST + 3,
413 (tmpU1b & (~BIT(7))));
415 /* Retrieve Chip version.
416 * Recognize IC version by Reg0x4 BIT15.
418 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
421 padapter->registrypriv.chip_version =
424 padapter->registrypriv.chip_version =
426 switch (padapter->registrypriv.chip_version) {
432 padapter->registrypriv.chip_version =
437 for (i = 0, offset = 0; i < 128; i += 8, offset++)
438 r8712_efuse_pg_packet_read(padapter, offset,
441 if (!r8712_initmac || !mac_pton(r8712_initmac, mac)) {
442 /* Use the mac address stored in the Efuse
443 * offset = 0x12 for usb in efuse
445 ether_addr_copy(mac, &pdata[0x12]);
447 eeprom_CustomerID = pdata[0x52];
448 switch (eeprom_CustomerID) {
449 case EEPROM_CID_ALPHA:
450 padapter->eeprompriv.CustomerID =
453 case EEPROM_CID_CAMEO:
454 padapter->eeprompriv.CustomerID =
457 case EEPROM_CID_SITECOM:
458 padapter->eeprompriv.CustomerID =
461 case EEPROM_CID_COREGA:
462 padapter->eeprompriv.CustomerID =
465 case EEPROM_CID_Senao:
466 padapter->eeprompriv.CustomerID =
469 case EEPROM_CID_EDIMAX_BELKIN:
470 padapter->eeprompriv.CustomerID =
471 RT_CID_819x_Edimax_Belkin;
473 case EEPROM_CID_SERCOMM_BELKIN:
474 padapter->eeprompriv.CustomerID =
475 RT_CID_819x_Sercomm_Belkin;
477 case EEPROM_CID_WNC_COREGA:
478 padapter->eeprompriv.CustomerID =
479 RT_CID_819x_WNC_COREGA;
481 case EEPROM_CID_WHQL:
483 case EEPROM_CID_NetCore:
484 padapter->eeprompriv.CustomerID =
487 case EEPROM_CID_CAMEO1:
488 padapter->eeprompriv.CustomerID =
491 case EEPROM_CID_CLEVO:
492 padapter->eeprompriv.CustomerID =
496 padapter->eeprompriv.CustomerID =
500 dev_info(&udev->dev, "r8712u: CustomerID = 0x%.4x\n",
501 padapter->eeprompriv.CustomerID);
503 switch (padapter->eeprompriv.CustomerID) {
505 case RT_CID_819x_ALPHA:
506 case RT_CID_819x_CAMEO:
507 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
508 padapter->ledpriv.bRegUseLed = true;
510 case RT_CID_819x_Sitecom:
511 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
512 padapter->ledpriv.bRegUseLed = true;
515 case RT_CID_819x_Senao:
516 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
517 padapter->ledpriv.bRegUseLed = true;
519 case RT_CID_819x_Edimax_Belkin:
520 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
521 padapter->ledpriv.bRegUseLed = true;
523 case RT_CID_819x_Sercomm_Belkin:
524 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
525 padapter->ledpriv.bRegUseLed = true;
527 case RT_CID_819x_WNC_COREGA:
528 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
529 padapter->ledpriv.bRegUseLed = true;
532 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
533 padapter->ledpriv.bRegUseLed = false;
537 AutoloadFail = false;
539 if ((!AutoloadFail) ||
540 ((mac[0] == 0xff) && (mac[1] == 0xff) &&
541 (mac[2] == 0xff) && (mac[3] == 0xff) &&
542 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
543 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
544 (mac[2] == 0x00) && (mac[3] == 0x00) &&
545 (mac[4] == 0x00) && (mac[5] == 0x00))) {
554 /* Make sure the user did not select a multicast
555 * address by setting bit 1 of first octet.
559 "r8712u: MAC Address from user = %pM\n", mac);
562 "r8712u: MAC Address from efuse = %pM\n", mac);
564 eth_hw_addr_set(pnetdev, mac);
566 /* step 6. Load the firmware asynchronously */
567 if (rtl871x_load_fw(padapter))
569 init_completion(&padapter->rx_filter_ready);
570 mutex_init(&padapter->mutex_start);
574 r8712_free_drv_sw(padapter);
576 padapter->dvobj_deinit(padapter);
578 free_netdev(pnetdev);
581 usb_set_intfdata(pusb_intf, NULL);
585 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
586 * => how to recognize both
588 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
590 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
591 struct usb_device *udev = interface_to_usbdev(pusb_intf);
592 struct _adapter *padapter = netdev_priv(pnetdev);
594 /* never exit with a firmware callback pending */
595 wait_for_completion(&padapter->rtl8712_fw_ready);
596 if (pnetdev->reg_state != NETREG_UNINITIALIZED)
597 unregister_netdev(pnetdev); /* will call netdev_close() */
598 usb_set_intfdata(pusb_intf, NULL);
599 release_firmware(padapter->fw);
600 if (drvpriv.drv_registered)
601 padapter->surprise_removed = true;
602 r8712_flush_rwctrl_works(padapter);
603 r8712_flush_led_works(padapter);
605 /* Stop driver mlme relation timer */
606 r8712_stop_drv_timers(padapter);
607 r871x_dev_unload(padapter);
608 if (padapter->dvobj_deinit)
609 padapter->dvobj_deinit(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);