CHROMIUM: gobi: protect access to dev->urbs, initialize active
[cascardo/linux.git] / drivers / net / usb / gobi / qcusbnet.c
1 /* qcusbnet.c - gobi network device
2  * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
3
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.
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  * 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
16  * 02110-1301, USA.
17  */
18
19 #include "structs.h"
20 #include "qmidevice.h"
21 #include "qmi.h"
22 #include "qcusbnet.h"
23
24 #include <linux/module.h>
25 #include <linux/ctype.h>
26
27 #define DRIVER_VERSION "1.0.110+google+w0"
28 #define DRIVER_AUTHOR "Qualcomm Innovation Center"
29 #define DRIVER_DESC "gobi"
30
31 static LIST_HEAD(qcusbnet_list);
32 static DEFINE_MUTEX(qcusbnet_lock);
33
34 int qcusbnet_debug;
35 static struct class *devclass;
36
37 static void free_dev(struct kref *ref)
38 {
39         struct qcusbnet *dev = container_of(ref, struct qcusbnet, refcount);
40         list_del(&dev->node);
41         kfree(dev);
42 }
43
44 void qcusbnet_put(struct qcusbnet *dev)
45 {
46         mutex_lock(&qcusbnet_lock);
47         kref_put(&dev->refcount, free_dev);
48         mutex_unlock(&qcusbnet_lock);
49 }
50
51 struct qcusbnet *qcusbnet_get(struct qcusbnet *key)
52 {
53         /* Given a putative qcusbnet struct, return either the struct itself
54          * (with a ref taken) if the struct is still visible, or NULL if it's
55          * not. This prevents object-visibility races where someone is looking
56          * up an object as the last ref gets dropped; dropping the last ref and
57          * removing the object from the list are atomic with respect to getting
58          * a new ref. */
59         struct qcusbnet *entry;
60         mutex_lock(&qcusbnet_lock);
61         list_for_each_entry(entry, &qcusbnet_list, node) {
62                 if (entry == key) {
63                         kref_get(&entry->refcount);
64                         mutex_unlock(&qcusbnet_lock);
65                         return entry;
66                 }
67         }
68         mutex_unlock(&qcusbnet_lock);
69         return NULL;
70 }
71
72 int qc_suspend(struct usb_interface *iface, pm_message_t event)
73 {
74         struct usbnet *usbnet;
75         struct qcusbnet *dev;
76
77         if (!iface)
78                 return -ENOMEM;
79
80         usbnet = usb_get_intfdata(iface);
81
82         if (!usbnet || !usbnet->net) {
83                 DBG("failed to get netdevice\n");
84                 return -ENXIO;
85         }
86
87         dev = (struct qcusbnet *)usbnet->data[0];
88         if (!dev) {
89                 DBG("failed to get QMIDevice\n");
90                 return -ENXIO;
91         }
92
93         if (!(event.event & PM_EVENT_AUTO)) {
94                 DBG("device suspended to power level %d\n",
95                     event.event);
96                 qc_setdown(dev, DOWN_DRIVER_SUSPENDED);
97         } else {
98                 DBG("device autosuspend\n");
99         }
100
101         if (event.event & PM_EVENT_SUSPEND) {
102                 qc_stopread(dev);
103                 usbnet->udev->reset_resume = 0;
104                 iface->dev.power.power_state.event = event.event;
105         } else {
106                 usbnet->udev->reset_resume = 1;
107         }
108
109         return usbnet_suspend(iface, event);
110 }
111
112 static int qc_resume(struct usb_interface *iface)
113 {
114         struct usbnet *usbnet;
115         struct qcusbnet *dev;
116         int ret;
117         int oldstate;
118
119         if (iface == 0)
120                 return -ENOMEM;
121
122         usbnet = usb_get_intfdata(iface);
123
124         if (!usbnet || !usbnet->net) {
125                 DBG("failed to get netdevice\n");
126                 return -ENXIO;
127         }
128
129         dev = (struct qcusbnet *)usbnet->data[0];
130         if (!dev) {
131                 DBG("failed to get QMIDevice\n");
132                 return -ENXIO;
133         }
134
135         oldstate = iface->dev.power.power_state.event;
136         iface->dev.power.power_state.event = PM_EVENT_ON;
137         DBG("resuming from power mode %d\n", oldstate);
138
139         if (oldstate & PM_EVENT_SUSPEND) {
140                 qc_cleardown(dev, DOWN_DRIVER_SUSPENDED);
141
142                 ret = usbnet_resume(iface);
143                 if (ret) {
144                         DBG("usbnet_resume error %d\n", ret);
145                         return ret;
146                 }
147
148                 ret = qc_startread(dev);
149                 if (ret) {
150                         DBG("qc_startread error %d\n", ret);
151                         return ret;
152                 }
153         } else {
154                 DBG("nothing to resume\n");
155                 return 0;
156         }
157
158         return ret;
159 }
160
161 static int qcnet_bind(struct usbnet *usbnet, struct usb_interface *iface)
162 {
163         int numends;
164         int i;
165         struct usb_host_endpoint *endpoint = NULL;
166         struct usb_host_endpoint *in = NULL;
167         struct usb_host_endpoint *out = NULL;
168
169         if (iface->num_altsetting != 1) {
170                 DBG("invalid num_altsetting %u\n", iface->num_altsetting);
171                 return -EINVAL;
172         }
173
174         if (iface->cur_altsetting->desc.bInterfaceNumber != 0
175             && iface->cur_altsetting->desc.bInterfaceNumber != 5) {
176                 DBG("invalid interface %d\n",
177                           iface->cur_altsetting->desc.bInterfaceNumber);
178                 return -EINVAL;
179         }
180
181         numends = iface->cur_altsetting->desc.bNumEndpoints;
182         for (i = 0; i < numends; i++) {
183                 endpoint = iface->cur_altsetting->endpoint + i;
184                 if (!endpoint) {
185                         DBG("invalid endpoint %u\n", i);
186                         return -EINVAL;
187                 }
188
189                 if (usb_endpoint_dir_in(&endpoint->desc)
190                 &&  !usb_endpoint_xfer_int(&endpoint->desc)) {
191                         in = endpoint;
192                 } else if (!usb_endpoint_dir_out(&endpoint->desc)) {
193                         out = endpoint;
194                 }
195         }
196
197         if (!in || !out) {
198                 DBG("invalid endpoints\n");
199                 return -EINVAL;
200         }
201
202         if (usb_set_interface(usbnet->udev,
203                               iface->cur_altsetting->desc.bInterfaceNumber, 0)) {
204                 DBG("unable to set interface\n");
205                 return -EINVAL;
206         }
207
208         usbnet->in = usb_rcvbulkpipe(usbnet->udev, in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
209         usbnet->out = usb_sndbulkpipe(usbnet->udev, out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
210
211         DBG("in %x, out %x\n",
212             in->desc.bEndpointAddress,
213             out->desc.bEndpointAddress);
214
215         return 0;
216 }
217
218 static void qcnet_unbind(struct usbnet *usbnet, struct usb_interface *iface)
219 {
220         struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
221
222         netif_carrier_off(usbnet->net);
223         qc_deregister(dev);
224
225         kfree(usbnet->net->netdev_ops);
226         usbnet->net->netdev_ops = NULL;
227         /* drop the list's ref */
228         qcusbnet_put(dev);
229 }
230
231 static void qcnet_bg_complete(struct work_struct *work)
232 {
233         unsigned long listflags;
234         struct qcusbnet *dev = container_of(work, struct qcusbnet, complete);
235         BUG_ON(!dev->active);
236         usb_free_urb(dev->active);
237         dev->active = NULL;
238         usb_autopm_put_interface(dev->iface);
239         spin_lock_irqsave(&dev->urbs_lock, listflags);
240         if (!list_empty(&dev->urbs))
241                 queue_work(dev->workqueue, &dev->startxmit);
242         spin_unlock_irqrestore(&dev->urbs_lock, listflags);
243 }
244
245 static void qcnet_complete(struct urb *urb)
246 {
247         struct qcusbnet *dev = urb->context;
248         queue_work(dev->workqueue, &dev->complete);
249 }
250
251 static void qcnet_bg_txtimeout(struct work_struct *work)
252 {
253         struct qcusbnet *dev = container_of(work, struct qcusbnet, txtimeout);
254         struct list_head *node, *tmp;
255         struct urbreq *req;
256         if (dev->active)
257                 usb_kill_urb(dev->active);
258         list_for_each_safe(node, tmp, &dev->urbs) {
259                 req = list_entry(node, struct urbreq, node);
260                 usb_free_urb(req->urb);
261                 list_del(&req->node);
262                 kfree(req);
263         }
264 }
265
266 static void qcnet_txtimeout(struct net_device *netdev)
267 {
268         struct usbnet *usbnet = netdev_priv(netdev);
269         struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
270         queue_work(dev->workqueue, &dev->txtimeout);
271 }
272
273 static void qcnet_bg_startxmit(struct work_struct *work)
274 {
275         unsigned long listflags;
276         struct qcusbnet *dev = container_of(work, struct qcusbnet, startxmit);
277         struct urbreq *req = NULL;
278         int status;
279
280         if (dev->active)
281                 return;
282
283         status = usb_autopm_get_interface(dev->iface);
284         if (status < 0) {
285                 printk(KERN_WARNING "gobi: can't autoresume interface: %d", status);
286                 if (status == -EPERM)
287                         qc_suspend(dev->iface, PMSG_SUSPEND);
288                 /* We could just drop the packet here, right...? It seems like
289                  * if this ever happens, we'll spin, but the old driver did that
290                  * as well. */
291                 queue_work(dev->workqueue, &dev->startxmit);
292                 return;
293         }
294
295         spin_lock_irqsave(&dev->urbs_lock, listflags);
296         if (!list_empty(&dev->urbs)) {
297                 req = list_first_entry(&dev->urbs, struct urbreq, node);
298                 list_del(&req->node);
299         }
300         spin_unlock_irqrestore(&dev->urbs_lock, listflags);
301         if (req == NULL) {
302                 /* If we hit this case, it means that we added our urb to the
303                  * list while there was an urb in flight, and that urb
304                  * completed, causing our urb to be submitted; in addition, our
305                  * urb completed too, all before we got to schedule this work.
306                  * Unlikely, but possible. */
307                 usb_autopm_put_interface(dev->iface);
308                 return;
309         }
310
311         dev->active = req->urb;
312         status = usb_submit_urb(dev->active, GFP_KERNEL);
313         if (status < 0) {
314                 printk(KERN_WARNING "gobi: couldn't submit, packet dropped: %d", status);
315                 usb_free_urb(dev->active);
316                 dev->active = NULL;
317                 usb_autopm_put_interface(dev->iface);
318         }
319
320         kfree(req);
321 }
322
323 static int qcnet_startxmit(struct sk_buff *skb, struct net_device *netdev)
324 {
325         unsigned long listflags;
326         struct urbreq *req;
327         void *data;
328         struct usbnet *usbnet = netdev_priv(netdev);
329         struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
330
331         if (qc_isdown(dev, DOWN_DRIVER_SUSPENDED)) {
332                 DBG("device is suspended\n");
333                 dump_stack();
334                 return NETDEV_TX_BUSY;
335         }
336
337         req = kmalloc(sizeof(*req), GFP_ATOMIC);
338         if (!req) {
339                 DBG("unable to allocate URBList memory\n");
340                 return NETDEV_TX_BUSY;
341         }
342
343         req->urb = usb_alloc_urb(0, GFP_ATOMIC);
344
345         if (!req->urb) {
346                 kfree(req);
347                 DBG("unable to allocate URB\n");
348                 return NETDEV_TX_BUSY;
349         }
350
351         data = kmalloc(skb->len, GFP_ATOMIC);
352         if (!data) {
353                 usb_free_urb(req->urb);
354                 kfree(req);
355                 DBG("unable to allocate URB data\n");
356                 return NETDEV_TX_BUSY;
357         }
358         memcpy(data, skb->data, skb->len);
359
360         usb_fill_bulk_urb(req->urb, dev->usbnet->udev, dev->usbnet->out,
361                           data, skb->len, qcnet_complete, dev);
362
363         spin_lock_irqsave(&dev->urbs_lock, listflags);
364         list_add_tail(&req->node, &dev->urbs);
365         spin_unlock_irqrestore(&dev->urbs_lock, listflags);
366
367         queue_work(dev->workqueue, &dev->startxmit);
368
369         netdev->trans_start = jiffies;
370         dev_kfree_skb_any(skb);
371
372         return NETDEV_TX_OK;
373 }
374
375 static int qcnet_open(struct net_device *netdev)
376 {
377         int status = 0;
378         struct qcusbnet *dev;
379         struct usbnet *usbnet = netdev_priv(netdev);
380
381         if (!usbnet) {
382                 DBG("failed to get usbnet device\n");
383                 return -ENXIO;
384         }
385
386         dev = (struct qcusbnet *)usbnet->data[0];
387         if (!dev) {
388                 DBG("failed to get QMIDevice\n");
389                 return -ENXIO;
390         }
391
392         qc_cleardown(dev, DOWN_NET_IFACE_STOPPED);
393         if (dev->open) {
394                 status = dev->open(netdev);
395                 if (status == 0) {
396                         usb_autopm_put_interface(dev->iface);
397                 }
398         } else {
399                 DBG("no USBNetOpen defined\n");
400         }
401
402         return status;
403 }
404
405 int qcnet_stop(struct net_device *netdev)
406 {
407         struct qcusbnet *dev;
408         struct usbnet *usbnet = netdev_priv(netdev);
409
410         if (!usbnet || !usbnet->net) {
411                 DBG("failed to get netdevice\n");
412                 return -ENXIO;
413         }
414
415         dev = (struct qcusbnet *)usbnet->data[0];
416         if (!dev) {
417                 DBG("failed to get QMIDevice\n");
418                 return -ENXIO;
419         }
420
421         qc_setdown(dev, DOWN_NET_IFACE_STOPPED);
422
423         if (dev->stop != NULL)
424                 return dev->stop(netdev);
425         return 0;
426 }
427
428 static const struct driver_info qc_netinfo = {
429         .description   = "QCUSBNet Ethernet Device",
430         .flags         = FLAG_ETHER,
431         .bind          = qcnet_bind,
432         .unbind        = qcnet_unbind,
433         .data          = 0,
434 };
435
436 #define MKVIDPID(v, p)                                  \
437 {                                                       \
438         USB_DEVICE(v, p),                               \
439         .driver_info = (unsigned long)&qc_netinfo,      \
440 }
441
442 static const struct usb_device_id qc_vidpids[] = {
443         MKVIDPID(0x05c6, 0x9215),       /* Acer Gobi 2000 */
444         MKVIDPID(0x05c6, 0x9265),       /* Asus Gobi 2000 */
445         MKVIDPID(0x16d8, 0x8002),       /* CMOTech Gobi 2000 */
446         MKVIDPID(0x413c, 0x8186),       /* Dell Gobi 2000 */
447         MKVIDPID(0x1410, 0xa010),       /* Entourage Gobi 2000 */
448         MKVIDPID(0x1410, 0xa011),       /* Entourage Gobi 2000 */
449         MKVIDPID(0x1410, 0xa012),       /* Entourage Gobi 2000 */
450         MKVIDPID(0x1410, 0xa013),       /* Entourage Gobi 2000 */
451         MKVIDPID(0x03f0, 0x251d),       /* HP Gobi 2000 */
452         MKVIDPID(0x05c6, 0x9205),       /* Lenovo Gobi 2000 */
453         MKVIDPID(0x05c6, 0x920b),       /* Generic Gobi 2000 */
454         MKVIDPID(0x04da, 0x250f),       /* Panasonic Gobi 2000 */
455         MKVIDPID(0x05c6, 0x9245),       /* Samsung Gobi 2000 */
456         MKVIDPID(0x1199, 0x9001),       /* Sierra Wireless Gobi 2000 */
457         MKVIDPID(0x1199, 0x9002),       /* Sierra Wireless Gobi 2000 */
458         MKVIDPID(0x1199, 0x9003),       /* Sierra Wireless Gobi 2000 */
459         MKVIDPID(0x1199, 0x9004),       /* Sierra Wireless Gobi 2000 */
460         MKVIDPID(0x1199, 0x9005),       /* Sierra Wireless Gobi 2000 */
461         MKVIDPID(0x1199, 0x9006),       /* Sierra Wireless Gobi 2000 */
462         MKVIDPID(0x1199, 0x9007),       /* Sierra Wireless Gobi 2000 */
463         MKVIDPID(0x1199, 0x9008),       /* Sierra Wireless Gobi 2000 */
464         MKVIDPID(0x1199, 0x9009),       /* Sierra Wireless Gobi 2000 */
465         MKVIDPID(0x1199, 0x900a),       /* Sierra Wireless Gobi 2000 */
466         MKVIDPID(0x05c6, 0x9225),       /* Sony Gobi 2000 */
467         MKVIDPID(0x05c6, 0x9235),       /* Top Global Gobi 2000 */
468         MKVIDPID(0x05c6, 0x9275),       /* iRex Technologies Gobi 2000 */
469
470         MKVIDPID(0x05c6, 0x920d),       /* Qualcomm Gobi 3000 */
471         MKVIDPID(0x1410, 0xa021),       /* Novatel Gobi 3000 */
472         MKVIDPID(0x413c, 0x8194),       /* Dell Gobi 3000 */
473         MKVIDPID(0x12D1, 0x14F1),       /* Sony Gobi 3000 */
474         { }
475 };
476
477 MODULE_DEVICE_TABLE(usb, qc_vidpids);
478
479 static u8 nibble(unsigned char c)
480 {
481         if (likely(isdigit(c)))
482                 return c - '0';
483         c = toupper(c);
484         if (likely(isxdigit(c)))
485                 return 10 + c - 'A';
486         return 0;
487 }
488
489 int qcnet_probe(struct usb_interface *iface, const struct usb_device_id *vidpids)
490 {
491         int status;
492         struct usbnet *usbnet;
493         struct qcusbnet *dev;
494         struct net_device_ops *netdevops;
495         int i;
496         u8 *addr;
497
498         status = usbnet_probe(iface, vidpids);
499         if (status < 0) {
500                 DBG("usbnet_probe failed %d\n", status);
501                 return status;
502         }
503
504         usbnet = usb_get_intfdata(iface);
505
506         if (!usbnet || !usbnet->net) {
507                 DBG("failed to get netdevice\n");
508                 return -ENXIO;
509         }
510
511         dev = kmalloc(sizeof(struct qcusbnet), GFP_KERNEL);
512         if (!dev) {
513                 DBG("failed to allocate device buffers\n");
514                 return -ENOMEM;
515         }
516
517         usbnet->data[0] = (unsigned long)dev;
518
519         dev->usbnet = usbnet;
520
521         netdevops = kmalloc(sizeof(struct net_device_ops), GFP_KERNEL);
522         if (!netdevops) {
523                 DBG("failed to allocate net device ops\n");
524                 return -ENOMEM;
525         }
526         memcpy(netdevops, usbnet->net->netdev_ops, sizeof(struct net_device_ops));
527
528         dev->open = netdevops->ndo_open;
529         netdevops->ndo_open = qcnet_open;
530         dev->stop = netdevops->ndo_stop;
531         netdevops->ndo_stop = qcnet_stop;
532         netdevops->ndo_start_xmit = qcnet_startxmit;
533         netdevops->ndo_tx_timeout = qcnet_txtimeout;
534
535         usbnet->net->netdev_ops = netdevops;
536
537         memset(&(dev->usbnet->net->stats), 0, sizeof(struct net_device_stats));
538
539         dev->iface = iface;
540         memset(&(dev->meid), '0', 14);
541
542         dev->valid = false;
543         memset(&dev->qmi, 0, sizeof(dev->qmi));
544
545         dev->qmi.devclass = devclass;
546
547         kref_init(&dev->refcount);
548         INIT_LIST_HEAD(&dev->node);
549         INIT_LIST_HEAD(&dev->qmi.clients);
550         dev->workqueue = alloc_ordered_workqueue("gobi", 0);
551
552         spin_lock_init(&dev->urbs_lock);
553         INIT_LIST_HEAD(&dev->urbs);
554         dev->active = NULL;
555         INIT_WORK(&dev->startxmit, qcnet_bg_startxmit);
556         INIT_WORK(&dev->txtimeout, qcnet_bg_txtimeout);
557         INIT_WORK(&dev->complete, qcnet_bg_complete);
558
559         spin_lock_init(&dev->qmi.clients_lock);
560
561         dev->down = 0;
562         qc_setdown(dev, DOWN_NO_NDIS_CONNECTION);
563         qc_setdown(dev, DOWN_NET_IFACE_STOPPED);
564
565         status = qc_register(dev);
566         if (status) {
567                 qc_deregister(dev);
568         } else {
569                 mutex_lock(&qcusbnet_lock);
570                 /* Give our initial ref to the list */
571                 list_add(&dev->node, &qcusbnet_list);
572                 mutex_unlock(&qcusbnet_lock);
573         }
574         /* After calling qc_register, MEID is valid */
575         addr = &usbnet->net->dev_addr[0];
576         for (i = 0; i < 6; i++)
577                 addr[i] = (nibble(dev->meid[i*2+2]) << 4)+
578                         nibble(dev->meid[i*2+3]);
579         addr[0] &= 0xfe;                /* clear multicast bit */
580         addr[0] |= 0x02;                /* set local assignment bit (IEEE802) */
581
582         return status;
583 }
584 EXPORT_SYMBOL_GPL(qcnet_probe);
585
586 static void qcnet_disconnect(struct usb_interface *intf)
587 {
588         struct usbnet *usbnet = usb_get_intfdata(intf);
589         struct qcusbnet *dev = (struct qcusbnet *)usbnet->data[0];
590         struct list_head *node, *tmp;
591         struct urbreq *req;
592         destroy_workqueue(dev->workqueue);
593         list_for_each_safe(node, tmp, &dev->urbs) {
594                 req = list_entry(node, struct urbreq, node);
595                 usb_free_urb(req->urb);
596                 list_del(&req->node);
597                 kfree(req);
598         }
599         usbnet_disconnect(intf);
600 }
601
602 static struct usb_driver qcusbnet = {
603         .name       = "gobi",
604         .id_table   = qc_vidpids,
605         .probe      = qcnet_probe,
606         .disconnect = qcnet_disconnect,
607         .suspend    = qc_suspend,
608         .resume     = qc_resume,
609         .supports_autosuspend = true,
610 };
611
612 static int __init modinit(void)
613 {
614         devclass = class_create(THIS_MODULE, "QCQMI");
615         if (IS_ERR(devclass)) {
616                 DBG("error at class_create %ld\n", PTR_ERR(devclass));
617                 return -ENOMEM;
618         }
619         printk(KERN_INFO "%s: %s\n", DRIVER_DESC, DRIVER_VERSION);
620         return usb_register(&qcusbnet);
621 }
622 module_init(modinit);
623
624 static void __exit modexit(void)
625 {
626         usb_deregister(&qcusbnet);
627         class_destroy(devclass);
628 }
629 module_exit(modexit);
630
631 MODULE_VERSION(DRIVER_VERSION);
632 MODULE_AUTHOR(DRIVER_AUTHOR);
633 MODULE_DESCRIPTION(DRIVER_DESC);
634 MODULE_LICENSE("Dual BSD/GPL");
635
636 module_param(qcusbnet_debug, bool, S_IRUGO | S_IWUSR);
637 MODULE_PARM_DESC(qcusbnet_debug, "Debugging enabled or not");