2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
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.
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.
14 #include <linux/module.h>
15 #include <linux/version.h>
16 #include <linux/kernel.h>
17 #include <linux/usb.h>
18 #include <asm/byteorder.h>
21 #include "gdm_wimax.h"
27 MODULE_DEVICE_TABLE(usb
, id_table
);
29 #define TX_BUF_SIZE 2048
30 #if defined(CONFIG_WIMAX_GDM72XX_WIMAX2)
31 #define RX_BUF_SIZE (128*1024) /* For packet aggregation */
33 #define RX_BUF_SIZE 2048
36 #define GDM7205_PADDING 256
38 #define H2B(x) __cpu_to_be16(x)
39 #define B2H(x) __be16_to_cpu(x)
40 #define DB2H(x) __be32_to_cpu(x)
42 #define DOWNLOAD_CONF_VALUE 0x21
44 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
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
;
51 #define K_WAIT_TIME (2 * HZ / 100)
53 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
55 static int init_usb(struct usbwm_dev
*udev
);
56 static void release_usb(struct usbwm_dev
*udev
);
60 static void hexdump(char *title
, u8
*data
, int len
)
64 printk(KERN_DEBUG
"%s: length = %d\n", title
, len
);
65 for (i
= 0; i
< len
; i
++) {
66 printk(KERN_DEBUG
"%02x ", data
[i
]);
68 printk(KERN_DEBUG
"\n");
70 printk(KERN_DEBUG
"\n");
74 static struct usb_tx
*alloc_tx_struct(struct tx_cxt
*tx
)
76 struct usb_tx
*t
= NULL
;
78 t
= kmalloc(sizeof(*t
), GFP_ATOMIC
);
82 memset(t
, 0, sizeof(*t
));
84 t
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
85 t
->buf
= kmalloc(TX_BUF_SIZE
, GFP_ATOMIC
);
86 if (t
->urb
== NULL
|| t
->buf
== NULL
)
101 static void free_tx_struct(struct usb_tx
*t
)
104 usb_free_urb(t
->urb
);
110 static struct usb_rx
*alloc_rx_struct(struct rx_cxt
*rx
)
112 struct usb_rx
*r
= NULL
;
114 r
= kmalloc(sizeof(*r
), GFP_ATOMIC
);
118 memset(r
, 0, sizeof(*r
));
120 r
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
121 r
->buf
= kmalloc(RX_BUF_SIZE
, GFP_ATOMIC
);
122 if (r
->urb
== NULL
|| r
->buf
== NULL
)
129 usb_free_urb(r
->urb
);
136 static void free_rx_struct(struct usb_rx
*r
)
139 usb_free_urb(r
->urb
);
145 /* Before this function is called, spin lock should be locked. */
146 static struct usb_tx
*get_tx_struct(struct tx_cxt
*tx
, int *no_spc
)
150 if (list_empty(&tx
->free_list
)) {
155 t
= list_entry(tx
->free_list
.next
, struct usb_tx
, list
);
158 *no_spc
= list_empty(&tx
->free_list
) ? 1 : 0;
163 /* Before this function is called, spin lock should be locked. */
164 static void put_tx_struct(struct tx_cxt
*tx
, struct usb_tx
*t
)
166 list_add_tail(&t
->list
, &tx
->free_list
);
169 /* Before this function is called, spin lock should be locked. */
170 static struct usb_rx
*get_rx_struct(struct rx_cxt
*rx
)
174 if (list_empty(&rx
->free_list
)) {
175 r
= alloc_rx_struct(rx
);
179 list_add(&r
->list
, &rx
->free_list
);
182 r
= list_entry(rx
->free_list
.next
, struct usb_rx
, list
);
184 list_add_tail(&r
->list
, &rx
->used_list
);
189 /* Before this function is called, spin lock should be locked. */
190 static void put_rx_struct(struct rx_cxt
*rx
, struct usb_rx
*r
)
193 list_add(&r
->list
, &rx
->free_list
);
196 static int init_usb(struct usbwm_dev
*udev
)
199 struct tx_cxt
*tx
= &udev
->tx
;
200 struct rx_cxt
*rx
= &udev
->rx
;
204 INIT_LIST_HEAD(&tx
->free_list
);
205 INIT_LIST_HEAD(&tx
->sdu_list
);
206 INIT_LIST_HEAD(&tx
->hci_list
);
207 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
208 INIT_LIST_HEAD(&tx
->pending_list
);
211 INIT_LIST_HEAD(&rx
->free_list
);
212 INIT_LIST_HEAD(&rx
->used_list
);
214 spin_lock_init(&tx
->lock
);
215 spin_lock_init(&rx
->lock
);
217 for (i
= 0; i
< MAX_NR_SDU_BUF
; i
++) {
218 t
= alloc_tx_struct(tx
);
223 list_add(&t
->list
, &tx
->free_list
);
226 r
= alloc_rx_struct(rx
);
232 list_add(&r
->list
, &rx
->free_list
);
240 static void release_usb(struct usbwm_dev
*udev
)
242 struct tx_cxt
*tx
= &udev
->tx
;
243 struct rx_cxt
*rx
= &udev
->rx
;
244 struct usb_tx
*t
, *t_next
;
245 struct usb_rx
*r
, *r_next
;
247 list_for_each_entry_safe(t
, t_next
, &tx
->sdu_list
, list
) {
252 list_for_each_entry_safe(t
, t_next
, &tx
->hci_list
, list
) {
257 list_for_each_entry_safe(t
, t_next
, &tx
->free_list
, list
) {
262 list_for_each_entry_safe(r
, r_next
, &rx
->free_list
, list
) {
267 list_for_each_entry_safe(r
, r_next
, &rx
->used_list
, list
) {
273 static void gdm_usb_send_complete(struct urb
*urb
)
275 struct usb_tx
*t
= urb
->context
;
276 struct tx_cxt
*tx
= t
->tx_cxt
;
281 /* Completion by usb_unlink_urb */
282 if (urb
->status
== -ECONNRESET
)
285 spin_lock_irqsave(&tx
->lock
, flags
);
288 t
->callback(t
->cb_data
);
290 /* Delete from sdu list or hci list. */
293 cmd_evt
= (pkt
[0] << 8) | pkt
[1];
294 if (cmd_evt
== WIMAX_TX_SDU
)
295 put_tx_struct(tx
, t
);
299 spin_unlock_irqrestore(&tx
->lock
, flags
);
302 static int gdm_usb_send(void *priv_dev
, void *data
, int len
,
303 void (*cb
)(void *data
), void *cb_data
)
305 struct usbwm_dev
*udev
= priv_dev
;
306 struct usb_device
*usbdev
= udev
->usbdev
;
307 struct tx_cxt
*tx
= &udev
->tx
;
309 int padding
= udev
->padding
;
316 printk(KERN_ERR
"%s: No such device\n", __func__
);
320 BUG_ON(len
> TX_BUF_SIZE
- padding
- 1);
322 spin_lock_irqsave(&tx
->lock
, flags
);
324 cmd_evt
= (pkt
[0] << 8) | pkt
[1];
325 if (cmd_evt
== WIMAX_TX_SDU
) {
326 t
= get_tx_struct(tx
, &no_spc
);
328 /* This case must not happen. */
329 spin_unlock_irqrestore(&tx
->lock
, flags
);
332 list_add_tail(&t
->list
, &tx
->sdu_list
);
334 t
= alloc_tx_struct(tx
);
336 spin_unlock_irqrestore(&tx
->lock
, flags
);
339 list_add_tail(&t
->list
, &tx
->hci_list
);
342 memcpy(t
->buf
+ padding
, data
, len
);
344 t
->cb_data
= cb_data
;
347 * In some cases, USB Module of WiMax is blocked when data size is
348 * the multiple of 512. So, increment length by one in that case.
350 if ((len
% 512) == 0)
353 usb_fill_bulk_urb(t
->urb
,
355 usb_sndbulkpipe(usbdev
, 1),
358 gdm_usb_send_complete
,
362 hexdump("usb_send", t
->buf
, len
+ padding
);
364 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
365 if (usbdev
->state
& USB_STATE_SUSPENDED
) {
366 list_add_tail(&t
->p_list
, &tx
->pending_list
);
367 schedule_work(&udev
->pm_ws
);
370 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
372 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
373 if (udev
->bw_switch
) {
374 list_add_tail(&t
->p_list
, &tx
->pending_list
);
376 } else if (cmd_evt
== WIMAX_SCAN
) {
382 list_for_each_entry(r
, &rx
->used_list
, list
)
383 usb_unlink_urb(r
->urb
);
387 list_add_tail(&udev
->list
, &k_list
);
388 spin_unlock(&k_lock
);
392 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
394 ret
= usb_submit_urb(t
->urb
, GFP_ATOMIC
);
398 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
399 usb_mark_last_busy(usbdev
);
400 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
402 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
405 spin_unlock_irqrestore(&tx
->lock
, flags
);
414 gdm_usb_send_complete(t
->urb
);
415 spin_unlock_irqrestore(&tx
->lock
, flags
);
419 static void gdm_usb_rcv_complete(struct urb
*urb
)
421 struct usb_rx
*r
= urb
->context
;
422 struct rx_cxt
*rx
= r
->rx_cxt
;
423 struct usbwm_dev
*udev
= container_of(r
->rx_cxt
, struct usbwm_dev
, rx
);
424 struct tx_cxt
*tx
= &udev
->tx
;
429 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
430 struct usb_device
*dev
= urb
->dev
;
433 /* Completion by usb_unlink_urb */
434 if (urb
->status
== -ECONNRESET
)
437 spin_lock_irqsave(&tx
->lock
, flags
);
440 cmd_evt
= (r
->buf
[0] << 8) | (r
->buf
[1]);
442 hexdump("usb_receive", r
->buf
, urb
->actual_length
);
444 if (cmd_evt
== WIMAX_SDU_TX_FLOW
) {
445 if (r
->buf
[4] == 0) {
447 printk(KERN_DEBUG
"WIMAX ==> STOP SDU TX\n");
449 list_for_each_entry(t
, &tx
->sdu_list
, list
)
450 usb_unlink_urb(t
->urb
);
451 } else if (r
->buf
[4] == 1) {
453 printk(KERN_DEBUG
"WIMAX ==> START SDU TX\n");
455 list_for_each_entry(t
, &tx
->sdu_list
, list
) {
456 usb_submit_urb(t
->urb
, GFP_ATOMIC
);
459 * If free buffer for sdu tx doesn't
460 * exist, then tx queue should not be
461 * woken. For this reason, don't pass
462 * the command, START_SDU_TX.
464 if (list_empty(&tx
->free_list
))
465 urb
->actual_length
= 0;
470 if (!urb
->status
&& r
->callback
)
471 r
->callback(r
->cb_data
, r
->buf
, urb
->actual_length
);
473 spin_lock(&rx
->lock
);
474 put_rx_struct(rx
, r
);
475 spin_unlock(&rx
->lock
);
477 spin_unlock_irqrestore(&tx
->lock
, flags
);
479 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
480 usb_mark_last_busy(dev
);
484 static int gdm_usb_receive(void *priv_dev
,
485 void (*cb
)(void *cb_data
, void *data
, int len
),
488 struct usbwm_dev
*udev
= priv_dev
;
489 struct usb_device
*usbdev
= udev
->usbdev
;
490 struct rx_cxt
*rx
= &udev
->rx
;
495 printk(KERN_ERR
"%s: No such device\n", __func__
);
499 spin_lock_irqsave(&rx
->lock
, flags
);
500 r
= get_rx_struct(rx
);
501 spin_unlock_irqrestore(&rx
->lock
, flags
);
507 r
->cb_data
= cb_data
;
509 usb_fill_bulk_urb(r
->urb
,
511 usb_rcvbulkpipe(usbdev
, 0x82),
514 gdm_usb_rcv_complete
,
517 return usb_submit_urb(r
->urb
, GFP_ATOMIC
);
520 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
521 static void do_pm_control(struct work_struct
*work
)
523 struct usbwm_dev
*udev
= container_of(work
, struct usbwm_dev
, pm_ws
);
524 struct tx_cxt
*tx
= &udev
->tx
;
528 ret
= usb_autopm_get_interface(udev
->intf
);
530 usb_autopm_put_interface(udev
->intf
);
532 spin_lock_irqsave(&tx
->lock
, flags
);
533 if (!(udev
->usbdev
->state
& USB_STATE_SUSPENDED
)
534 && (!list_empty(&tx
->hci_list
) || !list_empty(&tx
->sdu_list
))) {
535 struct usb_tx
*t
, *temp
;
537 list_for_each_entry_safe(t
, temp
, &tx
->pending_list
, p_list
) {
538 list_del(&t
->p_list
);
539 ret
= usb_submit_urb(t
->urb
, GFP_ATOMIC
);
543 gdm_usb_send_complete(t
->urb
);
547 spin_unlock_irqrestore(&tx
->lock
, flags
);
549 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
551 static int gdm_usb_probe(struct usb_interface
*intf
,
552 const struct usb_device_id
*id
)
555 u8 bConfigurationValue
;
556 struct phy_dev
*phy_dev
= NULL
;
557 struct usbwm_dev
*udev
= NULL
;
558 u16 idVendor
, idProduct
, bcdDevice
;
560 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
563 bConfigurationValue
= usbdev
->actconfig
->desc
.bConfigurationValue
;
565 /*USB description is set up with Little-Endian*/
566 idVendor
= L2H(usbdev
->descriptor
.idVendor
);
567 idProduct
= L2H(usbdev
->descriptor
.idProduct
);
568 bcdDevice
= L2H(usbdev
->descriptor
.bcdDevice
);
570 printk(KERN_INFO
"Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
571 idVendor
, idProduct
);
572 printk(KERN_INFO
"GCT WiMax driver version %s\n", DRIVER_VERSION
);
575 if (idProduct
== EMERGENCY_PID
) {
576 ret
= usb_emergency(usbdev
);
580 /* Support for EEPROM bootloader */
581 if (bConfigurationValue
== DOWNLOAD_CONF_VALUE
||
582 idProduct
& B_DOWNLOAD
) {
583 ret
= usb_boot(usbdev
, bcdDevice
);
587 phy_dev
= kmalloc(sizeof(*phy_dev
), GFP_KERNEL
);
588 if (phy_dev
== NULL
) {
592 udev
= kmalloc(sizeof(*udev
), GFP_KERNEL
);
598 memset(phy_dev
, 0, sizeof(*phy_dev
));
599 memset(udev
, 0, sizeof(*udev
));
601 if (idProduct
== 0x7205 || idProduct
== 0x7206)
602 udev
->padding
= GDM7205_PADDING
;
606 phy_dev
->priv_dev
= (void *)udev
;
607 phy_dev
->send_func
= gdm_usb_send
;
608 phy_dev
->rcv_func
= gdm_usb_receive
;
610 ret
= init_usb(udev
);
614 udev
->usbdev
= usbdev
;
616 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
619 intf
->needs_remote_wakeup
= 1;
620 device_init_wakeup(&intf
->dev
, 1);
622 pm_runtime_set_autosuspend_delay(&usbdev
->dev
, 10 * 1000); /* msec */
624 INIT_WORK(&udev
->pm_ws
, do_pm_control
);
625 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
627 ret
= register_wimax_device(phy_dev
);
634 usb_set_intfdata(intf
, phy_dev
);
638 static void gdm_usb_disconnect(struct usb_interface
*intf
)
640 u8 bConfigurationValue
;
641 struct phy_dev
*phy_dev
;
642 struct usbwm_dev
*udev
;
644 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
646 bConfigurationValue
= usbdev
->actconfig
->desc
.bConfigurationValue
;
647 phy_dev
= usb_get_intfdata(intf
);
649 /*USB description is set up with Little-Endian*/
650 idProduct
= L2H(usbdev
->descriptor
.idProduct
);
652 if (idProduct
!= EMERGENCY_PID
&&
653 bConfigurationValue
!= DOWNLOAD_CONF_VALUE
&&
654 (idProduct
& B_DOWNLOAD
) == 0) {
655 udev
= phy_dev
->priv_dev
;
658 unregister_wimax_device(phy_dev
);
667 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
668 static int gdm_suspend(struct usb_interface
*intf
, pm_message_t pm_msg
)
670 struct phy_dev
*phy_dev
;
671 struct usbwm_dev
*udev
;
675 phy_dev
= usb_get_intfdata(intf
);
676 udev
= phy_dev
->priv_dev
;
679 list_for_each_entry(r
, &rx
->used_list
, list
)
680 usb_unlink_urb(r
->urb
);
685 static int gdm_resume(struct usb_interface
*intf
)
687 struct phy_dev
*phy_dev
;
688 struct usbwm_dev
*udev
;
692 phy_dev
= usb_get_intfdata(intf
);
693 udev
= phy_dev
->priv_dev
;
696 list_for_each_entry(r
, &rx
->used_list
, list
)
697 usb_submit_urb(r
->urb
, GFP_ATOMIC
);
702 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
704 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
705 static int k_mode_thread(void *arg
)
707 struct usbwm_dev
*udev
;
710 struct usb_tx
*t
, *temp
;
712 unsigned long flags
, flags2
, expire
;
715 daemonize("k_mode_wimax");
717 while (!k_mode_stop
) {
719 spin_lock_irqsave(&k_lock
, flags2
);
720 while (!list_empty(&k_list
)) {
722 udev
= list_entry(k_list
.next
, struct usbwm_dev
, list
);
726 list_del(&udev
->list
);
727 spin_unlock_irqrestore(&k_lock
, flags2
);
729 expire
= jiffies
+ K_WAIT_TIME
;
730 while (jiffies
< expire
)
731 schedule_timeout(K_WAIT_TIME
);
733 list_for_each_entry(r
, &rx
->used_list
, list
)
734 usb_submit_urb(r
->urb
, GFP_ATOMIC
);
736 spin_lock_irqsave(&tx
->lock
, flags
);
738 list_for_each_entry_safe(t
, temp
, &tx
->pending_list
,
740 list_del(&t
->p_list
);
741 ret
= usb_submit_urb(t
->urb
, GFP_ATOMIC
);
745 gdm_usb_send_complete(t
->urb
);
750 spin_unlock_irqrestore(&tx
->lock
, flags
);
752 spin_lock_irqsave(&k_lock
, flags2
);
754 spin_unlock_irqrestore(&k_lock
, flags2
);
756 interruptible_sleep_on(&k_wait
);
760 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
762 static struct usb_driver gdm_usb_driver
= {
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
,
775 static int __init
usb_gdm_wimax_init(void)
777 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
778 kernel_thread(k_mode_thread
, NULL
, CLONE_KERNEL
);
779 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
780 return usb_register(&gdm_usb_driver
);
783 static void __exit
usb_gdm_wimax_exit(void)
785 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
789 usb_deregister(&gdm_usb_driver
);
792 module_init(usb_gdm_wimax_init
);
793 module_exit(usb_gdm_wimax_exit
);
795 MODULE_VERSION(DRIVER_VERSION
);
796 MODULE_DESCRIPTION("GCT WiMax Device Driver");
797 MODULE_AUTHOR("Ethan Park");
798 MODULE_LICENSE("GPL");