]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/rt2860/usb_main_dev.c
Staging: rt2860: fix coding style issue in mac_pci.h, mac_usb.h, rtmp_mac.h, rtmp_phy.h
[net-next-2.6.git] / drivers / staging / rt2860 / usb_main_dev.c
CommitLineData
ca97b838
BZ
1/*
2 *************************************************************************
3 * Ralink Tech Inc.
4 * 5F., No.36, Taiyuan St., Jhubei City,
5 * Hsinchu County 302,
6 * Taiwan, R.O.C.
7 *
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24 * *
25 *************************************************************************/
26
27#include "rt_config.h"
28
9f548a2a
BZ
29/* Following information will be show when you run 'modinfo' */
30/* *** If you have a solution for the bug in current version of driver, please mail to me. */
31/* Otherwise post to forum in ralinktech's web site(www.ralinktech.com) and let all users help you. *** */
ca97b838
BZ
32MODULE_AUTHOR("Paul Lin <paul_lin@ralinktech.com>");
33MODULE_DESCRIPTION("RT2870/RT3070 Wireless Lan Linux Driver");
34MODULE_LICENSE("GPL");
35#ifdef MODULE_VERSION
36MODULE_VERSION(STA_DRIVER_VERSION);
37#endif
38
ca97b838
BZ
39/* module table */
40struct usb_device_id rtusb_usb_id[] = {
41#ifdef RT2870
66cd8d6e
BZ
42 {USB_DEVICE(0x148F, 0x2770)}, /* Ralink */
43 {USB_DEVICE(0x148F, 0x2870)}, /* Ralink */
44 {USB_DEVICE(0x07B8, 0x2870)}, /* AboCom */
45 {USB_DEVICE(0x07B8, 0x2770)}, /* AboCom */
46 {USB_DEVICE(0x0DF6, 0x0039)}, /* Sitecom 2770 */
47 {USB_DEVICE(0x083A, 0x7512)}, /* Arcadyan 2770 */
48 {USB_DEVICE(0x0789, 0x0162)}, /* Logitec 2870 */
49 {USB_DEVICE(0x0789, 0x0163)}, /* Logitec 2870 */
50 {USB_DEVICE(0x0789, 0x0164)}, /* Logitec 2870 */
51 {USB_DEVICE(0x177f, 0x0302)}, /* lsusb */
52 {USB_DEVICE(0x0B05, 0x1731)}, /* Asus */
53 {USB_DEVICE(0x0B05, 0x1732)}, /* Asus */
54 {USB_DEVICE(0x0B05, 0x1742)}, /* Asus */
55 {USB_DEVICE(0x0DF6, 0x0017)}, /* Sitecom */
56 {USB_DEVICE(0x0DF6, 0x002B)}, /* Sitecom */
57 {USB_DEVICE(0x0DF6, 0x002C)}, /* Sitecom */
58 {USB_DEVICE(0x0DF6, 0x002D)}, /* Sitecom */
59 {USB_DEVICE(0x14B2, 0x3C06)}, /* Conceptronic */
60 {USB_DEVICE(0x14B2, 0x3C28)}, /* Conceptronic */
61 {USB_DEVICE(0x2019, 0xED06)}, /* Planex Communications, Inc. */
62 {USB_DEVICE(0x07D1, 0x3C09)}, /* D-Link */
63 {USB_DEVICE(0x07D1, 0x3C11)}, /* D-Link */
64 {USB_DEVICE(0x14B2, 0x3C07)}, /* AL */
65 {USB_DEVICE(0x050D, 0x8053)}, /* Belkin */
66 {USB_DEVICE(0x14B2, 0x3C23)}, /* Airlink */
67 {USB_DEVICE(0x14B2, 0x3C27)}, /* Airlink */
68 {USB_DEVICE(0x07AA, 0x002F)}, /* Corega */
69 {USB_DEVICE(0x07AA, 0x003C)}, /* Corega */
70 {USB_DEVICE(0x07AA, 0x003F)}, /* Corega */
71 {USB_DEVICE(0x1044, 0x800B)}, /* Gigabyte */
72 {USB_DEVICE(0x15A9, 0x0006)}, /* Sparklan */
73 {USB_DEVICE(0x083A, 0xB522)}, /* SMC */
74 {USB_DEVICE(0x083A, 0xA618)}, /* SMC */
75 {USB_DEVICE(0x083A, 0x8522)}, /* Arcadyan */
76 {USB_DEVICE(0x083A, 0x7522)}, /* Arcadyan */
77 {USB_DEVICE(0x0CDE, 0x0022)}, /* ZCOM */
78 {USB_DEVICE(0x0586, 0x3416)}, /* Zyxel */
79 {USB_DEVICE(0x0CDE, 0x0025)}, /* Zyxel */
80 {USB_DEVICE(0x1740, 0x9701)}, /* EnGenius */
81 {USB_DEVICE(0x1740, 0x9702)}, /* EnGenius */
82 {USB_DEVICE(0x0471, 0x200f)}, /* Philips */
83 {USB_DEVICE(0x14B2, 0x3C25)}, /* Draytek */
84 {USB_DEVICE(0x13D3, 0x3247)}, /* AzureWave */
85 {USB_DEVICE(0x083A, 0x6618)}, /* Accton */
86 {USB_DEVICE(0x15c5, 0x0008)}, /* Amit */
87 {USB_DEVICE(0x0E66, 0x0001)}, /* Hawking */
88 {USB_DEVICE(0x0E66, 0x0003)}, /* Hawking */
89 {USB_DEVICE(0x129B, 0x1828)}, /* Siemens */
90 {USB_DEVICE(0x157E, 0x300E)}, /* U-Media */
91 {USB_DEVICE(0x050d, 0x805c)},
92 {USB_DEVICE(0x050d, 0x815c)},
93 {USB_DEVICE(0x1482, 0x3C09)}, /* Abocom */
94 {USB_DEVICE(0x14B2, 0x3C09)}, /* Alpha */
95 {USB_DEVICE(0x04E8, 0x2018)}, /* samsung */
96 {USB_DEVICE(0x5A57, 0x0280)}, /* Zinwell */
97 {USB_DEVICE(0x5A57, 0x0282)}, /* Zinwell */
98 {USB_DEVICE(0x7392, 0x7718)},
99 {USB_DEVICE(0x7392, 0x7717)},
100 {USB_DEVICE(0x1737, 0x0070)}, /* Linksys WUSB100 */
101 {USB_DEVICE(0x1737, 0x0071)}, /* Linksys WUSB600N */
102 {USB_DEVICE(0x0411, 0x00e8)}, /* Buffalo WLI-UC-G300N */
103 {USB_DEVICE(0x050d, 0x815c)}, /* Belkin F5D8053 */
9f548a2a 104#endif /* RT2870 // */
ca97b838 105#ifdef RT3070
66cd8d6e
BZ
106 {USB_DEVICE(0x148F, 0x3070)}, /* Ralink 3070 */
107 {USB_DEVICE(0x148F, 0x3071)}, /* Ralink 3071 */
108 {USB_DEVICE(0x148F, 0x3072)}, /* Ralink 3072 */
109 {USB_DEVICE(0x0DB0, 0x3820)}, /* Ralink 3070 */
110 {USB_DEVICE(0x0DF6, 0x003E)}, /* Sitecom 3070 */
111 {USB_DEVICE(0x0DF6, 0x0042)}, /* Sitecom 3072 */
112 {USB_DEVICE(0x14B2, 0x3C12)}, /* AL 3070 */
113 {USB_DEVICE(0x18C5, 0x0012)}, /* Corega 3070 */
114 {USB_DEVICE(0x083A, 0x7511)}, /* Arcadyan 3070 */
115 {USB_DEVICE(0x1740, 0x9703)}, /* EnGenius 3070 */
116 {USB_DEVICE(0x1740, 0x9705)}, /* EnGenius 3071 */
117 {USB_DEVICE(0x1740, 0x9706)}, /* EnGenius 3072 */
118 {USB_DEVICE(0x13D3, 0x3273)}, /* AzureWave 3070 */
119 {USB_DEVICE(0x1044, 0x800D)}, /* Gigabyte GN-WB32L 3070 */
120 {USB_DEVICE(0x2019, 0xAB25)}, /* Planex Communications, Inc. RT3070 */
121 {USB_DEVICE(0x07B8, 0x3070)}, /* AboCom 3070 */
122 {USB_DEVICE(0x07B8, 0x3071)}, /* AboCom 3071 */
123 {USB_DEVICE(0x07B8, 0x3072)}, /* Abocom 3072 */
124 {USB_DEVICE(0x7392, 0x7711)}, /* Edimax 3070 */
125 {USB_DEVICE(0x1A32, 0x0304)}, /* Quanta 3070 */
126 {USB_DEVICE(0x1EDA, 0x2310)}, /* AirTies 3070 */
127 {USB_DEVICE(0x07D1, 0x3C0A)}, /* D-Link 3072 */
128 {USB_DEVICE(0x07D1, 0x3C0D)}, /* D-Link 3070 */
129 {USB_DEVICE(0x07D1, 0x3C0E)}, /* D-Link 3070 */
130 {USB_DEVICE(0x07D1, 0x3C0F)}, /* D-Link 3070 */
131 {USB_DEVICE(0x1D4D, 0x000C)}, /* Pegatron Corporation 3070 */
132 {USB_DEVICE(0x1D4D, 0x000E)}, /* Pegatron Corporation 3070 */
133 {USB_DEVICE(0x5A57, 0x5257)}, /* Zinwell 3070 */
134 {USB_DEVICE(0x5A57, 0x0283)}, /* Zinwell 3072 */
135 {USB_DEVICE(0x04BB, 0x0945)}, /* I-O DATA 3072 */
136 {USB_DEVICE(0x203D, 0x1480)}, /* Encore 3070 */
9f548a2a 137#endif /* RT3070 // */
66cd8d6e
BZ
138 {USB_DEVICE(0x0DF6, 0x003F)}, /* Sitecom WL-608 */
139 {USB_DEVICE(0x1737, 0x0077)}, /* Linksys WUSB54GC-EU v3 */
140 {USB_DEVICE(0x2001, 0x3C09)}, /* D-Link */
141 {USB_DEVICE(0x2001, 0x3C0A)}, /* D-Link 3072 */
142 {USB_DEVICE(0x2019, 0xED14)}, /* Planex Communications, Inc. */
143 {} /* Terminating entry */
ca97b838
BZ
144};
145
51126deb 146int const rtusb_usb_id_len =
66cd8d6e 147 sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
ca97b838
BZ
148
149MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
150
62eb734b 151static void rt2870_disconnect(struct usb_device *dev, struct rt_rtmp_adapter *pAd);
ca97b838 152
66cd8d6e
BZ
153static int __devinit rt2870_probe(IN struct usb_interface *intf,
154 IN struct usb_device *usb_dev,
155 IN const struct usb_device_id *dev_id,
62eb734b 156 struct rt_rtmp_adapter ** ppAd);
ca97b838
BZ
157
158#ifndef PF_NOFREEZE
159#define PF_NOFREEZE 0
160#endif
161
ca97b838
BZ
162extern int rt28xx_close(IN struct net_device *net_dev);
163extern int rt28xx_open(struct net_device *net_dev);
164
66cd8d6e
BZ
165static BOOLEAN USBDevConfigInit(IN struct usb_device *dev,
166 IN struct usb_interface *intf,
62eb734b 167 struct rt_rtmp_adapter *pAd);
ca97b838
BZ
168
169/*
170========================================================================
171Routine Description:
172 Check the chipset vendor/product ID.
173
174Arguments:
175 _dev_p Point to the PCI or USB device
176
177Return Value:
178 TRUE Check ok
179 FALSE Check fail
180
181Note:
182========================================================================
183*/
66cd8d6e 184BOOLEAN RT28XXChipsetCheck(IN void *_dev_p)
ca97b838
BZ
185{
186 struct usb_interface *intf = (struct usb_interface *)_dev_p;
187 struct usb_device *dev_p = interface_to_usbdev(intf);
51126deb 188 u32 i;
ca97b838 189
66cd8d6e 190 for (i = 0; i < rtusb_usb_id_len; i++) {
ca97b838 191 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
66cd8d6e 192 dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct) {
ca97b838 193 printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
66cd8d6e
BZ
194 dev_p->descriptor.idVendor,
195 dev_p->descriptor.idProduct);
ca97b838
BZ
196 break;
197 }
198 }
199
66cd8d6e 200 if (i == rtusb_usb_id_len) {
ca97b838
BZ
201 printk("rt2870: Error! Device Descriptor not matching!\n");
202 return FALSE;
203 }
204
205 return TRUE;
206}
207
208/**************************************************************************/
209/**************************************************************************/
9f548a2a 210/*tested for kernel 2.6series */
ca97b838
BZ
211/**************************************************************************/
212/**************************************************************************/
213
214#ifdef CONFIG_PM
215static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
216static int rt2870_resume(struct usb_interface *intf);
9f548a2a 217#endif /* CONFIG_PM // */
ca97b838 218
66cd8d6e
BZ
219static BOOLEAN USBDevConfigInit(IN struct usb_device *dev,
220 IN struct usb_interface *intf,
62eb734b 221 struct rt_rtmp_adapter *pAd)
ca97b838
BZ
222{
223 struct usb_host_interface *iface_desc;
51126deb
BZ
224 unsigned long BulkOutIdx;
225 u32 i;
ca97b838 226
ca97b838
BZ
227 /* get the active interface descriptor */
228 iface_desc = intf->cur_altsetting;
229
230 /* get # of enpoints */
231 pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
66cd8d6e
BZ
232 DBGPRINT(RT_DEBUG_TRACE,
233 ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
ca97b838
BZ
234
235 /* Configure Pipes */
236 BulkOutIdx = 0;
237
66cd8d6e 238 for (i = 0; i < pAd->NumberOfPipes; i++) {
ca97b838 239 if ((iface_desc->endpoint[i].desc.bmAttributes ==
66cd8d6e
BZ
240 USB_ENDPOINT_XFER_BULK) &&
241 ((iface_desc->endpoint[i].desc.bEndpointAddress &
242 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)) {
243 pAd->BulkInEpAddr =
244 iface_desc->endpoint[i].desc.bEndpointAddress;
245 pAd->BulkInMaxPacketSize =
246 le2cpu16(iface_desc->endpoint[i].desc.
247 wMaxPacketSize);
248
249 DBGPRINT_RAW(RT_DEBUG_TRACE,
250 ("BULK IN MaxPacketSize = %d\n",
251 pAd->BulkInMaxPacketSize));
252 DBGPRINT_RAW(RT_DEBUG_TRACE,
253 ("EP address = 0x%2x\n",
254 iface_desc->endpoint[i].desc.
255 bEndpointAddress));
256 } else
257 if ((iface_desc->endpoint[i].desc.bmAttributes ==
258 USB_ENDPOINT_XFER_BULK)
259 &&
260 ((iface_desc->endpoint[i].desc.
261 bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
262 USB_DIR_OUT)) {
9f548a2a
BZ
263 /* there are 6 bulk out EP. EP6 highest priority. */
264 /* EP1-4 is EDCA. EP5 is HCCA. */
66cd8d6e
BZ
265 pAd->BulkOutEpAddr[BulkOutIdx++] =
266 iface_desc->endpoint[i].desc.bEndpointAddress;
267 pAd->BulkOutMaxPacketSize =
268 le2cpu16(iface_desc->endpoint[i].desc.
269 wMaxPacketSize);
270
271 DBGPRINT_RAW(RT_DEBUG_TRACE,
272 ("BULK OUT MaxPacketSize = %d\n",
273 pAd->BulkOutMaxPacketSize));
274 DBGPRINT_RAW(RT_DEBUG_TRACE,
275 ("EP address = 0x%2x \n",
276 iface_desc->endpoint[i].desc.
277 bEndpointAddress));
ca97b838
BZ
278 }
279 }
280
66cd8d6e
BZ
281 if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0])) {
282 printk
283 ("%s: Could not find both bulk-in and bulk-out endpoints\n",
284 __FUNCTION__);
ca97b838
BZ
285 return FALSE;
286 }
287
288 pAd->config = &dev->config->desc;
289 usb_set_intfdata(intf, pAd);
290
291 return TRUE;
292
293}
294
c33c2065 295static int __devinit rtusb_probe(struct usb_interface *intf,
66cd8d6e 296 const struct usb_device_id *id)
ca97b838 297{
62eb734b 298 struct rt_rtmp_adapter *pAd;
ca97b838
BZ
299 struct usb_device *dev;
300 int rv;
301
302 dev = interface_to_usbdev(intf);
303 dev = usb_get_dev(dev);
304
305 rv = rt2870_probe(intf, dev, id, &pAd);
306 if (rv != 0)
307 usb_put_dev(dev);
308
309 return rv;
310}
311
ca97b838
BZ
312static void rtusb_disconnect(struct usb_interface *intf)
313{
66cd8d6e 314 struct usb_device *dev = interface_to_usbdev(intf);
62eb734b 315 struct rt_rtmp_adapter *pAd;
ca97b838
BZ
316
317 pAd = usb_get_intfdata(intf);
318 usb_set_intfdata(intf, NULL);
319
320 rt2870_disconnect(dev, pAd);
321}
322
ca97b838 323struct usb_driver rtusb_driver = {
66cd8d6e
BZ
324 .name = "rt2870",
325 .probe = rtusb_probe,
326 .disconnect = rtusb_disconnect,
327 .id_table = rtusb_usb_id,
ca97b838
BZ
328
329#ifdef CONFIG_PM
66cd8d6e
BZ
330suspend:rt2870_suspend,
331resume:rt2870_resume,
ca97b838 332#endif
66cd8d6e 333};
ca97b838
BZ
334
335#ifdef CONFIG_PM
336
62eb734b 337void RT2870RejectPendingPackets(struct rt_rtmp_adapter *pAd)
ca97b838 338{
9f548a2a
BZ
339 /* clear PS packets */
340 /* clear TxSw packets */
ca97b838
BZ
341}
342
66cd8d6e 343static int rt2870_suspend(struct usb_interface *intf, pm_message_t state)
ca97b838
BZ
344{
345 struct net_device *net_dev;
62eb734b 346 struct rt_rtmp_adapter *pAd = usb_get_intfdata(intf);
ca97b838 347
ca97b838
BZ
348 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
349 net_dev = pAd->net_dev;
350 netif_device_detach(net_dev);
351
352 pAd->PM_FlgSuspend = 1;
353 if (netif_running(net_dev)) {
354 RTUSBCancelPendingBulkInIRP(pAd);
355 RTUSBCancelPendingBulkOutIRP(pAd);
356 }
357 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
358 return 0;
359}
360
66cd8d6e 361static int rt2870_resume(struct usb_interface *intf)
ca97b838
BZ
362{
363 struct net_device *net_dev;
62eb734b 364 struct rt_rtmp_adapter *pAd = usb_get_intfdata(intf);
ca97b838 365
ca97b838
BZ
366 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
367
368 pAd->PM_FlgSuspend = 0;
369 net_dev = pAd->net_dev;
370 netif_device_attach(net_dev);
371 netif_start_queue(net_dev);
372 netif_carrier_on(net_dev);
373 netif_wake_queue(net_dev);
374
375 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
376 return 0;
377}
9f548a2a 378#endif /* CONFIG_PM // */
ca97b838 379
9f548a2a 380/* Init driver module */
51126deb 381int __init rtusb_init(void)
ca97b838
BZ
382{
383 printk("rtusb init --->\n");
384 return usb_register(&rtusb_driver);
385}
386
9f548a2a 387/* Deinit driver module */
51126deb 388void __exit rtusb_exit(void)
ca97b838
BZ
389{
390 usb_deregister(&rtusb_driver);
391 printk("<--- rtusb exit\n");
392}
393
394module_init(rtusb_init);
395module_exit(rtusb_exit);
396
ca97b838
BZ
397/*--------------------------------------------------------------------- */
398/* function declarations */
399/*--------------------------------------------------------------------- */
400
401/*
402========================================================================
403Routine Description:
404 MLME kernel thread.
405
406Arguments:
407 *Context the pAd, driver control block pointer
408
409Return Value:
410 0 close the thread
411
412Note:
413========================================================================
414*/
51126deb 415int MlmeThread(IN void *Context)
ca97b838 416{
62eb734b
BZ
417 struct rt_rtmp_adapter *pAd;
418 struct rt_rtmp_os_task *pTask;
ca97b838
BZ
419 int status;
420 status = 0;
421
62eb734b
BZ
422 pTask = (struct rt_rtmp_os_task *)Context;
423 pAd = (struct rt_rtmp_adapter *)pTask->priv;
ca97b838
BZ
424
425 RtmpOSTaskCustomize(pTask);
426
66cd8d6e 427 while (!pTask->task_killed) {
ca97b838
BZ
428#ifdef KTHREAD_SUPPORT
429 RTMP_WAIT_EVENT_INTERRUPTIBLE(pAd, pTask);
430#else
431 RTMP_SEM_EVENT_WAIT(&(pTask->taskSema), status);
432
433 /* unlock the device pointers */
66cd8d6e 434 if (status != 0) {
ca97b838
BZ
435 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
436 break;
437 }
438#endif
439
66cd8d6e 440 /* lock the device pointers , need to check if required */
9f548a2a 441 /*down(&(pAd->usbdev_semaphore)); */
ca97b838
BZ
442
443 if (!pAd->PM_FlgSuspend)
444 MlmeHandler(pAd);
445 }
446
447 /* notify the exit routine that we're actually exiting now
448 *
449 * complete()/wait_for_completion() is similar to up()/down(),
450 * except that complete() is safe in the case where the structure
451 * is getting deleted in a parallel mode of execution (i.e. just
452 * after the down() -- that's necessary for the thread-shutdown
453 * case.
454 *
455 * complete_and_exit() goes even further than this -- it is safe in
456 * the case that the thread of the caller is going away (not just
457 * the structure) -- this is necessary for the module-remove case.
458 * This is important in preemption kernels, which transfer the flow
459 * of execution immediately upon a complete().
460 */
66cd8d6e 461 DBGPRINT(RT_DEBUG_TRACE, ("<---%s\n", __FUNCTION__));
ca97b838
BZ
462#ifndef KTHREAD_SUPPORT
463 pTask->taskPID = THREAD_PID_INIT_VALUE;
66cd8d6e 464 complete_and_exit(&pTask->taskComplete, 0);
ca97b838
BZ
465#endif
466 return 0;
467
468}
469
ca97b838
BZ
470/*
471========================================================================
472Routine Description:
473 USB command kernel thread.
474
475Arguments:
476 *Context the pAd, driver control block pointer
477
478Return Value:
479 0 close the thread
480
481Note:
482========================================================================
483*/
51126deb 484int RTUSBCmdThread(IN void *Context)
ca97b838 485{
62eb734b
BZ
486 struct rt_rtmp_adapter *pAd;
487 struct rt_rtmp_os_task *pTask;
ca97b838
BZ
488 int status;
489 status = 0;
490
62eb734b
BZ
491 pTask = (struct rt_rtmp_os_task *)Context;
492 pAd = (struct rt_rtmp_adapter *)pTask->priv;
ca97b838
BZ
493
494 RtmpOSTaskCustomize(pTask);
495
496 NdisAcquireSpinLock(&pAd->CmdQLock);
497 pAd->CmdQ.CmdQState = RTMP_TASK_STAT_RUNNING;
498 NdisReleaseSpinLock(&pAd->CmdQLock);
499
66cd8d6e 500 while (pAd && pAd->CmdQ.CmdQState == RTMP_TASK_STAT_RUNNING) {
ca97b838
BZ
501#ifdef KTHREAD_SUPPORT
502 RTMP_WAIT_EVENT_INTERRUPTIBLE(pAd, pTask);
503#else
504 /* lock the device pointers */
505 RTMP_SEM_EVENT_WAIT(&(pTask->taskSema), status);
506
66cd8d6e 507 if (status != 0) {
ca97b838
BZ
508 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
509 break;
510 }
511#endif
512
513 if (pAd->CmdQ.CmdQState == RTMP_TASK_STAT_STOPED)
514 break;
515
516 if (!pAd->PM_FlgSuspend)
517 CMDHandler(pAd);
518 }
519
9f548a2a 520 if (pAd && !pAd->PM_FlgSuspend) { /* Clear the CmdQElements. */
62eb734b 521 struct rt_cmdqelmt *pCmdQElmt = NULL;
ca97b838
BZ
522
523 NdisAcquireSpinLock(&pAd->CmdQLock);
524 pAd->CmdQ.CmdQState = RTMP_TASK_STAT_STOPED;
66cd8d6e 525 while (pAd->CmdQ.size) {
ca97b838 526 RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
66cd8d6e
BZ
527 if (pCmdQElmt) {
528 if (pCmdQElmt->CmdFromNdis == TRUE) {
ca97b838 529 if (pCmdQElmt->buffer != NULL)
66cd8d6e
BZ
530 os_free_mem(pAd,
531 pCmdQElmt->buffer);
51126deb 532 os_free_mem(pAd, (u8 *)pCmdQElmt);
66cd8d6e
BZ
533 } else {
534 if ((pCmdQElmt->buffer != NULL)
535 && (pCmdQElmt->bufferlength != 0))
536 os_free_mem(pAd,
537 pCmdQElmt->buffer);
51126deb 538 os_free_mem(pAd, (u8 *)pCmdQElmt);
ca97b838
BZ
539 }
540 }
541 }
542
543 NdisReleaseSpinLock(&pAd->CmdQLock);
544 }
545 /* notify the exit routine that we're actually exiting now
546 *
547 * complete()/wait_for_completion() is similar to up()/down(),
548 * except that complete() is safe in the case where the structure
549 * is getting deleted in a parallel mode of execution (i.e. just
550 * after the down() -- that's necessary for the thread-shutdown
551 * case.
552 *
553 * complete_and_exit() goes even further than this -- it is safe in
554 * the case that the thread of the caller is going away (not just
555 * the structure) -- this is necessary for the module-remove case.
556 * This is important in preemption kernels, which transfer the flow
557 * of execution immediately upon a complete().
558 */
66cd8d6e 559 DBGPRINT(RT_DEBUG_TRACE, ("<---RTUSBCmdThread\n"));
ca97b838
BZ
560
561#ifndef KTHREAD_SUPPORT
562 pTask->taskPID = THREAD_PID_INIT_VALUE;
66cd8d6e 563 complete_and_exit(&pTask->taskComplete, 0);
ca97b838
BZ
564#endif
565 return 0;
566
567}
568
62eb734b 569void RTUSBWatchDog(struct rt_rtmp_adapter *pAd)
ca97b838 570{
62eb734b 571 struct rt_ht_tx_context *pHTTXContext;
66cd8d6e 572 int idx;
51126deb 573 unsigned long irqFlags;
66cd8d6e
BZ
574 PURB pUrb;
575 BOOLEAN needDumpSeq = FALSE;
51126deb
BZ
576 u32 MACValue;
577 u32 TxRxQ_Pcnt;
ca97b838
BZ
578
579 idx = 0;
580 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
66cd8d6e
BZ
581 if ((MACValue & 0xff) != 0) {
582 DBGPRINT(RT_DEBUG_TRACE,
06aea994 583 ("TX QUEUE 0 Not EMPTY(Value=0x%0x)!\n",
66cd8d6e 584 MACValue));
ca97b838 585 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
66cd8d6e
BZ
586 while ((MACValue & 0xff) != 0 && (idx++ < 10)) {
587 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
588 RTMPusecDelay(1);
ca97b838
BZ
589 }
590 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
591 }
592
66cd8d6e
BZ
593 if (pAd->watchDogRxOverFlowCnt >= 2) {
594 DBGPRINT(RT_DEBUG_TRACE,
595 ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
596 if ((!RTMP_TEST_FLAG
597 (pAd,
598 (fRTMP_ADAPTER_RESET_IN_PROGRESS |
599 fRTMP_ADAPTER_BULKIN_RESET |
600 fRTMP_ADAPTER_HALT_IN_PROGRESS |
601 fRTMP_ADAPTER_NIC_NOT_EXIST)))) {
602 DBGPRINT(RT_DEBUG_TRACE,
603 ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
ca97b838 604 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
66cd8d6e
BZ
605 RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN,
606 NULL, 0);
ca97b838
BZ
607 needDumpSeq = TRUE;
608 }
609 pAd->watchDogRxOverFlowCnt = 0;
610 }
611
612 RTUSBReadMACRegister(pAd, 0x438, &TxRxQ_Pcnt);
613
66cd8d6e 614 for (idx = 0; idx < NUM_OF_TX_RING; idx++) {
ca97b838
BZ
615 pUrb = NULL;
616
617 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
66cd8d6e
BZ
618 if ((pAd->BulkOutPending[idx] == TRUE)
619 && pAd->watchDogTxPendingCnt) {
51126deb 620 int actual_length = 0, transfer_buffer_length = 0;
66cd8d6e 621 BOOLEAN isDataPacket = FALSE;
ca97b838
BZ
622 pAd->watchDogTxPendingCnt[idx]++;
623
624 if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
66cd8d6e
BZ
625 (!RTMP_TEST_FLAG
626 (pAd,
627 (fRTMP_ADAPTER_RESET_IN_PROGRESS |
628 fRTMP_ADAPTER_HALT_IN_PROGRESS |
629 fRTMP_ADAPTER_NIC_NOT_EXIST |
630 fRTMP_ADAPTER_BULKOUT_RESET)))
631 ) {
9f548a2a 632 /* FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it! */
66cd8d6e 633 pHTTXContext =
62eb734b 634 (struct rt_ht_tx_context *)(&pAd->TxContext[idx]);
9f548a2a 635 if (pHTTXContext->IRPPending) { /* Check TxContext. */
ca97b838
BZ
636 pUrb = pHTTXContext->pUrb;
637
66cd8d6e
BZ
638 actual_length = pUrb->actual_length;
639 transfer_buffer_length =
640 pUrb->transfer_buffer_length;
641 isDataPacket = TRUE;
642 } else if (idx == MGMTPIPEIDX) {
62eb734b
BZ
643 struct rt_tx_context *pMLMEContext, *pNULLContext,
644 *pPsPollContext;
ca97b838 645
9f548a2a 646 /*Check MgmtContext. */
66cd8d6e 647 pMLMEContext =
62eb734b 648 (struct rt_tx_context *)(pAd->MgmtRing.
66cd8d6e
BZ
649 Cell[pAd->MgmtRing.
650 TxDmaIdx].
651 AllocVa);
652 pPsPollContext =
62eb734b 653 (struct rt_tx_context *)(&pAd->PsPollContext);
66cd8d6e 654 pNULLContext =
62eb734b 655 (struct rt_tx_context *)(&pAd->NullContext);
66cd8d6e
BZ
656
657 if (pMLMEContext->IRPPending) {
658 ASSERT(pMLMEContext->
659 IRPPending);
ca97b838 660 pUrb = pMLMEContext->pUrb;
66cd8d6e
BZ
661 } else if (pNULLContext->IRPPending) {
662 ASSERT(pNULLContext->
663 IRPPending);
ca97b838 664 pUrb = pNULLContext->pUrb;
66cd8d6e
BZ
665 } else if (pPsPollContext->IRPPending) {
666 ASSERT(pPsPollContext->
667 IRPPending);
ca97b838
BZ
668 pUrb = pPsPollContext->pUrb;
669 }
670 }
671
66cd8d6e
BZ
672 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx],
673 irqFlags);
ca97b838 674
ca58fb30
BZ
675 printk(KERN_INFO "%d:%lu LTL=%d , TL=%d L:%d\n",
676 idx, pAd->watchDogTxPendingCnt[idx],
677 pAd->TransferedLength[idx],
678 actual_length, transfer_buffer_length);
ca97b838 679
66cd8d6e 680 if (pUrb) {
ca97b838 681 if ((isDataPacket
66cd8d6e
BZ
682 && pAd->TransferedLength[idx] ==
683 actual_length
684 && pAd->TransferedLength[idx] <
685 transfer_buffer_length
686 && actual_length != 0
9f548a2a 687/* && TxRxQ_Pcnt==0 */
66cd8d6e
BZ
688 && pAd->watchDogTxPendingCnt[idx] >
689 3)
690 || isDataPacket == FALSE
691 || pAd->watchDogTxPendingCnt[idx] >
692 6) {
693 DBGPRINT(RT_DEBUG_TRACE,
694 ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n",
695 idx));
696 DBGPRINT(RT_DEBUG_TRACE,
697 ("Unlink the pending URB!\n"));
9f548a2a 698 /* unlink it now */
66cd8d6e 699 RTUSB_UNLINK_URB(pUrb);
9f548a2a
BZ
700 /* Sleep 200 microseconds to give cancellation time to work */
701 /*RTMPusecDelay(200); */
66cd8d6e 702 needDumpSeq = TRUE;
ca97b838 703 }
66cd8d6e
BZ
704 } else {
705 DBGPRINT(RT_DEBUG_ERROR,
06aea994 706 ("Unknown bulkOut URB maybe hanged!\n"));
ca97b838 707 }
66cd8d6e
BZ
708 } else {
709 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx],
710 irqFlags);
ca97b838
BZ
711 }
712
66cd8d6e
BZ
713 if (isDataPacket == TRUE)
714 pAd->TransferedLength[idx] = actual_length;
715 } else {
ca97b838
BZ
716 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
717 }
718 }
719
9f548a2a 720 /* For Sigma debug, dump the ba_reordering sequence. */
66cd8d6e 721 if ((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0)) {
51126deb 722 u16 Idx;
62eb734b 723 struct rt_ba_rec_entry *pBAEntry = NULL;
51126deb 724 u8 count = 0;
ca97b838
BZ
725 struct reordering_mpdu *mpdu_blk;
726
727 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
728
729 pBAEntry = &pAd->BATable.BARecEntry[Idx];
66cd8d6e
BZ
730 if ((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL)) {
731 DBGPRINT(RT_DEBUG_TRACE,
732 ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
ca97b838
BZ
733 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
734 mpdu_blk = pBAEntry->list.next;
66cd8d6e
BZ
735 while (mpdu_blk) {
736 DBGPRINT(RT_DEBUG_TRACE,
737 ("\t%d:Seq-%d, bAMSDU-%d!\n", count,
738 mpdu_blk->Sequence,
739 mpdu_blk->bAMSDU));
ca97b838
BZ
740 mpdu_blk = mpdu_blk->next;
741 count++;
742 }
743
66cd8d6e
BZ
744 DBGPRINT(RT_DEBUG_TRACE,
745 ("\npBAEntry->LastIndSeq=%d!\n",
746 pBAEntry->LastIndSeq));
ca97b838
BZ
747 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
748 }
749 }
750}
751
752/*
753========================================================================
754Routine Description:
755 Release allocated resources.
756
757Arguments:
758 *dev Point to the PCI or USB device
759 pAd driver control block pointer
760
761Return Value:
762 None
763
764Note:
765========================================================================
766*/
62eb734b 767static void rt2870_disconnect(struct usb_device *dev, struct rt_rtmp_adapter *pAd)
ca97b838 768{
66cd8d6e
BZ
769 DBGPRINT(RT_DEBUG_ERROR,
770 ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
771 dev->bus->bus_name, dev->devpath));
772 if (!pAd) {
ca97b838
BZ
773 usb_put_dev(dev);
774 printk("rtusb_disconnect: pAd == NULL!\n");
775 return;
776 }
777 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
778
9f548a2a 779 /* for debug, wait to show some messages to /proc system */
ca97b838
BZ
780 udelay(1);
781
ca97b838
BZ
782 RtmpPhyNetDevExit(pAd, pAd->net_dev);
783
9f548a2a 784 /* FIXME: Shall we need following delay and flush the schedule?? */
ca97b838
BZ
785 udelay(1);
786 flush_scheduled_work();
787 udelay(1);
788
9f548a2a 789 /* free the root net_device */
ca97b838
BZ
790 RtmpOSNetDevFree(pAd->net_dev);
791
792 RtmpRaDevCtrlExit(pAd);
793
9f548a2a 794 /* release a use of the usb device structure */
ca97b838
BZ
795 usb_put_dev(dev);
796 udelay(1);
797
798 DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
799}
800
66cd8d6e
BZ
801static int __devinit rt2870_probe(IN struct usb_interface *intf,
802 IN struct usb_device *usb_dev,
803 IN const struct usb_device_id *dev_id,
62eb734b 804 struct rt_rtmp_adapter ** ppAd)
ca97b838 805{
66cd8d6e 806 struct net_device *net_dev = NULL;
62eb734b 807 struct rt_rtmp_adapter *pAd = (struct rt_rtmp_adapter *)NULL;
51126deb
BZ
808 int status, rv;
809 void *handle;
62eb734b 810 struct rt_rtmp_os_netdev_op_hook netDevHook;
ca97b838
BZ
811
812 DBGPRINT(RT_DEBUG_TRACE, ("===>rt2870_probe()!\n"));
813
9f548a2a
BZ
814 /* Check chipset vendor/product ID */
815 /*if (RT28XXChipsetCheck(_dev_p) == FALSE) */
816 /* goto err_out; */
ca97b838 817
9f548a2a 818/*RtmpDevInit============================================= */
62eb734b 819 /* Allocate struct rt_rtmp_adapter adapter structure */
ca97b838 820 handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
66cd8d6e
BZ
821 if (handle == NULL) {
822 printk
823 ("rt2870_probe(): Allocate memory for os handle failed!\n");
ca97b838
BZ
824 return -ENOMEM;
825 }
8a10a546 826 ((struct os_cookie *)handle)->pUsb_Dev = usb_dev;
ca97b838
BZ
827
828 rv = RTMPAllocAdapterBlock(handle, &pAd);
66cd8d6e 829 if (rv != NDIS_STATUS_SUCCESS) {
ca97b838
BZ
830 kfree(handle);
831 goto err_out;
832 }
9f548a2a 833/*USBDevInit============================================== */
ca97b838
BZ
834 if (USBDevConfigInit(usb_dev, intf, pAd) == FALSE)
835 goto err_out_free_radev;
836
837 RtmpRaDevCtrlInit(pAd, RTMP_DEV_INF_USB);
838
9f548a2a 839/*NetDevInit============================================== */
ca97b838
BZ
840 net_dev = RtmpPhyNetDevInit(pAd, &netDevHook);
841 if (net_dev == NULL)
842 goto err_out_free_radev;
843
9f548a2a 844 /* Here are the net_device structure with usb specific parameters. */
ca97b838 845 /* for supporting Network Manager.
66cd8d6e
BZ
846 * Set the sysfs physical device reference for the network logical device if set prior to registration will
847 * cause a symlink during initialization.
ca97b838
BZ
848 */
849 SET_NETDEV_DEV(net_dev, &(usb_dev->dev));
850
66cd8d6e 851 pAd->StaCfg.OriDevType = net_dev->type;
ca97b838 852
9f548a2a
BZ
853/*All done, it's time to register the net device to linux kernel. */
854 /* Register this device */
ca97b838
BZ
855 status = RtmpOSNetDevAttach(net_dev, &netDevHook);
856 if (status != 0)
857 goto err_out_free_netdev;
858
859#ifdef KTHREAD_SUPPORT
860 init_waitqueue_head(&pAd->mlmeTask.kthread_q);
861 init_waitqueue_head(&pAd->timerTask.kthread_q);
862 init_waitqueue_head(&pAd->cmdQTask.kthread_q);
863#endif
864
865 *ppAd = pAd;
866
867 DBGPRINT(RT_DEBUG_TRACE, ("<===rt2870_probe()!\n"));
868
869 return 0;
870
871 /* --------------------------- ERROR HANDLE --------------------------- */
872err_out_free_netdev:
873 RtmpOSNetDevFree(net_dev);
874
875err_out_free_radev:
876 RTMPFreeAdapter(pAd);
877
878err_out:
879 *ppAd = NULL;
880
881 return -1;
882
883}