Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / staging / gdm724x / gdm_usb.c
blob781134af69d13fe37481ed06f4685fc746f34f64
1 /*
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/usb.h>
19 #include <linux/sched.h>
20 #include <linux/kthread.h>
21 #include <linux/usb/cdc.h>
22 #include <linux/wait.h>
23 #include <linux/if_ether.h>
24 #include <linux/pm_runtime.h>
26 #include "gdm_usb.h"
27 #include "gdm_lte.h"
28 #include "hci.h"
29 #include "hci_packet.h"
30 #include "gdm_endian.h"
32 #define USB_DEVICE_CDC_DATA(vid, pid) \
33 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS,\
34 .idVendor = vid,\
35 .idProduct = pid,\
36 .bInterfaceClass = USB_CLASS_COMM,\
37 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET
39 #define USB_DEVICE_MASS_DATA(vid, pid) \
40 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,\
41 .idVendor = vid,\
42 .idProduct = pid,\
43 .bInterfaceSubClass = USB_SC_SCSI, \
44 .bInterfaceClass = USB_CLASS_MASS_STORAGE,\
45 .bInterfaceProtocol = USB_PR_BULK
47 static const struct usb_device_id id_table[] = {
48 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7240) }, /* GCT GDM7240 */
49 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7243) }, /* GCT GDM7243 */
50 { }
53 MODULE_DEVICE_TABLE(usb, id_table);
55 static struct workqueue_struct *usb_tx_wq;
56 static struct workqueue_struct *usb_rx_wq;
58 static void do_tx(struct work_struct *work);
59 static void do_rx(struct work_struct *work);
61 static int gdm_usb_recv(void *priv_dev,
62 int (*cb)(void *cb_data, void *data, int len, int context),
63 void *cb_data,
64 int context);
66 static int request_mac_address(struct lte_udev *udev)
68 u8 buf[16] = {0,};
69 struct hci_packet *hci = (struct hci_packet *)buf;
70 struct usb_device *usbdev = udev->usbdev;
71 int actual;
72 int ret = -1;
74 hci->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_GET_INFORMATION);
75 hci->len = gdm_cpu_to_dev16(&udev->gdm_ed, 1);
76 hci->data[0] = MAC_ADDRESS;
78 ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5,
79 &actual, 1000);
81 udev->request_mac_addr = 1;
83 return ret;
86 static struct usb_tx *alloc_tx_struct(int len)
88 struct usb_tx *t = NULL;
89 int ret = 0;
91 t = kzalloc(sizeof(struct usb_tx), GFP_ATOMIC);
92 if (!t) {
93 ret = -ENOMEM;
94 goto out;
97 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
98 if (!(len % 512))
99 len++;
101 t->buf = kmalloc(len, GFP_ATOMIC);
102 if (!t->urb || !t->buf) {
103 ret = -ENOMEM;
104 goto out;
107 out:
108 if (ret < 0) {
109 if (t) {
110 usb_free_urb(t->urb);
111 kfree(t->buf);
112 kfree(t);
114 return NULL;
117 return t;
120 static struct usb_tx_sdu *alloc_tx_sdu_struct(void)
122 struct usb_tx_sdu *t_sdu = NULL;
123 int ret = 0;
126 t_sdu = kzalloc(sizeof(struct usb_tx_sdu), GFP_ATOMIC);
127 if (!t_sdu) {
128 ret = -ENOMEM;
129 goto out;
132 t_sdu->buf = kmalloc(SDU_BUF_SIZE, GFP_ATOMIC);
133 if (!t_sdu->buf) {
134 ret = -ENOMEM;
135 goto out;
137 out:
139 if (ret < 0) {
140 if (t_sdu) {
141 kfree(t_sdu->buf);
142 kfree(t_sdu);
144 return NULL;
147 return t_sdu;
150 static void free_tx_struct(struct usb_tx *t)
152 if (t) {
153 usb_free_urb(t->urb);
154 kfree(t->buf);
155 kfree(t);
159 static void free_tx_sdu_struct(struct usb_tx_sdu *t_sdu)
161 if (t_sdu) {
162 kfree(t_sdu->buf);
163 kfree(t_sdu);
167 static struct usb_tx_sdu *get_tx_sdu_struct(struct tx_cxt *tx, int *no_spc)
169 struct usb_tx_sdu *t_sdu;
171 if (list_empty(&tx->free_list))
172 return NULL;
174 t_sdu = list_entry(tx->free_list.next, struct usb_tx_sdu, list);
175 list_del(&t_sdu->list);
177 tx->avail_count--;
179 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
181 return t_sdu;
184 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx_sdu *t_sdu)
186 list_add_tail(&t_sdu->list, &tx->free_list);
187 tx->avail_count++;
190 static struct usb_rx *alloc_rx_struct(void)
192 struct usb_rx *r = NULL;
193 int ret = 0;
195 r = kmalloc(sizeof(struct usb_rx), GFP_ATOMIC);
196 if (!r) {
197 ret = -ENOMEM;
198 goto out;
201 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
202 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
203 if (!r->urb || !r->buf) {
204 ret = -ENOMEM;
205 goto out;
207 out:
209 if (ret < 0) {
210 if (r) {
211 usb_free_urb(r->urb);
212 kfree(r->buf);
213 kfree(r);
215 return NULL;
218 return r;
221 static void free_rx_struct(struct usb_rx *r)
223 if (r) {
224 usb_free_urb(r->urb);
225 kfree(r->buf);
226 kfree(r);
230 static struct usb_rx *get_rx_struct(struct rx_cxt *rx, int *no_spc)
232 struct usb_rx *r;
233 unsigned long flags;
235 spin_lock_irqsave(&rx->rx_lock, flags);
237 if (list_empty(&rx->free_list)) {
238 spin_unlock_irqrestore(&rx->rx_lock, flags);
239 return NULL;
242 r = list_entry(rx->free_list.next, struct usb_rx, free_list);
243 list_del(&r->free_list);
245 rx->avail_count--;
247 *no_spc = list_empty(&rx->free_list) ? 1 : 0;
249 spin_unlock_irqrestore(&rx->rx_lock, flags);
251 return r;
254 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
256 unsigned long flags;
258 spin_lock_irqsave(&rx->rx_lock, flags);
260 list_add_tail(&r->free_list, &rx->free_list);
261 rx->avail_count++;
263 spin_unlock_irqrestore(&rx->rx_lock, flags);
266 static void release_usb(struct lte_udev *udev)
268 struct rx_cxt *rx = &udev->rx;
269 struct tx_cxt *tx = &udev->tx;
270 struct usb_tx *t, *t_next;
271 struct usb_rx *r, *r_next;
272 struct usb_tx_sdu *t_sdu, *t_sdu_next;
273 unsigned long flags;
275 spin_lock_irqsave(&tx->lock, flags);
276 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->sdu_list, list)
278 list_del(&t_sdu->list);
279 free_tx_sdu_struct(t_sdu);
282 list_for_each_entry_safe(t, t_next, &tx->hci_list, list)
284 list_del(&t->list);
285 free_tx_struct(t);
288 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->free_list, list)
290 list_del(&t_sdu->list);
291 free_tx_sdu_struct(t_sdu);
293 spin_unlock_irqrestore(&tx->lock, flags);
295 spin_lock_irqsave(&rx->submit_lock, flags);
296 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list)
298 spin_unlock_irqrestore(&rx->submit_lock, flags);
299 usb_kill_urb(r->urb);
300 spin_lock_irqsave(&rx->submit_lock, flags);
302 spin_unlock_irqrestore(&rx->submit_lock, flags);
304 spin_lock_irqsave(&rx->rx_lock, flags);
305 list_for_each_entry_safe(r, r_next, &rx->free_list, free_list)
307 list_del(&r->free_list);
308 free_rx_struct(r);
310 spin_unlock_irqrestore(&rx->rx_lock, flags);
312 spin_lock_irqsave(&rx->to_host_lock, flags);
313 list_for_each_entry_safe(r, r_next, &rx->to_host_list, to_host_list)
315 if (r->index == (void *)udev) {
316 list_del(&r->to_host_list);
317 free_rx_struct(r);
320 spin_unlock_irqrestore(&rx->to_host_lock, flags);
323 static int init_usb(struct lte_udev *udev)
325 int ret = 0;
326 int i;
327 struct tx_cxt *tx = &udev->tx;
328 struct rx_cxt *rx = &udev->rx;
329 struct usb_tx_sdu *t_sdu = NULL;
330 struct usb_rx *r = NULL;
332 udev->send_complete = 1;
333 udev->tx_stop = 0;
334 udev->request_mac_addr = 0;
335 udev->usb_state = PM_NORMAL;
337 INIT_LIST_HEAD(&tx->sdu_list);
338 INIT_LIST_HEAD(&tx->hci_list);
339 INIT_LIST_HEAD(&tx->free_list);
340 INIT_LIST_HEAD(&rx->rx_submit_list);
341 INIT_LIST_HEAD(&rx->free_list);
342 INIT_LIST_HEAD(&rx->to_host_list);
343 spin_lock_init(&tx->lock);
344 spin_lock_init(&rx->rx_lock);
345 spin_lock_init(&rx->submit_lock);
346 spin_lock_init(&rx->to_host_lock);
348 tx->avail_count = 0;
349 rx->avail_count = 0;
351 udev->rx_cb = NULL;
353 for (i = 0; i < MAX_NUM_SDU_BUF; i++) {
354 t_sdu = alloc_tx_sdu_struct();
355 if (t_sdu == NULL) {
356 ret = -ENOMEM;
357 goto fail;
360 list_add(&t_sdu->list, &tx->free_list);
361 tx->avail_count++;
364 for (i = 0; i < MAX_RX_SUBMIT_COUNT*2; i++) {
365 r = alloc_rx_struct();
366 if (r == NULL) {
367 ret = -ENOMEM;
368 goto fail;
371 list_add(&r->free_list, &rx->free_list);
372 rx->avail_count++;
374 INIT_DELAYED_WORK(&udev->work_tx, do_tx);
375 INIT_DELAYED_WORK(&udev->work_rx, do_rx);
376 return 0;
377 fail:
378 return ret;
381 static int set_mac_address(u8 *data, void *arg)
383 struct phy_dev *phy_dev = (struct phy_dev *)arg;
384 struct lte_udev *udev = phy_dev->priv_dev;
385 struct tlv *tlv = (struct tlv *)data;
386 u8 mac_address[ETH_ALEN] = {0, };
388 if (tlv->type == MAC_ADDRESS && udev->request_mac_addr) {
389 memcpy(mac_address, tlv->data, tlv->len);
391 if (register_lte_device(phy_dev, &udev->intf->dev, mac_address) < 0)
392 pr_err("register lte device failed\n");
394 udev->request_mac_addr = 0;
396 return 1;
399 return 0;
402 static void do_rx(struct work_struct *work)
404 struct lte_udev *udev = container_of(work, struct lte_udev, work_rx.work);
405 struct rx_cxt *rx = &udev->rx;
406 struct usb_rx *r;
407 struct hci_packet *hci;
408 struct phy_dev *phy_dev;
409 u16 cmd_evt;
410 int ret;
411 unsigned long flags;
413 while (1) {
414 spin_lock_irqsave(&rx->to_host_lock, flags);
415 if (list_empty(&rx->to_host_list)) {
416 spin_unlock_irqrestore(&rx->to_host_lock, flags);
417 break;
419 r = list_entry(rx->to_host_list.next, struct usb_rx, to_host_list);
420 list_del(&r->to_host_list);
421 spin_unlock_irqrestore(&rx->to_host_lock, flags);
423 phy_dev = (struct phy_dev *)r->cb_data;
424 udev = (struct lte_udev *)phy_dev->priv_dev;
425 hci = (struct hci_packet *)r->buf;
426 cmd_evt = gdm_dev16_to_cpu(&udev->gdm_ed, hci->cmd_evt);
428 switch (cmd_evt) {
429 case LTE_GET_INFORMATION_RESULT:
430 if (set_mac_address(hci->data, r->cb_data) == 0) {
431 ret = r->callback(r->cb_data,
432 r->buf,
433 r->urb->actual_length,
434 KERNEL_THREAD);
436 break;
438 default:
439 if (r->callback) {
440 ret = r->callback(r->cb_data,
441 r->buf,
442 r->urb->actual_length,
443 KERNEL_THREAD);
445 if (ret == -EAGAIN)
446 pr_err("failed to send received data\n");
448 break;
451 put_rx_struct(rx, r);
453 gdm_usb_recv(udev,
454 r->callback,
455 r->cb_data,
456 USB_COMPLETE);
460 static void remove_rx_submit_list(struct usb_rx *r, struct rx_cxt *rx)
462 unsigned long flags;
463 struct usb_rx *r_remove, *r_remove_next;
465 spin_lock_irqsave(&rx->submit_lock, flags);
466 list_for_each_entry_safe(r_remove, r_remove_next, &rx->rx_submit_list, rx_submit_list)
468 if (r == r_remove) {
469 list_del(&r->rx_submit_list);
470 break;
473 spin_unlock_irqrestore(&rx->submit_lock, flags);
476 static void gdm_usb_rcv_complete(struct urb *urb)
478 struct usb_rx *r = urb->context;
479 struct rx_cxt *rx = r->rx;
480 unsigned long flags;
481 struct lte_udev *udev = container_of(r->rx, struct lte_udev, rx);
482 struct usb_device *usbdev = udev->usbdev;
484 remove_rx_submit_list(r, rx);
486 if (!urb->status && r->callback) {
487 spin_lock_irqsave(&rx->to_host_lock, flags);
488 list_add_tail(&r->to_host_list, &rx->to_host_list);
489 queue_work(usb_rx_wq, &udev->work_rx.work);
490 spin_unlock_irqrestore(&rx->to_host_lock, flags);
491 } else {
492 if (urb->status && udev->usb_state == PM_NORMAL)
493 pr_err("%s: urb status error %d\n",
494 __func__, urb->status);
496 put_rx_struct(rx, r);
499 usb_mark_last_busy(usbdev);
502 static int gdm_usb_recv(void *priv_dev,
503 int (*cb)(void *cb_data, void *data, int len, int context),
504 void *cb_data,
505 int context)
507 struct lte_udev *udev = priv_dev;
508 struct usb_device *usbdev = udev->usbdev;
509 struct rx_cxt *rx = &udev->rx;
510 struct usb_rx *r;
511 int no_spc;
512 int ret;
513 unsigned long flags;
515 if (!udev->usbdev) {
516 pr_err("invalid device\n");
517 return -ENODEV;
520 r = get_rx_struct(rx, &no_spc);
521 if (!r) {
522 pr_err("Out of Memory\n");
523 return -ENOMEM;
526 udev->rx_cb = cb;
527 r->callback = cb;
528 r->cb_data = cb_data;
529 r->index = (void *)udev;
530 r->rx = rx;
532 usb_fill_bulk_urb(r->urb,
533 usbdev,
534 usb_rcvbulkpipe(usbdev, 0x83),
535 r->buf,
536 RX_BUF_SIZE,
537 gdm_usb_rcv_complete,
540 spin_lock_irqsave(&rx->submit_lock, flags);
541 list_add_tail(&r->rx_submit_list, &rx->rx_submit_list);
542 spin_unlock_irqrestore(&rx->submit_lock, flags);
544 if (context == KERNEL_THREAD)
545 ret = usb_submit_urb(r->urb, GFP_KERNEL);
546 else
547 ret = usb_submit_urb(r->urb, GFP_ATOMIC);
549 if (ret) {
550 spin_lock_irqsave(&rx->submit_lock, flags);
551 list_del(&r->rx_submit_list);
552 spin_unlock_irqrestore(&rx->submit_lock, flags);
554 pr_err("usb_submit_urb failed (%p)\n", r);
555 put_rx_struct(rx, r);
558 return ret;
561 static void gdm_usb_send_complete(struct urb *urb)
563 struct usb_tx *t = urb->context;
564 struct tx_cxt *tx = t->tx;
565 struct lte_udev *udev = container_of(tx, struct lte_udev, tx);
566 unsigned long flags;
568 if (urb->status == -ECONNRESET) {
569 pr_info("CONNRESET\n");
570 return;
573 if (t->callback)
574 t->callback(t->cb_data);
576 free_tx_struct(t);
578 spin_lock_irqsave(&tx->lock, flags);
579 udev->send_complete = 1;
580 queue_work(usb_tx_wq, &udev->work_tx.work);
581 spin_unlock_irqrestore(&tx->lock, flags);
584 static int send_tx_packet(struct usb_device *usbdev, struct usb_tx *t, u32 len)
586 int ret = 0;
588 if (!(len%512))
589 len++;
591 usb_fill_bulk_urb(t->urb,
592 usbdev,
593 usb_sndbulkpipe(usbdev, 2),
594 t->buf,
595 len,
596 gdm_usb_send_complete,
599 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
601 if (ret)
602 pr_err("usb_submit_urb failed: %d\n", ret);
604 usb_mark_last_busy(usbdev);
606 return ret;
609 static u32 packet_aggregation(struct lte_udev *udev, u8 *send_buf)
611 struct tx_cxt *tx = &udev->tx;
612 struct usb_tx_sdu *t_sdu = NULL;
613 struct multi_sdu *multi_sdu = (struct multi_sdu *)send_buf;
614 u16 send_len = 0;
615 u16 num_packet = 0;
616 unsigned long flags;
618 multi_sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_MULTI_SDU);
620 while (num_packet < MAX_PACKET_IN_MULTI_SDU) {
621 spin_lock_irqsave(&tx->lock, flags);
622 if (list_empty(&tx->sdu_list)) {
623 spin_unlock_irqrestore(&tx->lock, flags);
624 break;
627 t_sdu = list_entry(tx->sdu_list.next, struct usb_tx_sdu, list);
628 if (send_len + t_sdu->len > MAX_SDU_SIZE) {
629 spin_unlock_irqrestore(&tx->lock, flags);
630 break;
633 list_del(&t_sdu->list);
634 spin_unlock_irqrestore(&tx->lock, flags);
636 memcpy(multi_sdu->data + send_len, t_sdu->buf, t_sdu->len);
638 send_len += (t_sdu->len + 3) & 0xfffc;
639 num_packet++;
641 if (tx->avail_count > 10)
642 t_sdu->callback(t_sdu->cb_data);
644 spin_lock_irqsave(&tx->lock, flags);
645 put_tx_struct(tx, t_sdu);
646 spin_unlock_irqrestore(&tx->lock, flags);
649 multi_sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len);
650 multi_sdu->num_packet = gdm_cpu_to_dev16(&udev->gdm_ed, num_packet);
652 return send_len + offsetof(struct multi_sdu, data);
655 static void do_tx(struct work_struct *work)
657 struct lte_udev *udev = container_of(work, struct lte_udev, work_tx.work);
658 struct usb_device *usbdev = udev->usbdev;
659 struct tx_cxt *tx = &udev->tx;
660 struct usb_tx *t = NULL;
661 int is_send = 0;
662 u32 len = 0;
663 unsigned long flags;
665 if (!usb_autopm_get_interface(udev->intf))
666 usb_autopm_put_interface(udev->intf);
668 if (udev->usb_state == PM_SUSPEND)
669 return;
671 spin_lock_irqsave(&tx->lock, flags);
672 if (!udev->send_complete) {
673 spin_unlock_irqrestore(&tx->lock, flags);
674 return;
675 } else {
676 udev->send_complete = 0;
679 if (!list_empty(&tx->hci_list)) {
680 t = list_entry(tx->hci_list.next, struct usb_tx, list);
681 list_del(&t->list);
682 len = t->len;
683 t->is_sdu = 0;
684 is_send = 1;
685 } else if (!list_empty(&tx->sdu_list)) {
686 if (udev->tx_stop) {
687 udev->send_complete = 1;
688 spin_unlock_irqrestore(&tx->lock, flags);
689 return;
692 t = alloc_tx_struct(TX_BUF_SIZE);
693 t->callback = NULL;
694 t->tx = tx;
695 t->is_sdu = 1;
696 is_send = 1;
699 if (!is_send) {
700 udev->send_complete = 1;
701 spin_unlock_irqrestore(&tx->lock, flags);
702 return;
704 spin_unlock_irqrestore(&tx->lock, flags);
706 if (t->is_sdu)
707 len = packet_aggregation(udev, t->buf);
709 if (send_tx_packet(usbdev, t, len)) {
710 pr_err("send_tx_packet failed\n");
711 t->callback = NULL;
712 gdm_usb_send_complete(t->urb);
716 #define SDU_PARAM_LEN 12
717 static int gdm_usb_sdu_send(void *priv_dev, void *data, int len,
718 unsigned int dftEpsId, unsigned int epsId,
719 void (*cb)(void *data), void *cb_data,
720 int dev_idx, int nic_type)
722 struct lte_udev *udev = priv_dev;
723 struct tx_cxt *tx = &udev->tx;
724 struct usb_tx_sdu *t_sdu;
725 struct sdu *sdu = NULL;
726 unsigned long flags;
727 int no_spc = 0;
728 u16 send_len;
730 if (!udev->usbdev) {
731 pr_err("sdu send - invalid device\n");
732 return TX_NO_DEV;
735 spin_lock_irqsave(&tx->lock, flags);
736 t_sdu = get_tx_sdu_struct(tx, &no_spc);
737 spin_unlock_irqrestore(&tx->lock, flags);
739 if (t_sdu == NULL) {
740 pr_err("sdu send - free list empty\n");
741 return TX_NO_SPC;
744 sdu = (struct sdu *)t_sdu->buf;
745 sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_SDU);
746 if (nic_type == NIC_TYPE_ARP) {
747 send_len = len + SDU_PARAM_LEN;
748 memcpy(sdu->data, data, len);
749 } else {
750 send_len = len - ETH_HLEN;
751 send_len += SDU_PARAM_LEN;
752 memcpy(sdu->data, data+ETH_HLEN, len-ETH_HLEN);
755 sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len);
756 sdu->dftEpsId = gdm_cpu_to_dev32(&udev->gdm_ed, dftEpsId);
757 sdu->bearer_ID = gdm_cpu_to_dev32(&udev->gdm_ed, epsId);
758 sdu->nic_type = gdm_cpu_to_dev32(&udev->gdm_ed, nic_type);
760 t_sdu->len = send_len + HCI_HEADER_SIZE;
761 t_sdu->callback = cb;
762 t_sdu->cb_data = cb_data;
764 spin_lock_irqsave(&tx->lock, flags);
765 list_add_tail(&t_sdu->list, &tx->sdu_list);
766 queue_work(usb_tx_wq, &udev->work_tx.work);
767 spin_unlock_irqrestore(&tx->lock, flags);
769 if (no_spc)
770 return TX_NO_BUFFER;
772 return 0;
775 static int gdm_usb_hci_send(void *priv_dev, void *data, int len,
776 void (*cb)(void *data), void *cb_data)
778 struct lte_udev *udev = priv_dev;
779 struct tx_cxt *tx = &udev->tx;
780 struct usb_tx *t;
781 unsigned long flags;
783 if (!udev->usbdev) {
784 pr_err("hci send - invalid device\n");
785 return -ENODEV;
788 t = alloc_tx_struct(len);
789 if (t == NULL) {
790 pr_err("hci_send - out of memory\n");
791 return -ENOMEM;
794 memcpy(t->buf, data, len);
795 t->callback = cb;
796 t->cb_data = cb_data;
797 t->len = len;
798 t->tx = tx;
799 t->is_sdu = 0;
801 spin_lock_irqsave(&tx->lock, flags);
802 list_add_tail(&t->list, &tx->hci_list);
803 queue_work(usb_tx_wq, &udev->work_tx.work);
804 spin_unlock_irqrestore(&tx->lock, flags);
806 return 0;
809 static struct gdm_endian *gdm_usb_get_endian(void *priv_dev)
811 struct lte_udev *udev = priv_dev;
813 return &udev->gdm_ed;
816 static int gdm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
818 int ret = 0;
819 struct phy_dev *phy_dev = NULL;
820 struct lte_udev *udev = NULL;
821 u16 idVendor, idProduct;
822 int bInterfaceNumber;
823 struct usb_device *usbdev = interface_to_usbdev(intf);
825 bInterfaceNumber = intf->cur_altsetting->desc.bInterfaceNumber;
826 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor);
827 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct);
829 pr_info("net vid = 0x%04x pid = 0x%04x\n", idVendor, idProduct);
831 if (bInterfaceNumber > NETWORK_INTERFACE) {
832 pr_info("not a network device\n");
833 return -1;
836 phy_dev = kmalloc(sizeof(struct phy_dev), GFP_ATOMIC);
837 if (!phy_dev) {
838 ret = -ENOMEM;
839 goto out;
842 udev = kmalloc(sizeof(struct lte_udev), GFP_ATOMIC);
843 if (!udev) {
844 ret = -ENOMEM;
845 goto out;
848 memset(phy_dev, 0, sizeof(struct phy_dev));
849 memset(udev, 0, sizeof(struct lte_udev));
851 phy_dev->priv_dev = (void *)udev;
852 phy_dev->send_hci_func = gdm_usb_hci_send;
853 phy_dev->send_sdu_func = gdm_usb_sdu_send;
854 phy_dev->rcv_func = gdm_usb_recv;
855 phy_dev->get_endian = gdm_usb_get_endian;
857 udev->usbdev = usbdev;
858 ret = init_usb(udev);
859 if (ret < 0) {
860 pr_err("init_usb func failed\n");
861 goto out;
863 udev->intf = intf;
865 intf->needs_remote_wakeup = 1;
866 usb_enable_autosuspend(usbdev);
867 pm_runtime_set_autosuspend_delay(&usbdev->dev, AUTO_SUSPEND_TIMER);
869 /* List up hosts with big endians, otherwise, defaults to little endian */
870 if (idProduct == PID_GDM7243)
871 gdm_set_endian(&udev->gdm_ed, ENDIANNESS_BIG);
872 else
873 gdm_set_endian(&udev->gdm_ed, ENDIANNESS_LITTLE);
875 ret = request_mac_address(udev);
876 if (ret < 0) {
877 pr_err("request Mac address failed\n");
878 goto out;
881 start_rx_proc(phy_dev);
882 out:
884 if (ret < 0) {
885 kfree(phy_dev);
886 if (udev) {
887 release_usb(udev);
888 kfree(udev);
892 usb_get_dev(usbdev);
893 usb_set_intfdata(intf, phy_dev);
895 return ret;
898 static void gdm_usb_disconnect(struct usb_interface *intf)
900 struct phy_dev *phy_dev;
901 struct lte_udev *udev;
902 u16 idVendor, idProduct;
903 struct usb_device *usbdev;
904 usbdev = interface_to_usbdev(intf);
906 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor);
907 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct);
909 phy_dev = usb_get_intfdata(intf);
911 udev = phy_dev->priv_dev;
912 unregister_lte_device(phy_dev);
914 release_usb(udev);
916 kfree(udev);
917 udev = NULL;
919 kfree(phy_dev);
920 phy_dev = NULL;
922 usb_put_dev(usbdev);
925 static int gdm_usb_suspend(struct usb_interface *intf, pm_message_t pm_msg)
927 struct phy_dev *phy_dev;
928 struct lte_udev *udev;
929 struct rx_cxt *rx;
930 struct usb_rx *r;
931 struct usb_rx *r_next;
932 unsigned long flags;
934 phy_dev = usb_get_intfdata(intf);
935 udev = phy_dev->priv_dev;
936 rx = &udev->rx;
937 if (udev->usb_state != PM_NORMAL) {
938 pr_err("usb suspend - invalid state\n");
939 return -1;
942 udev->usb_state = PM_SUSPEND;
944 spin_lock_irqsave(&rx->submit_lock, flags);
945 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list)
947 spin_unlock_irqrestore(&rx->submit_lock, flags);
948 usb_kill_urb(r->urb);
949 spin_lock_irqsave(&rx->submit_lock, flags);
951 spin_unlock_irqrestore(&rx->submit_lock, flags);
953 return 0;
956 static int gdm_usb_resume(struct usb_interface *intf)
958 struct phy_dev *phy_dev;
959 struct lte_udev *udev;
960 struct tx_cxt *tx;
961 struct rx_cxt *rx;
962 unsigned long flags;
963 int issue_count;
964 int i;
966 phy_dev = usb_get_intfdata(intf);
967 udev = phy_dev->priv_dev;
968 rx = &udev->rx;
970 if (udev->usb_state != PM_SUSPEND) {
971 pr_err("usb resume - invalid state\n");
972 return -1;
974 udev->usb_state = PM_NORMAL;
976 spin_lock_irqsave(&rx->rx_lock, flags);
977 issue_count = rx->avail_count - MAX_RX_SUBMIT_COUNT;
978 spin_unlock_irqrestore(&rx->rx_lock, flags);
980 if (issue_count >= 0) {
981 for (i = 0; i < issue_count; i++)
982 gdm_usb_recv(phy_dev->priv_dev,
983 udev->rx_cb,
984 phy_dev,
985 USB_COMPLETE);
988 tx = &udev->tx;
989 spin_lock_irqsave(&tx->lock, flags);
990 queue_work(usb_tx_wq, &udev->work_tx.work);
991 spin_unlock_irqrestore(&tx->lock, flags);
993 return 0;
996 static struct usb_driver gdm_usb_lte_driver = {
997 .name = "gdm_lte",
998 .probe = gdm_usb_probe,
999 .disconnect = gdm_usb_disconnect,
1000 .id_table = id_table,
1001 .supports_autosuspend = 1,
1002 .suspend = gdm_usb_suspend,
1003 .resume = gdm_usb_resume,
1004 .reset_resume = gdm_usb_resume,
1007 static int __init gdm_usb_lte_init(void)
1009 if (gdm_lte_event_init() < 0) {
1010 pr_err("error creating event\n");
1011 return -1;
1014 usb_tx_wq = create_workqueue("usb_tx_wq");
1015 if (usb_tx_wq == NULL)
1016 return -1;
1018 usb_rx_wq = create_workqueue("usb_rx_wq");
1019 if (usb_rx_wq == NULL)
1020 return -1;
1022 return usb_register(&gdm_usb_lte_driver);
1025 static void __exit gdm_usb_lte_exit(void)
1027 gdm_lte_event_exit();
1029 usb_deregister(&gdm_usb_lte_driver);
1031 if (usb_tx_wq) {
1032 flush_workqueue(usb_tx_wq);
1033 destroy_workqueue(usb_tx_wq);
1036 if (usb_rx_wq) {
1037 flush_workqueue(usb_rx_wq);
1038 destroy_workqueue(usb_rx_wq);
1042 module_init(gdm_usb_lte_init);
1043 module_exit(gdm_usb_lte_exit);
1045 MODULE_VERSION(DRIVER_VERSION);
1046 MODULE_DESCRIPTION("GCT LTE USB Device Driver");
1047 MODULE_LICENSE("GPL");