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 = kzalloc(sizeof(*t), GFP_ATOMIC);
81 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
82 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
83 if (!t->urb || !t->buf) {
95 static void free_tx_struct(struct usb_tx *t)
104 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
106 struct usb_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
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);
124 static void free_rx_struct(struct usb_rx *r)
127 usb_free_urb(r->urb);
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)
138 if (list_empty(&tx->free_list)) {
143 t = list_entry(tx->free_list.next, struct usb_tx, list);
146 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
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)
154 list_add_tail(&t->list, &tx->free_list);
157 /* Before this function is called, spin lock should be locked. */
158 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
162 if (list_empty(&rx->free_list)) {
163 r = alloc_rx_struct(rx);
167 list_add(&r->list, &rx->free_list);
170 r = list_entry(rx->free_list.next, struct usb_rx, list);
171 list_move_tail(&r->list, &rx->used_list);
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)
179 list_move(&r->list, &rx->free_list);
182 static int init_usb(struct usbwm_dev *udev)
185 struct tx_cxt *tx = &udev->tx;
186 struct rx_cxt *rx = &udev->rx;
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);
198 INIT_LIST_HEAD(&rx->free_list);
199 INIT_LIST_HEAD(&rx->used_list);
201 spin_lock_init(&tx->lock);
202 spin_lock_init(&rx->lock);
204 spin_lock_irqsave(&tx->lock, flags);
205 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
206 t = alloc_tx_struct(tx);
208 spin_unlock_irqrestore(&tx->lock, flags);
212 list_add(&t->list, &tx->free_list);
214 spin_unlock_irqrestore(&tx->lock, flags);
216 r = alloc_rx_struct(rx);
222 spin_lock_irqsave(&rx->lock, flags);
223 list_add(&r->list, &rx->free_list);
224 spin_unlock_irqrestore(&rx->lock, flags);
232 static void release_usb(struct usbwm_dev *udev)
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;
240 spin_lock_irqsave(&tx->lock, flags);
242 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
247 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
252 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
257 spin_unlock_irqrestore(&tx->lock, flags);
259 spin_lock_irqsave(&rx->lock, flags);
261 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
266 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
271 spin_unlock_irqrestore(&rx->lock, flags);
274 static void __gdm_usb_send_complete(struct urb *urb)
276 struct usb_tx *t = urb->context;
277 struct tx_cxt *tx = t->tx_cxt;
281 /* Completion by usb_unlink_urb */
282 if (urb->status == -ECONNRESET)
286 t->callback(t->cb_data);
288 /* Delete from sdu list or hci list. */
291 cmd_evt = (pkt[0] << 8) | pkt[1];
292 if (cmd_evt == WIMAX_TX_SDU)
293 put_tx_struct(tx, t);
298 static void gdm_usb_send_complete(struct urb *urb)
300 struct usb_tx *t = urb->context;
301 struct tx_cxt *tx = t->tx_cxt;
304 spin_lock_irqsave(&tx->lock, flags);
305 __gdm_usb_send_complete(urb);
306 spin_unlock_irqrestore(&tx->lock, flags);
309 static int gdm_usb_send(void *priv_dev, void *data, int len,
310 void (*cb)(void *data), void *cb_data)
312 struct usbwm_dev *udev = priv_dev;
313 struct usb_device *usbdev = udev->usbdev;
314 struct tx_cxt *tx = &udev->tx;
316 int padding = udev->padding;
321 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
322 unsigned long flags2;
323 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
326 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
330 BUG_ON(len > TX_BUF_SIZE - padding - 1);
332 spin_lock_irqsave(&tx->lock, flags);
334 cmd_evt = (pkt[0] << 8) | pkt[1];
335 if (cmd_evt == WIMAX_TX_SDU) {
336 t = get_tx_struct(tx, &no_spc);
338 /* This case must not happen. */
339 spin_unlock_irqrestore(&tx->lock, flags);
342 list_add_tail(&t->list, &tx->sdu_list);
344 t = alloc_tx_struct(tx);
346 spin_unlock_irqrestore(&tx->lock, flags);
349 list_add_tail(&t->list, &tx->hci_list);
352 memcpy(t->buf + padding, data, len);
354 t->cb_data = cb_data;
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.
360 if ((len % 512) == 0)
363 usb_fill_bulk_urb(t->urb,
365 usb_sndbulkpipe(usbdev, 1),
368 gdm_usb_send_complete,
372 hexdump("usb_send", t->buf, len + padding);
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);
380 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
382 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
383 if (udev->bw_switch) {
384 list_add_tail(&t->p_list, &tx->pending_list);
386 } else if (cmd_evt == WIMAX_SCAN) {
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);
399 spin_lock_irqsave(&k_lock, flags2);
400 list_add_tail(&udev->list, &k_list);
401 spin_unlock_irqrestore(&k_lock, flags2);
405 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
407 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
411 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
412 usb_mark_last_busy(usbdev);
413 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
415 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
418 spin_unlock_irqrestore(&tx->lock, flags);
427 __gdm_usb_send_complete(t->urb);
428 spin_unlock_irqrestore(&tx->lock, flags);
432 static void gdm_usb_rcv_complete(struct urb *urb)
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;
440 unsigned long flags, flags2;
442 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
443 struct usb_device *dev = urb->dev;
446 /* Completion by usb_unlink_urb */
447 if (urb->status == -ECONNRESET)
450 spin_lock_irqsave(&tx->lock, flags);
453 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
455 hexdump("usb_receive", r->buf, urb->actual_length);
457 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
458 if (r->buf[4] == 0) {
460 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
462 list_for_each_entry(t, &tx->sdu_list, list)
463 usb_unlink_urb(t->urb);
464 } else if (r->buf[4] == 1) {
466 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
468 list_for_each_entry(t, &tx->sdu_list, list) {
469 usb_submit_urb(t->urb, GFP_ATOMIC);
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.
477 if (list_empty(&tx->free_list))
478 urb->actual_length = 0;
483 if (!urb->status && r->callback)
484 r->callback(r->cb_data, r->buf, urb->actual_length);
486 spin_lock_irqsave(&rx->lock, flags2);
487 put_rx_struct(rx, r);
488 spin_unlock_irqrestore(&rx->lock, flags2);
490 spin_unlock_irqrestore(&tx->lock, flags);
492 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
493 usb_mark_last_busy(dev);
497 static int gdm_usb_receive(void *priv_dev,
498 void (*cb)(void *cb_data, void *data, int len),
501 struct usbwm_dev *udev = priv_dev;
502 struct usb_device *usbdev = udev->usbdev;
503 struct rx_cxt *rx = &udev->rx;
508 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
512 spin_lock_irqsave(&rx->lock, flags);
513 r = get_rx_struct(rx);
514 spin_unlock_irqrestore(&rx->lock, flags);
520 r->cb_data = cb_data;
522 usb_fill_bulk_urb(r->urb,
524 usb_rcvbulkpipe(usbdev, 0x82),
527 gdm_usb_rcv_complete,
530 return usb_submit_urb(r->urb, GFP_ATOMIC);
533 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
534 static void do_pm_control(struct work_struct *work)
536 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
537 struct tx_cxt *tx = &udev->tx;
541 ret = usb_autopm_get_interface(udev->intf);
543 usb_autopm_put_interface(udev->intf);
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;
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);
556 __gdm_usb_send_complete(t->urb);
560 spin_unlock_irqrestore(&tx->lock, flags);
562 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
564 static int gdm_usb_probe(struct usb_interface *intf,
565 const struct usb_device_id *id)
568 u8 bConfigurationValue;
569 struct phy_dev *phy_dev = NULL;
570 struct usbwm_dev *udev = NULL;
571 u16 idVendor, idProduct, bcdDevice;
573 struct usb_device *usbdev = interface_to_usbdev(intf);
576 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
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);
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);
588 if (idProduct == EMERGENCY_PID) {
589 ret = usb_emergency(usbdev);
593 /* Support for EEPROM bootloader */
594 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
595 idProduct & B_DOWNLOAD) {
596 ret = usb_boot(usbdev, bcdDevice);
600 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
601 if (phy_dev == NULL) {
605 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
611 if (idProduct == 0x7205 || idProduct == 0x7206)
612 udev->padding = GDM7205_PADDING;
616 phy_dev->priv_dev = (void *)udev;
617 phy_dev->send_func = gdm_usb_send;
618 phy_dev->rcv_func = gdm_usb_receive;
620 ret = init_usb(udev);
624 udev->usbdev = usbdev;
626 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
629 intf->needs_remote_wakeup = 1;
630 device_init_wakeup(&intf->dev, 1);
632 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
634 INIT_WORK(&udev->pm_ws, do_pm_control);
635 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
637 ret = register_wimax_device(phy_dev, &intf->dev);
647 usb_set_intfdata(intf, phy_dev);
652 static void gdm_usb_disconnect(struct usb_interface *intf)
654 u8 bConfigurationValue;
655 struct phy_dev *phy_dev;
656 struct usbwm_dev *udev;
658 struct usb_device *usbdev = interface_to_usbdev(intf);
660 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
661 phy_dev = usb_get_intfdata(intf);
663 /*USB description is set up with Little-Endian*/
664 idProduct = L2H(usbdev->descriptor.idProduct);
666 if (idProduct != EMERGENCY_PID &&
667 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
668 (idProduct & B_DOWNLOAD) == 0) {
669 udev = phy_dev->priv_dev;
672 unregister_wimax_device(phy_dev);
681 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
682 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
684 struct phy_dev *phy_dev;
685 struct usbwm_dev *udev;
690 phy_dev = usb_get_intfdata(intf);
694 udev = phy_dev->priv_dev;
697 spin_lock_irqsave(&rx->lock, flags);
699 list_for_each_entry(r, &rx->used_list, list)
700 usb_unlink_urb(r->urb);
702 spin_unlock_irqrestore(&rx->lock, flags);
707 static int gdm_resume(struct usb_interface *intf)
709 struct phy_dev *phy_dev;
710 struct usbwm_dev *udev;
715 phy_dev = usb_get_intfdata(intf);
719 udev = phy_dev->priv_dev;
722 spin_lock_irqsave(&rx->lock, flags);
724 list_for_each_entry(r, &rx->used_list, list)
725 usb_submit_urb(r->urb, GFP_ATOMIC);
727 spin_unlock_irqrestore(&rx->lock, flags);
732 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
734 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
735 static int k_mode_thread(void *arg)
737 struct usbwm_dev *udev;
740 struct usb_tx *t, *temp;
742 unsigned long flags, flags2, expire;
745 while (!k_mode_stop) {
747 spin_lock_irqsave(&k_lock, flags2);
748 while (!list_empty(&k_list)) {
750 udev = list_entry(k_list.next, struct usbwm_dev, list);
754 list_del(&udev->list);
755 spin_unlock_irqrestore(&k_lock, flags2);
757 expire = jiffies + K_WAIT_TIME;
758 while (jiffies < expire)
759 schedule_timeout(K_WAIT_TIME);
761 spin_lock_irqsave(&rx->lock, flags);
763 list_for_each_entry(r, &rx->used_list, list)
764 usb_submit_urb(r->urb, GFP_ATOMIC);
766 spin_unlock_irqrestore(&rx->lock, flags);
768 spin_lock_irqsave(&tx->lock, flags);
770 list_for_each_entry_safe(t, temp, &tx->pending_list,
772 list_del(&t->p_list);
773 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
777 __gdm_usb_send_complete(t->urb);
782 spin_unlock_irqrestore(&tx->lock, flags);
784 spin_lock_irqsave(&k_lock, flags2);
786 wait_event_interruptible_lock_irq(k_wait,
787 !list_empty(&k_list) || k_mode_stop,
789 spin_unlock_irqrestore(&k_lock, flags2);
793 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
795 static struct usb_driver gdm_usb_driver = {
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,
808 static int __init usb_gdm_wimax_init(void)
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);
816 static void __exit usb_gdm_wimax_exit(void)
818 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
822 usb_deregister(&gdm_usb_driver);
825 module_init(usb_gdm_wimax_init);
826 module_exit(usb_gdm_wimax_exit);
828 MODULE_VERSION(DRIVER_VERSION);
829 MODULE_DESCRIPTION("GCT WiMax Device Driver");
830 MODULE_AUTHOR("Ethan Park");
831 MODULE_LICENSE("GPL");