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