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/kernel.h>
16 #include <linux/usb.h>
17 #include <asm/byteorder.h>
18 #include <linux/kthread.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;
280 /* Completion by usb_unlink_urb */
281 if (urb->status == -ECONNRESET)
285 t->callback(t->cb_data);
287 /* Delete from sdu list or hci list. */
290 cmd_evt = (pkt[0] << 8) | pkt[1];
291 if (cmd_evt == WIMAX_TX_SDU)
292 put_tx_struct(tx, t);
297 static void gdm_usb_send_complete(struct urb *urb)
299 struct usb_tx *t = urb->context;
300 struct tx_cxt *tx = t->tx_cxt;
303 spin_lock_irqsave(&tx->lock, flags);
304 __gdm_usb_send_complete(urb);
305 spin_unlock_irqrestore(&tx->lock, flags);
308 static int gdm_usb_send(void *priv_dev, void *data, int len,
309 void (*cb)(void *data), void *cb_data)
311 struct usbwm_dev *udev = priv_dev;
312 struct usb_device *usbdev = udev->usbdev;
313 struct tx_cxt *tx = &udev->tx;
315 int padding = udev->padding;
322 printk(KERN_ERR "%s: No such device\n", __func__);
326 BUG_ON(len > TX_BUF_SIZE - padding - 1);
328 spin_lock_irqsave(&tx->lock, flags);
330 cmd_evt = (pkt[0] << 8) | pkt[1];
331 if (cmd_evt == WIMAX_TX_SDU) {
332 t = get_tx_struct(tx, &no_spc);
334 /* This case must not happen. */
335 spin_unlock_irqrestore(&tx->lock, flags);
338 list_add_tail(&t->list, &tx->sdu_list);
340 t = alloc_tx_struct(tx);
342 spin_unlock_irqrestore(&tx->lock, flags);
345 list_add_tail(&t->list, &tx->hci_list);
348 memcpy(t->buf + padding, data, len);
350 t->cb_data = cb_data;
353 * In some cases, USB Module of WiMax is blocked when data size is
354 * the multiple of 512. So, increment length by one in that case.
356 if ((len % 512) == 0)
359 usb_fill_bulk_urb(t->urb,
361 usb_sndbulkpipe(usbdev, 1),
364 gdm_usb_send_complete,
368 hexdump("usb_send", t->buf, len + padding);
370 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
371 if (usbdev->state & USB_STATE_SUSPENDED) {
372 list_add_tail(&t->p_list, &tx->pending_list);
373 schedule_work(&udev->pm_ws);
376 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
378 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
379 if (udev->bw_switch) {
380 list_add_tail(&t->p_list, &tx->pending_list);
382 } else if (cmd_evt == WIMAX_SCAN) {
388 list_for_each_entry(r, &rx->used_list, list)
389 usb_unlink_urb(r->urb);
393 list_add_tail(&udev->list, &k_list);
394 spin_unlock(&k_lock);
398 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
400 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
404 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
405 usb_mark_last_busy(usbdev);
406 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
408 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
411 spin_unlock_irqrestore(&tx->lock, flags);
420 __gdm_usb_send_complete(t->urb);
421 spin_unlock_irqrestore(&tx->lock, flags);
425 static void gdm_usb_rcv_complete(struct urb *urb)
427 struct usb_rx *r = urb->context;
428 struct rx_cxt *rx = r->rx_cxt;
429 struct usbwm_dev *udev = container_of(r->rx_cxt, struct usbwm_dev, rx);
430 struct tx_cxt *tx = &udev->tx;
435 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
436 struct usb_device *dev = urb->dev;
439 /* Completion by usb_unlink_urb */
440 if (urb->status == -ECONNRESET)
443 spin_lock_irqsave(&tx->lock, flags);
446 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
448 hexdump("usb_receive", r->buf, urb->actual_length);
450 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
451 if (r->buf[4] == 0) {
453 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
455 list_for_each_entry(t, &tx->sdu_list, list)
456 usb_unlink_urb(t->urb);
457 } else if (r->buf[4] == 1) {
459 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
461 list_for_each_entry(t, &tx->sdu_list, list) {
462 usb_submit_urb(t->urb, GFP_ATOMIC);
465 * If free buffer for sdu tx doesn't
466 * exist, then tx queue should not be
467 * woken. For this reason, don't pass
468 * the command, START_SDU_TX.
470 if (list_empty(&tx->free_list))
471 urb->actual_length = 0;
476 if (!urb->status && r->callback)
477 r->callback(r->cb_data, r->buf, urb->actual_length);
479 spin_lock(&rx->lock);
480 put_rx_struct(rx, r);
481 spin_unlock(&rx->lock);
483 spin_unlock_irqrestore(&tx->lock, flags);
485 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
486 usb_mark_last_busy(dev);
490 static int gdm_usb_receive(void *priv_dev,
491 void (*cb)(void *cb_data, void *data, int len),
494 struct usbwm_dev *udev = priv_dev;
495 struct usb_device *usbdev = udev->usbdev;
496 struct rx_cxt *rx = &udev->rx;
501 printk(KERN_ERR "%s: No such device\n", __func__);
505 spin_lock_irqsave(&rx->lock, flags);
506 r = get_rx_struct(rx);
507 spin_unlock_irqrestore(&rx->lock, flags);
513 r->cb_data = cb_data;
515 usb_fill_bulk_urb(r->urb,
517 usb_rcvbulkpipe(usbdev, 0x82),
520 gdm_usb_rcv_complete,
523 return usb_submit_urb(r->urb, GFP_ATOMIC);
526 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
527 static void do_pm_control(struct work_struct *work)
529 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
530 struct tx_cxt *tx = &udev->tx;
534 ret = usb_autopm_get_interface(udev->intf);
536 usb_autopm_put_interface(udev->intf);
538 spin_lock_irqsave(&tx->lock, flags);
539 if (!(udev->usbdev->state & USB_STATE_SUSPENDED)
540 && (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
541 struct usb_tx *t, *temp;
543 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
544 list_del(&t->p_list);
545 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
549 __gdm_usb_send_complete(t->urb);
553 spin_unlock_irqrestore(&tx->lock, flags);
555 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
557 static int gdm_usb_probe(struct usb_interface *intf,
558 const struct usb_device_id *id)
561 u8 bConfigurationValue;
562 struct phy_dev *phy_dev = NULL;
563 struct usbwm_dev *udev = NULL;
564 u16 idVendor, idProduct, bcdDevice;
566 struct usb_device *usbdev = interface_to_usbdev(intf);
569 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
571 /*USB description is set up with Little-Endian*/
572 idVendor = L2H(usbdev->descriptor.idVendor);
573 idProduct = L2H(usbdev->descriptor.idProduct);
574 bcdDevice = L2H(usbdev->descriptor.bcdDevice);
576 printk(KERN_INFO "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
577 idVendor, idProduct);
578 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
581 if (idProduct == EMERGENCY_PID) {
582 ret = usb_emergency(usbdev);
586 /* Support for EEPROM bootloader */
587 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
588 idProduct & B_DOWNLOAD) {
589 ret = usb_boot(usbdev, bcdDevice);
593 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
594 if (phy_dev == NULL) {
598 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
604 if (idProduct == 0x7205 || idProduct == 0x7206)
605 udev->padding = GDM7205_PADDING;
609 phy_dev->priv_dev = (void *)udev;
610 phy_dev->send_func = gdm_usb_send;
611 phy_dev->rcv_func = gdm_usb_receive;
613 ret = init_usb(udev);
617 udev->usbdev = usbdev;
619 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
622 intf->needs_remote_wakeup = 1;
623 device_init_wakeup(&intf->dev, 1);
625 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
627 INIT_WORK(&udev->pm_ws, do_pm_control);
628 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
630 ret = register_wimax_device(phy_dev, &intf->dev);
637 usb_set_intfdata(intf, phy_dev);
641 static void gdm_usb_disconnect(struct usb_interface *intf)
643 u8 bConfigurationValue;
644 struct phy_dev *phy_dev;
645 struct usbwm_dev *udev;
647 struct usb_device *usbdev = interface_to_usbdev(intf);
649 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
650 phy_dev = usb_get_intfdata(intf);
652 /*USB description is set up with Little-Endian*/
653 idProduct = L2H(usbdev->descriptor.idProduct);
655 if (idProduct != EMERGENCY_PID &&
656 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
657 (idProduct & B_DOWNLOAD) == 0) {
658 udev = phy_dev->priv_dev;
661 unregister_wimax_device(phy_dev);
670 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
671 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
673 struct phy_dev *phy_dev;
674 struct usbwm_dev *udev;
678 phy_dev = usb_get_intfdata(intf);
679 udev = phy_dev->priv_dev;
682 list_for_each_entry(r, &rx->used_list, list)
683 usb_unlink_urb(r->urb);
688 static int gdm_resume(struct usb_interface *intf)
690 struct phy_dev *phy_dev;
691 struct usbwm_dev *udev;
695 phy_dev = usb_get_intfdata(intf);
696 udev = phy_dev->priv_dev;
699 list_for_each_entry(r, &rx->used_list, list)
700 usb_submit_urb(r->urb, GFP_ATOMIC);
705 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
707 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
708 static int k_mode_thread(void *arg)
710 struct usbwm_dev *udev;
713 struct usb_tx *t, *temp;
715 unsigned long flags, flags2, expire;
718 daemonize("k_mode_wimax");
720 while (!k_mode_stop) {
722 spin_lock_irqsave(&k_lock, flags2);
723 while (!list_empty(&k_list)) {
725 udev = list_entry(k_list.next, struct usbwm_dev, list);
729 list_del(&udev->list);
730 spin_unlock_irqrestore(&k_lock, flags2);
732 expire = jiffies + K_WAIT_TIME;
733 while (jiffies < expire)
734 schedule_timeout(K_WAIT_TIME);
736 list_for_each_entry(r, &rx->used_list, list)
737 usb_submit_urb(r->urb, GFP_ATOMIC);
739 spin_lock_irqsave(&tx->lock, flags);
741 list_for_each_entry_safe(t, temp, &tx->pending_list,
743 list_del(&t->p_list);
744 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
748 __gdm_usb_send_complete(t->urb);
753 spin_unlock_irqrestore(&tx->lock, flags);
755 spin_lock_irqsave(&k_lock, flags2);
757 spin_unlock_irqrestore(&k_lock, flags2);
759 interruptible_sleep_on(&k_wait);
763 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
765 static struct usb_driver gdm_usb_driver = {
767 .probe = gdm_usb_probe,
768 .disconnect = gdm_usb_disconnect,
769 .id_table = id_table,
770 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
771 .supports_autosuspend = 1,
772 .suspend = gdm_suspend,
773 .resume = gdm_resume,
774 .reset_resume = gdm_resume,
778 static int __init usb_gdm_wimax_init(void)
780 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
781 kthread_run(k_mode_thread, NULL, "WiMax_thread");
782 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
783 return usb_register(&gdm_usb_driver);
786 static void __exit usb_gdm_wimax_exit(void)
788 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
792 usb_deregister(&gdm_usb_driver);
795 module_init(usb_gdm_wimax_init);
796 module_exit(usb_gdm_wimax_exit);
798 MODULE_VERSION(DRIVER_VERSION);
799 MODULE_DESCRIPTION("GCT WiMax Device Driver");
800 MODULE_AUTHOR("Ethan Park");
801 MODULE_LICENSE("GPL");