Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/staging/gdm724x/gdm_usb.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us