]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/gdm72xx/gdm_usb.c
c7a22fa55638f45385b08d0e801d9dfe9a4775da
[karo-tx-linux.git] / drivers / staging / gdm72xx / gdm_usb.c
1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/usb.h>
17 #include <asm/byteorder.h>
18 #include <linux/kthread.h>
19
20 #include "gdm_usb.h"
21 #include "gdm_wimax.h"
22 #include "usb_boot.h"
23 #include "hci.h"
24
25 #include "usb_ids.h"
26
27 MODULE_DEVICE_TABLE(usb, id_table);
28
29 #define TX_BUF_SIZE     2048
30 #if defined(CONFIG_WIMAX_GDM72XX_WIMAX2)
31 #define RX_BUF_SIZE     (128*1024)      /* For packet aggregation */
32 #else
33 #define RX_BUF_SIZE     2048
34 #endif
35
36 #define GDM7205_PADDING         256
37
38 #define H2B(x)          __cpu_to_be16(x)
39 #define B2H(x)          __be16_to_cpu(x)
40 #define DB2H(x)         __be32_to_cpu(x)
41
42 #define DOWNLOAD_CONF_VALUE             0x21
43
44 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
45
46 static DECLARE_WAIT_QUEUE_HEAD(k_wait);
47 static LIST_HEAD(k_list);
48 static DEFINE_SPINLOCK(k_lock);
49 static int k_mode_stop;
50
51 #define K_WAIT_TIME     (2 * HZ / 100)
52
53 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
54
55 static int init_usb(struct usbwm_dev *udev);
56 static void release_usb(struct usbwm_dev *udev);
57
58 /*#define DEBUG */
59 #ifdef DEBUG
60 static void hexdump(char *title, u8 *data, int len)
61 {
62         int i;
63
64         printk(KERN_DEBUG "%s: length = %d\n", title, len);
65         for (i = 0; i < len; i++) {
66                 printk(KERN_DEBUG "%02x ", data[i]);
67                 if ((i & 0xf) == 0xf)
68                         printk(KERN_DEBUG "\n");
69         }
70         printk(KERN_DEBUG "\n");
71 }
72 #endif
73
74 static struct usb_tx *alloc_tx_struct(struct tx_cxt *tx)
75 {
76         struct usb_tx *t = NULL;
77
78         t = kzalloc(sizeof(*t), GFP_ATOMIC);
79         if (t == NULL)
80                 goto out;
81
82         t->urb = usb_alloc_urb(0, GFP_ATOMIC);
83         t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
84         if (t->urb == NULL || t->buf == NULL)
85                 goto out;
86
87         t->tx_cxt = tx;
88
89         return t;
90 out:
91         if (t) {
92                 usb_free_urb(t->urb);
93                 kfree(t->buf);
94                 kfree(t);
95         }
96         return NULL;
97 }
98
99 static void free_tx_struct(struct usb_tx *t)
100 {
101         if (t) {
102                 usb_free_urb(t->urb);
103                 kfree(t->buf);
104                 kfree(t);
105         }
106 }
107
108 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
109 {
110         struct usb_rx *r = NULL;
111
112         r = kmalloc(sizeof(*r), GFP_ATOMIC);
113         if (r == NULL)
114                 goto out;
115
116         memset(r, 0, sizeof(*r));
117
118         r->urb = usb_alloc_urb(0, GFP_ATOMIC);
119         r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
120         if (r->urb == NULL || r->buf == NULL)
121                 goto out;
122
123         r->rx_cxt = rx;
124         return r;
125 out:
126         if (r) {
127                 usb_free_urb(r->urb);
128                 kfree(r->buf);
129                 kfree(r);
130         }
131         return NULL;
132 }
133
134 static void free_rx_struct(struct usb_rx *r)
135 {
136         if (r) {
137                 usb_free_urb(r->urb);
138                 kfree(r->buf);
139                 kfree(r);
140         }
141 }
142
143 /* Before this function is called, spin lock should be locked. */
144 static struct usb_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
145 {
146         struct usb_tx *t;
147
148         if (list_empty(&tx->free_list)) {
149                 *no_spc = 1;
150                 return NULL;
151         }
152
153         t = list_entry(tx->free_list.next, struct usb_tx, list);
154         list_del(&t->list);
155
156         *no_spc = list_empty(&tx->free_list) ? 1 : 0;
157
158         return t;
159 }
160
161 /* Before this function is called, spin lock should be locked. */
162 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx *t)
163 {
164         list_add_tail(&t->list, &tx->free_list);
165 }
166
167 /* Before this function is called, spin lock should be locked. */
168 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
169 {
170         struct usb_rx *r;
171
172         if (list_empty(&rx->free_list)) {
173                 r = alloc_rx_struct(rx);
174                 if (r == NULL)
175                         return NULL;
176
177                 list_add(&r->list, &rx->free_list);
178         }
179
180         r = list_entry(rx->free_list.next, struct usb_rx, list);
181         list_del(&r->list);
182         list_add_tail(&r->list, &rx->used_list);
183
184         return r;
185 }
186
187 /* Before this function is called, spin lock should be locked. */
188 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
189 {
190         list_del(&r->list);
191         list_add(&r->list, &rx->free_list);
192 }
193
194 static int init_usb(struct usbwm_dev *udev)
195 {
196         int ret = 0, i;
197         struct tx_cxt   *tx = &udev->tx;
198         struct rx_cxt   *rx = &udev->rx;
199         struct usb_tx   *t;
200         struct usb_rx   *r;
201
202         INIT_LIST_HEAD(&tx->free_list);
203         INIT_LIST_HEAD(&tx->sdu_list);
204         INIT_LIST_HEAD(&tx->hci_list);
205 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
206         INIT_LIST_HEAD(&tx->pending_list);
207 #endif
208
209         INIT_LIST_HEAD(&rx->free_list);
210         INIT_LIST_HEAD(&rx->used_list);
211
212         spin_lock_init(&tx->lock);
213         spin_lock_init(&rx->lock);
214
215         for (i = 0; i < MAX_NR_SDU_BUF; i++) {
216                 t = alloc_tx_struct(tx);
217                 if (t == NULL) {
218                         ret = -ENOMEM;
219                         goto fail;
220                 }
221                 list_add(&t->list, &tx->free_list);
222         }
223
224         r = alloc_rx_struct(rx);
225         if (r == NULL) {
226                 ret = -ENOMEM;
227                 goto fail;
228         }
229
230         list_add(&r->list, &rx->free_list);
231         return ret;
232
233 fail:
234         release_usb(udev);
235         return ret;
236 }
237
238 static void release_usb(struct usbwm_dev *udev)
239 {
240         struct tx_cxt   *tx = &udev->tx;
241         struct rx_cxt   *rx = &udev->rx;
242         struct usb_tx   *t, *t_next;
243         struct usb_rx   *r, *r_next;
244
245         list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
246                 list_del(&t->list);
247                 free_tx_struct(t);
248         }
249
250         list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
251                 list_del(&t->list);
252                 free_tx_struct(t);
253         }
254
255         list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
256                 list_del(&t->list);
257                 free_tx_struct(t);
258         }
259
260         list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
261                 list_del(&r->list);
262                 free_rx_struct(r);
263         }
264
265         list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
266                 list_del(&r->list);
267                 free_rx_struct(r);
268         }
269 }
270
271 static void __gdm_usb_send_complete(struct urb *urb)
272 {
273         struct usb_tx *t = urb->context;
274         struct tx_cxt *tx = t->tx_cxt;
275         u8 *pkt = t->buf;
276         u16 cmd_evt;
277
278         /* Completion by usb_unlink_urb */
279         if (urb->status == -ECONNRESET)
280                 return;
281
282         if (t->callback)
283                 t->callback(t->cb_data);
284
285         /* Delete from sdu list or hci list. */
286         list_del(&t->list);
287
288         cmd_evt = (pkt[0] << 8) | pkt[1];
289         if (cmd_evt == WIMAX_TX_SDU)
290                 put_tx_struct(tx, t);
291         else
292                 free_tx_struct(t);
293 }
294
295 static void gdm_usb_send_complete(struct urb *urb)
296 {
297         struct usb_tx *t = urb->context;
298         struct tx_cxt *tx = t->tx_cxt;
299         unsigned long flags;
300
301         spin_lock_irqsave(&tx->lock, flags);
302         __gdm_usb_send_complete(urb);
303         spin_unlock_irqrestore(&tx->lock, flags);
304 }
305
306 static int gdm_usb_send(void *priv_dev, void *data, int len,
307                         void (*cb)(void *data), void *cb_data)
308 {
309         struct usbwm_dev *udev = priv_dev;
310         struct usb_device *usbdev = udev->usbdev;
311         struct tx_cxt *tx = &udev->tx;
312         struct usb_tx *t;
313         int padding = udev->padding;
314         int no_spc = 0, ret;
315         u8 *pkt = data;
316         u16 cmd_evt;
317         unsigned long flags;
318
319         if (!udev->usbdev) {
320                 printk(KERN_ERR "%s: No such device\n", __func__);
321                 return -ENODEV;
322         }
323
324         BUG_ON(len > TX_BUF_SIZE - padding - 1);
325
326         spin_lock_irqsave(&tx->lock, flags);
327
328         cmd_evt = (pkt[0] << 8) | pkt[1];
329         if (cmd_evt == WIMAX_TX_SDU) {
330                 t = get_tx_struct(tx, &no_spc);
331                 if (t == NULL) {
332                         /* This case must not happen. */
333                         spin_unlock_irqrestore(&tx->lock, flags);
334                         return -ENOSPC;
335                 }
336                 list_add_tail(&t->list, &tx->sdu_list);
337         } else {
338                 t = alloc_tx_struct(tx);
339                 if (t == NULL) {
340                         spin_unlock_irqrestore(&tx->lock, flags);
341                         return -ENOMEM;
342                 }
343                 list_add_tail(&t->list, &tx->hci_list);
344         }
345
346         memcpy(t->buf + padding, data, len);
347         t->callback = cb;
348         t->cb_data = cb_data;
349
350         /*
351          * In some cases, USB Module of WiMax is blocked when data size is
352          * the multiple of 512. So, increment length by one in that case.
353          */
354         if ((len % 512) == 0)
355                 len++;
356
357         usb_fill_bulk_urb(t->urb,
358                         usbdev,
359                         usb_sndbulkpipe(usbdev, 1),
360                         t->buf,
361                         len + padding,
362                         gdm_usb_send_complete,
363                         t);
364
365 #ifdef DEBUG
366         hexdump("usb_send", t->buf, len + padding);
367 #endif
368 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
369         if (usbdev->state & USB_STATE_SUSPENDED) {
370                 list_add_tail(&t->p_list, &tx->pending_list);
371                 schedule_work(&udev->pm_ws);
372                 goto out;
373         }
374 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
375
376 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
377         if (udev->bw_switch) {
378                 list_add_tail(&t->p_list, &tx->pending_list);
379                 goto out;
380         } else if (cmd_evt == WIMAX_SCAN) {
381                 struct rx_cxt *rx;
382                 struct usb_rx *r;
383
384                 rx = &udev->rx;
385
386                 list_for_each_entry(r, &rx->used_list, list)
387                         usb_unlink_urb(r->urb);
388                 udev->bw_switch = 1;
389
390                 spin_lock(&k_lock);
391                 list_add_tail(&udev->list, &k_list);
392                 spin_unlock(&k_lock);
393
394                 wake_up(&k_wait);
395         }
396 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
397
398         ret = usb_submit_urb(t->urb, GFP_ATOMIC);
399         if (ret)
400                 goto send_fail;
401
402 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
403         usb_mark_last_busy(usbdev);
404 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
405
406 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
407 out:
408 #endif
409         spin_unlock_irqrestore(&tx->lock, flags);
410
411         if (no_spc)
412                 return -ENOSPC;
413
414         return 0;
415
416 send_fail:
417         t->callback = NULL;
418         __gdm_usb_send_complete(t->urb);
419         spin_unlock_irqrestore(&tx->lock, flags);
420         return ret;
421 }
422
423 static void gdm_usb_rcv_complete(struct urb *urb)
424 {
425         struct usb_rx *r = urb->context;
426         struct rx_cxt *rx = r->rx_cxt;
427         struct usbwm_dev *udev = container_of(r->rx_cxt, struct usbwm_dev, rx);
428         struct tx_cxt *tx = &udev->tx;
429         struct usb_tx *t;
430         u16 cmd_evt;
431         unsigned long flags;
432
433 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
434         struct usb_device *dev = urb->dev;
435 #endif
436
437         /* Completion by usb_unlink_urb */
438         if (urb->status == -ECONNRESET)
439                 return;
440
441         spin_lock_irqsave(&tx->lock, flags);
442
443         if (!urb->status) {
444                 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
445 #ifdef DEBUG
446                 hexdump("usb_receive", r->buf, urb->actual_length);
447 #endif
448                 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
449                         if (r->buf[4] == 0) {
450 #ifdef DEBUG
451                                 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
452 #endif
453                                 list_for_each_entry(t, &tx->sdu_list, list)
454                                         usb_unlink_urb(t->urb);
455                         } else if (r->buf[4] == 1) {
456 #ifdef DEBUG
457                                 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
458 #endif
459                                 list_for_each_entry(t, &tx->sdu_list, list) {
460                                         usb_submit_urb(t->urb, GFP_ATOMIC);
461                                 }
462                                 /*
463                                  * If free buffer for sdu tx doesn't
464                                  * exist, then tx queue should not be
465                                  * woken. For this reason, don't pass
466                                  * the command, START_SDU_TX.
467                                  */
468                                 if (list_empty(&tx->free_list))
469                                         urb->actual_length = 0;
470                         }
471                 }
472         }
473
474         if (!urb->status && r->callback)
475                 r->callback(r->cb_data, r->buf, urb->actual_length);
476
477         spin_lock(&rx->lock);
478         put_rx_struct(rx, r);
479         spin_unlock(&rx->lock);
480
481         spin_unlock_irqrestore(&tx->lock, flags);
482
483 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
484         usb_mark_last_busy(dev);
485 #endif
486 }
487
488 static int gdm_usb_receive(void *priv_dev,
489                         void (*cb)(void *cb_data, void *data, int len),
490                         void *cb_data)
491 {
492         struct usbwm_dev *udev = priv_dev;
493         struct usb_device *usbdev = udev->usbdev;
494         struct rx_cxt *rx = &udev->rx;
495         struct usb_rx *r;
496         unsigned long flags;
497
498         if (!udev->usbdev) {
499                 printk(KERN_ERR "%s: No such device\n", __func__);
500                 return -ENODEV;
501         }
502
503         spin_lock_irqsave(&rx->lock, flags);
504         r = get_rx_struct(rx);
505         spin_unlock_irqrestore(&rx->lock, flags);
506
507         if (r == NULL)
508                 return -ENOMEM;
509
510         r->callback = cb;
511         r->cb_data = cb_data;
512
513         usb_fill_bulk_urb(r->urb,
514                         usbdev,
515                         usb_rcvbulkpipe(usbdev, 0x82),
516                         r->buf,
517                         RX_BUF_SIZE,
518                         gdm_usb_rcv_complete,
519                         r);
520
521         return usb_submit_urb(r->urb, GFP_ATOMIC);
522 }
523
524 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
525 static void do_pm_control(struct work_struct *work)
526 {
527         struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
528         struct tx_cxt *tx = &udev->tx;
529         int ret;
530         unsigned long flags;
531
532         ret = usb_autopm_get_interface(udev->intf);
533         if (!ret)
534                 usb_autopm_put_interface(udev->intf);
535
536         spin_lock_irqsave(&tx->lock, flags);
537         if (!(udev->usbdev->state & USB_STATE_SUSPENDED)
538                 && (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
539                 struct usb_tx *t, *temp;
540
541                 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
542                         list_del(&t->p_list);
543                         ret =  usb_submit_urb(t->urb, GFP_ATOMIC);
544
545                         if (ret) {
546                                 t->callback = NULL;
547                                 __gdm_usb_send_complete(t->urb);
548                         }
549                 }
550         }
551         spin_unlock_irqrestore(&tx->lock, flags);
552 }
553 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
554
555 static int gdm_usb_probe(struct usb_interface *intf,
556                                 const struct usb_device_id *id)
557 {
558         int ret = 0;
559         u8 bConfigurationValue;
560         struct phy_dev *phy_dev = NULL;
561         struct usbwm_dev *udev = NULL;
562         u16 idVendor, idProduct, bcdDevice;
563
564         struct usb_device *usbdev = interface_to_usbdev(intf);
565
566         usb_get_dev(usbdev);
567         bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
568
569         /*USB description is set up with Little-Endian*/
570         idVendor = L2H(usbdev->descriptor.idVendor);
571         idProduct = L2H(usbdev->descriptor.idProduct);
572         bcdDevice = L2H(usbdev->descriptor.bcdDevice);
573
574         printk(KERN_INFO "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
575                 idVendor, idProduct);
576         printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
577
578
579         if (idProduct == EMERGENCY_PID) {
580                 ret = usb_emergency(usbdev);
581                 goto out;
582         }
583
584         /* Support for EEPROM bootloader */
585         if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
586                 idProduct & B_DOWNLOAD) {
587                 ret = usb_boot(usbdev, bcdDevice);
588                 goto out;
589         }
590
591         phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
592         if (phy_dev == NULL) {
593                 ret = -ENOMEM;
594                 goto out;
595         }
596         udev = kzalloc(sizeof(*udev), GFP_KERNEL);
597         if (udev == NULL) {
598                 ret = -ENOMEM;
599                 goto out;
600         }
601
602         if (idProduct == 0x7205 || idProduct == 0x7206)
603                 udev->padding = GDM7205_PADDING;
604         else
605                 udev->padding = 0;
606
607         phy_dev->priv_dev = (void *)udev;
608         phy_dev->send_func = gdm_usb_send;
609         phy_dev->rcv_func = gdm_usb_receive;
610
611         ret = init_usb(udev);
612         if (ret < 0)
613                 goto out;
614
615         udev->usbdev = usbdev;
616
617 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
618         udev->intf = intf;
619
620         intf->needs_remote_wakeup = 1;
621         device_init_wakeup(&intf->dev, 1);
622
623         pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
624
625         INIT_WORK(&udev->pm_ws, do_pm_control);
626 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
627
628         ret = register_wimax_device(phy_dev, &intf->dev);
629
630 out:
631         if (ret) {
632                 kfree(phy_dev);
633                 kfree(udev);
634         }
635         usb_set_intfdata(intf, phy_dev);
636         return ret;
637 }
638
639 static void gdm_usb_disconnect(struct usb_interface *intf)
640 {
641         u8 bConfigurationValue;
642         struct phy_dev *phy_dev;
643         struct usbwm_dev *udev;
644         u16 idProduct;
645         struct usb_device *usbdev = interface_to_usbdev(intf);
646
647         bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
648         phy_dev = usb_get_intfdata(intf);
649
650         /*USB description is set up with Little-Endian*/
651         idProduct = L2H(usbdev->descriptor.idProduct);
652
653         if (idProduct != EMERGENCY_PID &&
654                         bConfigurationValue != DOWNLOAD_CONF_VALUE &&
655                         (idProduct & B_DOWNLOAD) == 0) {
656                 udev = phy_dev->priv_dev;
657                 udev->usbdev = NULL;
658
659                 unregister_wimax_device(phy_dev);
660                 release_usb(udev);
661                 kfree(udev);
662                 kfree(phy_dev);
663         }
664
665         usb_put_dev(usbdev);
666 }
667
668 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
669 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
670 {
671         struct phy_dev *phy_dev;
672         struct usbwm_dev *udev;
673         struct rx_cxt *rx;
674         struct usb_rx *r;
675
676         phy_dev = usb_get_intfdata(intf);
677         udev = phy_dev->priv_dev;
678         rx = &udev->rx;
679
680         list_for_each_entry(r, &rx->used_list, list)
681                 usb_unlink_urb(r->urb);
682
683         return 0;
684 }
685
686 static int gdm_resume(struct usb_interface *intf)
687 {
688         struct phy_dev *phy_dev;
689         struct usbwm_dev *udev;
690         struct rx_cxt *rx;
691         struct usb_rx *r;
692
693         phy_dev = usb_get_intfdata(intf);
694         udev = phy_dev->priv_dev;
695         rx = &udev->rx;
696
697         list_for_each_entry(r, &rx->used_list, list)
698                 usb_submit_urb(r->urb, GFP_ATOMIC);
699
700         return 0;
701 }
702
703 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
704
705 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
706 static int k_mode_thread(void *arg)
707 {
708         struct usbwm_dev *udev;
709         struct tx_cxt *tx;
710         struct rx_cxt *rx;
711         struct usb_tx *t, *temp;
712         struct usb_rx *r;
713         unsigned long flags, flags2, expire;
714         int ret;
715
716         daemonize("k_mode_wimax");
717
718         while (!k_mode_stop) {
719
720                 spin_lock_irqsave(&k_lock, flags2);
721                 while (!list_empty(&k_list)) {
722
723                         udev = list_entry(k_list.next, struct usbwm_dev, list);
724                         tx = &udev->tx;
725                         rx = &udev->rx;
726
727                         list_del(&udev->list);
728                         spin_unlock_irqrestore(&k_lock, flags2);
729
730                         expire = jiffies + K_WAIT_TIME;
731                         while (jiffies < expire)
732                                 schedule_timeout(K_WAIT_TIME);
733
734                         list_for_each_entry(r, &rx->used_list, list)
735                                 usb_submit_urb(r->urb, GFP_ATOMIC);
736
737                         spin_lock_irqsave(&tx->lock, flags);
738
739                         list_for_each_entry_safe(t, temp, &tx->pending_list,
740                                                 p_list) {
741                                 list_del(&t->p_list);
742                                 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
743
744                                 if (ret) {
745                                         t->callback = NULL;
746                                         __gdm_usb_send_complete(t->urb);
747                                 }
748                         }
749
750                         udev->bw_switch = 0;
751                         spin_unlock_irqrestore(&tx->lock, flags);
752
753                         spin_lock_irqsave(&k_lock, flags2);
754                 }
755                 spin_unlock_irqrestore(&k_lock, flags2);
756
757                 interruptible_sleep_on(&k_wait);
758         }
759         return 0;
760 }
761 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
762
763 static struct usb_driver gdm_usb_driver = {
764         .name = "gdm_wimax",
765         .probe = gdm_usb_probe,
766         .disconnect = gdm_usb_disconnect,
767         .id_table = id_table,
768 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
769         .supports_autosuspend = 1,
770         .suspend = gdm_suspend,
771         .resume = gdm_resume,
772         .reset_resume = gdm_resume,
773 #endif
774 };
775
776 static int __init usb_gdm_wimax_init(void)
777 {
778 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
779         kthread_run(k_mode_thread, NULL, "WiMax_thread");
780 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
781         return usb_register(&gdm_usb_driver);
782 }
783
784 static void __exit usb_gdm_wimax_exit(void)
785 {
786 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
787         k_mode_stop = 1;
788         wake_up(&k_wait);
789 #endif
790         usb_deregister(&gdm_usb_driver);
791 }
792
793 module_init(usb_gdm_wimax_init);
794 module_exit(usb_gdm_wimax_exit);
795
796 MODULE_VERSION(DRIVER_VERSION);
797 MODULE_DESCRIPTION("GCT WiMax Device Driver");
798 MODULE_AUTHOR("Ethan Park");
799 MODULE_LICENSE("GPL");