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