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
31 #if defined(CONFIG_WIMAX_GDM72XX_WIMAX2)
32 #define RX_BUF_SIZE (128*1024) /* For packet aggregation */
34 #define RX_BUF_SIZE 2048
37 #define GDM7205_PADDING 256
39 #define H2B(x) __cpu_to_be16(x)
40 #define B2H(x) __be16_to_cpu(x)
41 #define DB2H(x) __be32_to_cpu(x)
43 #define DOWNLOAD_CONF_VALUE 0x21
45 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
47 static DECLARE_WAIT_QUEUE_HEAD(k_wait);
48 static LIST_HEAD(k_list);
49 static DEFINE_SPINLOCK(k_lock);
50 static int k_mode_stop;
52 #define K_WAIT_TIME (2 * HZ / 100)
54 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
56 static int init_usb(struct usbwm_dev *udev);
57 static void release_usb(struct usbwm_dev *udev);
59 static struct usb_tx *alloc_tx_struct(struct tx_cxt *tx)
61 struct usb_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
66 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
67 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
68 if (!t->urb || !t->buf) {
80 static void free_tx_struct(struct usb_tx *t)
89 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
91 struct usb_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
96 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
97 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
98 if (!r->urb || !r->buf) {
109 static void free_rx_struct(struct usb_rx *r)
112 usb_free_urb(r->urb);
118 /* Before this function is called, spin lock should be locked. */
119 static struct usb_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
123 if (list_empty(&tx->free_list)) {
128 t = list_entry(tx->free_list.next, struct usb_tx, list);
131 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
136 /* Before this function is called, spin lock should be locked. */
137 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx *t)
139 list_add_tail(&t->list, &tx->free_list);
142 /* Before this function is called, spin lock should be locked. */
143 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
147 if (list_empty(&rx->free_list)) {
148 r = alloc_rx_struct(rx);
152 list_add(&r->list, &rx->free_list);
155 r = list_entry(rx->free_list.next, struct usb_rx, list);
156 list_move_tail(&r->list, &rx->used_list);
161 /* Before this function is called, spin lock should be locked. */
162 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
164 list_move(&r->list, &rx->free_list);
167 static int init_usb(struct usbwm_dev *udev)
170 struct tx_cxt *tx = &udev->tx;
171 struct rx_cxt *rx = &udev->rx;
176 INIT_LIST_HEAD(&tx->free_list);
177 INIT_LIST_HEAD(&tx->sdu_list);
178 INIT_LIST_HEAD(&tx->hci_list);
179 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
180 INIT_LIST_HEAD(&tx->pending_list);
183 INIT_LIST_HEAD(&rx->free_list);
184 INIT_LIST_HEAD(&rx->used_list);
186 spin_lock_init(&tx->lock);
187 spin_lock_init(&rx->lock);
189 spin_lock_irqsave(&tx->lock, flags);
190 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
191 t = alloc_tx_struct(tx);
193 spin_unlock_irqrestore(&tx->lock, flags);
197 list_add(&t->list, &tx->free_list);
199 spin_unlock_irqrestore(&tx->lock, flags);
201 r = alloc_rx_struct(rx);
207 spin_lock_irqsave(&rx->lock, flags);
208 list_add(&r->list, &rx->free_list);
209 spin_unlock_irqrestore(&rx->lock, flags);
217 static void release_usb(struct usbwm_dev *udev)
219 struct tx_cxt *tx = &udev->tx;
220 struct rx_cxt *rx = &udev->rx;
221 struct usb_tx *t, *t_next;
222 struct usb_rx *r, *r_next;
225 spin_lock_irqsave(&tx->lock, flags);
227 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
232 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
237 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
242 spin_unlock_irqrestore(&tx->lock, flags);
244 spin_lock_irqsave(&rx->lock, flags);
246 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
251 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
256 spin_unlock_irqrestore(&rx->lock, flags);
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;
306 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
307 unsigned long flags2;
308 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
311 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
315 BUG_ON(len > TX_BUF_SIZE - padding - 1);
317 spin_lock_irqsave(&tx->lock, flags);
319 cmd_evt = (pkt[0] << 8) | pkt[1];
320 if (cmd_evt == WIMAX_TX_SDU) {
321 t = get_tx_struct(tx, &no_spc);
323 /* This case must not happen. */
324 spin_unlock_irqrestore(&tx->lock, flags);
327 list_add_tail(&t->list, &tx->sdu_list);
329 t = alloc_tx_struct(tx);
331 spin_unlock_irqrestore(&tx->lock, flags);
334 list_add_tail(&t->list, &tx->hci_list);
337 memcpy(t->buf + padding, data, len);
339 t->cb_data = cb_data;
342 * In some cases, USB Module of WiMax is blocked when data size is
343 * the multiple of 512. So, increment length by one in that case.
345 if ((len % 512) == 0)
348 usb_fill_bulk_urb(t->urb, usbdev, usb_sndbulkpipe(usbdev, 1), t->buf,
349 len + padding, gdm_usb_send_complete, t);
351 print_hex_dump_debug("usb_send: ", DUMP_PREFIX_NONE, 16, 1, t->buf,
352 len + padding, false);
353 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
354 if (usbdev->state & USB_STATE_SUSPENDED) {
355 list_add_tail(&t->p_list, &tx->pending_list);
356 schedule_work(&udev->pm_ws);
359 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
361 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
362 if (udev->bw_switch) {
363 list_add_tail(&t->p_list, &tx->pending_list);
365 } else if (cmd_evt == WIMAX_SCAN) {
371 spin_lock_irqsave(&rx->lock, flags2);
372 list_for_each_entry(r, &rx->used_list, list)
373 usb_unlink_urb(r->urb);
374 spin_unlock_irqrestore(&rx->lock, flags2);
378 spin_lock_irqsave(&k_lock, flags2);
379 list_add_tail(&udev->list, &k_list);
380 spin_unlock_irqrestore(&k_lock, flags2);
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;
419 unsigned long flags, flags2;
420 struct usb_device *dev = urb->dev;
422 /* Completion by usb_unlink_urb */
423 if (urb->status == -ECONNRESET)
426 spin_lock_irqsave(&tx->lock, flags);
429 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
430 print_hex_dump_debug("usb_receive: ", DUMP_PREFIX_NONE, 16, 1,
431 r->buf, urb->actual_length, false);
432 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
433 if (r->buf[4] == 0) {
434 dev_dbg(&dev->dev, "WIMAX ==> STOP SDU TX\n");
435 list_for_each_entry(t, &tx->sdu_list, list)
436 usb_unlink_urb(t->urb);
437 } else if (r->buf[4] == 1) {
438 dev_dbg(&dev->dev, "WIMAX ==> START SDU TX\n");
439 list_for_each_entry(t, &tx->sdu_list, list) {
440 usb_submit_urb(t->urb, GFP_ATOMIC);
443 * If free buffer for sdu tx doesn't
444 * exist, then tx queue should not be
445 * woken. For this reason, don't pass
446 * the command, START_SDU_TX.
448 if (list_empty(&tx->free_list))
449 urb->actual_length = 0;
454 if (!urb->status && r->callback)
455 r->callback(r->cb_data, r->buf, urb->actual_length);
457 spin_lock_irqsave(&rx->lock, flags2);
458 put_rx_struct(rx, r);
459 spin_unlock_irqrestore(&rx->lock, flags2);
461 spin_unlock_irqrestore(&tx->lock, flags);
463 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
464 usb_mark_last_busy(dev);
468 static int gdm_usb_receive(void *priv_dev,
469 void (*cb)(void *cb_data, void *data, int len),
472 struct usbwm_dev *udev = priv_dev;
473 struct usb_device *usbdev = udev->usbdev;
474 struct rx_cxt *rx = &udev->rx;
479 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
483 spin_lock_irqsave(&rx->lock, flags);
484 r = get_rx_struct(rx);
485 spin_unlock_irqrestore(&rx->lock, flags);
491 r->cb_data = cb_data;
493 usb_fill_bulk_urb(r->urb, usbdev, usb_rcvbulkpipe(usbdev, 0x82), r->buf,
494 RX_BUF_SIZE, gdm_usb_rcv_complete, r);
496 return usb_submit_urb(r->urb, GFP_ATOMIC);
499 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
500 static void do_pm_control(struct work_struct *work)
502 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
503 struct tx_cxt *tx = &udev->tx;
507 ret = usb_autopm_get_interface(udev->intf);
509 usb_autopm_put_interface(udev->intf);
511 spin_lock_irqsave(&tx->lock, flags);
512 if (!(udev->usbdev->state & USB_STATE_SUSPENDED) &&
513 (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
514 struct usb_tx *t, *temp;
516 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
517 list_del(&t->p_list);
518 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
522 __gdm_usb_send_complete(t->urb);
526 spin_unlock_irqrestore(&tx->lock, flags);
528 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
530 static int gdm_usb_probe(struct usb_interface *intf,
531 const struct usb_device_id *id)
534 u8 bConfigurationValue;
535 struct phy_dev *phy_dev = NULL;
536 struct usbwm_dev *udev = NULL;
537 u16 idVendor, idProduct, bcdDevice;
539 struct usb_device *usbdev = interface_to_usbdev(intf);
542 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
544 /*USB description is set up with Little-Endian*/
545 idVendor = L2H(usbdev->descriptor.idVendor);
546 idProduct = L2H(usbdev->descriptor.idProduct);
547 bcdDevice = L2H(usbdev->descriptor.bcdDevice);
549 dev_info(&intf->dev, "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
550 idVendor, idProduct);
551 dev_info(&intf->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
554 if (idProduct == EMERGENCY_PID) {
555 ret = usb_emergency(usbdev);
559 /* Support for EEPROM bootloader */
560 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
561 idProduct & B_DOWNLOAD) {
562 ret = usb_boot(usbdev, bcdDevice);
566 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
567 if (phy_dev == NULL) {
571 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
577 if (idProduct == 0x7205 || idProduct == 0x7206)
578 udev->padding = GDM7205_PADDING;
582 phy_dev->priv_dev = (void *)udev;
583 phy_dev->send_func = gdm_usb_send;
584 phy_dev->rcv_func = gdm_usb_receive;
586 ret = init_usb(udev);
590 udev->usbdev = usbdev;
592 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
595 intf->needs_remote_wakeup = 1;
596 device_init_wakeup(&intf->dev, 1);
598 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
600 INIT_WORK(&udev->pm_ws, do_pm_control);
601 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
603 ret = register_wimax_device(phy_dev, &intf->dev);
613 usb_set_intfdata(intf, phy_dev);
618 static void gdm_usb_disconnect(struct usb_interface *intf)
620 u8 bConfigurationValue;
621 struct phy_dev *phy_dev;
622 struct usbwm_dev *udev;
624 struct usb_device *usbdev = interface_to_usbdev(intf);
626 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
627 phy_dev = usb_get_intfdata(intf);
629 /*USB description is set up with Little-Endian*/
630 idProduct = L2H(usbdev->descriptor.idProduct);
632 if (idProduct != EMERGENCY_PID &&
633 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
634 (idProduct & B_DOWNLOAD) == 0) {
636 udev = phy_dev->priv_dev;
639 unregister_wimax_device(phy_dev);
648 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
649 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
651 struct phy_dev *phy_dev;
652 struct usbwm_dev *udev;
657 phy_dev = usb_get_intfdata(intf);
661 udev = phy_dev->priv_dev;
664 spin_lock_irqsave(&rx->lock, flags);
666 list_for_each_entry(r, &rx->used_list, list)
667 usb_unlink_urb(r->urb);
669 spin_unlock_irqrestore(&rx->lock, flags);
674 static int gdm_resume(struct usb_interface *intf)
676 struct phy_dev *phy_dev;
677 struct usbwm_dev *udev;
682 phy_dev = usb_get_intfdata(intf);
686 udev = phy_dev->priv_dev;
689 spin_lock_irqsave(&rx->lock, flags);
691 list_for_each_entry(r, &rx->used_list, list)
692 usb_submit_urb(r->urb, GFP_ATOMIC);
694 spin_unlock_irqrestore(&rx->lock, flags);
699 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
701 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
702 static int k_mode_thread(void *arg)
704 struct usbwm_dev *udev;
707 struct usb_tx *t, *temp;
709 unsigned long flags, flags2, expire;
712 while (!k_mode_stop) {
714 spin_lock_irqsave(&k_lock, flags2);
715 while (!list_empty(&k_list)) {
717 udev = list_entry(k_list.next, struct usbwm_dev, list);
721 list_del(&udev->list);
722 spin_unlock_irqrestore(&k_lock, flags2);
724 expire = jiffies + K_WAIT_TIME;
725 while (time_before(jiffies, expire))
726 schedule_timeout(K_WAIT_TIME);
728 spin_lock_irqsave(&rx->lock, flags);
730 list_for_each_entry(r, &rx->used_list, list)
731 usb_submit_urb(r->urb, GFP_ATOMIC);
733 spin_unlock_irqrestore(&rx->lock, flags);
735 spin_lock_irqsave(&tx->lock, flags);
737 list_for_each_entry_safe(t, temp, &tx->pending_list,
739 list_del(&t->p_list);
740 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
744 __gdm_usb_send_complete(t->urb);
749 spin_unlock_irqrestore(&tx->lock, flags);
751 spin_lock_irqsave(&k_lock, flags2);
753 wait_event_interruptible_lock_irq(k_wait,
754 !list_empty(&k_list) ||
755 k_mode_stop, k_lock);
756 spin_unlock_irqrestore(&k_lock, flags2);
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 kthread_run(k_mode_thread, NULL, "k_mode_wimax");
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");