1 /* qcusbnet.c - gobi network device
2 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "qmidevice.h"
24 #include <linux/module.h>
25 #include <linux/ctype.h>
27 #define DRIVER_VERSION "1.0.110+google+w0"
28 #define DRIVER_AUTHOR "Qualcomm Innovation Center"
29 #define DRIVER_DESC "gobi"
31 static LIST_HEAD(qcusbnet_list);
32 static DEFINE_MUTEX(qcusbnet_lock);
35 static struct class *devclass;
37 static void free_dev(struct kref *ref)
39 struct qcusbnet *dev = container_of(ref, struct qcusbnet, refcount);
44 static void free_urb_with_skb(struct urb *urb)
46 struct sk_buff *skb = (struct sk_buff *)urb->context;
47 dev_kfree_skb_any(skb);
51 void qcusbnet_put(struct qcusbnet *dev)
53 mutex_lock(&qcusbnet_lock);
54 kref_put(&dev->refcount, free_dev);
55 mutex_unlock(&qcusbnet_lock);
58 struct qcusbnet *qcusbnet_get(struct qcusbnet *key)
60 /* Given a putative qcusbnet struct, return either the struct itself
61 * (with a ref taken) if the struct is still visible, or NULL if it's
62 * not. This prevents object-visibility races where someone is looking
63 * up an object as the last ref gets dropped; dropping the last ref and
64 * removing the object from the list are atomic with respect to getting
66 struct qcusbnet *entry;
67 mutex_lock(&qcusbnet_lock);
68 list_for_each_entry(entry, &qcusbnet_list, node) {
70 kref_get(&entry->refcount);
71 mutex_unlock(&qcusbnet_lock);
75 mutex_unlock(&qcusbnet_lock);
79 int qc_suspend(struct usb_interface *iface, pm_message_t event)
81 struct usbnet *usbnet;
86 usbnet = usb_get_intfdata(iface);
87 BUG_ON(!usbnet || !usbnet->net);
89 dev = (struct qcusbnet *)usbnet->data[0];
92 if (!(event.event & PM_EVENT_AUTO)) {
93 GOBI_DEBUG("device suspended to power level %d",
95 qc_setdown(dev, DOWN_DRIVER_SUSPENDED);
97 GOBI_DEBUG("device autosuspend");
100 if (event.event & PM_EVENT_SUSPEND) {
102 usbnet->udev->reset_resume = 0;
103 iface->dev.power.power_state.event = event.event;
105 usbnet->udev->reset_resume = 1;
108 return usbnet_suspend(iface, event);
111 static int qc_resume(struct usb_interface *iface)
113 struct usbnet *usbnet;
114 struct qcusbnet *dev;
120 usbnet = usb_get_intfdata(iface);
121 BUG_ON(!usbnet || !usbnet->net);
123 dev = (struct qcusbnet *)usbnet->data[0];
126 oldstate = iface->dev.power.power_state.event;
127 iface->dev.power.power_state.event = PM_EVENT_ON;
128 GOBI_DEBUG("resuming from power mode %d", oldstate);
130 if (oldstate & PM_EVENT_SUSPEND) {
131 qc_cleardown(dev, DOWN_DRIVER_SUSPENDED);
133 ret = usbnet_resume(iface);
135 GOBI_ERROR("usbnet_resume failed: %d", ret);
139 ret = qc_startread(dev);
141 GOBI_ERROR("qc_startread failed: %d", ret);
145 GOBI_DEBUG("nothing to resume");
152 static int qcnet_bind(struct usbnet *usbnet, struct usb_interface *iface)
156 struct usb_host_endpoint *endpoint = NULL;
157 struct usb_host_endpoint *in = NULL;
158 struct usb_host_endpoint *out = NULL;
159 int dir_in, dir_out, xfer_int;
161 if (iface->num_altsetting != 1) {
162 GOBI_WARN("invalid num_altsetting %u", iface->num_altsetting);
166 if (iface->cur_altsetting->desc.bInterfaceNumber != 0
167 && iface->cur_altsetting->desc.bInterfaceNumber != 5) {
168 GOBI_WARN("invalid interface %d",
169 iface->cur_altsetting->desc.bInterfaceNumber);
173 GOBI_DEBUG("interface number: %d",
174 iface->cur_altsetting->desc.bInterfaceNumber);
176 numends = iface->cur_altsetting->desc.bNumEndpoints;
177 for (i = 0; i < numends; i++) {
178 endpoint = iface->cur_altsetting->endpoint + i;
180 dir_in = usb_endpoint_dir_in(&endpoint->desc);
181 dir_out = usb_endpoint_dir_out(&endpoint->desc);
182 xfer_int = usb_endpoint_xfer_int(&endpoint->desc);
184 GOBI_DEBUG("endpoint %d: addr %u "
185 "dir_in %d dir_out %d xfer_int %d",
186 i, endpoint->desc.bEndpointAddress,
187 dir_in, dir_out, xfer_int);
189 if (dir_in && !xfer_int) {
191 GOBI_WARN("multiple in endpoints");
194 GOBI_DEBUG("setting endpoint %d as in", i);
196 } else if (dir_out && !xfer_int) {
198 GOBI_WARN("multiple out endpoints");
201 GOBI_DEBUG("setting endpoint %d as out", i);
204 GOBI_DEBUG("ignoring endpoint %d", i);
209 GOBI_WARN("missing endpoint(s)");
211 GOBI_WARN("found in endpoint: %u",
212 in->desc.bEndpointAddress);
214 GOBI_WARN("didn't find in endpoint");
216 GOBI_WARN("found out endpoint: %u",
217 out->desc.bEndpointAddress);
219 GOBI_WARN("didn't find out endpoint");
223 if (usb_set_interface(usbnet->udev,
224 iface->cur_altsetting->desc.bInterfaceNumber, 0)) {
225 GOBI_WARN("unable to set interface");
229 GOBI_DEBUG("found endpoints: in %u, out %u",
230 in->desc.bEndpointAddress,
231 out->desc.bEndpointAddress);
233 usbnet->in = usb_rcvbulkpipe(usbnet->udev,
234 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
235 usbnet->out = usb_sndbulkpipe(usbnet->udev,
236 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
241 static void qcnet_unbind(struct usbnet *usbnet, struct usb_interface *iface)
243 struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
247 kfree(usbnet->net->netdev_ops);
248 usbnet->net->netdev_ops = NULL;
251 static void qcnet_bg_complete(struct work_struct *work)
253 unsigned long listflags;
254 struct qcusbnet *dev = container_of(work, struct qcusbnet, complete);
256 BUG_ON(!dev->active);
257 free_urb_with_skb(dev->active);
260 usb_autopm_put_interface(dev->iface);
262 spin_lock_irqsave(&dev->urbs_lock, listflags);
263 if (!list_empty(&dev->urbs))
264 queue_work(dev->workqueue, &dev->startxmit);
265 spin_unlock_irqrestore(&dev->urbs_lock, listflags);
268 static void qcnet_complete(struct urb *urb)
270 struct sk_buff *skb = urb->context;
271 struct qcusbnet *dev = *(struct qcusbnet **)skb->cb;
273 BUG_ON(urb != dev->active);
274 queue_work(dev->workqueue, &dev->complete);
277 static void qcnet_bg_txtimeout(struct work_struct *work)
279 struct qcusbnet *dev = container_of(work, struct qcusbnet, txtimeout);
280 struct list_head *node, *tmp;
283 usb_kill_urb(dev->active);
284 list_for_each_safe(node, tmp, &dev->urbs) {
285 urb = list_entry(node, struct urb, urb_list);
286 list_del(&urb->urb_list);
287 free_urb_with_skb(urb);
291 static void qcnet_txtimeout(struct net_device *netdev)
293 struct usbnet *usbnet = netdev_priv(netdev);
294 struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
295 queue_work(dev->workqueue, &dev->txtimeout);
298 static void qcnet_bg_startxmit(struct work_struct *work)
300 unsigned long listflags;
301 struct qcusbnet *dev = container_of(work, struct qcusbnet, startxmit);
302 struct urb *urb = NULL;
306 GOBI_WARN("dying device");
307 /* Fear not; queued urbs will be freed in qcnet_disconnect. */
314 status = usb_autopm_get_interface(dev->iface);
316 GOBI_ERROR("failed to autoresume interface: %d", status);
317 if (status == -EPERM)
318 qc_suspend(dev->iface, PMSG_SUSPEND);
319 /* We could just drop the packet here, right...? It seems like
320 * if this ever happens, we'll spin, but the old driver did that
322 queue_work(dev->workqueue, &dev->startxmit);
326 spin_lock_irqsave(&dev->urbs_lock, listflags);
327 if (!list_empty(&dev->urbs)) {
328 urb = list_first_entry(&dev->urbs, struct urb, urb_list);
329 list_del(&urb->urb_list);
331 spin_unlock_irqrestore(&dev->urbs_lock, listflags);
333 /* If we hit this case, it means that we added our urb to the
334 * list while there was an urb in flight, and that urb
335 * completed, causing our urb to be submitted; in addition, our
336 * urb completed too, all before we got to schedule this work.
337 * Unlikely, but possible. */
338 usb_autopm_put_interface(dev->iface);
343 status = usb_submit_urb(urb, GFP_KERNEL);
345 GOBI_ERROR("failed to submit urb: %d (packet dropped)", status);
346 free_urb_with_skb(urb);
348 usb_autopm_put_interface(dev->iface);
352 static int qcnet_startxmit(struct sk_buff *skb, struct net_device *netdev)
354 unsigned long listflags;
356 struct usbnet *usbnet = netdev_priv(netdev);
357 struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
359 if (qc_isdown(dev, DOWN_DRIVER_SUSPENDED)) {
360 GOBI_ERROR("device is suspended (packet requeued)");
361 return NETDEV_TX_BUSY;
364 urb = usb_alloc_urb(0, GFP_ATOMIC);
366 GOBI_ERROR("failed to allocate urb (packet requeued)");
367 return NETDEV_TX_BUSY;
370 usb_fill_bulk_urb(urb, dev->usbnet->udev, dev->usbnet->out,
371 skb->data, skb->len, qcnet_complete, skb);
372 *(struct qcusbnet **)skb->cb = dev;
374 spin_lock_irqsave(&dev->urbs_lock, listflags);
375 list_add_tail(&urb->urb_list, &dev->urbs);
376 spin_unlock_irqrestore(&dev->urbs_lock, listflags);
378 queue_work(dev->workqueue, &dev->startxmit);
380 netdev->trans_start = jiffies;
385 static int qcnet_open(struct net_device *netdev)
388 struct qcusbnet *dev;
389 struct usbnet *usbnet = netdev_priv(netdev);
393 dev = (struct qcusbnet *)usbnet->data[0];
396 qc_cleardown(dev, DOWN_NET_IFACE_STOPPED);
398 status = dev->open(netdev);
400 usb_autopm_put_interface(dev->iface);
403 GOBI_WARN("no USBNetOpen defined");
409 int qcnet_stop(struct net_device *netdev)
411 struct qcusbnet *dev;
412 struct usbnet *usbnet = netdev_priv(netdev);
416 dev = (struct qcusbnet *)usbnet->data[0];
419 qc_setdown(dev, DOWN_NET_IFACE_STOPPED);
421 if (dev->stop != NULL)
422 return dev->stop(netdev);
426 static const struct driver_info qc_netinfo = {
427 .description = "QCUSBNet Ethernet Device",
428 .flags = FLAG_ETHER | FLAG_WWAN,
430 .unbind = qcnet_unbind,
434 #define MKVIDPID(v, p) \
437 .driver_info = (unsigned long)&qc_netinfo, \
440 static const struct usb_device_id qc_vidpids[] = {
441 MKVIDPID(0x05c6, 0x9215), /* Acer Gobi 2000 */
442 MKVIDPID(0x05c6, 0x9265), /* Asus Gobi 2000 */
443 MKVIDPID(0x16d8, 0x8002), /* CMOTech Gobi 2000 */
444 MKVIDPID(0x413c, 0x8186), /* Dell Gobi 2000 */
445 MKVIDPID(0x1410, 0xa010), /* Entourage Gobi 2000 */
446 MKVIDPID(0x1410, 0xa011), /* Entourage Gobi 2000 */
447 MKVIDPID(0x1410, 0xa012), /* Entourage Gobi 2000 */
448 MKVIDPID(0x1410, 0xa013), /* Entourage Gobi 2000 */
449 MKVIDPID(0x03f0, 0x251d), /* HP Gobi 2000 */
450 MKVIDPID(0x05c6, 0x9205), /* Lenovo Gobi 2000 */
451 MKVIDPID(0x05c6, 0x920b), /* Generic Gobi 2000 */
452 MKVIDPID(0x04da, 0x250f), /* Panasonic Gobi 2000 */
453 MKVIDPID(0x05c6, 0x9245), /* Samsung Gobi 2000 */
454 MKVIDPID(0x1199, 0x9001), /* Sierra Wireless Gobi 2000 */
455 MKVIDPID(0x1199, 0x9002), /* Sierra Wireless Gobi 2000 */
456 MKVIDPID(0x1199, 0x9003), /* Sierra Wireless Gobi 2000 */
457 MKVIDPID(0x1199, 0x9004), /* Sierra Wireless Gobi 2000 */
458 MKVIDPID(0x1199, 0x9005), /* Sierra Wireless Gobi 2000 */
459 MKVIDPID(0x1199, 0x9006), /* Sierra Wireless Gobi 2000 */
460 MKVIDPID(0x1199, 0x9007), /* Sierra Wireless Gobi 2000 */
461 MKVIDPID(0x1199, 0x9008), /* Sierra Wireless Gobi 2000 */
462 MKVIDPID(0x1199, 0x9009), /* Sierra Wireless Gobi 2000 */
463 MKVIDPID(0x1199, 0x900a), /* Sierra Wireless Gobi 2000 */
464 MKVIDPID(0x05c6, 0x9225), /* Sony Gobi 2000 */
465 MKVIDPID(0x05c6, 0x9235), /* Top Global Gobi 2000 */
466 MKVIDPID(0x05c6, 0x9275), /* iRex Technologies Gobi 2000 */
468 MKVIDPID(0x05c6, 0x920d), /* Qualcomm Gobi 3000 */
469 MKVIDPID(0x1410, 0xa021), /* Novatel Gobi 3000 */
470 MKVIDPID(0x413c, 0x8194), /* Dell Gobi 3000 */
471 MKVIDPID(0x12D1, 0x14F1), /* Sony Gobi 3000 */
475 MODULE_DEVICE_TABLE(usb, qc_vidpids);
477 static u8 nibble(unsigned char c)
479 if (likely(isdigit(c)))
482 if (likely(isxdigit(c)))
487 static int discover_endpoints(struct qcusbnet *dev)
491 struct usb_host_endpoint *endpoint = NULL;
492 struct usb_host_endpoint *int_in = NULL;
493 struct usb_host_endpoint *bulk_in = NULL;
494 struct usb_host_endpoint *bulk_out = NULL;
495 int dir_in, dir_out, xfer_int;
497 GOBI_DEBUG("interface number: %d",
498 dev->iface->cur_altsetting->desc.bInterfaceNumber);
500 numends = dev->iface->cur_altsetting->desc.bNumEndpoints;
501 for (i = 0; i < numends; i++) {
502 endpoint = dev->iface->cur_altsetting->endpoint + i;
504 dir_in = usb_endpoint_dir_in(&endpoint->desc);
505 dir_out = usb_endpoint_dir_out(&endpoint->desc);
506 xfer_int = usb_endpoint_xfer_int(&endpoint->desc);
508 GOBI_DEBUG("endpoint %d: addr %x "
509 "dir_in %d dir_out %d xfer_int %d",
510 i, endpoint->desc.bEndpointAddress,
511 dir_in, dir_out, xfer_int);
513 if (dir_in && xfer_int) {
515 GOBI_ERROR("multiple int_in endpoints");
518 GOBI_DEBUG("setting endpoint %d as int in", i);
520 } else if (dir_in && !xfer_int) {
522 GOBI_ERROR("multiple bulk_in endpoints");
525 GOBI_DEBUG("setting endpoint %d as bulk in", i);
527 } else if (dir_out && !xfer_int) {
529 GOBI_ERROR("multiple bulk_out endpoints");
532 GOBI_DEBUG("setting endpoint %d as bulk out", i);
535 GOBI_DEBUG("ignoring endpoint %d", i);
539 if (!int_in || !bulk_in || !bulk_out) {
540 GOBI_ERROR("missing endpoint(s)");
542 GOBI_ERROR("found int_in endpoint: %u",
543 int_in->desc.bEndpointAddress);
545 GOBI_ERROR("didn't find int_in endpoint");
547 GOBI_ERROR("found bulk_in endpoint: %u",
548 bulk_in->desc.bEndpointAddress);
550 GOBI_ERROR("didn't find bulk_in endpoint");
552 GOBI_ERROR("found bulk_out endpoint: %u",
553 bulk_out->desc.bEndpointAddress);
555 GOBI_ERROR("didn't find bulk_out endpoint");
559 dev->iface_num = dev->iface->cur_altsetting->desc.bInterfaceNumber;
560 dev->int_in_endp = int_in->desc.bEndpointAddress;
561 dev->bulk_in_endp = bulk_in->desc.bEndpointAddress;
562 dev->bulk_out_endp = bulk_out->desc.bEndpointAddress;
564 GOBI_DEBUG("found endpoints: iface_num %u "
565 "int_in %u bulk_in %u bulk_out %u",
567 dev->int_in_endp, dev->bulk_in_endp, dev->bulk_out_endp);
572 int qcnet_probe(struct usb_interface *iface, const struct usb_device_id *vidpids)
575 struct usbnet *usbnet;
576 struct qcusbnet *dev;
577 struct net_device_ops *netdevops;
581 status = usbnet_probe(iface, vidpids);
583 GOBI_WARN("usbnet_probe failed: %d", status);
587 usbnet = usb_get_intfdata(iface);
590 GOBI_ERROR("usbnet is NULL");
592 goto fail_disconnect;
596 GOBI_ERROR("usbnet->net is NULL");
598 goto fail_disconnect;
601 dev = kmalloc(sizeof(struct qcusbnet), GFP_KERNEL);
603 GOBI_ERROR("failed to allocate struct qcusbnet");
605 goto fail_disconnect;
611 status = discover_endpoints(dev);
613 GOBI_ERROR("discover_endpoints failed: %d", status);
617 usbnet->data[0] = (unsigned long)dev;
619 dev->usbnet = usbnet;
621 netdevops = kmalloc(sizeof(struct net_device_ops), GFP_KERNEL);
623 GOBI_ERROR("failed to allocate net device ops");
627 memcpy(netdevops, usbnet->net->netdev_ops, sizeof(struct net_device_ops));
629 /* TODO(ttuttle): Can we just make a static copy of this? */
630 dev->open = netdevops->ndo_open;
631 netdevops->ndo_open = qcnet_open;
632 dev->stop = netdevops->ndo_stop;
633 netdevops->ndo_stop = qcnet_stop;
634 netdevops->ndo_start_xmit = qcnet_startxmit;
635 netdevops->ndo_tx_timeout = qcnet_txtimeout;
637 usbnet->net->netdev_ops = netdevops;
639 memset(&(dev->usbnet->net->stats), 0, sizeof(struct net_device_stats));
641 memset(&(dev->meid), '0', 14);
644 memset(&dev->qmi, 0, sizeof(dev->qmi));
646 dev->qmi.devclass = devclass;
648 kref_init(&dev->refcount);
649 INIT_LIST_HEAD(&dev->node);
650 INIT_LIST_HEAD(&dev->qmi.clients);
651 dev->workqueue = alloc_ordered_workqueue("gobi", 0);
653 spin_lock_init(&dev->urbs_lock);
654 INIT_LIST_HEAD(&dev->urbs);
656 INIT_WORK(&dev->startxmit, qcnet_bg_startxmit);
657 INIT_WORK(&dev->txtimeout, qcnet_bg_txtimeout);
658 INIT_WORK(&dev->complete, qcnet_bg_complete);
660 spin_lock_init(&dev->qmi.clients_lock);
663 qc_setdown(dev, DOWN_NO_NDIS_CONNECTION);
664 qc_setdown(dev, DOWN_NET_IFACE_STOPPED);
666 status = qc_register(dev);
668 GOBI_ERROR("qc_register failed: %d", status);
672 iface->needs_remote_wakeup = 1;
674 mutex_lock(&qcusbnet_lock);
675 /* Give our initial ref to the list */
676 list_add(&dev->node, &qcusbnet_list);
677 mutex_unlock(&qcusbnet_lock);
679 /* After calling qc_register, MEID is valid */
680 addr = &usbnet->net->dev_addr[0];
681 for (i = 0; i < 6; i++)
682 addr[i] = (nibble(dev->meid[i*2+2]) << 4)+
683 nibble(dev->meid[i*2+3]);
684 addr[0] &= 0xfe; /* clear multicast bit */
685 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
690 destroy_workqueue(dev->workqueue);
694 usbnet_disconnect(iface);
698 EXPORT_SYMBOL_GPL(qcnet_probe);
700 static void qcnet_disconnect(struct usb_interface *intf)
702 struct usbnet *usbnet = usb_get_intfdata(intf);
703 struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
704 struct list_head *node, *tmp;
707 intf->needs_remote_wakeup = 0;
708 netif_carrier_off(usbnet->net);
709 usbnet_disconnect(intf);
713 destroy_workqueue(dev->workqueue);
714 list_for_each_safe(node, tmp, &dev->urbs) {
715 urb = list_entry(node, struct urb, urb_list);
716 list_del(&urb->urb_list);
717 free_urb_with_skb(urb);
722 static struct usb_driver qcusbnet = {
724 .id_table = qc_vidpids,
725 .probe = qcnet_probe,
726 .disconnect = qcnet_disconnect,
727 .suspend = qc_suspend,
729 .supports_autosuspend = true,
732 static int __init modinit(void)
734 devclass = class_create(THIS_MODULE, "QCQMI");
735 if (IS_ERR(devclass)) {
736 GOBI_ERROR("class_create failed: %ld", PTR_ERR(devclass));
739 printk(KERN_INFO "%s: %s", DRIVER_DESC, DRIVER_VERSION);
740 return usb_register(&qcusbnet);
742 module_init(modinit);
744 static void __exit modexit(void)
746 usb_deregister(&qcusbnet);
747 class_destroy(devclass);
749 module_exit(modexit);
751 MODULE_VERSION(DRIVER_VERSION);
752 MODULE_AUTHOR(DRIVER_AUTHOR);
753 MODULE_DESCRIPTION(DRIVER_DESC);
754 MODULE_LICENSE("Dual BSD/GPL");
756 module_param(gobi_debug, int, S_IRUGO | S_IWUSR);
757 MODULE_PARM_DESC(gobi_debug, "Debugging level");