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;
190 INIT_LIST_HEAD(&tx->free_list);
191 INIT_LIST_HEAD(&tx->sdu_list);
192 INIT_LIST_HEAD(&tx->hci_list);
193 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
194 INIT_LIST_HEAD(&tx->pending_list);
197 INIT_LIST_HEAD(&rx->free_list);
198 INIT_LIST_HEAD(&rx->used_list);
200 spin_lock_init(&tx->lock);
201 spin_lock_init(&rx->lock);
203 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
204 t = alloc_tx_struct(tx);
209 list_add(&t->list, &tx->free_list);
212 r = alloc_rx_struct(rx);
218 list_add(&r->list, &rx->free_list);
226 static void release_usb(struct usbwm_dev *udev)
228 struct tx_cxt *tx = &udev->tx;
229 struct rx_cxt *rx = &udev->rx;
230 struct usb_tx *t, *t_next;
231 struct usb_rx *r, *r_next;
233 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
238 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
243 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
248 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
253 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
259 static void __gdm_usb_send_complete(struct urb *urb)
261 struct usb_tx *t = urb->context;
262 struct tx_cxt *tx = t->tx_cxt;
266 /* Completion by usb_unlink_urb */
267 if (urb->status == -ECONNRESET)
271 t->callback(t->cb_data);
273 /* Delete from sdu list or hci list. */
276 cmd_evt = (pkt[0] << 8) | pkt[1];
277 if (cmd_evt == WIMAX_TX_SDU)
278 put_tx_struct(tx, t);
283 static void gdm_usb_send_complete(struct urb *urb)
285 struct usb_tx *t = urb->context;
286 struct tx_cxt *tx = t->tx_cxt;
289 spin_lock_irqsave(&tx->lock, flags);
290 __gdm_usb_send_complete(urb);
291 spin_unlock_irqrestore(&tx->lock, flags);
294 static int gdm_usb_send(void *priv_dev, void *data, int len,
295 void (*cb)(void *data), void *cb_data)
297 struct usbwm_dev *udev = priv_dev;
298 struct usb_device *usbdev = udev->usbdev;
299 struct tx_cxt *tx = &udev->tx;
301 int padding = udev->padding;
308 printk(KERN_ERR "%s: No such device\n", __func__);
312 BUG_ON(len > TX_BUF_SIZE - padding - 1);
314 spin_lock_irqsave(&tx->lock, flags);
316 cmd_evt = (pkt[0] << 8) | pkt[1];
317 if (cmd_evt == WIMAX_TX_SDU) {
318 t = get_tx_struct(tx, &no_spc);
320 /* This case must not happen. */
321 spin_unlock_irqrestore(&tx->lock, flags);
324 list_add_tail(&t->list, &tx->sdu_list);
326 t = alloc_tx_struct(tx);
328 spin_unlock_irqrestore(&tx->lock, flags);
331 list_add_tail(&t->list, &tx->hci_list);
334 memcpy(t->buf + padding, data, len);
336 t->cb_data = cb_data;
339 * In some cases, USB Module of WiMax is blocked when data size is
340 * the multiple of 512. So, increment length by one in that case.
342 if ((len % 512) == 0)
345 usb_fill_bulk_urb(t->urb,
347 usb_sndbulkpipe(usbdev, 1),
350 gdm_usb_send_complete,
354 hexdump("usb_send", t->buf, len + padding);
356 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
357 if (usbdev->state & USB_STATE_SUSPENDED) {
358 list_add_tail(&t->p_list, &tx->pending_list);
359 schedule_work(&udev->pm_ws);
362 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
364 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
365 if (udev->bw_switch) {
366 list_add_tail(&t->p_list, &tx->pending_list);
368 } else if (cmd_evt == WIMAX_SCAN) {
374 list_for_each_entry(r, &rx->used_list, list)
375 usb_unlink_urb(r->urb);
379 list_add_tail(&udev->list, &k_list);
380 spin_unlock(&k_lock);
384 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
386 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
390 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
391 usb_mark_last_busy(usbdev);
392 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
394 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
397 spin_unlock_irqrestore(&tx->lock, flags);
406 __gdm_usb_send_complete(t->urb);
407 spin_unlock_irqrestore(&tx->lock, flags);
411 static void gdm_usb_rcv_complete(struct urb *urb)
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;
421 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
422 struct usb_device *dev = urb->dev;
425 /* Completion by usb_unlink_urb */
426 if (urb->status == -ECONNRESET)
429 spin_lock_irqsave(&tx->lock, flags);
432 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
434 hexdump("usb_receive", r->buf, urb->actual_length);
436 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
437 if (r->buf[4] == 0) {
439 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
441 list_for_each_entry(t, &tx->sdu_list, list)
442 usb_unlink_urb(t->urb);
443 } else if (r->buf[4] == 1) {
445 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
447 list_for_each_entry(t, &tx->sdu_list, list) {
448 usb_submit_urb(t->urb, GFP_ATOMIC);
451 * If free buffer for sdu tx doesn't
452 * exist, then tx queue should not be
453 * woken. For this reason, don't pass
454 * the command, START_SDU_TX.
456 if (list_empty(&tx->free_list))
457 urb->actual_length = 0;
462 if (!urb->status && r->callback)
463 r->callback(r->cb_data, r->buf, urb->actual_length);
465 spin_lock(&rx->lock);
466 put_rx_struct(rx, r);
467 spin_unlock(&rx->lock);
469 spin_unlock_irqrestore(&tx->lock, flags);
471 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
472 usb_mark_last_busy(dev);
476 static int gdm_usb_receive(void *priv_dev,
477 void (*cb)(void *cb_data, void *data, int len),
480 struct usbwm_dev *udev = priv_dev;
481 struct usb_device *usbdev = udev->usbdev;
482 struct rx_cxt *rx = &udev->rx;
487 printk(KERN_ERR "%s: No such device\n", __func__);
491 spin_lock_irqsave(&rx->lock, flags);
492 r = get_rx_struct(rx);
493 spin_unlock_irqrestore(&rx->lock, flags);
499 r->cb_data = cb_data;
501 usb_fill_bulk_urb(r->urb,
503 usb_rcvbulkpipe(usbdev, 0x82),
506 gdm_usb_rcv_complete,
509 return usb_submit_urb(r->urb, GFP_ATOMIC);
512 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
513 static void do_pm_control(struct work_struct *work)
515 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
516 struct tx_cxt *tx = &udev->tx;
520 ret = usb_autopm_get_interface(udev->intf);
522 usb_autopm_put_interface(udev->intf);
524 spin_lock_irqsave(&tx->lock, flags);
525 if (!(udev->usbdev->state & USB_STATE_SUSPENDED)
526 && (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
527 struct usb_tx *t, *temp;
529 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
530 list_del(&t->p_list);
531 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
535 __gdm_usb_send_complete(t->urb);
539 spin_unlock_irqrestore(&tx->lock, flags);
541 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
543 static int gdm_usb_probe(struct usb_interface *intf,
544 const struct usb_device_id *id)
547 u8 bConfigurationValue;
548 struct phy_dev *phy_dev = NULL;
549 struct usbwm_dev *udev = NULL;
550 u16 idVendor, idProduct, bcdDevice;
552 struct usb_device *usbdev = interface_to_usbdev(intf);
555 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
557 /*USB description is set up with Little-Endian*/
558 idVendor = L2H(usbdev->descriptor.idVendor);
559 idProduct = L2H(usbdev->descriptor.idProduct);
560 bcdDevice = L2H(usbdev->descriptor.bcdDevice);
562 printk(KERN_INFO "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
563 idVendor, idProduct);
564 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
567 if (idProduct == EMERGENCY_PID) {
568 ret = usb_emergency(usbdev);
572 /* Support for EEPROM bootloader */
573 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
574 idProduct & B_DOWNLOAD) {
575 ret = usb_boot(usbdev, bcdDevice);
579 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
580 if (phy_dev == NULL) {
584 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
590 if (idProduct == 0x7205 || idProduct == 0x7206)
591 udev->padding = GDM7205_PADDING;
595 phy_dev->priv_dev = (void *)udev;
596 phy_dev->send_func = gdm_usb_send;
597 phy_dev->rcv_func = gdm_usb_receive;
599 ret = init_usb(udev);
603 udev->usbdev = usbdev;
605 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
608 intf->needs_remote_wakeup = 1;
609 device_init_wakeup(&intf->dev, 1);
611 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
613 INIT_WORK(&udev->pm_ws, do_pm_control);
614 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
616 ret = register_wimax_device(phy_dev, &intf->dev);
623 usb_set_intfdata(intf, phy_dev);
627 static void gdm_usb_disconnect(struct usb_interface *intf)
629 u8 bConfigurationValue;
630 struct phy_dev *phy_dev;
631 struct usbwm_dev *udev;
633 struct usb_device *usbdev = interface_to_usbdev(intf);
635 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
636 phy_dev = usb_get_intfdata(intf);
638 /*USB description is set up with Little-Endian*/
639 idProduct = L2H(usbdev->descriptor.idProduct);
641 if (idProduct != EMERGENCY_PID &&
642 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
643 (idProduct & B_DOWNLOAD) == 0) {
644 udev = phy_dev->priv_dev;
647 unregister_wimax_device(phy_dev);
656 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
657 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
659 struct phy_dev *phy_dev;
660 struct usbwm_dev *udev;
664 phy_dev = usb_get_intfdata(intf);
665 udev = phy_dev->priv_dev;
668 list_for_each_entry(r, &rx->used_list, list)
669 usb_unlink_urb(r->urb);
674 static int gdm_resume(struct usb_interface *intf)
676 struct phy_dev *phy_dev;
677 struct usbwm_dev *udev;
681 phy_dev = usb_get_intfdata(intf);
682 udev = phy_dev->priv_dev;
685 list_for_each_entry(r, &rx->used_list, list)
686 usb_submit_urb(r->urb, GFP_ATOMIC);
691 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
693 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
694 static int k_mode_thread(void *arg)
696 struct usbwm_dev *udev;
699 struct usb_tx *t, *temp;
701 unsigned long flags, flags2, expire;
704 while (!k_mode_stop) {
706 spin_lock_irqsave(&k_lock, flags2);
707 while (!list_empty(&k_list)) {
709 udev = list_entry(k_list.next, struct usbwm_dev, list);
713 list_del(&udev->list);
714 spin_unlock_irqrestore(&k_lock, flags2);
716 expire = jiffies + K_WAIT_TIME;
717 while (jiffies < expire)
718 schedule_timeout(K_WAIT_TIME);
720 list_for_each_entry(r, &rx->used_list, list)
721 usb_submit_urb(r->urb, GFP_ATOMIC);
723 spin_lock_irqsave(&tx->lock, flags);
725 list_for_each_entry_safe(t, temp, &tx->pending_list,
727 list_del(&t->p_list);
728 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
732 __gdm_usb_send_complete(t->urb);
737 spin_unlock_irqrestore(&tx->lock, flags);
739 spin_lock_irqsave(&k_lock, flags2);
741 spin_unlock_irqrestore(&k_lock, flags2);
743 interruptible_sleep_on(&k_wait);
747 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
749 static struct usb_driver gdm_usb_driver = {
751 .probe = gdm_usb_probe,
752 .disconnect = gdm_usb_disconnect,
753 .id_table = id_table,
754 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
755 .supports_autosuspend = 1,
756 .suspend = gdm_suspend,
757 .resume = gdm_resume,
758 .reset_resume = gdm_resume,
762 static int __init usb_gdm_wimax_init(void)
764 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
765 kthread_run(k_mode_thread, NULL, "k_mode_wimax");
766 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
767 return usb_register(&gdm_usb_driver);
770 static void __exit usb_gdm_wimax_exit(void)
772 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
776 usb_deregister(&gdm_usb_driver);
779 module_init(usb_gdm_wimax_init);
780 module_exit(usb_gdm_wimax_exit);
782 MODULE_VERSION(DRIVER_VERSION);
783 MODULE_DESCRIPTION("GCT WiMax Device Driver");
784 MODULE_AUTHOR("Ethan Park");
785 MODULE_LICENSE("GPL");