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);
58 static struct usb_tx *alloc_tx_struct(struct tx_cxt *tx)
60 struct usb_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
65 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
66 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
67 if (!t->urb || !t->buf) {
79 static void free_tx_struct(struct usb_tx *t)
88 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
90 struct usb_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
95 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
96 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
97 if (!r->urb || !r->buf) {
108 static void free_rx_struct(struct usb_rx *r)
111 usb_free_urb(r->urb);
117 /* Before this function is called, spin lock should be locked. */
118 static struct usb_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
122 if (list_empty(&tx->free_list)) {
127 t = list_entry(tx->free_list.next, struct usb_tx, list);
130 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
135 /* Before this function is called, spin lock should be locked. */
136 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx *t)
138 list_add_tail(&t->list, &tx->free_list);
141 /* Before this function is called, spin lock should be locked. */
142 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
146 if (list_empty(&rx->free_list)) {
147 r = alloc_rx_struct(rx);
151 list_add(&r->list, &rx->free_list);
154 r = list_entry(rx->free_list.next, struct usb_rx, list);
155 list_move_tail(&r->list, &rx->used_list);
160 /* Before this function is called, spin lock should be locked. */
161 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
163 list_move(&r->list, &rx->free_list);
166 static int init_usb(struct usbwm_dev *udev)
169 struct tx_cxt *tx = &udev->tx;
170 struct rx_cxt *rx = &udev->rx;
175 INIT_LIST_HEAD(&tx->free_list);
176 INIT_LIST_HEAD(&tx->sdu_list);
177 INIT_LIST_HEAD(&tx->hci_list);
178 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
179 INIT_LIST_HEAD(&tx->pending_list);
182 INIT_LIST_HEAD(&rx->free_list);
183 INIT_LIST_HEAD(&rx->used_list);
185 spin_lock_init(&tx->lock);
186 spin_lock_init(&rx->lock);
188 spin_lock_irqsave(&tx->lock, flags);
189 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
190 t = alloc_tx_struct(tx);
192 spin_unlock_irqrestore(&tx->lock, flags);
196 list_add(&t->list, &tx->free_list);
198 spin_unlock_irqrestore(&tx->lock, flags);
200 r = alloc_rx_struct(rx);
206 spin_lock_irqsave(&rx->lock, flags);
207 list_add(&r->list, &rx->free_list);
208 spin_unlock_irqrestore(&rx->lock, flags);
216 static void release_usb(struct usbwm_dev *udev)
218 struct tx_cxt *tx = &udev->tx;
219 struct rx_cxt *rx = &udev->rx;
220 struct usb_tx *t, *t_next;
221 struct usb_rx *r, *r_next;
224 spin_lock_irqsave(&tx->lock, flags);
226 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
231 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
236 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
241 spin_unlock_irqrestore(&tx->lock, flags);
243 spin_lock_irqsave(&rx->lock, flags);
245 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
250 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
255 spin_unlock_irqrestore(&rx->lock, flags);
258 static void __gdm_usb_send_complete(struct urb *urb)
260 struct usb_tx *t = urb->context;
261 struct tx_cxt *tx = t->tx_cxt;
265 /* Completion by usb_unlink_urb */
266 if (urb->status == -ECONNRESET)
270 t->callback(t->cb_data);
272 /* Delete from sdu list or hci list. */
275 cmd_evt = (pkt[0] << 8) | pkt[1];
276 if (cmd_evt == WIMAX_TX_SDU)
277 put_tx_struct(tx, t);
282 static void gdm_usb_send_complete(struct urb *urb)
284 struct usb_tx *t = urb->context;
285 struct tx_cxt *tx = t->tx_cxt;
288 spin_lock_irqsave(&tx->lock, flags);
289 __gdm_usb_send_complete(urb);
290 spin_unlock_irqrestore(&tx->lock, flags);
293 static int gdm_usb_send(void *priv_dev, void *data, int len,
294 void (*cb)(void *data), void *cb_data)
296 struct usbwm_dev *udev = priv_dev;
297 struct usb_device *usbdev = udev->usbdev;
298 struct tx_cxt *tx = &udev->tx;
300 int padding = udev->padding;
305 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
306 unsigned long flags2;
307 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
310 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
314 BUG_ON(len > TX_BUF_SIZE - padding - 1);
316 spin_lock_irqsave(&tx->lock, flags);
318 cmd_evt = (pkt[0] << 8) | pkt[1];
319 if (cmd_evt == WIMAX_TX_SDU) {
320 t = get_tx_struct(tx, &no_spc);
322 /* This case must not happen. */
323 spin_unlock_irqrestore(&tx->lock, flags);
326 list_add_tail(&t->list, &tx->sdu_list);
328 t = alloc_tx_struct(tx);
330 spin_unlock_irqrestore(&tx->lock, flags);
333 list_add_tail(&t->list, &tx->hci_list);
336 memcpy(t->buf + padding, data, len);
338 t->cb_data = cb_data;
341 * In some cases, USB Module of WiMax is blocked when data size is
342 * the multiple of 512. So, increment length by one in that case.
344 if ((len % 512) == 0)
347 usb_fill_bulk_urb(t->urb,
349 usb_sndbulkpipe(usbdev, 1),
352 gdm_usb_send_complete,
355 print_hex_dump_debug("usb_send: ", DUMP_PREFIX_NONE, 16, 1,
356 t->buf, len + padding, false);
357 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
358 if (usbdev->state & USB_STATE_SUSPENDED) {
359 list_add_tail(&t->p_list, &tx->pending_list);
360 schedule_work(&udev->pm_ws);
363 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
365 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
366 if (udev->bw_switch) {
367 list_add_tail(&t->p_list, &tx->pending_list);
369 } else if (cmd_evt == WIMAX_SCAN) {
375 spin_lock_irqsave(&rx->lock, flags2);
376 list_for_each_entry(r, &rx->used_list, list)
377 usb_unlink_urb(r->urb);
378 spin_unlock_irqrestore(&rx->lock, flags2);
382 spin_lock_irqsave(&k_lock, flags2);
383 list_add_tail(&udev->list, &k_list);
384 spin_unlock_irqrestore(&k_lock, flags2);
388 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
390 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
394 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
395 usb_mark_last_busy(usbdev);
396 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
398 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
401 spin_unlock_irqrestore(&tx->lock, flags);
410 __gdm_usb_send_complete(t->urb);
411 spin_unlock_irqrestore(&tx->lock, flags);
415 static void gdm_usb_rcv_complete(struct urb *urb)
417 struct usb_rx *r = urb->context;
418 struct rx_cxt *rx = r->rx_cxt;
419 struct usbwm_dev *udev = container_of(r->rx_cxt, struct usbwm_dev, rx);
420 struct tx_cxt *tx = &udev->tx;
423 unsigned long flags, flags2;
424 struct usb_device *dev = urb->dev;
426 /* Completion by usb_unlink_urb */
427 if (urb->status == -ECONNRESET)
430 spin_lock_irqsave(&tx->lock, flags);
433 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
434 print_hex_dump_debug("usb_receive: ", DUMP_PREFIX_NONE, 16, 1,
435 r->buf, urb->actual_length, false);
436 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
437 if (r->buf[4] == 0) {
438 dev_dbg(&dev->dev, "WIMAX ==> STOP SDU TX\n");
439 list_for_each_entry(t, &tx->sdu_list, list)
440 usb_unlink_urb(t->urb);
441 } else if (r->buf[4] == 1) {
442 dev_dbg(&dev->dev, "WIMAX ==> START SDU TX\n");
443 list_for_each_entry(t, &tx->sdu_list, list) {
444 usb_submit_urb(t->urb, GFP_ATOMIC);
447 * If free buffer for sdu tx doesn't
448 * exist, then tx queue should not be
449 * woken. For this reason, don't pass
450 * the command, START_SDU_TX.
452 if (list_empty(&tx->free_list))
453 urb->actual_length = 0;
458 if (!urb->status && r->callback)
459 r->callback(r->cb_data, r->buf, urb->actual_length);
461 spin_lock_irqsave(&rx->lock, flags2);
462 put_rx_struct(rx, r);
463 spin_unlock_irqrestore(&rx->lock, flags2);
465 spin_unlock_irqrestore(&tx->lock, flags);
467 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
468 usb_mark_last_busy(dev);
472 static int gdm_usb_receive(void *priv_dev,
473 void (*cb)(void *cb_data, void *data, int len),
476 struct usbwm_dev *udev = priv_dev;
477 struct usb_device *usbdev = udev->usbdev;
478 struct rx_cxt *rx = &udev->rx;
483 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
487 spin_lock_irqsave(&rx->lock, flags);
488 r = get_rx_struct(rx);
489 spin_unlock_irqrestore(&rx->lock, flags);
495 r->cb_data = cb_data;
497 usb_fill_bulk_urb(r->urb,
499 usb_rcvbulkpipe(usbdev, 0x82),
502 gdm_usb_rcv_complete,
505 return usb_submit_urb(r->urb, GFP_ATOMIC);
508 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
509 static void do_pm_control(struct work_struct *work)
511 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
512 struct tx_cxt *tx = &udev->tx;
516 ret = usb_autopm_get_interface(udev->intf);
518 usb_autopm_put_interface(udev->intf);
520 spin_lock_irqsave(&tx->lock, flags);
521 if (!(udev->usbdev->state & USB_STATE_SUSPENDED)
522 && (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
523 struct usb_tx *t, *temp;
525 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
526 list_del(&t->p_list);
527 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
531 __gdm_usb_send_complete(t->urb);
535 spin_unlock_irqrestore(&tx->lock, flags);
537 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
539 static int gdm_usb_probe(struct usb_interface *intf,
540 const struct usb_device_id *id)
543 u8 bConfigurationValue;
544 struct phy_dev *phy_dev = NULL;
545 struct usbwm_dev *udev = NULL;
546 u16 idVendor, idProduct, bcdDevice;
548 struct usb_device *usbdev = interface_to_usbdev(intf);
551 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
553 /*USB description is set up with Little-Endian*/
554 idVendor = L2H(usbdev->descriptor.idVendor);
555 idProduct = L2H(usbdev->descriptor.idProduct);
556 bcdDevice = L2H(usbdev->descriptor.bcdDevice);
558 dev_info(&intf->dev, "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
559 idVendor, idProduct);
560 dev_info(&intf->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
563 if (idProduct == EMERGENCY_PID) {
564 ret = usb_emergency(usbdev);
568 /* Support for EEPROM bootloader */
569 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
570 idProduct & B_DOWNLOAD) {
571 ret = usb_boot(usbdev, bcdDevice);
575 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
576 if (phy_dev == NULL) {
580 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
586 if (idProduct == 0x7205 || idProduct == 0x7206)
587 udev->padding = GDM7205_PADDING;
591 phy_dev->priv_dev = (void *)udev;
592 phy_dev->send_func = gdm_usb_send;
593 phy_dev->rcv_func = gdm_usb_receive;
595 ret = init_usb(udev);
599 udev->usbdev = usbdev;
601 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
604 intf->needs_remote_wakeup = 1;
605 device_init_wakeup(&intf->dev, 1);
607 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
609 INIT_WORK(&udev->pm_ws, do_pm_control);
610 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
612 ret = register_wimax_device(phy_dev, &intf->dev);
622 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;
665 phy_dev = usb_get_intfdata(intf);
669 udev = phy_dev->priv_dev;
672 spin_lock_irqsave(&rx->lock, flags);
674 list_for_each_entry(r, &rx->used_list, list)
675 usb_unlink_urb(r->urb);
677 spin_unlock_irqrestore(&rx->lock, flags);
682 static int gdm_resume(struct usb_interface *intf)
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_submit_urb(r->urb, GFP_ATOMIC);
702 spin_unlock_irqrestore(&rx->lock, flags);
707 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
709 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
710 static int k_mode_thread(void *arg)
712 struct usbwm_dev *udev;
715 struct usb_tx *t, *temp;
717 unsigned long flags, flags2, expire;
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 spin_lock_irqsave(&rx->lock, flags);
738 list_for_each_entry(r, &rx->used_list, list)
739 usb_submit_urb(r->urb, GFP_ATOMIC);
741 spin_unlock_irqrestore(&rx->lock, flags);
743 spin_lock_irqsave(&tx->lock, flags);
745 list_for_each_entry_safe(t, temp, &tx->pending_list,
747 list_del(&t->p_list);
748 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
752 __gdm_usb_send_complete(t->urb);
757 spin_unlock_irqrestore(&tx->lock, flags);
759 spin_lock_irqsave(&k_lock, flags2);
761 wait_event_interruptible_lock_irq(k_wait,
762 !list_empty(&k_list) || k_mode_stop,
764 spin_unlock_irqrestore(&k_lock, flags2);
768 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
770 static struct usb_driver gdm_usb_driver = {
772 .probe = gdm_usb_probe,
773 .disconnect = gdm_usb_disconnect,
774 .id_table = id_table,
775 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
776 .supports_autosuspend = 1,
777 .suspend = gdm_suspend,
778 .resume = gdm_resume,
779 .reset_resume = gdm_resume,
783 static int __init usb_gdm_wimax_init(void)
785 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
786 kthread_run(k_mode_thread, NULL, "k_mode_wimax");
787 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
788 return usb_register(&gdm_usb_driver);
791 static void __exit usb_gdm_wimax_exit(void)
793 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
797 usb_deregister(&gdm_usb_driver);
800 module_init(usb_gdm_wimax_init);
801 module_exit(usb_gdm_wimax_exit);
803 MODULE_VERSION(DRIVER_VERSION);
804 MODULE_DESCRIPTION("GCT WiMax Device Driver");
805 MODULE_AUTHOR("Ethan Park");
806 MODULE_LICENSE("GPL");