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