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