]> Pileus Git - ~andy/linux/blob - drivers/staging/gdm72xx/gdm_usb.c
Merge branch 'for-3.14-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[~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         if (ret)
639                 release_usb(udev);
640
641 out:
642         if (ret) {
643                 kfree(phy_dev);
644                 kfree(udev);
645                 usb_put_dev(usbdev);
646         } else {
647                 usb_set_intfdata(intf, phy_dev);
648         }
649         return ret;
650 }
651
652 static void gdm_usb_disconnect(struct usb_interface *intf)
653 {
654         u8 bConfigurationValue;
655         struct phy_dev *phy_dev;
656         struct usbwm_dev *udev;
657         u16 idProduct;
658         struct usb_device *usbdev = interface_to_usbdev(intf);
659
660         bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
661         phy_dev = usb_get_intfdata(intf);
662
663         /*USB description is set up with Little-Endian*/
664         idProduct = L2H(usbdev->descriptor.idProduct);
665
666         if (idProduct != EMERGENCY_PID &&
667                         bConfigurationValue != DOWNLOAD_CONF_VALUE &&
668                         (idProduct & B_DOWNLOAD) == 0) {
669                 udev = phy_dev->priv_dev;
670                 udev->usbdev = NULL;
671
672                 unregister_wimax_device(phy_dev);
673                 release_usb(udev);
674                 kfree(udev);
675                 kfree(phy_dev);
676         }
677
678         usb_put_dev(usbdev);
679 }
680
681 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
682 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
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_unlink_urb(r->urb);
701
702         spin_unlock_irqrestore(&rx->lock, flags);
703
704         return 0;
705 }
706
707 static int gdm_resume(struct usb_interface *intf)
708 {
709         struct phy_dev *phy_dev;
710         struct usbwm_dev *udev;
711         struct rx_cxt *rx;
712         struct usb_rx *r;
713         unsigned long flags;
714
715         phy_dev = usb_get_intfdata(intf);
716         if (!phy_dev)
717                 return 0;
718
719         udev = phy_dev->priv_dev;
720         rx = &udev->rx;
721
722         spin_lock_irqsave(&rx->lock, flags);
723
724         list_for_each_entry(r, &rx->used_list, list)
725                 usb_submit_urb(r->urb, GFP_ATOMIC);
726
727         spin_unlock_irqrestore(&rx->lock, flags);
728
729         return 0;
730 }
731
732 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
733
734 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
735 static int k_mode_thread(void *arg)
736 {
737         struct usbwm_dev *udev;
738         struct tx_cxt *tx;
739         struct rx_cxt *rx;
740         struct usb_tx *t, *temp;
741         struct usb_rx *r;
742         unsigned long flags, flags2, expire;
743         int ret;
744
745         while (!k_mode_stop) {
746
747                 spin_lock_irqsave(&k_lock, flags2);
748                 while (!list_empty(&k_list)) {
749
750                         udev = list_entry(k_list.next, struct usbwm_dev, list);
751                         tx = &udev->tx;
752                         rx = &udev->rx;
753
754                         list_del(&udev->list);
755                         spin_unlock_irqrestore(&k_lock, flags2);
756
757                         expire = jiffies + K_WAIT_TIME;
758                         while (jiffies < expire)
759                                 schedule_timeout(K_WAIT_TIME);
760
761                         spin_lock_irqsave(&rx->lock, flags);
762
763                         list_for_each_entry(r, &rx->used_list, list)
764                                 usb_submit_urb(r->urb, GFP_ATOMIC);
765
766                         spin_unlock_irqrestore(&rx->lock, flags);
767
768                         spin_lock_irqsave(&tx->lock, flags);
769
770                         list_for_each_entry_safe(t, temp, &tx->pending_list,
771                                                 p_list) {
772                                 list_del(&t->p_list);
773                                 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
774
775                                 if (ret) {
776                                         t->callback = NULL;
777                                         __gdm_usb_send_complete(t->urb);
778                                 }
779                         }
780
781                         udev->bw_switch = 0;
782                         spin_unlock_irqrestore(&tx->lock, flags);
783
784                         spin_lock_irqsave(&k_lock, flags2);
785                 }
786                 wait_event_interruptible_lock_irq(k_wait,
787                                                   !list_empty(&k_list) || k_mode_stop,
788                                                   k_lock);
789                 spin_unlock_irqrestore(&k_lock, flags2);
790         }
791         return 0;
792 }
793 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
794
795 static struct usb_driver gdm_usb_driver = {
796         .name = "gdm_wimax",
797         .probe = gdm_usb_probe,
798         .disconnect = gdm_usb_disconnect,
799         .id_table = id_table,
800 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
801         .supports_autosuspend = 1,
802         .suspend = gdm_suspend,
803         .resume = gdm_resume,
804         .reset_resume = gdm_resume,
805 #endif
806 };
807
808 static int __init usb_gdm_wimax_init(void)
809 {
810 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
811         kthread_run(k_mode_thread, NULL, "k_mode_wimax");
812 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
813         return usb_register(&gdm_usb_driver);
814 }
815
816 static void __exit usb_gdm_wimax_exit(void)
817 {
818 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
819         k_mode_stop = 1;
820         wake_up(&k_wait);
821 #endif
822         usb_deregister(&gdm_usb_driver);
823 }
824
825 module_init(usb_gdm_wimax_init);
826 module_exit(usb_gdm_wimax_exit);
827
828 MODULE_VERSION(DRIVER_VERSION);
829 MODULE_DESCRIPTION("GCT WiMax Device Driver");
830 MODULE_AUTHOR("Ethan Park");
831 MODULE_LICENSE("GPL");