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