blob: 2e658f84486c15ee5881f30f128a0ea92c0995ee [file] [log] [blame]
Won Kang61e12102013-07-25 03:36:17 +09001/*
2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3 *
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.
7 *
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.
12 */
13
Joe Perches0ec473b2013-07-24 14:13:03 -070014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Won Kang61e12102013-07-25 03:36:17 +090016#include <linux/module.h>
17#include <linux/version.h>
18#include <linux/kernel.h>
19#include <linux/usb.h>
20#include <linux/sched.h>
21#include <linux/kthread.h>
22#include <linux/usb/cdc.h>
23#include <linux/wait.h>
24#include <linux/if_ether.h>
25#include <linux/pm_runtime.h>
26
27#include "gdm_usb.h"
28#include "gdm_lte.h"
29#include "hci.h"
30#include "hci_packet.h"
31#include "gdm_endian.h"
32
33#define USB_DEVICE_CDC_DATA(vid, pid) \
34 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS,\
35 .idVendor = vid,\
36 .idProduct = pid,\
37 .bInterfaceClass = USB_CLASS_COMM,\
38 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET
39
40#define USB_DEVICE_MASS_DATA(vid, pid) \
41 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO,\
42 .idVendor = vid,\
43 .idProduct = pid,\
44 .bInterfaceSubClass = USB_SC_SCSI, \
45 .bInterfaceClass = USB_CLASS_MASS_STORAGE,\
46 .bInterfaceProtocol = USB_PR_BULK
47
48static const struct usb_device_id id_table[] = {
49 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7240) }, /* GCT GDM7240 */
50 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7243) }, /* GCT GDM7243 */
51 { }
52};
53
54MODULE_DEVICE_TABLE(usb, id_table);
55
56static struct workqueue_struct *usb_tx_wq;
57static struct workqueue_struct *usb_rx_wq;
58
59static void do_tx(struct work_struct *work);
60static void do_rx(struct work_struct *work);
61
62static int gdm_usb_recv(void *priv_dev,
63 int (*cb)(void *cb_data, void *data, int len, int context),
64 void *cb_data,
65 int context);
66
67static int request_mac_address(struct lte_udev *udev)
68{
69 u8 buf[16] = {0,};
70 struct hci_packet *hci = (struct hci_packet *)buf;
71 struct usb_device *usbdev = udev->usbdev;
72 int actual;
73 int ret = -1;
74
75 hci->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_GET_INFORMATION);
76 hci->len = gdm_cpu_to_dev16(&udev->gdm_ed, 1);
77 hci->data[0] = MAC_ADDRESS;
78
79 ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5,
80 &actual, 1000);
81
82 udev->request_mac_addr = 1;
83
84 return ret;
85}
86
87static struct usb_tx *alloc_tx_struct(int len)
88{
89 struct usb_tx *t = NULL;
90 int ret = 0;
91
92 t = kmalloc(sizeof(struct usb_tx), GFP_ATOMIC);
93 if (!t) {
94 ret = -ENOMEM;
95 goto out;
96 }
97 memset(t, 0, sizeof(struct usb_tx));
98
99 t->urb = usb_alloc_urb(0, GFP_ATOMIC);
100 if (!(len % 512))
101 len++;
102
103 t->buf = kmalloc(len, GFP_ATOMIC);
104 if (!t->urb || !t->buf) {
105 ret = -ENOMEM;
106 goto out;
107 }
108
109out:
110 if (ret < 0) {
111 if (t) {
112 usb_free_urb(t->urb);
113 kfree(t->buf);
114 kfree(t);
115 }
116 return NULL;
117 }
118
119 return t;
120}
121
122static struct usb_tx_sdu *alloc_tx_sdu_struct(void)
123{
124 struct usb_tx_sdu *t_sdu = NULL;
125 int ret = 0;
126
127
128 t_sdu = kmalloc(sizeof(struct usb_tx_sdu), GFP_ATOMIC);
129 if (!t_sdu) {
130 ret = -ENOMEM;
131 goto out;
132 }
133 memset(t_sdu, 0, sizeof(struct usb_tx_sdu));
134
135 t_sdu->buf = kmalloc(SDU_BUF_SIZE, GFP_ATOMIC);
136 if (!t_sdu->buf) {
137 ret = -ENOMEM;
138 goto out;
139 }
140out:
141
142 if (ret < 0) {
143 if (t_sdu) {
144 kfree(t_sdu->buf);
145 kfree(t_sdu);
146 }
147 return NULL;
148 }
149
150 return t_sdu;
151}
152
153static void free_tx_struct(struct usb_tx *t)
154{
155 if (t) {
156 usb_free_urb(t->urb);
157 kfree(t->buf);
158 kfree(t);
159 }
160}
161
162static void free_tx_sdu_struct(struct usb_tx_sdu *t_sdu)
163{
164 if (t_sdu) {
165 kfree(t_sdu->buf);
166 kfree(t_sdu);
167 }
168}
169
170static struct usb_tx_sdu *get_tx_sdu_struct(struct tx_cxt *tx, int *no_spc)
171{
172 struct usb_tx_sdu *t_sdu;
173
174 if (list_empty(&tx->free_list))
175 return NULL;
176
177 t_sdu = list_entry(tx->free_list.next, struct usb_tx_sdu, list);
178 list_del(&t_sdu->list);
179
180 tx->avail_count--;
181
182 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
183
184 return t_sdu;
185}
186
187static void put_tx_struct(struct tx_cxt *tx, struct usb_tx_sdu *t_sdu)
188{
189 list_add_tail(&t_sdu->list, &tx->free_list);
190 tx->avail_count++;
191}
192
193static struct usb_rx *alloc_rx_struct(void)
194{
195 struct usb_rx *r = NULL;
196 int ret = 0;
197
198 r = kmalloc(sizeof(struct usb_rx), GFP_ATOMIC);
199 if (!r) {
200 ret = -ENOMEM;
201 goto out;
202 }
203
204 r->urb = usb_alloc_urb(0, GFP_ATOMIC);
205 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC);
206 if (!r->urb || !r->buf) {
207 ret = -ENOMEM;
208 goto out;
209 }
210out:
211
212 if (ret < 0) {
213 if (r) {
214 usb_free_urb(r->urb);
215 kfree(r->buf);
216 kfree(r);
217 }
218 return NULL;
219 }
220
221 return r;
222}
223
224static void free_rx_struct(struct usb_rx *r)
225{
226 if (r) {
227 usb_free_urb(r->urb);
228 kfree(r->buf);
229 kfree(r);
230 }
231}
232
233static struct usb_rx *get_rx_struct(struct rx_cxt *rx, int *no_spc)
234{
235 struct usb_rx *r;
236 unsigned long flags;
237
238 spin_lock_irqsave(&rx->rx_lock, flags);
239
240 if (list_empty(&rx->free_list)) {
241 spin_unlock_irqrestore(&rx->rx_lock, flags);
242 return NULL;
243 }
244
245 r = list_entry(rx->free_list.next, struct usb_rx, free_list);
246 list_del(&r->free_list);
247
248 rx->avail_count--;
249
250 *no_spc = list_empty(&rx->free_list) ? 1 : 0;
251
252 spin_unlock_irqrestore(&rx->rx_lock, flags);
253
254 return r;
255}
256
257static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r)
258{
259 unsigned long flags;
260
261 spin_lock_irqsave(&rx->rx_lock, flags);
262
263 list_add_tail(&r->free_list, &rx->free_list);
264 rx->avail_count++;
265
266 spin_unlock_irqrestore(&rx->rx_lock, flags);
267}
268
269static void release_usb(struct lte_udev *udev)
270{
271 struct rx_cxt *rx = &udev->rx;
272 struct tx_cxt *tx = &udev->tx;
273 struct usb_tx *t, *t_next;
274 struct usb_rx *r, *r_next;
275 struct usb_tx_sdu *t_sdu, *t_sdu_next;
276 unsigned long flags;
277
278 spin_lock_irqsave(&tx->lock, flags);
279 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->sdu_list, list)
280 {
281 list_del(&t_sdu->list);
282 free_tx_sdu_struct(t_sdu);
283 }
284
285 list_for_each_entry_safe(t, t_next, &tx->hci_list, list)
286 {
287 list_del(&t->list);
288 free_tx_struct(t);
289 }
290
291 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->free_list, list)
292 {
293 list_del(&t_sdu->list);
294 free_tx_sdu_struct(t_sdu);
295 }
296 spin_unlock_irqrestore(&tx->lock, flags);
297
298 spin_lock_irqsave(&rx->submit_lock, flags);
299 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list)
300 {
301 spin_unlock_irqrestore(&rx->submit_lock, flags);
302 usb_kill_urb(r->urb);
303 spin_lock_irqsave(&rx->submit_lock, flags);
304 }
305 spin_unlock_irqrestore(&rx->submit_lock, flags);
306
307 spin_lock_irqsave(&rx->rx_lock, flags);
308 list_for_each_entry_safe(r, r_next, &rx->free_list, free_list)
309 {
310 list_del(&r->free_list);
311 free_rx_struct(r);
312 }
313 spin_unlock_irqrestore(&rx->rx_lock, flags);
314
315 spin_lock_irqsave(&rx->to_host_lock, flags);
316 list_for_each_entry_safe(r, r_next, &rx->to_host_list, to_host_list)
317 {
318 if (r->index == (void *)udev) {
319 list_del(&r->to_host_list);
320 free_rx_struct(r);
321 }
322 }
323 spin_unlock_irqrestore(&rx->to_host_lock, flags);
324}
325
326static int init_usb(struct lte_udev *udev)
327{
328 int ret = 0;
329 int i;
330 struct tx_cxt *tx = &udev->tx;
331 struct rx_cxt *rx = &udev->rx;
332 struct usb_tx_sdu *t_sdu = NULL;
333 struct usb_rx *r = NULL;
334
335 udev->send_complete = 1;
336 udev->tx_stop = 0;
337 udev->request_mac_addr = 0;
338 udev->usb_state = PM_NORMAL;
339
340 INIT_LIST_HEAD(&tx->sdu_list);
341 INIT_LIST_HEAD(&tx->hci_list);
342 INIT_LIST_HEAD(&tx->free_list);
343 INIT_LIST_HEAD(&rx->rx_submit_list);
344 INIT_LIST_HEAD(&rx->free_list);
345 INIT_LIST_HEAD(&rx->to_host_list);
346 spin_lock_init(&tx->lock);
347 spin_lock_init(&rx->rx_lock);
348 spin_lock_init(&rx->submit_lock);
349 spin_lock_init(&rx->to_host_lock);
350
351 tx->avail_count = 0;
352 rx->avail_count = 0;
353
354 udev->rx_cb = NULL;
355
356 for (i = 0; i < MAX_NUM_SDU_BUF; i++) {
357 t_sdu = alloc_tx_sdu_struct();
358 if (t_sdu == NULL) {
359 ret = -ENOMEM;
360 goto fail;
361 }
362
363 list_add(&t_sdu->list, &tx->free_list);
364 tx->avail_count++;
365 }
366
367 for (i = 0; i < MAX_RX_SUBMIT_COUNT*2; i++) {
368 r = alloc_rx_struct();
369 if (r == NULL) {
370 ret = -ENOMEM;
371 goto fail;
372 }
373
374 list_add(&r->free_list, &rx->free_list);
375 rx->avail_count++;
376 }
377 INIT_DELAYED_WORK(&udev->work_tx, do_tx);
378 INIT_DELAYED_WORK(&udev->work_rx, do_rx);
379 return 0;
380fail:
381 return ret;
382}
383
384static int set_mac_address(u8 *data, void *arg)
385{
386 struct phy_dev *phy_dev = (struct phy_dev *)arg;
387 struct lte_udev *udev = phy_dev->priv_dev;
388 struct tlv *tlv = (struct tlv *)data;
389 u8 mac_address[ETH_ALEN] = {0, };
390
391 if (tlv->type == MAC_ADDRESS && udev->request_mac_addr) {
392 memcpy(mac_address, tlv->data, tlv->len);
393
394 if (register_lte_device(phy_dev, &udev->intf->dev, mac_address) < 0)
Joe Perches0ec473b2013-07-24 14:13:03 -0700395 pr_err("register lte device failed\n");
Won Kang61e12102013-07-25 03:36:17 +0900396
397 udev->request_mac_addr = 0;
398
399 return 1;
400 }
401
402 return 0;
403}
404
405static void do_rx(struct work_struct *work)
406{
407 struct lte_udev *udev = container_of(work, struct lte_udev, work_rx.work);
408 struct rx_cxt *rx = &udev->rx;
409 struct usb_rx *r;
410 struct hci_packet *hci;
411 struct phy_dev *phy_dev;
412 u16 cmd_evt;
413 int ret;
414 unsigned long flags;
415
416 while (1) {
417 spin_lock_irqsave(&rx->to_host_lock, flags);
418 if (list_empty(&rx->to_host_list)) {
419 spin_unlock_irqrestore(&rx->to_host_lock, flags);
420 break;
421 }
422 r = list_entry(rx->to_host_list.next, struct usb_rx, to_host_list);
423 list_del(&r->to_host_list);
424 spin_unlock_irqrestore(&rx->to_host_lock, flags);
425
426 phy_dev = (struct phy_dev *)r->cb_data;
427 udev = (struct lte_udev *)phy_dev->priv_dev;
428 hci = (struct hci_packet *)r->buf;
429 cmd_evt = gdm_dev16_to_cpu(&udev->gdm_ed, hci->cmd_evt);
430
431 switch (cmd_evt) {
432 case LTE_GET_INFORMATION_RESULT:
433 if (set_mac_address(hci->data, r->cb_data) == 0) {
434 ret = r->callback(r->cb_data,
435 r->buf,
436 r->urb->actual_length,
437 KERNEL_THREAD);
438 }
439 break;
440
441 default:
442 if (r->callback) {
443 ret = r->callback(r->cb_data,
444 r->buf,
445 r->urb->actual_length,
446 KERNEL_THREAD);
447
448 if (ret == -EAGAIN)
Joe Perches0ec473b2013-07-24 14:13:03 -0700449 pr_err("failed to send received data\n");
Won Kang61e12102013-07-25 03:36:17 +0900450 }
451 break;
452 }
453
454 put_rx_struct(rx, r);
455
456 gdm_usb_recv(udev,
457 r->callback,
458 r->cb_data,
459 USB_COMPLETE);
460 }
461}
462
463static void remove_rx_submit_list(struct usb_rx *r, struct rx_cxt *rx)
464{
465 unsigned long flags;
466 struct usb_rx *r_remove, *r_remove_next;
467
468 spin_lock_irqsave(&rx->submit_lock, flags);
469 list_for_each_entry_safe(r_remove, r_remove_next, &rx->rx_submit_list, rx_submit_list)
470 {
471 if (r == r_remove) {
472 list_del(&r->rx_submit_list);
473 break;
474 }
475 }
476 spin_unlock_irqrestore(&rx->submit_lock, flags);
477}
478
479static void gdm_usb_rcv_complete(struct urb *urb)
480{
481 struct usb_rx *r = urb->context;
482 struct rx_cxt *rx = r->rx;
483 unsigned long flags;
484 struct lte_udev *udev = container_of(r->rx, struct lte_udev, rx);
485 struct usb_device *usbdev = udev->usbdev;
486
487 remove_rx_submit_list(r, rx);
488
489 if (!urb->status && r->callback) {
490 spin_lock_irqsave(&rx->to_host_lock, flags);
491 list_add_tail(&r->to_host_list, &rx->to_host_list);
492 queue_work(usb_rx_wq, &udev->work_rx.work);
493 spin_unlock_irqrestore(&rx->to_host_lock, flags);
494 } else {
495 if (urb->status && udev->usb_state == PM_NORMAL)
Joe Perches0ec473b2013-07-24 14:13:03 -0700496 pr_err("%s: urb status error %d\n",
497 __func__, urb->status);
Won Kang61e12102013-07-25 03:36:17 +0900498
499 put_rx_struct(rx, r);
500 }
501
502 usb_mark_last_busy(usbdev);
503}
504
505static int gdm_usb_recv(void *priv_dev,
506 int (*cb)(void *cb_data, void *data, int len, int context),
507 void *cb_data,
508 int context)
509{
510 struct lte_udev *udev = priv_dev;
511 struct usb_device *usbdev = udev->usbdev;
512 struct rx_cxt *rx = &udev->rx;
513 struct usb_rx *r;
514 int no_spc;
515 int ret;
516 unsigned long flags;
517
518 if (!udev->usbdev) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700519 pr_err("invalid device\n");
Won Kang61e12102013-07-25 03:36:17 +0900520 return -ENODEV;
521 }
522
523 r = get_rx_struct(rx, &no_spc);
524 if (!r) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700525 pr_err("Out of Memory\n");
Won Kang61e12102013-07-25 03:36:17 +0900526 return -ENOMEM;
527 }
528
529 udev->rx_cb = cb;
530 r->callback = cb;
531 r->cb_data = cb_data;
532 r->index = (void *)udev;
533 r->rx = rx;
534
535 usb_fill_bulk_urb(r->urb,
536 usbdev,
537 usb_rcvbulkpipe(usbdev, 0x83),
538 r->buf,
539 RX_BUF_SIZE,
540 gdm_usb_rcv_complete,
541 r);
542
543 spin_lock_irqsave(&rx->submit_lock, flags);
544 list_add_tail(&r->rx_submit_list, &rx->rx_submit_list);
545 spin_unlock_irqrestore(&rx->submit_lock, flags);
546
547 if (context == KERNEL_THREAD)
548 ret = usb_submit_urb(r->urb, GFP_KERNEL);
549 else
550 ret = usb_submit_urb(r->urb, GFP_ATOMIC);
551
552 if (ret) {
553 spin_lock_irqsave(&rx->submit_lock, flags);
554 list_del(&r->rx_submit_list);
555 spin_unlock_irqrestore(&rx->submit_lock, flags);
556
Joe Perches0ec473b2013-07-24 14:13:03 -0700557 pr_err("usb_submit_urb failed (%p)\n", r);
Won Kang61e12102013-07-25 03:36:17 +0900558 put_rx_struct(rx, r);
559 }
560
561 return ret;
562}
563
564static void gdm_usb_send_complete(struct urb *urb)
565{
566 struct usb_tx *t = urb->context;
567 struct tx_cxt *tx = t->tx;
568 struct lte_udev *udev = container_of(tx, struct lte_udev, tx);
569 unsigned long flags;
570
571 if (urb->status == -ECONNRESET) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700572 pr_info("CONNRESET\n");
Won Kang61e12102013-07-25 03:36:17 +0900573 return;
574 }
575
576 if (t->callback)
577 t->callback(t->cb_data);
578
579 free_tx_struct(t);
580
581 spin_lock_irqsave(&tx->lock, flags);
582 udev->send_complete = 1;
583 queue_work(usb_tx_wq, &udev->work_tx.work);
584 spin_unlock_irqrestore(&tx->lock, flags);
585}
586
587static int send_tx_packet(struct usb_device *usbdev, struct usb_tx *t, u32 len)
588{
589 int ret = 0;
590
591 if (!(len%512))
592 len++;
593
594 usb_fill_bulk_urb(t->urb,
595 usbdev,
596 usb_sndbulkpipe(usbdev, 2),
597 t->buf,
598 len,
599 gdm_usb_send_complete,
600 t);
601
602 ret = usb_submit_urb(t->urb, GFP_ATOMIC);
603
604 if (ret)
Joe Perches0ec473b2013-07-24 14:13:03 -0700605 pr_err("usb_submit_urb failed: %d\n", ret);
Won Kang61e12102013-07-25 03:36:17 +0900606
607 usb_mark_last_busy(usbdev);
608
609 return ret;
610}
611
612static u32 packet_aggregation(struct lte_udev *udev, u8 *send_buf)
613{
614 struct tx_cxt *tx = &udev->tx;
615 struct usb_tx_sdu *t_sdu = NULL;
616 struct multi_sdu *multi_sdu = (struct multi_sdu *)send_buf;
617 u16 send_len = 0;
618 u16 num_packet = 0;
619 unsigned long flags;
620
621 multi_sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_MULTI_SDU);
622
623 while (num_packet < MAX_PACKET_IN_MULTI_SDU) {
624 spin_lock_irqsave(&tx->lock, flags);
625 if (list_empty(&tx->sdu_list)) {
626 spin_unlock_irqrestore(&tx->lock, flags);
627 break;
628 }
629
630 t_sdu = list_entry(tx->sdu_list.next, struct usb_tx_sdu, list);
631 if (send_len + t_sdu->len > MAX_SDU_SIZE) {
632 spin_unlock_irqrestore(&tx->lock, flags);
633 break;
634 }
635
636 list_del(&t_sdu->list);
637 spin_unlock_irqrestore(&tx->lock, flags);
638
639 memcpy(multi_sdu->data + send_len, t_sdu->buf, t_sdu->len);
640
641 send_len += (t_sdu->len + 3) & 0xfffc;
642 num_packet++;
643
644 if (tx->avail_count > 10)
645 t_sdu->callback(t_sdu->cb_data);
646
647 spin_lock_irqsave(&tx->lock, flags);
648 put_tx_struct(tx, t_sdu);
649 spin_unlock_irqrestore(&tx->lock, flags);
650 }
651
652 multi_sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len);
653 multi_sdu->num_packet = gdm_cpu_to_dev16(&udev->gdm_ed, num_packet);
654
655 return send_len + offsetof(struct multi_sdu, data);
656}
657
658static void do_tx(struct work_struct *work)
659{
660 struct lte_udev *udev = container_of(work, struct lte_udev, work_tx.work);
661 struct usb_device *usbdev = udev->usbdev;
662 struct tx_cxt *tx = &udev->tx;
663 struct usb_tx *t = NULL;
664 int is_send = 0;
665 u32 len = 0;
666 unsigned long flags;
667
668 if (!usb_autopm_get_interface(udev->intf))
669 usb_autopm_put_interface(udev->intf);
670
671 if (udev->usb_state == PM_SUSPEND)
672 return;
673
674 spin_lock_irqsave(&tx->lock, flags);
675 if (!udev->send_complete) {
676 spin_unlock_irqrestore(&tx->lock, flags);
677 return;
678 } else {
679 udev->send_complete = 0;
680 }
681
682 if (!list_empty(&tx->hci_list)) {
683 t = list_entry(tx->hci_list.next, struct usb_tx, list);
684 list_del(&t->list);
685 len = t->len;
686 t->is_sdu = 0;
687 is_send = 1;
688 } else if (!list_empty(&tx->sdu_list)) {
689 if (udev->tx_stop) {
690 udev->send_complete = 1;
691 spin_unlock_irqrestore(&tx->lock, flags);
692 return;
693 }
694
695 t = alloc_tx_struct(TX_BUF_SIZE);
696 t->callback = NULL;
697 t->tx = tx;
698 t->is_sdu = 1;
699 is_send = 1;
700 }
701
702 if (!is_send) {
703 udev->send_complete = 1;
704 spin_unlock_irqrestore(&tx->lock, flags);
705 return;
706 }
707 spin_unlock_irqrestore(&tx->lock, flags);
708
709 if (t->is_sdu)
710 len = packet_aggregation(udev, t->buf);
711
712 if (send_tx_packet(usbdev, t, len)) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700713 pr_err("send_tx_packet failed\n");
Won Kang61e12102013-07-25 03:36:17 +0900714 t->callback = NULL;
715 gdm_usb_send_complete(t->urb);
716 }
717}
718
719#define SDU_PARAM_LEN 12
720static int gdm_usb_sdu_send(void *priv_dev, void *data, int len,
721 unsigned int dftEpsId, unsigned int epsId,
722 void (*cb)(void *data), void *cb_data,
723 int dev_idx, int nic_type)
724{
725 struct lte_udev *udev = priv_dev;
726 struct tx_cxt *tx = &udev->tx;
727 struct usb_tx_sdu *t_sdu;
728 struct sdu *sdu = NULL;
729 unsigned long flags;
730 int no_spc = 0;
731 u16 send_len;
732
733 if (!udev->usbdev) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700734 pr_err("sdu send - invalid device\n");
Won Kang61e12102013-07-25 03:36:17 +0900735 return TX_NO_DEV;
736 }
737
738 spin_lock_irqsave(&tx->lock, flags);
739 t_sdu = get_tx_sdu_struct(tx, &no_spc);
740 spin_unlock_irqrestore(&tx->lock, flags);
741
742 if (t_sdu == NULL) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700743 pr_err("sdu send - free list empty\n");
Won Kang61e12102013-07-25 03:36:17 +0900744 return TX_NO_SPC;
745 }
746
747 sdu = (struct sdu *)t_sdu->buf;
748 sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_SDU);
749 if (nic_type == NIC_TYPE_ARP) {
750 send_len = len + SDU_PARAM_LEN;
751 memcpy(sdu->data, data, len);
752 } else {
753 send_len = len - ETH_HLEN;
754 send_len += SDU_PARAM_LEN;
755 memcpy(sdu->data, data+ETH_HLEN, len-ETH_HLEN);
756 }
757
758 sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len);
759 sdu->dftEpsId = gdm_cpu_to_dev32(&udev->gdm_ed, dftEpsId);
760 sdu->bearer_ID = gdm_cpu_to_dev32(&udev->gdm_ed, epsId);
761 sdu->nic_type = gdm_cpu_to_dev32(&udev->gdm_ed, nic_type);
762
763 t_sdu->len = send_len + HCI_HEADER_SIZE;
764 t_sdu->callback = cb;
765 t_sdu->cb_data = cb_data;
766
767 spin_lock_irqsave(&tx->lock, flags);
768 list_add_tail(&t_sdu->list, &tx->sdu_list);
769 queue_work(usb_tx_wq, &udev->work_tx.work);
770 spin_unlock_irqrestore(&tx->lock, flags);
771
772 if (no_spc)
773 return TX_NO_BUFFER;
774
775 return 0;
776}
777
778static int gdm_usb_hci_send(void *priv_dev, void *data, int len,
779 void (*cb)(void *data), void *cb_data)
780{
781 struct lte_udev *udev = priv_dev;
782 struct tx_cxt *tx = &udev->tx;
783 struct usb_tx *t;
784 unsigned long flags;
785
786 if (!udev->usbdev) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700787 pr_err("hci send - invalid device\n");
Won Kang61e12102013-07-25 03:36:17 +0900788 return -ENODEV;
789 }
790
791 t = alloc_tx_struct(len);
792 if (t == NULL) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700793 pr_err("hci_send - out of memory\n");
Won Kang61e12102013-07-25 03:36:17 +0900794 return -ENOMEM;
795 }
796
797 memcpy(t->buf, data, len);
798 t->callback = cb;
799 t->cb_data = cb_data;
800 t->len = len;
801 t->tx = tx;
802 t->is_sdu = 0;
803
804 spin_lock_irqsave(&tx->lock, flags);
805 list_add_tail(&t->list, &tx->hci_list);
806 queue_work(usb_tx_wq, &udev->work_tx.work);
807 spin_unlock_irqrestore(&tx->lock, flags);
808
809 return 0;
810}
811
812static struct gdm_endian *gdm_usb_get_endian(void *priv_dev)
813{
814 struct lte_udev *udev = priv_dev;
815
816 return &udev->gdm_ed;
817}
818
819static int gdm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
820{
821 int ret = 0;
822 struct phy_dev *phy_dev = NULL;
823 struct lte_udev *udev = NULL;
824 u16 idVendor, idProduct;
825 int bInterfaceNumber;
826 struct usb_device *usbdev = interface_to_usbdev(intf);
827
828 bInterfaceNumber = intf->cur_altsetting->desc.bInterfaceNumber;
829 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor);
830 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct);
831
Joe Perches0ec473b2013-07-24 14:13:03 -0700832 pr_info("net vid = 0x%04x pid = 0x%04x\n", idVendor, idProduct);
Won Kang61e12102013-07-25 03:36:17 +0900833
834 if (bInterfaceNumber > NETWORK_INTERFACE) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700835 pr_info("not a network device\n");
Won Kang61e12102013-07-25 03:36:17 +0900836 return -1;
837 }
838
839 phy_dev = kmalloc(sizeof(struct phy_dev), GFP_ATOMIC);
840 if (!phy_dev) {
841 ret = -ENOMEM;
842 goto out;
843 }
844
845 udev = kmalloc(sizeof(struct lte_udev), GFP_ATOMIC);
846 if (!udev) {
847 ret = -ENOMEM;
848 goto out;
849 }
850
851 memset(phy_dev, 0, sizeof(struct phy_dev));
852 memset(udev, 0, sizeof(struct lte_udev));
853
854 phy_dev->priv_dev = (void *)udev;
855 phy_dev->send_hci_func = gdm_usb_hci_send;
856 phy_dev->send_sdu_func = gdm_usb_sdu_send;
857 phy_dev->rcv_func = gdm_usb_recv;
858 phy_dev->get_endian = gdm_usb_get_endian;
859
860 udev->usbdev = usbdev;
861 ret = init_usb(udev);
862 if (ret < 0) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700863 pr_err("init_usb func failed\n");
Won Kang61e12102013-07-25 03:36:17 +0900864 goto out;
865 }
866 udev->intf = intf;
867
868 intf->needs_remote_wakeup = 1;
869 usb_enable_autosuspend(usbdev);
870 pm_runtime_set_autosuspend_delay(&usbdev->dev, AUTO_SUSPEND_TIMER);
871
872 /* List up hosts with big endians, otherwise, defaults to little endian */
873 if (idProduct == PID_GDM7243)
874 set_endian(&udev->gdm_ed, ENDIANNESS_BIG);
875 else
876 set_endian(&udev->gdm_ed, ENDIANNESS_LITTLE);
877
878 ret = request_mac_address(udev);
879 if (ret < 0) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700880 pr_err("request Mac address failed\n");
Won Kang61e12102013-07-25 03:36:17 +0900881 goto out;
882 }
883
884 start_rx_proc(phy_dev);
885out:
886
887 if (ret < 0) {
888 kfree(phy_dev);
889 if (udev) {
890 release_usb(udev);
891 kfree(udev);
892 }
893 }
894
895 usb_get_dev(usbdev);
896 usb_set_intfdata(intf, phy_dev);
897
898 return ret;
899}
900
901static void gdm_usb_disconnect(struct usb_interface *intf)
902{
903 struct phy_dev *phy_dev;
904 struct lte_udev *udev;
905 u16 idVendor, idProduct;
906 struct usb_device *usbdev;
907 usbdev = interface_to_usbdev(intf);
908
909 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor);
910 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct);
911
912 phy_dev = usb_get_intfdata(intf);
913
914 udev = phy_dev->priv_dev;
915 unregister_lte_device(phy_dev);
916
917 release_usb(udev);
918
919 kfree(udev);
920 udev = NULL;
921
922 kfree(phy_dev);
923 phy_dev = NULL;
924
925 usb_put_dev(usbdev);
926}
927
928static int gdm_usb_suspend(struct usb_interface *intf, pm_message_t pm_msg)
929{
930 struct phy_dev *phy_dev;
931 struct lte_udev *udev;
932 struct rx_cxt *rx;
933 struct usb_rx *r;
934 struct usb_rx *r_next;
935 unsigned long flags;
936
937 phy_dev = usb_get_intfdata(intf);
938 udev = phy_dev->priv_dev;
939 rx = &udev->rx;
940 if (udev->usb_state != PM_NORMAL) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700941 pr_err("usb suspend - invalid state\n");
Won Kang61e12102013-07-25 03:36:17 +0900942 return -1;
943 }
944
945 udev->usb_state = PM_SUSPEND;
946
947 spin_lock_irqsave(&rx->submit_lock, flags);
948 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list)
949 {
950 spin_unlock_irqrestore(&rx->submit_lock, flags);
951 usb_kill_urb(r->urb);
952 spin_lock_irqsave(&rx->submit_lock, flags);
953 }
954 spin_unlock_irqrestore(&rx->submit_lock, flags);
955
956 return 0;
957}
958
959static int gdm_usb_resume(struct usb_interface *intf)
960{
961 struct phy_dev *phy_dev;
962 struct lte_udev *udev;
963 struct tx_cxt *tx;
964 struct rx_cxt *rx;
965 unsigned long flags;
966 int issue_count;
967 int i;
968
969 phy_dev = usb_get_intfdata(intf);
970 udev = phy_dev->priv_dev;
971 rx = &udev->rx;
972
973 if (udev->usb_state != PM_SUSPEND) {
Joe Perches0ec473b2013-07-24 14:13:03 -0700974 pr_err("usb resume - invalid state\n");
Won Kang61e12102013-07-25 03:36:17 +0900975 return -1;
976 }
977 udev->usb_state = PM_NORMAL;
978
979 spin_lock_irqsave(&rx->rx_lock, flags);
980 issue_count = rx->avail_count - MAX_RX_SUBMIT_COUNT;
981 spin_unlock_irqrestore(&rx->rx_lock, flags);
982
983 if (issue_count >= 0) {
984 for (i = 0; i < issue_count; i++)
985 gdm_usb_recv(phy_dev->priv_dev,
986 udev->rx_cb,
987 phy_dev,
988 USB_COMPLETE);
989 }
990
991 tx = &udev->tx;
992 spin_lock_irqsave(&tx->lock, flags);
993 queue_work(usb_tx_wq, &udev->work_tx.work);
994 spin_unlock_irqrestore(&tx->lock, flags);
995
996 return 0;
997}
998
999static struct usb_driver gdm_usb_lte_driver = {
1000 .name = "gdm_lte",
1001 .probe = gdm_usb_probe,
1002 .disconnect = gdm_usb_disconnect,
1003 .id_table = id_table,
1004 .supports_autosuspend = 1,
1005 .suspend = gdm_usb_suspend,
1006 .resume = gdm_usb_resume,
1007 .reset_resume = gdm_usb_resume,
1008};
1009
1010static int __init gdm_usb_lte_init(void)
1011{
1012 if (gdm_lte_event_init() < 0) {
Joe Perches0ec473b2013-07-24 14:13:03 -07001013 pr_err("error creating event\n");
Won Kang61e12102013-07-25 03:36:17 +09001014 return -1;
1015 }
1016
1017 usb_tx_wq = create_workqueue("usb_tx_wq");
1018 if (usb_tx_wq == NULL)
1019 return -1;
1020
1021 usb_rx_wq = create_workqueue("usb_rx_wq");
1022 if (usb_rx_wq == NULL)
1023 return -1;
1024
1025 return usb_register(&gdm_usb_lte_driver);
1026}
1027
1028static void __exit gdm_usb_lte_exit(void)
1029{
1030 gdm_lte_event_exit();
1031
1032 usb_deregister(&gdm_usb_lte_driver);
1033
1034 if (usb_tx_wq) {
1035 flush_workqueue(usb_tx_wq);
1036 destroy_workqueue(usb_tx_wq);
1037 }
1038
1039 if (usb_rx_wq) {
1040 flush_workqueue(usb_rx_wq);
1041 destroy_workqueue(usb_rx_wq);
1042 }
1043}
1044
1045module_init(gdm_usb_lte_init);
1046module_exit(gdm_usb_lte_exit);
1047
1048MODULE_VERSION(DRIVER_VERSION);
1049MODULE_DESCRIPTION("GCT LTE USB Device Driver");
1050MODULE_LICENSE("GPL");