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 = kzalloc(sizeof(*t), GFP_ATOMIC);
82 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
83 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
84 if (t->urb == NULL || t->buf == NULL)
99 static void free_tx_struct(struct usb_tx *t)
102 usb_free_urb(t->urb);
108 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
110 struct usb_rx *r = NULL;
112 r = kmalloc(sizeof(*r), GFP_ATOMIC);
116 memset(r, 0, sizeof(*r));
118 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
119 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
120 if (r->urb == NULL || r->buf == NULL)
127 usb_free_urb(r->urb);
134 static void free_rx_struct(struct usb_rx *r)
137 usb_free_urb(r->urb);
143 /* Before this function is called, spin lock should be locked. */
144 static struct usb_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
148 if (list_empty(&tx->free_list)) {
153 t = list_entry(tx->free_list.next, struct usb_tx, list);
156 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
161 /* Before this function is called, spin lock should be locked. */
162 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx *t)
164 list_add_tail(&t->list, &tx->free_list);
167 /* Before this function is called, spin lock should be locked. */
168 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
172 if (list_empty(&rx->free_list)) {
173 r = alloc_rx_struct(rx);
177 list_add(&r->list, &rx->free_list);
180 r = list_entry(rx->free_list.next, struct usb_rx, list);
182 list_add_tail(&r->list, &rx->used_list);
187 /* Before this function is called, spin lock should be locked. */
188 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
191 list_add(&r->list, &rx->free_list);
194 static int init_usb(struct usbwm_dev *udev)
197 struct tx_cxt *tx = &udev->tx;
198 struct rx_cxt *rx = &udev->rx;
202 INIT_LIST_HEAD(&tx->free_list);
203 INIT_LIST_HEAD(&tx->sdu_list);
204 INIT_LIST_HEAD(&tx->hci_list);
205 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
206 INIT_LIST_HEAD(&tx->pending_list);
209 INIT_LIST_HEAD(&rx->free_list);
210 INIT_LIST_HEAD(&rx->used_list);
212 spin_lock_init(&tx->lock);
213 spin_lock_init(&rx->lock);
215 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
216 t = alloc_tx_struct(tx);
221 list_add(&t->list, &tx->free_list);
224 r = alloc_rx_struct(rx);
230 list_add(&r->list, &rx->free_list);
238 static void release_usb(struct usbwm_dev *udev)
240 struct tx_cxt *tx = &udev->tx;
241 struct rx_cxt *rx = &udev->rx;
242 struct usb_tx *t, *t_next;
243 struct usb_rx *r, *r_next;
245 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
250 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
255 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
260 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
265 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
271 static void __gdm_usb_send_complete(struct urb *urb)
273 struct usb_tx *t = urb->context;
274 struct tx_cxt *tx = t->tx_cxt;
278 /* Completion by usb_unlink_urb */
279 if (urb->status == -ECONNRESET)
283 t->callback(t->cb_data);
285 /* Delete from sdu list or hci list. */
288 cmd_evt = (pkt[0] << 8) | pkt[1];
289 if (cmd_evt == WIMAX_TX_SDU)
290 put_tx_struct(tx, t);
295 static void gdm_usb_send_complete(struct urb *urb)
297 struct usb_tx *t = urb->context;
298 struct tx_cxt *tx = t->tx_cxt;
301 spin_lock_irqsave(&tx->lock, flags);
302 __gdm_usb_send_complete(urb);
303 spin_unlock_irqrestore(&tx->lock, flags);
306 static int gdm_usb_send(void *priv_dev, void *data, int len,
307 void (*cb)(void *data), void *cb_data)
309 struct usbwm_dev *udev = priv_dev;
310 struct usb_device *usbdev = udev->usbdev;
311 struct tx_cxt *tx = &udev->tx;
313 int padding = udev->padding;
320 printk(KERN_ERR "%s: No such device\n", __func__);
324 BUG_ON(len > TX_BUF_SIZE - padding - 1);
326 spin_lock_irqsave(&tx->lock, flags);
328 cmd_evt = (pkt[0] << 8) | pkt[1];
329 if (cmd_evt == WIMAX_TX_SDU) {
330 t = get_tx_struct(tx, &no_spc);
332 /* This case must not happen. */
333 spin_unlock_irqrestore(&tx->lock, flags);
336 list_add_tail(&t->list, &tx->sdu_list);
338 t = alloc_tx_struct(tx);
340 spin_unlock_irqrestore(&tx->lock, flags);
343 list_add_tail(&t->list, &tx->hci_list);
346 memcpy(t->buf + padding, data, len);
348 t->cb_data = cb_data;
351 * In some cases, USB Module of WiMax is blocked when data size is
352 * the multiple of 512. So, increment length by one in that case.
354 if ((len % 512) == 0)
357 usb_fill_bulk_urb(t->urb,
359 usb_sndbulkpipe(usbdev, 1),
362 gdm_usb_send_complete,
366 hexdump("usb_send", t->buf, len + padding);
368 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
369 if (usbdev->state & USB_STATE_SUSPENDED) {
370 list_add_tail(&t->p_list, &tx->pending_list);
371 schedule_work(&udev->pm_ws);
374 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
376 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
377 if (udev->bw_switch) {
378 list_add_tail(&t->p_list, &tx->pending_list);
380 } else if (cmd_evt == WIMAX_SCAN) {
386 list_for_each_entry(r, &rx->used_list, list)
387 usb_unlink_urb(r->urb);
391 list_add_tail(&udev->list, &k_list);
392 spin_unlock(&k_lock);
396 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
398 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
402 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
403 usb_mark_last_busy(usbdev);
404 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
406 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
409 spin_unlock_irqrestore(&tx->lock, flags);
418 __gdm_usb_send_complete(t->urb);
419 spin_unlock_irqrestore(&tx->lock, flags);
423 static void gdm_usb_rcv_complete(struct urb *urb)
425 struct usb_rx *r = urb->context;
426 struct rx_cxt *rx = r->rx_cxt;
427 struct usbwm_dev *udev = container_of(r->rx_cxt, struct usbwm_dev, rx);
428 struct tx_cxt *tx = &udev->tx;
433 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
434 struct usb_device *dev = urb->dev;
437 /* Completion by usb_unlink_urb */
438 if (urb->status == -ECONNRESET)
441 spin_lock_irqsave(&tx->lock, flags);
444 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
446 hexdump("usb_receive", r->buf, urb->actual_length);
448 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
449 if (r->buf[4] == 0) {
451 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
453 list_for_each_entry(t, &tx->sdu_list, list)
454 usb_unlink_urb(t->urb);
455 } else if (r->buf[4] == 1) {
457 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
459 list_for_each_entry(t, &tx->sdu_list, list) {
460 usb_submit_urb(t->urb, GFP_ATOMIC);
463 * If free buffer for sdu tx doesn't
464 * exist, then tx queue should not be
465 * woken. For this reason, don't pass
466 * the command, START_SDU_TX.
468 if (list_empty(&tx->free_list))
469 urb->actual_length = 0;
474 if (!urb->status && r->callback)
475 r->callback(r->cb_data, r->buf, urb->actual_length);
477 spin_lock(&rx->lock);
478 put_rx_struct(rx, r);
479 spin_unlock(&rx->lock);
481 spin_unlock_irqrestore(&tx->lock, flags);
483 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
484 usb_mark_last_busy(dev);
488 static int gdm_usb_receive(void *priv_dev,
489 void (*cb)(void *cb_data, void *data, int len),
492 struct usbwm_dev *udev = priv_dev;
493 struct usb_device *usbdev = udev->usbdev;
494 struct rx_cxt *rx = &udev->rx;
499 printk(KERN_ERR "%s: No such device\n", __func__);
503 spin_lock_irqsave(&rx->lock, flags);
504 r = get_rx_struct(rx);
505 spin_unlock_irqrestore(&rx->lock, flags);
511 r->cb_data = cb_data;
513 usb_fill_bulk_urb(r->urb,
515 usb_rcvbulkpipe(usbdev, 0x82),
518 gdm_usb_rcv_complete,
521 return usb_submit_urb(r->urb, GFP_ATOMIC);
524 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
525 static void do_pm_control(struct work_struct *work)
527 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
528 struct tx_cxt *tx = &udev->tx;
532 ret = usb_autopm_get_interface(udev->intf);
534 usb_autopm_put_interface(udev->intf);
536 spin_lock_irqsave(&tx->lock, flags);
537 if (!(udev->usbdev->state & USB_STATE_SUSPENDED)
538 && (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
539 struct usb_tx *t, *temp;
541 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
542 list_del(&t->p_list);
543 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
547 __gdm_usb_send_complete(t->urb);
551 spin_unlock_irqrestore(&tx->lock, flags);
553 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
555 static int gdm_usb_probe(struct usb_interface *intf,
556 const struct usb_device_id *id)
559 u8 bConfigurationValue;
560 struct phy_dev *phy_dev = NULL;
561 struct usbwm_dev *udev = NULL;
562 u16 idVendor, idProduct, bcdDevice;
564 struct usb_device *usbdev = interface_to_usbdev(intf);
567 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
569 /*USB description is set up with Little-Endian*/
570 idVendor = L2H(usbdev->descriptor.idVendor);
571 idProduct = L2H(usbdev->descriptor.idProduct);
572 bcdDevice = L2H(usbdev->descriptor.bcdDevice);
574 printk(KERN_INFO "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
575 idVendor, idProduct);
576 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
579 if (idProduct == EMERGENCY_PID) {
580 ret = usb_emergency(usbdev);
584 /* Support for EEPROM bootloader */
585 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
586 idProduct & B_DOWNLOAD) {
587 ret = usb_boot(usbdev, bcdDevice);
591 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
592 if (phy_dev == NULL) {
596 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
602 if (idProduct == 0x7205 || idProduct == 0x7206)
603 udev->padding = GDM7205_PADDING;
607 phy_dev->priv_dev = (void *)udev;
608 phy_dev->send_func = gdm_usb_send;
609 phy_dev->rcv_func = gdm_usb_receive;
611 ret = init_usb(udev);
615 udev->usbdev = usbdev;
617 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
620 intf->needs_remote_wakeup = 1;
621 device_init_wakeup(&intf->dev, 1);
623 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
625 INIT_WORK(&udev->pm_ws, do_pm_control);
626 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
628 ret = register_wimax_device(phy_dev, &intf->dev);
635 usb_set_intfdata(intf, phy_dev);
639 static void gdm_usb_disconnect(struct usb_interface *intf)
641 u8 bConfigurationValue;
642 struct phy_dev *phy_dev;
643 struct usbwm_dev *udev;
645 struct usb_device *usbdev = interface_to_usbdev(intf);
647 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
648 phy_dev = usb_get_intfdata(intf);
650 /*USB description is set up with Little-Endian*/
651 idProduct = L2H(usbdev->descriptor.idProduct);
653 if (idProduct != EMERGENCY_PID &&
654 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
655 (idProduct & B_DOWNLOAD) == 0) {
656 udev = phy_dev->priv_dev;
659 unregister_wimax_device(phy_dev);
668 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
669 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
671 struct phy_dev *phy_dev;
672 struct usbwm_dev *udev;
676 phy_dev = usb_get_intfdata(intf);
677 udev = phy_dev->priv_dev;
680 list_for_each_entry(r, &rx->used_list, list)
681 usb_unlink_urb(r->urb);
686 static int gdm_resume(struct usb_interface *intf)
688 struct phy_dev *phy_dev;
689 struct usbwm_dev *udev;
693 phy_dev = usb_get_intfdata(intf);
694 udev = phy_dev->priv_dev;
697 list_for_each_entry(r, &rx->used_list, list)
698 usb_submit_urb(r->urb, GFP_ATOMIC);
703 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
705 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
706 static int k_mode_thread(void *arg)
708 struct usbwm_dev *udev;
711 struct usb_tx *t, *temp;
713 unsigned long flags, flags2, expire;
716 daemonize("k_mode_wimax");
718 while (!k_mode_stop) {
720 spin_lock_irqsave(&k_lock, flags2);
721 while (!list_empty(&k_list)) {
723 udev = list_entry(k_list.next, struct usbwm_dev, list);
727 list_del(&udev->list);
728 spin_unlock_irqrestore(&k_lock, flags2);
730 expire = jiffies + K_WAIT_TIME;
731 while (jiffies < expire)
732 schedule_timeout(K_WAIT_TIME);
734 list_for_each_entry(r, &rx->used_list, list)
735 usb_submit_urb(r->urb, GFP_ATOMIC);
737 spin_lock_irqsave(&tx->lock, flags);
739 list_for_each_entry_safe(t, temp, &tx->pending_list,
741 list_del(&t->p_list);
742 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
746 __gdm_usb_send_complete(t->urb);
751 spin_unlock_irqrestore(&tx->lock, flags);
753 spin_lock_irqsave(&k_lock, flags2);
755 spin_unlock_irqrestore(&k_lock, flags2);
757 interruptible_sleep_on(&k_wait);
761 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
763 static struct usb_driver gdm_usb_driver = {
765 .probe = gdm_usb_probe,
766 .disconnect = gdm_usb_disconnect,
767 .id_table = id_table,
768 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
769 .supports_autosuspend = 1,
770 .suspend = gdm_suspend,
771 .resume = gdm_resume,
772 .reset_resume = gdm_resume,
776 static int __init usb_gdm_wimax_init(void)
778 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
779 kthread_run(k_mode_thread, NULL, "WiMax_thread");
780 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
781 return usb_register(&gdm_usb_driver);
784 static void __exit usb_gdm_wimax_exit(void)
786 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
790 usb_deregister(&gdm_usb_driver);
793 module_init(usb_gdm_wimax_init);
794 module_exit(usb_gdm_wimax_exit);
796 MODULE_VERSION(DRIVER_VERSION);
797 MODULE_DESCRIPTION("GCT WiMax Device Driver");
798 MODULE_AUTHOR("Ethan Park");
799 MODULE_LICENSE("GPL");