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 DOWNLOAD_CONF_VALUE 0x21
41 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
43 static DECLARE_WAIT_QUEUE_HEAD(k_wait);
44 static LIST_HEAD(k_list);
45 static DEFINE_SPINLOCK(k_lock);
46 static int k_mode_stop;
48 #define K_WAIT_TIME (2 * HZ / 100)
50 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
52 static struct usb_tx *alloc_tx_struct(struct tx_cxt *tx)
54 struct usb_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
59 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
60 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
61 if (!t->urb || !t->buf) {
73 static void free_tx_struct(struct usb_tx *t)
82 static struct usb_rx *alloc_rx_struct(struct rx_cxt *rx)
84 struct usb_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
89 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
90 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
91 if (!r->urb || !r->buf) {
102 static void free_rx_struct(struct usb_rx *r)
105 usb_free_urb(r->urb);
111 /* Before this function is called, spin lock should be locked. */
112 static struct usb_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
116 if (list_empty(&tx->free_list)) {
121 t = list_entry(tx->free_list.next, struct usb_tx, list);
124 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
129 /* Before this function is called, spin lock should be locked. */
130 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx *t)
132 list_add_tail(&t->list, &tx->free_list);
135 /* Before this function is called, spin lock should be locked. */
136 static struct usb_rx *get_rx_struct(struct rx_cxt *rx)
140 if (list_empty(&rx->free_list)) {
141 r = alloc_rx_struct(rx);
145 list_add(&r->list, &rx->free_list);
148 r = list_entry(rx->free_list.next, struct usb_rx, list);
149 list_move_tail(&r->list, &rx->used_list);
154 /* Before this function is called, spin lock should be locked. */
155 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
157 list_move(&r->list, &rx->free_list);
160 static void release_usb(struct usbwm_dev *udev)
162 struct tx_cxt *tx = &udev->tx;
163 struct rx_cxt *rx = &udev->rx;
164 struct usb_tx *t, *t_next;
165 struct usb_rx *r, *r_next;
168 spin_lock_irqsave(&tx->lock, flags);
170 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
175 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
180 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
185 spin_unlock_irqrestore(&tx->lock, flags);
187 spin_lock_irqsave(&rx->lock, flags);
189 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
194 list_for_each_entry_safe(r, r_next, &rx->used_list, list) {
199 spin_unlock_irqrestore(&rx->lock, flags);
202 static int init_usb(struct usbwm_dev *udev)
205 struct tx_cxt *tx = &udev->tx;
206 struct rx_cxt *rx = &udev->rx;
211 INIT_LIST_HEAD(&tx->free_list);
212 INIT_LIST_HEAD(&tx->sdu_list);
213 INIT_LIST_HEAD(&tx->hci_list);
214 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
215 INIT_LIST_HEAD(&tx->pending_list);
218 INIT_LIST_HEAD(&rx->free_list);
219 INIT_LIST_HEAD(&rx->used_list);
221 spin_lock_init(&tx->lock);
222 spin_lock_init(&rx->lock);
224 spin_lock_irqsave(&tx->lock, flags);
225 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
226 t = alloc_tx_struct(tx);
228 spin_unlock_irqrestore(&tx->lock, flags);
232 list_add(&t->list, &tx->free_list);
234 spin_unlock_irqrestore(&tx->lock, flags);
236 r = alloc_rx_struct(rx);
242 spin_lock_irqsave(&rx->lock, flags);
243 list_add(&r->list, &rx->free_list);
244 spin_unlock_irqrestore(&rx->lock, flags);
252 static void __gdm_usb_send_complete(struct urb *urb)
254 struct usb_tx *t = urb->context;
255 struct tx_cxt *tx = t->tx_cxt;
259 /* Completion by usb_unlink_urb */
260 if (urb->status == -ECONNRESET)
264 t->callback(t->cb_data);
266 /* Delete from sdu list or hci list. */
269 cmd_evt = (pkt[0] << 8) | pkt[1];
270 if (cmd_evt == WIMAX_TX_SDU)
271 put_tx_struct(tx, t);
276 static void gdm_usb_send_complete(struct urb *urb)
278 struct usb_tx *t = urb->context;
279 struct tx_cxt *tx = t->tx_cxt;
282 spin_lock_irqsave(&tx->lock, flags);
283 __gdm_usb_send_complete(urb);
284 spin_unlock_irqrestore(&tx->lock, flags);
287 static int gdm_usb_send(void *priv_dev, void *data, int len,
288 void (*cb)(void *data), void *cb_data)
290 struct usbwm_dev *udev = priv_dev;
291 struct usb_device *usbdev = udev->usbdev;
292 struct tx_cxt *tx = &udev->tx;
294 int padding = udev->padding;
299 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
300 unsigned long flags2;
301 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
304 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
308 if (len > TX_BUF_SIZE - padding - 1)
311 spin_lock_irqsave(&tx->lock, flags);
313 cmd_evt = (pkt[0] << 8) | pkt[1];
314 if (cmd_evt == WIMAX_TX_SDU) {
315 t = get_tx_struct(tx, &no_spc);
317 /* This case must not happen. */
318 spin_unlock_irqrestore(&tx->lock, flags);
321 list_add_tail(&t->list, &tx->sdu_list);
323 t = alloc_tx_struct(tx);
325 spin_unlock_irqrestore(&tx->lock, flags);
328 list_add_tail(&t->list, &tx->hci_list);
331 memcpy(t->buf + padding, data, len);
333 t->cb_data = cb_data;
335 /* In some cases, USB Module of WiMax is blocked when data size is
336 * the multiple of 512. So, increment length by one in that case.
338 if ((len % 512) == 0)
341 usb_fill_bulk_urb(t->urb, usbdev, usb_sndbulkpipe(usbdev, 1), t->buf,
342 len + padding, gdm_usb_send_complete, t);
344 dev_dbg(&usbdev->dev, "usb_send: %*ph\n", len + padding, t->buf);
346 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
347 if (usbdev->state & USB_STATE_SUSPENDED) {
348 list_add_tail(&t->p_list, &tx->pending_list);
349 schedule_work(&udev->pm_ws);
352 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
354 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
355 if (udev->bw_switch) {
356 list_add_tail(&t->p_list, &tx->pending_list);
358 } else if (cmd_evt == WIMAX_SCAN) {
364 spin_lock_irqsave(&rx->lock, flags2);
365 list_for_each_entry(r, &rx->used_list, list)
366 usb_unlink_urb(r->urb);
367 spin_unlock_irqrestore(&rx->lock, flags2);
371 spin_lock_irqsave(&k_lock, flags2);
372 list_add_tail(&udev->list, &k_list);
373 spin_unlock_irqrestore(&k_lock, flags2);
377 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
379 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
383 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
384 usb_mark_last_busy(usbdev);
385 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
387 #if defined(CONFIG_WIMAX_GDM72XX_USB_PM) || defined(CONFIG_WIMAX_GDM72XX_K_MODE)
390 spin_unlock_irqrestore(&tx->lock, flags);
399 __gdm_usb_send_complete(t->urb);
400 spin_unlock_irqrestore(&tx->lock, flags);
404 static void gdm_usb_rcv_complete(struct urb *urb)
406 struct usb_rx *r = urb->context;
407 struct rx_cxt *rx = r->rx_cxt;
408 struct usbwm_dev *udev = container_of(r->rx_cxt, struct usbwm_dev, rx);
409 struct tx_cxt *tx = &udev->tx;
412 unsigned long flags, flags2;
413 struct usb_device *dev = urb->dev;
415 /* Completion by usb_unlink_urb */
416 if (urb->status == -ECONNRESET)
419 spin_lock_irqsave(&tx->lock, flags);
422 cmd_evt = (r->buf[0] << 8) | (r->buf[1]);
424 dev_dbg(&dev->dev, "usb_receive: %*ph\n", urb->actual_length,
427 if (cmd_evt == WIMAX_SDU_TX_FLOW) {
428 if (r->buf[4] == 0) {
429 dev_dbg(&dev->dev, "WIMAX ==> STOP SDU TX\n");
430 list_for_each_entry(t, &tx->sdu_list, list)
431 usb_unlink_urb(t->urb);
432 } else if (r->buf[4] == 1) {
433 dev_dbg(&dev->dev, "WIMAX ==> START SDU TX\n");
434 list_for_each_entry(t, &tx->sdu_list, list) {
435 usb_submit_urb(t->urb, GFP_ATOMIC);
437 /* If free buffer for sdu tx doesn't
438 * exist, then tx queue should not be
439 * woken. For this reason, don't pass
440 * the command, START_SDU_TX.
442 if (list_empty(&tx->free_list))
443 urb->actual_length = 0;
448 if (!urb->status && r->callback)
449 r->callback(r->cb_data, r->buf, urb->actual_length);
451 spin_lock_irqsave(&rx->lock, flags2);
452 put_rx_struct(rx, r);
453 spin_unlock_irqrestore(&rx->lock, flags2);
455 spin_unlock_irqrestore(&tx->lock, flags);
457 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
458 usb_mark_last_busy(dev);
462 static int gdm_usb_receive(void *priv_dev,
463 void (*cb)(void *cb_data, void *data, int len),
466 struct usbwm_dev *udev = priv_dev;
467 struct usb_device *usbdev = udev->usbdev;
468 struct rx_cxt *rx = &udev->rx;
473 dev_err(&usbdev->dev, "%s: No such device\n", __func__);
477 spin_lock_irqsave(&rx->lock, flags);
478 r = get_rx_struct(rx);
479 spin_unlock_irqrestore(&rx->lock, flags);
485 r->cb_data = cb_data;
487 usb_fill_bulk_urb(r->urb, usbdev, usb_rcvbulkpipe(usbdev, 0x82), r->buf,
488 RX_BUF_SIZE, gdm_usb_rcv_complete, r);
490 return usb_submit_urb(r->urb, GFP_ATOMIC);
493 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
494 static void do_pm_control(struct work_struct *work)
496 struct usbwm_dev *udev = container_of(work, struct usbwm_dev, pm_ws);
497 struct tx_cxt *tx = &udev->tx;
501 ret = usb_autopm_get_interface(udev->intf);
503 usb_autopm_put_interface(udev->intf);
505 spin_lock_irqsave(&tx->lock, flags);
506 if (!(udev->usbdev->state & USB_STATE_SUSPENDED) &&
507 (!list_empty(&tx->hci_list) || !list_empty(&tx->sdu_list))) {
508 struct usb_tx *t, *temp;
510 list_for_each_entry_safe(t, temp, &tx->pending_list, p_list) {
511 list_del(&t->p_list);
512 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
516 __gdm_usb_send_complete(t->urb);
520 spin_unlock_irqrestore(&tx->lock, flags);
522 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
524 static int gdm_usb_probe(struct usb_interface *intf,
525 const struct usb_device_id *id)
528 u8 bConfigurationValue;
529 struct phy_dev *phy_dev = NULL;
530 struct usbwm_dev *udev = NULL;
531 u16 idVendor, idProduct, bcdDevice;
533 struct usb_device *usbdev = interface_to_usbdev(intf);
536 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
538 /*USB description is set up with Little-Endian*/
539 idVendor = le16_to_cpu(usbdev->descriptor.idVendor);
540 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
541 bcdDevice = le16_to_cpu(usbdev->descriptor.bcdDevice);
543 dev_info(&intf->dev, "Found GDM USB VID = 0x%04x PID = 0x%04x...\n",
544 idVendor, idProduct);
545 dev_info(&intf->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
548 if (idProduct == EMERGENCY_PID) {
549 ret = usb_emergency(usbdev);
553 /* Support for EEPROM bootloader */
554 if (bConfigurationValue == DOWNLOAD_CONF_VALUE ||
555 idProduct & B_DOWNLOAD) {
556 ret = usb_boot(usbdev, bcdDevice);
560 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
561 if (phy_dev == NULL) {
565 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
571 if (idProduct == 0x7205 || idProduct == 0x7206)
572 udev->padding = GDM7205_PADDING;
576 phy_dev->priv_dev = (void *)udev;
577 phy_dev->send_func = gdm_usb_send;
578 phy_dev->rcv_func = gdm_usb_receive;
580 ret = init_usb(udev);
584 udev->usbdev = usbdev;
586 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
589 intf->needs_remote_wakeup = 1;
590 device_init_wakeup(&intf->dev, 1);
592 pm_runtime_set_autosuspend_delay(&usbdev->dev, 10 * 1000); /* msec */
594 INIT_WORK(&udev->pm_ws, do_pm_control);
595 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
597 ret = register_wimax_device(phy_dev, &intf->dev);
607 usb_set_intfdata(intf, phy_dev);
612 static void gdm_usb_disconnect(struct usb_interface *intf)
614 u8 bConfigurationValue;
615 struct phy_dev *phy_dev;
616 struct usbwm_dev *udev;
618 struct usb_device *usbdev = interface_to_usbdev(intf);
620 bConfigurationValue = usbdev->actconfig->desc.bConfigurationValue;
621 phy_dev = usb_get_intfdata(intf);
623 /*USB description is set up with Little-Endian*/
624 idProduct = le16_to_cpu(usbdev->descriptor.idProduct);
626 if (idProduct != EMERGENCY_PID &&
627 bConfigurationValue != DOWNLOAD_CONF_VALUE &&
628 (idProduct & B_DOWNLOAD) == 0) {
629 udev = phy_dev->priv_dev;
632 unregister_wimax_device(phy_dev);
641 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
642 static int gdm_suspend(struct usb_interface *intf, pm_message_t pm_msg)
644 struct phy_dev *phy_dev;
645 struct usbwm_dev *udev;
650 phy_dev = usb_get_intfdata(intf);
654 udev = phy_dev->priv_dev;
657 spin_lock_irqsave(&rx->lock, flags);
659 list_for_each_entry(r, &rx->used_list, list)
660 usb_unlink_urb(r->urb);
662 spin_unlock_irqrestore(&rx->lock, flags);
667 static int gdm_resume(struct usb_interface *intf)
669 struct phy_dev *phy_dev;
670 struct usbwm_dev *udev;
675 phy_dev = usb_get_intfdata(intf);
679 udev = phy_dev->priv_dev;
682 spin_lock_irqsave(&rx->lock, flags);
684 list_for_each_entry(r, &rx->used_list, list)
685 usb_submit_urb(r->urb, GFP_ATOMIC);
687 spin_unlock_irqrestore(&rx->lock, flags);
692 #endif /* CONFIG_WIMAX_GDM72XX_USB_PM */
694 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
695 static int k_mode_thread(void *arg)
697 struct usbwm_dev *udev;
700 struct usb_tx *t, *temp;
702 unsigned long flags, flags2, expire;
705 while (!k_mode_stop) {
706 spin_lock_irqsave(&k_lock, flags2);
707 while (!list_empty(&k_list)) {
708 udev = list_entry(k_list.next, struct usbwm_dev, list);
712 list_del(&udev->list);
713 spin_unlock_irqrestore(&k_lock, flags2);
715 expire = jiffies + K_WAIT_TIME;
716 while (time_before(jiffies, expire))
717 schedule_timeout(K_WAIT_TIME);
719 spin_lock_irqsave(&rx->lock, flags);
721 list_for_each_entry(r, &rx->used_list, list)
722 usb_submit_urb(r->urb, GFP_ATOMIC);
724 spin_unlock_irqrestore(&rx->lock, flags);
726 spin_lock_irqsave(&tx->lock, flags);
728 list_for_each_entry_safe(t, temp, &tx->pending_list,
730 list_del(&t->p_list);
731 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
735 __gdm_usb_send_complete(t->urb);
740 spin_unlock_irqrestore(&tx->lock, flags);
742 spin_lock_irqsave(&k_lock, flags2);
744 wait_event_interruptible_lock_irq(k_wait,
745 !list_empty(&k_list) ||
746 k_mode_stop, k_lock);
747 spin_unlock_irqrestore(&k_lock, flags2);
751 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
753 static struct usb_driver gdm_usb_driver = {
755 .probe = gdm_usb_probe,
756 .disconnect = gdm_usb_disconnect,
757 .id_table = id_table,
758 #ifdef CONFIG_WIMAX_GDM72XX_USB_PM
759 .supports_autosuspend = 1,
760 .suspend = gdm_suspend,
761 .resume = gdm_resume,
762 .reset_resume = gdm_resume,
766 static int __init usb_gdm_wimax_init(void)
768 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
769 kthread_run(k_mode_thread, NULL, "k_mode_wimax");
770 #endif /* CONFIG_WIMAX_GDM72XX_K_MODE */
771 return usb_register(&gdm_usb_driver);
774 static void __exit usb_gdm_wimax_exit(void)
776 #ifdef CONFIG_WIMAX_GDM72XX_K_MODE
780 usb_deregister(&gdm_usb_driver);
783 module_init(usb_gdm_wimax_init);
784 module_exit(usb_gdm_wimax_exit);
786 MODULE_VERSION(DRIVER_VERSION);
787 MODULE_DESCRIPTION("GCT WiMax Device Driver");
788 MODULE_AUTHOR("Ethan Park");
789 MODULE_LICENSE("GPL");