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:
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)},
66 /* ISY IWL - Belkin clone */
67 {USB_DEVICE(0x050D, 0x11F1)},
69 {USB_DEVICE(0x07AA, 0x0047)},
71 {USB_DEVICE(0x2001, 0x3306)},
72 {USB_DEVICE(0x07D1, 0x3306)}, /* 11n mode disable */
74 {USB_DEVICE(0x7392, 0x7611)},
76 {USB_DEVICE(0x1740, 0x9603)},
78 {USB_DEVICE(0x0E66, 0x0016)},
80 {USB_DEVICE(0x06F8, 0xE034)},
81 {USB_DEVICE(0x06F8, 0xE032)},
83 {USB_DEVICE(0x0789, 0x0167)},
85 {USB_DEVICE(0x2019, 0xAB28)},
86 {USB_DEVICE(0x2019, 0xED16)},
88 {USB_DEVICE(0x0DF6, 0x0057)},
89 {USB_DEVICE(0x0DF6, 0x0045)},
90 {USB_DEVICE(0x0DF6, 0x0059)}, /* 11n mode disable */
91 {USB_DEVICE(0x0DF6, 0x004B)},
92 {USB_DEVICE(0x0DF6, 0x005B)},
93 {USB_DEVICE(0x0DF6, 0x005D)},
94 {USB_DEVICE(0x0DF6, 0x0063)},
96 {USB_DEVICE(0x177F, 0x0154)},
98 {USB_DEVICE(0x0BDA, 0x5077)},
100 {USB_DEVICE(0x1690, 0x0752)},
102 {USB_DEVICE(0x20F4, 0x646B)},
103 {USB_DEVICE(0x083A, 0xC512)},
104 {USB_DEVICE(0x25D4, 0x4CA1)},
105 {USB_DEVICE(0x25D4, 0x4CAB)},
109 {USB_DEVICE(0x0BDA, 0x8172)},
110 {USB_DEVICE(0x0BDA, 0x8192)},
112 {USB_DEVICE(0x0EB0, 0x9061)},
114 {USB_DEVICE(0x13D3, 0x3323)},
115 {USB_DEVICE(0x13D3, 0x3311)}, /* 11n mode disable */
116 {USB_DEVICE(0x13D3, 0x3342)},
118 {USB_DEVICE(0x13D3, 0x3333)},
119 {USB_DEVICE(0x13D3, 0x3334)},
120 {USB_DEVICE(0x13D3, 0x3335)}, /* 11n mode disable */
121 {USB_DEVICE(0x13D3, 0x3336)}, /* 11n mode disable */
123 {USB_DEVICE(0x13D3, 0x3309)},
125 {USB_DEVICE(0x050D, 0x815F)},
127 {USB_DEVICE(0x07D1, 0x3302)},
128 {USB_DEVICE(0x07D1, 0x3300)},
129 {USB_DEVICE(0x07D1, 0x3303)},
131 {USB_DEVICE(0x7392, 0x7612)},
133 {USB_DEVICE(0x1740, 0x9605)},
135 {USB_DEVICE(0x06F8, 0xE031)},
137 {USB_DEVICE(0x0E66, 0x0015)},
139 {USB_DEVICE(0x13D3, 0x3306)},
141 {USB_DEVICE(0x2019, 0xED18)},
142 {USB_DEVICE(0x2019, 0x4901)},
144 {USB_DEVICE(0x0DF6, 0x0058)},
145 {USB_DEVICE(0x0DF6, 0x0049)},
146 {USB_DEVICE(0x0DF6, 0x004C)},
147 {USB_DEVICE(0x0DF6, 0x0064)},
149 {USB_DEVICE(0x14b2, 0x3300)},
150 {USB_DEVICE(0x14b2, 0x3301)},
151 {USB_DEVICE(0x14B2, 0x3302)},
153 {USB_DEVICE(0x04F2, 0xAFF2)},
154 {USB_DEVICE(0x04F2, 0xAFF5)},
155 {USB_DEVICE(0x04F2, 0xAFF6)},
156 {USB_DEVICE(0x13D3, 0x3339)},
157 {USB_DEVICE(0x13D3, 0x3340)}, /* 11n mode disable */
158 {USB_DEVICE(0x13D3, 0x3341)}, /* 11n mode disable */
159 {USB_DEVICE(0x13D3, 0x3310)},
160 {USB_DEVICE(0x13D3, 0x3325)},
164 {USB_DEVICE(0x0BDA, 0x8174)},
166 {USB_DEVICE(0x050D, 0x845A)},
168 {USB_DEVICE(0x07AA, 0x0051)},
170 {USB_DEVICE(0x7392, 0x7622)},
172 {USB_DEVICE(0x0409, 0x02B6)},
176 MODULE_DEVICE_TABLE(usb, rtl871x_usb_id_tbl);
178 static struct specific_device_id specific_device_id_tbl[] = {
179 {.idVendor = 0x0b05, .idProduct = 0x1791,
180 .flags = SPEC_DEV_ID_DISABLE_HT},
181 {.idVendor = 0x0df6, .idProduct = 0x0059,
182 .flags = SPEC_DEV_ID_DISABLE_HT},
183 {.idVendor = 0x13d3, .idProduct = 0x3306,
184 .flags = SPEC_DEV_ID_DISABLE_HT},
185 {.idVendor = 0x13D3, .idProduct = 0x3311,
186 .flags = SPEC_DEV_ID_DISABLE_HT},
187 {.idVendor = 0x13d3, .idProduct = 0x3335,
188 .flags = SPEC_DEV_ID_DISABLE_HT},
189 {.idVendor = 0x13d3, .idProduct = 0x3336,
190 .flags = SPEC_DEV_ID_DISABLE_HT},
191 {.idVendor = 0x13d3, .idProduct = 0x3340,
192 .flags = SPEC_DEV_ID_DISABLE_HT},
193 {.idVendor = 0x13d3, .idProduct = 0x3341,
194 .flags = SPEC_DEV_ID_DISABLE_HT},
199 struct usb_driver r871xu_drv;
204 static int r871x_suspend(struct usb_interface *pusb_intf, pm_message_t state)
206 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
208 netdev_info(pnetdev, "Suspending...\n");
209 if (!pnetdev || !netif_running(pnetdev)) {
210 netdev_info(pnetdev, "Unable to suspend\n");
213 if (pnetdev->netdev_ops->ndo_stop)
214 pnetdev->netdev_ops->ndo_stop(pnetdev);
216 netif_device_detach(pnetdev);
220 static int r871x_resume(struct usb_interface *pusb_intf)
222 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
224 netdev_info(pnetdev, "Resuming...\n");
225 if (!pnetdev || !netif_running(pnetdev)) {
226 netdev_info(pnetdev, "Unable to resume\n");
229 netif_device_attach(pnetdev);
230 if (pnetdev->netdev_ops->ndo_open)
231 pnetdev->netdev_ops->ndo_open(pnetdev);
235 static int r871x_reset_resume(struct usb_interface *pusb_intf)
243 static struct drv_priv drvpriv = {
244 .r871xu_drv.name = "r8712u",
245 .r871xu_drv.id_table = rtl871x_usb_id_tbl,
246 .r871xu_drv.probe = r871xu_drv_init,
247 .r871xu_drv.disconnect = r871xu_dev_remove,
249 .r871xu_drv.suspend = r871x_suspend,
250 .r871xu_drv.resume = r871x_resume,
251 .r871xu_drv.reset_resume = r871x_reset_resume,
255 static uint r8712_usb_dvobj_init(struct _adapter *padapter)
257 uint status = _SUCCESS;
258 struct usb_host_interface *phost_iface;
259 struct usb_interface_descriptor *piface_desc;
260 struct dvobj_priv *pdvobjpriv = &padapter->dvobjpriv;
261 struct usb_device *pusbd = pdvobjpriv->pusbdev;
263 pdvobjpriv->padapter = padapter;
264 padapter->EepromAddressSize = 6;
265 phost_iface = &pintf->altsetting[0];
266 piface_desc = &phost_iface->desc;
267 pdvobjpriv->nr_endpoint = piface_desc->bNumEndpoints;
268 if (pusbd->speed == USB_SPEED_HIGH) {
269 pdvobjpriv->ishighspeed = true;
270 dev_info(&pusbd->dev, "r8712u: USB_SPEED_HIGH with %d endpoints\n",
271 pdvobjpriv->nr_endpoint);
273 pdvobjpriv->ishighspeed = false;
274 dev_info(&pusbd->dev, "r8712u: USB_SPEED_LOW with %d endpoints\n",
275 pdvobjpriv->nr_endpoint);
277 if ((r8712_alloc_io_queue(padapter)) == _FAIL)
282 static void r8712_usb_dvobj_deinit(struct _adapter *padapter)
286 void rtl871x_intf_stop(struct _adapter *padapter)
288 /*disable_hw_interrupt*/
289 if (!padapter->bSurpriseRemoved) {
290 /*device still exists, so driver can do i/o operation
295 if (padapter->dvobjpriv.inirp_deinit)
296 padapter->dvobjpriv.inirp_deinit(padapter);
298 r8712_usb_write_port_cancel(padapter);
299 /* TODO:cancel other irps */
302 void r871x_dev_unload(struct _adapter *padapter)
304 if (padapter->bup == true) {
306 padapter->bDriverStopped = true;
309 rtl871x_intf_stop(padapter);
312 r8712_stop_drv_threads(padapter);
315 if (!padapter->bSurpriseRemoved) {
316 padapter->hw_init_completed = false;
317 rtl8712_hal_deinit(padapter);
321 if (padapter->dvobj_deinit)
322 padapter->dvobj_deinit(padapter);
323 padapter->bup = false;
327 static void disable_ht_for_spec_devid(const struct usb_device_id *pdid,
328 struct _adapter *padapter)
333 int num = sizeof(specific_device_id_tbl) /
334 sizeof(struct specific_device_id);
336 for (i = 0; i < num; i++) {
337 vid = specific_device_id_tbl[i].idVendor;
338 pid = specific_device_id_tbl[i].idProduct;
339 flags = specific_device_id_tbl[i].flags;
341 if ((pdid->idVendor == vid) && (pdid->idProduct == pid) &&
342 (flags&SPEC_DEV_ID_DISABLE_HT)) {
343 padapter->registrypriv.ht_enable = 0;
344 padapter->registrypriv.cbw40_enable = 0;
345 padapter->registrypriv.ampdu_enable = 0;
350 static const struct device_type wlan_type = {
355 * drv_init() - a device potentially for us
357 * notes: drv_init() is called when the bus driver has located a card for us
358 * to support. We accept the new device by returning 0.
360 static int r871xu_drv_init(struct usb_interface *pusb_intf,
361 const struct usb_device_id *pdid)
364 struct _adapter *padapter = NULL;
365 struct dvobj_priv *pdvobjpriv;
366 struct net_device *pnetdev;
367 struct usb_device *udev;
369 /* In this probe function, O.S. will provide the usb interface pointer
370 * to driver. We have to increase the reference count of the usb device
371 * structure by using the usb_get_dev function.
373 udev = interface_to_usbdev(pusb_intf);
377 pnetdev = r8712_init_netdev();
380 padapter = netdev_priv(pnetdev);
381 disable_ht_for_spec_devid(pdid, padapter);
382 pdvobjpriv = &padapter->dvobjpriv;
383 pdvobjpriv->padapter = padapter;
384 padapter->dvobjpriv.pusbdev = udev;
385 padapter->pusb_intf = pusb_intf;
386 usb_set_intfdata(pusb_intf, pnetdev);
387 SET_NETDEV_DEV(pnetdev, &pusb_intf->dev);
388 pnetdev->dev.type = &wlan_type;
390 padapter->dvobj_init = &r8712_usb_dvobj_init;
391 padapter->dvobj_deinit = &r8712_usb_dvobj_deinit;
392 padapter->halpriv.hal_bus_init = &r8712_usb_hal_bus_init;
393 padapter->dvobjpriv.inirp_init = &r8712_usb_inirp_init;
394 padapter->dvobjpriv.inirp_deinit = &r8712_usb_inirp_deinit;
396 * initialize the dvobj_priv
398 if (!padapter->dvobj_init)
401 status = padapter->dvobj_init(padapter);
402 if (status != _SUCCESS)
406 status = r8712_init_drv_sw(padapter);
409 /* step 5. read efuse/eeprom data and get mac_addr */
413 u8 tmpU1b, AutoloadFail, eeprom_CustomerID;
414 u8 *pdata = padapter->eeprompriv.efuse_eeprom_data;
416 tmpU1b = r8712_read8(padapter, EE_9346CR);/*CR9346*/
418 /* To check system boot selection.*/
419 dev_info(&udev->dev, "r8712u: Boot from %s: Autoload %s\n",
420 (tmpU1b & _9356SEL) ? "EEPROM" : "EFUSE",
421 (tmpU1b & _EEPROM_EN) ? "OK" : "Failed");
423 /* To check autoload success or not.*/
424 if (tmpU1b & _EEPROM_EN) {
426 /* The following operations prevent Efuse leakage by
429 tmpU1b = r8712_read8(padapter, EFUSE_TEST+3);
430 r8712_write8(padapter, EFUSE_TEST + 3, tmpU1b | 0x80);
432 r8712_write8(padapter, EFUSE_TEST + 3,
433 (tmpU1b & (~BIT(7))));
435 /* Retrieve Chip version.
436 * Recognize IC version by Reg0x4 BIT15.
438 tmpU1b = (u8)((r8712_read32(padapter, PMC_FSM) >> 15) &
441 padapter->registrypriv.chip_version =
444 padapter->registrypriv.chip_version =
446 switch (padapter->registrypriv.chip_version) {
452 padapter->registrypriv.chip_version =
457 for (i = 0, offset = 0; i < 128; i += 8, offset++)
458 r8712_efuse_pg_packet_read(padapter, offset,
461 if (!r8712_initmac || !mac_pton(r8712_initmac, mac)) {
462 /* Use the mac address stored in the Efuse
463 * offset = 0x12 for usb in efuse
465 ether_addr_copy(mac, &pdata[0x12]);
467 eeprom_CustomerID = pdata[0x52];
468 switch (eeprom_CustomerID) {
469 case EEPROM_CID_ALPHA:
470 padapter->eeprompriv.CustomerID =
473 case EEPROM_CID_CAMEO:
474 padapter->eeprompriv.CustomerID =
477 case EEPROM_CID_SITECOM:
478 padapter->eeprompriv.CustomerID =
481 case EEPROM_CID_COREGA:
482 padapter->eeprompriv.CustomerID =
485 case EEPROM_CID_Senao:
486 padapter->eeprompriv.CustomerID =
489 case EEPROM_CID_EDIMAX_BELKIN:
490 padapter->eeprompriv.CustomerID =
491 RT_CID_819x_Edimax_Belkin;
493 case EEPROM_CID_SERCOMM_BELKIN:
494 padapter->eeprompriv.CustomerID =
495 RT_CID_819x_Sercomm_Belkin;
497 case EEPROM_CID_WNC_COREGA:
498 padapter->eeprompriv.CustomerID =
499 RT_CID_819x_WNC_COREGA;
501 case EEPROM_CID_WHQL:
503 case EEPROM_CID_NetCore:
504 padapter->eeprompriv.CustomerID =
507 case EEPROM_CID_CAMEO1:
508 padapter->eeprompriv.CustomerID =
511 case EEPROM_CID_CLEVO:
512 padapter->eeprompriv.CustomerID =
516 padapter->eeprompriv.CustomerID =
520 dev_info(&udev->dev, "r8712u: CustomerID = 0x%.4x\n",
521 padapter->eeprompriv.CustomerID);
523 switch (padapter->eeprompriv.CustomerID) {
525 case RT_CID_819x_ALPHA:
526 case RT_CID_819x_CAMEO:
527 padapter->ledpriv.LedStrategy = SW_LED_MODE1;
528 padapter->ledpriv.bRegUseLed = true;
530 case RT_CID_819x_Sitecom:
531 padapter->ledpriv.LedStrategy = SW_LED_MODE2;
532 padapter->ledpriv.bRegUseLed = true;
535 case RT_CID_819x_Senao:
536 padapter->ledpriv.LedStrategy = SW_LED_MODE3;
537 padapter->ledpriv.bRegUseLed = true;
539 case RT_CID_819x_Edimax_Belkin:
540 padapter->ledpriv.LedStrategy = SW_LED_MODE4;
541 padapter->ledpriv.bRegUseLed = true;
543 case RT_CID_819x_Sercomm_Belkin:
544 padapter->ledpriv.LedStrategy = SW_LED_MODE5;
545 padapter->ledpriv.bRegUseLed = true;
547 case RT_CID_819x_WNC_COREGA:
548 padapter->ledpriv.LedStrategy = SW_LED_MODE6;
549 padapter->ledpriv.bRegUseLed = true;
552 padapter->ledpriv.LedStrategy = SW_LED_MODE0;
553 padapter->ledpriv.bRegUseLed = false;
557 AutoloadFail = false;
558 if (((mac[0] == 0xff) && (mac[1] == 0xff) &&
559 (mac[2] == 0xff) && (mac[3] == 0xff) &&
560 (mac[4] == 0xff) && (mac[5] == 0xff)) ||
561 ((mac[0] == 0x00) && (mac[1] == 0x00) &&
562 (mac[2] == 0x00) && (mac[3] == 0x00) &&
563 (mac[4] == 0x00) && (mac[5] == 0x00)) ||
573 /* Make sure the user did not select a multicast
574 * address by setting bit 1 of first octet.
578 "r8712u: MAC Address from user = %pM\n", mac);
581 "r8712u: MAC Address from efuse = %pM\n", mac);
582 ether_addr_copy(pnetdev->dev_addr, mac);
584 /* step 6. Load the firmware asynchronously */
585 if (rtl871x_load_fw(padapter))
587 spin_lock_init(&padapter->lockRxFF0Filter);
588 mutex_init(&padapter->mutex_start);
592 usb_set_intfdata(pusb_intf, NULL);
593 if (padapter && padapter->dvobj_deinit != NULL)
594 padapter->dvobj_deinit(padapter);
596 free_netdev(pnetdev);
600 /* rmmod module & unplug(SurpriseRemoved) will call r871xu_dev_remove()
601 * => how to recognize both */
602 static void r871xu_dev_remove(struct usb_interface *pusb_intf)
604 struct net_device *pnetdev = usb_get_intfdata(pusb_intf);
605 struct usb_device *udev = interface_to_usbdev(pusb_intf);
608 struct _adapter *padapter = netdev_priv(pnetdev);
610 usb_set_intfdata(pusb_intf, NULL);
611 release_firmware(padapter->fw);
612 /* never exit with a firmware callback pending */
613 wait_for_completion(&padapter->rtl8712_fw_ready);
614 if (drvpriv.drv_registered == true)
615 padapter->bSurpriseRemoved = true;
616 unregister_netdev(pnetdev); /* will call netdev_close() */
617 flush_scheduled_work();
619 /* Stop driver mlme relation timer */
620 r8712_stop_drv_timers(padapter);
621 r871x_dev_unload(padapter);
622 r8712_free_drv_sw(padapter);
624 /* decrease the reference count of the usb device structure
628 /* If we didn't unplug usb dongle and remove/insert module, driver
629 * fails on sitesurvey for the first time when device is up.
630 * Reset usb port for sitesurvey fail issue. */
631 if (udev->state != USB_STATE_NOTATTACHED)
632 usb_reset_device(udev);
635 static int __init r8712u_drv_entry(void)
637 drvpriv.drv_registered = true;
638 return usb_register(&drvpriv.r871xu_drv);
641 static void __exit r8712u_drv_halt(void)
643 drvpriv.drv_registered = false;
644 usb_deregister(&drvpriv.r871xu_drv);
647 module_init(r8712u_drv_entry);
648 module_exit(r8712u_drv_halt);