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[] = {
42 {USB_DEVICE(0x0BDA, 0x8171)},
43 {USB_DEVICE(0x0bda, 0x8173)},
44 {USB_DEVICE(0x0bda, 0x8712)},
45 {USB_DEVICE(0x0bda, 0x8713)},
46 {USB_DEVICE(0x0bda, 0xC512)},
48 {USB_DEVICE(0x07B8, 0x8188)},
50 {USB_DEVICE(0x0B05, 0x1786)},
51 {USB_DEVICE(0x0B05, 0x1791)}, /* 11n mode disable */
53 {USB_DEVICE(0x050D, 0x945A)},
54 /* ISY IWL - Belkin clone */
55 {USB_DEVICE(0x050D, 0x11F1)},
57 {USB_DEVICE(0x07AA, 0x0047)},
59 {USB_DEVICE(0x2001, 0x3306)},
60 {USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
62 {USB_DEVICE(0x7392, 0x7611)},
64 {USB_DEVICE(0x1740, 0x9603)},
66 {USB_DEVICE(0x0E66, 0x0016)},
68 {USB_DEVICE(0x06F8, 0xE034)},
69 {USB_DEVICE(0x06F8, 0xE032)},
71 {USB_DEVICE(0x0789, 0x0167)},
73 {USB_DEVICE(0x2019, 0xAB28)},
74 {USB_DEVICE(0x2019, 0xED16)},
76 {USB_DEVICE(0x0DF6, 0x0057)},
77 {USB_DEVICE(0x0DF6, 0x0045)},
78 {USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
79 {USB_DEVICE(0x0DF6, 0x004B)},
80 {USB_DEVICE(0x0DF6, 0x005B)},
81 {USB_DEVICE(0x0DF6, 0x005D)},
82 {USB_DEVICE(0x0DF6, 0x0063)},
84 {USB_DEVICE(0x177F, 0x0154)},
86 {USB_DEVICE(0x0BDA, 0x5077)},
88 {USB_DEVICE(0x1690, 0x0752)},
90 {USB_DEVICE(0x20F4, 0x646B)},
91 {USB_DEVICE(0x083A, 0xC512)},
92 {USB_DEVICE(0x25D4, 0x4CA1)},
93 {USB_DEVICE(0x25D4, 0x4CAB)},
97 {USB_DEVICE(0x0BDA, 0x8172)},
98 {USB_DEVICE(0x0BDA, 0x8192)},
100 {USB_DEVICE(0x0EB0, 0x9061)},
102 {USB_DEVICE(0x13D3, 0x3323)},
103 {USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
104 {USB_DEVICE(0x13D3, 0x3342)},
106 {USB_DEVICE(0x13D3, 0x3333)},
107 {USB_DEVICE(0x13D3, 0x3334)},
108 {USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
109 {USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
111 {USB_DEVICE(0x13D3, 0x3309)},
113 {USB_DEVICE(0x050D, 0x815F)},
115 {USB_DEVICE(0x07D1, 0x3302)},
116 {USB_DEVICE(0x07D1, 0x3300)},
117 {USB_DEVICE(0x07D1, 0x3303)},
119 {USB_DEVICE(0x7392, 0x7612)},
121 {USB_DEVICE(0x1740, 0x9605)},
123 {USB_DEVICE(0x06F8, 0xE031)},
125 {USB_DEVICE(0x0E66, 0x0015)},
127 {USB_DEVICE(0x13D3, 0x3306)},
129 {USB_DEVICE(0x2019, 0xED18)},
130 {USB_DEVICE(0x2019, 0x4901)},
132 {USB_DEVICE(0x0DF6, 0x0058)},
133 {USB_DEVICE(0x0DF6, 0x0049)},
134 {USB_DEVICE(0x0DF6, 0x004C)},
135 {USB_DEVICE(0x0DF6, 0x006C)},
136 {USB_DEVICE(0x0DF6, 0x0064)},
138 {USB_DEVICE(0x14b2, 0x3300)},
139 {USB_DEVICE(0x14b2, 0x3301)},
140 {USB_DEVICE(0x14B2, 0x3302)},
142 {USB_DEVICE(0x04F2, 0xAFF2)},
143 {USB_DEVICE(0x04F2, 0xAFF5)},
144 {USB_DEVICE(0x04F2, 0xAFF6)},
145 {USB_DEVICE(0x13D3, 0x3339)},
146 {USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
147 {USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
148 {USB_DEVICE(0x13D3, 0x3310)},
149 {USB_DEVICE(0x13D3, 0x3325)},
153 {USB_DEVICE(0x0BDA, 0x8174)},
155 {USB_DEVICE(0x050D, 0x845A)},
157 {USB_DEVICE(0x07AA, 0x0051)},
159 {USB_DEVICE(0x7392, 0x7622)},
161 {USB_DEVICE(0x0409, 0x02B6)},
165 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
167 static struct specific_device_id specific_device_id_tbl[] = {
168 {.idVendor = 0x0b05, .idProduct = 0x1791,
169 .flags = SPEC_DEV_ID_DISABLE_HT},
170 {.idVendor = 0x0df6, .idProduct = 0x0059,
171 .flags = SPEC_DEV_ID_DISABLE_HT},
172 {.idVendor = 0x13d3, .idProduct = 0x3306,
173 .flags = SPEC_DEV_ID_DISABLE_HT},
174 {.idVendor = 0x13D3, .idProduct = 0x3311,
175 .flags = SPEC_DEV_ID_DISABLE_HT},
176 {.idVendor = 0x13d3, .idProduct = 0x3335,
177 .flags = SPEC_DEV_ID_DISABLE_HT},
178 {.idVendor = 0x13d3, .idProduct = 0x3336,
179 .flags = SPEC_DEV_ID_DISABLE_HT},
180 {.idVendor = 0x13d3, .idProduct = 0x3340,
181 .flags = SPEC_DEV_ID_DISABLE_HT},
182 {.idVendor = 0x13d3, .idProduct = 0x3341,
183 .flags = SPEC_DEV_ID_DISABLE_HT},
188 struct usb_driver r871xu_drv;
193 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
195 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
196 struct _adapter *padapter = netdev_priv(pnetdev);
198 netdev_info(pnetdev, "Suspending...\n");
199 padapter->bSuspended = true;
200 rtl871x_intf_stop(padapter);
201 if (pnetdev->netdev_ops->ndo_stop)
202 pnetdev->netdev_ops->ndo_stop(pnetdev);
204 netif_device_detach(pnetdev);
208 static void rtl871x_intf_resume(struct _adapter *padapter)
210 if (padapter->dvobjpriv.inirp_init)
211 padapter->dvobjpriv.inirp_init(padapter);
214 static int r871x_resume(struct usb_interface *pusb_intf)
216 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
217 struct _adapter *padapter = netdev_priv(pnetdev);
219 netdev_info(pnetdev, "Resuming...\n");
220 netif_device_attach(pnetdev);
221 if (pnetdev->netdev_ops->ndo_open)
222 pnetdev->netdev_ops->ndo_open(pnetdev);
223 padapter->bSuspended = false;
224 rtl871x_intf_resume(padapter);
229 static struct drv_priv drvpriv = {
230 .r871xu_drv.name = "r8712u",
231 .r871xu_drv.id_table = rtl871x_usb_id_tbl,
232 .r871xu_drv.probe = r871xu_drv_init,
233 .r871xu_drv.disconnect = r871xu_dev_remove,
235 .r871xu_drv.suspend = r871x_suspend,
236 .r871xu_drv.resume = r871x_resume,
240 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
242 uint status = _SUCCESS;
243 struct usb_host_interface *phost_iface;
244 struct usb_interface_descriptor *piface_desc;
245 struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
246 struct usb_device *pusbd = pdvobjpriv->pusbdev;
248 pdvobjpriv->padapter = padapter;
249 padapter->EepromAddressSize = 6;
250 phost_iface = &pintf->altsetting[0];
251 piface_desc = &phost_iface->desc;
252 pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
253 if (pusbd->speed == USB_SPEED_HIGH) {
254 pdvobjpriv->ishighspeed = true;
255 dev_info(&pusbd->dev, "r8712u: USB_SPEED_HIGH with %d endpoints\n",
256 pdvobjpriv->nr_endpoint);
258 pdvobjpriv->ishighspeed = false;
259 dev_info(&pusbd->dev, "r8712u: USB_SPEED_LOW with %d endpoints\n",
260 pdvobjpriv->nr_endpoint);
262 if ((r8712_alloc_io_queue(padapter)) == _FAIL)
267 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
271 void rtl871x_intf_stop(struct _adapter *padapter)
273 /*disable_hw_interrupt*/
274 if (!padapter->bSurpriseRemoved) {
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->bDriverStopped = true;
295 rtl871x_intf_stop(padapter);
298 r8712_stop_drv_threads(padapter);
301 if (!padapter->bSurpriseRemoved) {
302 padapter->hw_init_completed = false;
303 rtl8712_hal_deinit(padapter);
307 if (padapter->dvobj_deinit)
308 padapter->dvobj_deinit(padapter);
309 padapter->bup = false;
313 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
314 struct _adapter *padapter)
319 int num = ARRAY_SIZE(specific_device_id_tbl);
321 for (i = 0; i < num; i++) {
322 vid = specific_device_id_tbl[i].idVendor;
323 pid = specific_device_id_tbl[i].idProduct;
324 flags = specific_device_id_tbl[i].flags;
326 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
327 (flags & SPEC_DEV_ID_DISABLE_HT)) {
328 padapter->registrypriv.ht_enable = 0;
329 padapter->registrypriv.cbw40_enable = 0;
330 padapter->registrypriv.ampdu_enable = 0;
335 static const struct device_type wlan_type = {
340 * drv_init() - a device potentially for us
342 * notes: drv_init() is called when the bus driver has located a card for us
343 * to support. We accept the new device by returning 0.
345 static int r871xu_drv_init(struct usb_interface *pusb_intf,
346 const struct usb_device_id *pdid)
349 struct _adapter *padapter = NULL;
350 struct dvobj_priv *pdvobjpriv;
351 struct net_device *pnetdev;
352 struct usb_device *udev;
354 /* In this probe function, O.S. will provide the usb interface pointer
355 * to driver. We have to increase the reference count of the usb device
356 * structure by using the usb_get_dev function.
358 udev = interface_to_usbdev(pusb_intf);
362 pnetdev = r8712_init_netdev();
365 padapter = netdev_priv(pnetdev);
366 disable_ht_for_spec_devid(pdid, padapter);
367 pdvobjpriv = &padapter->dvobjpriv;
368 pdvobjpriv->padapter = padapter;
369 padapter->dvobjpriv.pusbdev = udev;
370 padapter->pusb_intf = pusb_intf;
371 usb_set_intfdata(pusb_intf, pnetdev);
372 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
373 pnetdev->dev.type = &wlan_type;
375 padapter->dvobj_init = r8712_usb_dvobj_init;
376 padapter->dvobj_deinit = r8712_usb_dvobj_deinit;
377 padapter->halpriv.hal_bus_init = r8712_usb_hal_bus_init;
378 padapter->dvobjpriv.inirp_init = r8712_usb_inirp_init;
379 padapter->dvobjpriv.inirp_deinit = r8712_usb_inirp_deinit;
381 * initialize the dvobj_priv
383 if (!padapter->dvobj_init) {
386 status = padapter->dvobj_init(padapter);
387 if (status != _SUCCESS)
391 status = r8712_init_drv_sw(padapter);
394 /* step 5. read efuse/eeprom data and get mac_addr */
398 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
399 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
401 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
403 /* To check system boot selection.*/
404 dev_info(&udev->dev, "r8712u: Boot from %s: Autoload %s\n",
405 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
406 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
408 /* To check autoload success or not.*/
409 if (tmpU1b & _EEPROM_EN) {
411 /* The following operations prevent Efuse leakage by
414 tmpU1b = r8712_read8(padapter, EFUSE_TEST + 3);
415 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
417 r8712_write8(padapter, EFUSE_TEST + 3,
418 (tmpU1b & (~BIT(7))));
420 /* Retrieve Chip version.
421 * Recognize IC version by Reg0x4 BIT15.
423 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
426 padapter->registrypriv.chip_version =
429 padapter->registrypriv.chip_version =
431 switch (padapter->registrypriv.chip_version) {
437 padapter->registrypriv.chip_version =
442 for (i = 0, offset = 0; i < 128; i += 8, offset++)
443 r8712_efuse_pg_packet_read(padapter, offset,
446 if (!r8712_initmac || !mac_pton(r8712_initmac, mac)) {
447 /* Use the mac address stored in the Efuse
448 * offset = 0x12 for usb in efuse
450 ether_addr_copy(mac, &pdata[0x12]);
452 eeprom_CustomerID = pdata[0x52];
453 switch (eeprom_CustomerID) {
454 case EEPROM_CID_ALPHA:
455 padapter->eeprompriv.CustomerID =
458 case EEPROM_CID_CAMEO:
459 padapter->eeprompriv.CustomerID =
462 case EEPROM_CID_SITECOM:
463 padapter->eeprompriv.CustomerID =
466 case EEPROM_CID_COREGA:
467 padapter->eeprompriv.CustomerID =
470 case EEPROM_CID_Senao:
471 padapter->eeprompriv.CustomerID =
474 case EEPROM_CID_EDIMAX_BELKIN:
475 padapter->eeprompriv.CustomerID =
476 RT_CID_819x_Edimax_Belkin;
478 case EEPROM_CID_SERCOMM_BELKIN:
479 padapter->eeprompriv.CustomerID =
480 RT_CID_819x_Sercomm_Belkin;
482 case EEPROM_CID_WNC_COREGA:
483 padapter->eeprompriv.CustomerID =
484 RT_CID_819x_WNC_COREGA;
486 case EEPROM_CID_WHQL:
488 case EEPROM_CID_NetCore:
489 padapter->eeprompriv.CustomerID =
492 case EEPROM_CID_CAMEO1:
493 padapter->eeprompriv.CustomerID =
496 case EEPROM_CID_CLEVO:
497 padapter->eeprompriv.CustomerID =
501 padapter->eeprompriv.CustomerID =
505 dev_info(&udev->dev, "r8712u: CustomerID = 0x%.4x\n",
506 padapter->eeprompriv.CustomerID);
508 switch (padapter->eeprompriv.CustomerID) {
510 case RT_CID_819x_ALPHA:
511 case RT_CID_819x_CAMEO:
512 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
513 padapter->ledpriv.bRegUseLed = true;
515 case RT_CID_819x_Sitecom:
516 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
517 padapter->ledpriv.bRegUseLed = true;
520 case RT_CID_819x_Senao:
521 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
522 padapter->ledpriv.bRegUseLed = true;
524 case RT_CID_819x_Edimax_Belkin:
525 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
526 padapter->ledpriv.bRegUseLed = true;
528 case RT_CID_819x_Sercomm_Belkin:
529 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
530 padapter->ledpriv.bRegUseLed = true;
532 case RT_CID_819x_WNC_COREGA:
533 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
534 padapter->ledpriv.bRegUseLed = true;
537 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
538 padapter->ledpriv.bRegUseLed = false;
542 AutoloadFail = false;
544 if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
545 (mac[2] == 0xff) && (mac[3] == 0xff) &&
546 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
547 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
548 (mac[2] == 0x00) && (mac[3] == 0x00) &&
549 (mac[4] == 0x00) && (mac[5] == 0x00)) ||
559 /* Make sure the user did not select a multicast
560 * address by setting bit 1 of first octet.
564 "r8712u: MAC Address from user = %pM\n", mac);
567 "r8712u: MAC Address from efuse = %pM\n", mac);
569 ether_addr_copy(pnetdev->dev_addr, mac);
571 /* step 6. Load the firmware asynchronously */
572 if (rtl871x_load_fw(padapter))
574 spin_lock_init(&padapter->lockRxFF0Filter);
575 mutex_init(&padapter->mutex_start);
579 usb_set_intfdata(pusb_intf, NULL);
580 if (padapter && padapter->dvobj_deinit != NULL)
581 padapter->dvobj_deinit(padapter);
583 free_netdev(pnetdev);
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);
596 struct _adapter *padapter = netdev_priv(pnetdev);
598 usb_set_intfdata(pusb_intf, NULL);
599 release_firmware(padapter->fw);
600 /* never exit with a firmware callback pending */
601 wait_for_completion(&padapter->rtl8712_fw_ready);
602 if (drvpriv.drv_registered)
603 padapter->bSurpriseRemoved = true;
604 unregister_netdev(pnetdev); /* will call netdev_close() */
605 flush_scheduled_work();
607 /* Stop driver mlme relation timer */
608 r8712_stop_drv_timers(padapter);
609 r871x_dev_unload(padapter);
610 r8712_free_drv_sw(padapter);
612 /* decrease the reference count of the usb device structure
617 /* If we didn't unplug usb dongle and remove/insert module, driver
618 * fails on sitesurvey for the first time when device is up.
619 * Reset usb port for sitesurvey fail issue.
621 if (udev->state != USB_STATE_NOTATTACHED)
622 usb_reset_device(udev);
625 static int __init r8712u_drv_entry(void)
627 drvpriv.drv_registered = true;
628 return usb_register(&drvpriv.r871xu_drv);
631 static void __exit r8712u_drv_halt(void)
633 drvpriv.drv_registered = false;
634 usb_deregister(&drvpriv.r871xu_drv);
637 module_init(r8712u_drv_entry);
638 module_exit(r8712u_drv_halt);