usb: cdc_ether: Add rmnet device info to ZM5250
[linux-2.6.git] / drivers / net / usb / raw_ip_net.c
1 /*
2  * raw_ip_net.c
3  *
4  * USB network driver for RAW-IP modems.
5  *
6  * Copyright (c) 2011-2012, NVIDIA Corporation.  All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/fs.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/etherdevice.h>
31 #include <linux/usb.h>
32
33 #define BASEBAND_USB_NET_DEV_NAME               "rmnet%d"
34
35 /* ethernet packet ethertype for IP packets */
36 #define NET_IP_ETHERTYPE                0x08, 0x00
37
38 #define TX_TIMEOUT              10
39
40 #ifndef USB_NET_BUFSIZ
41 #define USB_NET_BUFSIZ                          8192
42 #endif  /* USB_NET_BUFSIZ */
43
44 /* maximum interface number supported */
45 #define MAX_INTFS       5
46
47 MODULE_LICENSE("GPL");
48
49 static int g_i;
50
51 /* To support more rmnet interfaces, increase the default max_intfs or
52  * pass kernel module parameter.
53  * e.g. insmod raw_ip_net.ko max_intfs=5
54  */
55 static int max_intfs = 2;       /* default number of interfaces */
56
57 static unsigned long usb_net_raw_ip_intf[MAX_INTFS] = { 3, 5, 9, 11, 13};
58 unsigned long usb_net_raw_ip_rx_debug;
59 unsigned long usb_net_raw_ip_tx_debug;
60
61 module_param(max_intfs, int, 0644);
62 MODULE_PARM_DESC(max_intfs, "usb net (raw-ip) - max. interfaces supported");
63 module_param(usb_net_raw_ip_rx_debug, ulong, 0644);
64 MODULE_PARM_DESC(usb_net_raw_ip_rx_debug, "usb net (raw-ip) - rx debug");
65 module_param(usb_net_raw_ip_tx_debug, ulong, 0644);
66 MODULE_PARM_DESC(usb_net_raw_ip_tx_debug, "usb net (raw-ip) - tx debug");
67
68 struct baseband_usb {
69         /* semaphore between disconnect/suspend/resume */
70         struct semaphore sem;
71         /* instance */
72         int baseband_index;
73         /* network statistics */
74         struct net_device_stats stats;
75         /* usb context */
76         struct {
77                 struct usb_driver *driver;
78                 struct usb_device *device;
79                 struct usb_interface *interface;
80                 struct {
81                         struct {
82                                 unsigned int in;
83                                 unsigned int out;
84                         } isoch, bulk, interrupt;
85                 } pipe;
86                 /* currently active rx urb */
87                 struct urb *rx_urb;
88                 /* currently active tx urb */
89                 struct urb *tx_urb;
90                 struct usb_anchor tx_urb_deferred;
91                 struct workqueue_struct *tx_workqueue;
92                 struct work_struct tx_work;
93         } usb;
94         /* re-usable rx urb */
95         struct urb *urb_r;
96         void *buff;
97         /* suspend count */
98         int susp_count;
99 };
100
101 static struct baseband_usb *baseband_usb_net[MAX_INTFS] = { 0, 0, 0, 0, 0};
102
103 static struct net_device *usb_net_raw_ip_dev[MAX_INTFS] = { 0, 0, 0, 0, 0};
104
105 static struct usb_interface *g_usb_interface[MAX_INTFS];
106
107 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb);
108 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb);
109
110 static int usb_net_raw_ip_tx_urb_submit(struct baseband_usb *usb,
111         struct sk_buff *skb);
112 static void usb_net_raw_ip_tx_urb_work(struct work_struct *work);
113 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb);
114
115 static int baseband_usb_driver_probe(struct usb_interface *intf,
116         const struct usb_device_id *id)
117 {
118         int i = g_i, j;
119
120         pr_debug("%s(%d) { intf %p id %p\n", __func__, __LINE__, intf, id);
121
122         pr_debug("i %d\n", i);
123
124         pr_debug("intf->cur_altsetting->desc.bInterfaceNumber %02x\n",
125                 intf->cur_altsetting->desc.bInterfaceNumber);
126         pr_debug("intf->cur_altsetting->desc.bAlternateSetting %02x\n",
127                 intf->cur_altsetting->desc.bAlternateSetting);
128         pr_debug("intf->cur_altsetting->desc.bNumEndpoints %02x\n",
129                 intf->cur_altsetting->desc.bNumEndpoints);
130         pr_debug("intf->cur_altsetting->desc.bInterfaceClass %02x\n",
131                 intf->cur_altsetting->desc.bInterfaceClass);
132         pr_debug("intf->cur_altsetting->desc.bInterfaceSubClass %02x\n",
133                 intf->cur_altsetting->desc.bInterfaceSubClass);
134         pr_debug("intf->cur_altsetting->desc.bInterfaceProtocol %02x\n",
135                 intf->cur_altsetting->desc.bInterfaceProtocol);
136         pr_debug("intf->cur_altsetting->desc.iInterface %02x\n",
137                 intf->cur_altsetting->desc.iInterface);
138
139         /* register interfaces that are assigned to raw-ip */
140         for (j = 0; j < max_intfs; j++) {
141                 if (usb_net_raw_ip_intf[j] ==
142                                 intf->cur_altsetting->desc.bInterfaceNumber) {
143                         pr_info("%s: raw_ip using interface %d\n", __func__,
144                                 intf->cur_altsetting->desc.bInterfaceNumber);
145                         g_usb_interface[j] = intf;
146                         return 0;
147                 }
148         }
149         pr_debug("%s(%d) }\n", __func__, __LINE__);
150         return -ENODEV;
151 }
152
153 static void baseband_usb_driver_disconnect(struct usb_interface *intf)
154 {
155         int i;
156         struct urb *urb;
157
158         pr_debug("%s intf %p\n", __func__, intf);
159
160         for (i = 0; i < max_intfs; i++) {
161                 pr_debug("[%d]\n", i);
162                 if (!baseband_usb_net[i])
163                         continue;
164                 if (baseband_usb_net[i]->usb.interface != intf) {
165                         pr_debug("%p != %p\n",
166                                 baseband_usb_net[i]->usb.interface, intf);
167                         continue;
168                 }
169                 /* acquire semaphore */
170                 if (down_interruptible(&baseband_usb_net[i]->sem)) {
171                         pr_err("%s: cannot acquire semaphore\n", __func__);
172                         continue;
173                 }
174                 /* kill usb tx */
175                 while ((urb = usb_get_from_anchor(&baseband_usb_net[i]->
176                         usb.tx_urb_deferred)) != (struct urb *) 0) {
177                         pr_info("%s: kill deferred tx urb %p\n",
178                                 __func__, urb);
179                         /* decrement count from usb_get_from_anchor() */
180                         usb_free_urb(urb);
181                         /* kill tx urb */
182                         usb_kill_urb(urb);
183                         /* free tx urb + tx urb transfer buffer */
184                         if (urb->transfer_buffer) {
185                                 kfree(urb->transfer_buffer);
186                                 urb->transfer_buffer = (void *) 0;
187                         }
188                         usb_free_urb(urb);
189                 }
190                 if (baseband_usb_net[i]->usb.tx_workqueue) {
191                         flush_workqueue(baseband_usb_net[i]
192                                 ->usb.tx_workqueue);
193                 }
194                 if (baseband_usb_net[i]->usb.tx_urb) {
195                         usb_kill_urb(baseband_usb_net[i]->usb.tx_urb);
196                         baseband_usb_net[i]->usb.tx_urb
197                                 = (struct urb *) 0;
198                 }
199                 /* kill usb rx */
200                 if (baseband_usb_net[i]->usb.rx_urb) {
201                         usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
202                         baseband_usb_net[i]->usb.rx_urb
203                                 = (struct urb *) 0;
204                 }
205                 /* mark interface as disconnected */
206                 baseband_usb_net[i]->usb.interface
207                         = (struct usb_interface *) 0;
208                 /* release semaphore */
209                 up(&baseband_usb_net[i]->sem);
210         }
211
212 }
213
214 #ifdef CONFIG_PM
215 static int baseband_usb_driver_suspend(struct usb_interface *intf,
216         pm_message_t message)
217 {
218         int i, susp_count;
219
220         pr_debug("%s intf %p\n", __func__, intf);
221
222         pr_debug("%s: cnt %d intf=%p &intf->dev=%p kobj=%s\n",
223                         __func__, atomic_read(&intf->dev.power.usage_count),
224                         intf, &intf->dev, kobject_name(&intf->dev.kobj));
225
226         for (i = 0; i < max_intfs; i++) {
227                 pr_debug("[%d]\n", i);
228                 if (!baseband_usb_net[i])
229                         continue;
230                 if (baseband_usb_net[i]->usb.interface != intf) {
231                         pr_debug("%p != %p\n",
232                                 baseband_usb_net[i]->usb.interface, intf);
233                         continue;
234                 }
235                 /* increment suspend count */
236                 susp_count = (baseband_usb_net[i]->susp_count)++;
237                 if (susp_count > 0) {
238                         pr_debug("%s: susp_count %d > 0 (already suspended)\n",
239                                 __func__, susp_count);
240                         continue;
241                 }
242                 if (susp_count < 0) {
243                         pr_debug("%s: susp_count %d < 0 (ILLEGAL VALUE)\n",
244                                 __func__, susp_count);
245                         baseband_usb_net[i]->susp_count = 0;
246                         continue;
247                 }
248                 pr_debug("%s: susp_count = %d (suspending...)\n",
249                         __func__, susp_count);
250                 /* acquire semaphore */
251                 if (down_interruptible(&baseband_usb_net[i]->sem)) {
252                         pr_err("%s: cannot acquire semaphore\n", __func__);
253                         continue;
254                 }
255                 /* kill usb rx */
256                 if (!baseband_usb_net[i]->usb.rx_urb) {
257                         pr_debug("rx_usb already killed\n");
258                         up(&baseband_usb_net[i]->sem);
259                         continue;
260                 }
261                 pr_debug("%s: kill rx_urb {\n",__func__);
262                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
263                 pr_debug("%s: kill rx_urb }\n",__func__);
264                 baseband_usb_net[i]->usb.rx_urb = (struct urb *) 0;
265                 /* cancel tx urb work (will restart after resume) */
266                 if (!baseband_usb_net[i]->usb.tx_workqueue) {
267                         pr_err("%s: !tx_workqueue\n", __func__);
268                         up(&baseband_usb_net[i]->sem);
269                         continue;
270                 }
271                 pr_debug("%s: cancel_work_sync {\n",__func__);
272                 cancel_work_sync(&baseband_usb_net[i]->usb.tx_work);
273                 pr_debug("%s: cancel_work_sync }\n",__func__);
274                 /* release semaphore */
275                 up(&baseband_usb_net[i]->sem);
276         }
277
278         return 0;
279 }
280
281 static int baseband_usb_driver_resume(struct usb_interface *intf)
282 {
283         int i, err, susp_count;
284
285         pr_debug("%s intf %p\n", __func__, intf);
286
287         pr_debug("%s: cnt %d intf=%p &intf->dev=%p kobj=%s\n",
288                         __func__, atomic_read(&intf->dev.power.usage_count),
289                         intf, &intf->dev, kobject_name(&intf->dev.kobj));
290
291         for (i = 0; i < max_intfs; i++) {
292                 pr_debug("[%d]\n", i);
293                 if (!baseband_usb_net[i])
294                         continue;
295                 if (baseband_usb_net[i]->usb.interface != intf) {
296                         pr_debug("%p != %p\n",
297                                 baseband_usb_net[i]->usb.interface, intf);
298                         continue;
299                 }
300                 /* decrement suspend count */
301                 susp_count = --(baseband_usb_net[i]->susp_count);
302                 if (susp_count > 0) {
303                         pr_debug("%s: susp_count %d > 0 (not resuming yet)\n",
304                                 __func__, susp_count);
305                         continue;
306                 }
307                 if (susp_count < 0) {
308                         pr_debug("%s: susp_count %d < 0 (ILLEGAL VALUE)\n",
309                                 __func__, susp_count);
310                         baseband_usb_net[i]->susp_count = 0;
311                         continue;
312                 }
313                 pr_debug("%s: susp_count = %d (resuming...)\n",
314                         __func__, susp_count);
315                 /* acquire semaphore */
316                 if (down_interruptible(&baseband_usb_net[i]->sem)) {
317                         pr_err("%s: cannot acquire semaphore\n", __func__);
318                         continue;
319                 }
320                 /* start usb rx */
321                 if (baseband_usb_net[i]->usb.rx_urb) {
322                         pr_debug("rx_usb already exists\n");
323                         up(&baseband_usb_net[i]->sem);
324                         continue;
325                 }
326                 err = usb_net_raw_ip_rx_urb_submit(baseband_usb_net[i]);
327                 if (err < 0) {
328                         pr_err("submit rx failed - err %d\n", err);
329                         up(&baseband_usb_net[i]->sem);
330                         continue;
331                 }
332                 /* restart tx urb work (cancelled in suspend) */
333                 if (!baseband_usb_net[i]->usb.tx_workqueue) {
334                         pr_err("%s: !tx_workqueue\n", __func__);
335                         up(&baseband_usb_net[i]->sem);
336                         continue;
337                 }
338                 queue_work(baseband_usb_net[i]->usb.tx_workqueue,
339                         &baseband_usb_net[i]->usb.tx_work);
340                 /* release semaphore */
341                 up(&baseband_usb_net[i]->sem);
342         }
343
344         return 0;
345 }
346 static int baseband_usb_driver_reset_resume(struct usb_interface *intf)
347 {
348         pr_debug("%s intf %p\n", __func__, intf);
349         return baseband_usb_driver_resume(intf);
350 }
351 #endif /* CONFIG_PM */
352
353 static struct usb_device_id baseband_usb_driver_id_table[] = {
354         /* xmm modem vid, pid */
355         { USB_DEVICE(0x1519, 0x0020), },
356         { },
357 };
358
359 static struct usb_driver baseband_usb_driver = {
360                 .name = "bb_raw_ip_net",
361                 .probe = baseband_usb_driver_probe,
362                 .disconnect = baseband_usb_driver_disconnect,
363                 .id_table = baseband_usb_driver_id_table,
364 #ifdef CONFIG_PM
365                 .suspend = baseband_usb_driver_suspend,
366                 .resume = baseband_usb_driver_resume,
367                 .reset_resume = baseband_usb_driver_reset_resume,
368                 .supports_autosuspend = 1,
369 #endif
370 };
371
372 MODULE_DEVICE_TABLE(usb, baseband_usb_driver_id_table);
373
374 static void find_usb_pipe(struct baseband_usb *usb)
375 {
376         struct usb_device *usbdev = usb->usb.device;
377         struct usb_interface *intf = usb->usb.interface;
378         unsigned char numendpoint = intf->cur_altsetting->desc.bNumEndpoints;
379         struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
380         unsigned char n;
381
382         for (n = 0; n < numendpoint; n++) {
383                 if (usb_endpoint_is_isoc_in(&endpoint[n].desc)) {
384                         pr_debug("endpoint[%d] isochronous in\n", n);
385                         usb->usb.pipe.isoch.in = usb_rcvisocpipe(usbdev,
386                                 endpoint[n].desc.bEndpointAddress);
387                 } else if (usb_endpoint_is_isoc_out(&endpoint[n].desc)) {
388                         pr_debug("endpoint[%d] isochronous out\n", n);
389                         usb->usb.pipe.isoch.out = usb_sndisocpipe(usbdev,
390                                 endpoint[n].desc.bEndpointAddress);
391                 } else if (usb_endpoint_is_bulk_in(&endpoint[n].desc)) {
392                         pr_debug("endpoint[%d] bulk in\n", n);
393                         usb->usb.pipe.bulk.in = usb_rcvbulkpipe(usbdev,
394                                 endpoint[n].desc.bEndpointAddress);
395                 } else if (usb_endpoint_is_bulk_out(&endpoint[n].desc)) {
396                         pr_debug("endpoint[%d] bulk out\n", n);
397                         usb->usb.pipe.bulk.out = usb_sndbulkpipe(usbdev,
398                                 endpoint[n].desc.bEndpointAddress);
399                 } else if (usb_endpoint_is_int_in(&endpoint[n].desc)) {
400                         pr_debug("endpoint[%d] interrupt in\n", n);
401                         usb->usb.pipe.interrupt.in = usb_rcvintpipe(usbdev,
402                                 endpoint[n].desc.bEndpointAddress);
403                 } else if (usb_endpoint_is_int_out(&endpoint[n].desc)) {
404                         pr_debug("endpoint[%d] interrupt out\n", n);
405                         usb->usb.pipe.interrupt.out = usb_sndintpipe(usbdev,
406                                 endpoint[n].desc.bEndpointAddress);
407                 } else {
408                         pr_debug("endpoint[%d] skipped\n", n);
409                 }
410         }
411 }
412
413 void baseband_usb_close(struct baseband_usb *usb);
414
415 struct baseband_usb *baseband_usb_open(int index, unsigned int intf)
416 {
417         struct baseband_usb *usb;
418         int i;
419
420         pr_debug("baseband_usb_open {\n");
421
422         /* allocate baseband usb structure */
423         usb = kzalloc(sizeof(struct baseband_usb),
424                 GFP_KERNEL);
425         if (!usb)
426                 return (struct baseband_usb *) 0;
427
428         /* create semaphores */
429         sema_init(&usb->sem, 1);
430
431         /* open usb interface */
432         usb->baseband_index = index;
433         usb->usb.driver = &baseband_usb_driver;
434         if (!g_usb_interface[index]) {
435                 /* wait for usb probe */
436                 for (i = 0; i < 50; i++)
437                         if (!g_usb_interface[index])
438                                 msleep(20);
439                 if (!g_usb_interface[index]) {
440                         pr_err("can't open usb: !g_usb_interface[%d]\n", index);
441                         kfree(usb);
442                         return NULL;
443                 }
444         }
445         usb->usb.device = interface_to_usbdev(g_usb_interface[index]);
446         usb->usb.interface = g_usb_interface[index];
447         find_usb_pipe(usb);
448         usb->usb.rx_urb = (struct urb *) 0;
449         usb->usb.tx_urb = (struct urb *) 0;
450         g_usb_interface[index] = (struct usb_interface *) 0;
451         pr_debug("usb->usb.driver->name %s\n", usb->usb.driver->name);
452         pr_debug("usb->usb.device %p\n", usb->usb.device);
453         pr_debug("usb->usb.interface %p\n", usb->usb.interface);
454         pr_debug("usb->usb.pipe.isoch.in %x\n", usb->usb.pipe.isoch.in);
455         pr_debug("usb->usb.pipe.isoch.out %x\n", usb->usb.pipe.isoch.out);
456         pr_debug("usb->usb.pipe.bulk.in %x\n", usb->usb.pipe.bulk.in);
457         pr_debug("usb->usb.pipe.bulk.out %x\n", usb->usb.pipe.bulk.out);
458         pr_debug("usb->usb.pipe.interrupt.in %x\n", usb->usb.pipe.interrupt.in);
459         pr_debug("usb->usb.pipe.interrupt.out %x\n",
460                 usb->usb.pipe.interrupt.out);
461
462         pr_debug("baseband_usb_open }\n");
463         return usb;
464 }
465
466 void baseband_usb_close(struct baseband_usb *usb)
467 {
468         pr_debug("baseband_usb_close {\n");
469
470         /* check input */
471         if (!usb)
472                 return;
473
474         /* close usb driver */
475         usb->usb.driver = (struct usb_driver *) 0;
476
477         /* destroy semaphores */
478         memset(&usb->sem, 0, sizeof(usb->sem));
479
480         /* free baseband usb structure */
481         kfree(usb);
482
483         pr_debug("baseband_usb_close }\n");
484 }
485
486 static int baseband_usb_netdev_init(struct net_device *dev)
487 {
488         pr_debug("baseband_usb_netdev_init\n");
489         return 0;
490 }
491
492 static void baseband_usb_netdev_uninit(struct net_device *dev)
493 {
494         pr_debug("baseband_usb_netdev_uninit\n");
495 }
496
497 static int baseband_usb_netdev_open(struct net_device *dev)
498 {
499         pr_debug("baseband_usb_netdev_open\n");
500         netif_start_queue(dev);
501         return 0;
502 }
503
504 static int baseband_usb_netdev_stop(struct net_device *dev)
505 {
506         pr_debug("baseband_usb_netdev_stop\n");
507         netif_stop_queue(dev);
508         return 0;
509 }
510
511 static netdev_tx_t baseband_usb_netdev_start_xmit(
512         struct sk_buff *skb, struct net_device *dev)
513 {
514         int i;
515         struct baseband_usb *usb;
516         int err;
517
518         pr_debug("baseband_usb_netdev_start_xmit\n");
519
520         /* check input */
521         if (!skb) {
522                 pr_err("no skb\n");
523                 return NETDEV_TX_BUSY;
524         }
525         if (!dev) {
526                 pr_err("no net dev\n");
527                 return NETDEV_TX_BUSY;
528         }
529
530         /* find index of network device which is transmitting */
531         for (i = 0; i < max_intfs; i++) {
532                 if (usb_net_raw_ip_dev[i] == dev)
533                         break;
534         }
535         if (i >= max_intfs) {
536                 pr_err("unknown net dev %p\n", dev);
537                 return NETDEV_TX_BUSY;
538         }
539         usb = baseband_usb_net[i];
540
541         /* autoresume if suspended */
542         if (usb->usb.interface) {
543                 usb_autopm_get_interface_async(usb->usb.interface);
544         } else {
545                 pr_err("%s: tx get interface error\n", __func__);
546                 netif_stop_queue(dev);
547                 usb->stats.tx_errors++;
548                 return NETDEV_TX_BUSY;
549         }
550
551         /* submit tx urb */
552         err = usb_net_raw_ip_tx_urb_submit(usb, skb);
553         if (err < 0) {
554                 pr_err("%s: tx urb submit error\n", __func__);
555                 netif_stop_queue(dev);
556                 usb->stats.tx_errors++;
557                 return NETDEV_TX_BUSY;
558         }
559
560         return NETDEV_TX_OK;
561 }
562
563 static struct net_device_stats *baseband_usb_netdev_get_stats(
564                                 struct net_device *dev)
565 {
566         int i;
567         for (i = 0; i < max_intfs; i++) {
568                 if (dev == usb_net_raw_ip_dev[i]) {
569                         pr_debug("%s idx(%d)\n", __func__, i);
570                         return &baseband_usb_net[i]->stats;
571                 }
572         }
573         pr_debug("%s mismatch dev, default idx(0)\n", __func__);
574         return &baseband_usb_net[0]->stats;
575 }
576
577 static struct net_device_ops usb_net_raw_ip_ops = {
578         .ndo_init =             baseband_usb_netdev_init,
579         .ndo_uninit =           baseband_usb_netdev_uninit,
580         .ndo_open =             baseband_usb_netdev_open,
581         .ndo_stop =             baseband_usb_netdev_stop,
582         .ndo_start_xmit =       baseband_usb_netdev_start_xmit,
583         .ndo_get_stats = baseband_usb_netdev_get_stats,
584 };
585
586 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb)
587 {
588         struct urb *urb;
589         void *buf;
590         int err;
591
592         pr_debug("usb_net_raw_ip_rx_urb_submit { usb %p\n", usb);
593
594         /* check input */
595         if (!usb) {
596                 pr_err("%s: !usb\n", __func__);
597                 return -EINVAL;
598         }
599         if (!usb->usb.interface) {
600                 pr_err("usb interface disconnected - not submitting rx urb\n");
601                 return -EINVAL;
602         }
603         if (usb->usb.rx_urb) {
604                 pr_err("previous urb still active\n");
605                 return -EBUSY;
606         }
607         if (!usb->urb_r || !usb->buff) {
608                 pr_err("no reusable rx urb found\n");
609                 return -ENOMEM;
610         }
611
612         /* reuse rx urb */
613         urb = usb->urb_r;
614         buf = usb->buff;
615         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.in,
616                 buf, USB_NET_BUFSIZ,
617                 usb_net_raw_ip_rx_urb_comp,
618                 usb);
619         urb->transfer_flags = 0;
620
621         /* submit rx urb */
622         usb_mark_last_busy(usb->usb.device);
623         usb->usb.rx_urb = urb;
624         err = usb_submit_urb(urb, GFP_ATOMIC);
625         if (err < 0) {
626                 pr_err("usb_submit_urb() failed - err %d\n", err);
627                 usb->usb.rx_urb = (struct urb *) 0;
628                 return err;
629         }
630
631         pr_debug("usb_net_raw_ip_rx_urb_submit }\n");
632         return err;
633 }
634
635 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb)
636 {
637         struct baseband_usb *usb;
638         int i;
639         struct sk_buff *skb;
640         unsigned char *dst;
641         unsigned char ethernet_header[14] = {
642                 /* Destination MAC */
643                 0x00, 0x00,
644                 0x00, 0x00,
645                 0x00, 0x00,
646                 /* Source MAC */
647                 0x00, 0x00,
648                 0x00, 0x00,
649                 0x00, 0x00,
650                 /* EtherType */
651                 NET_IP_ETHERTYPE,
652         };
653
654         pr_debug("usb_net_raw_ip_rx_urb_comp { urb %p\n", urb);
655
656         /* check input */
657         if (!urb) {
658                 pr_err("no urb\n");
659                 return;
660         }
661         usb = (struct baseband_usb *)urb->context;
662         i = usb->baseband_index;
663         switch (urb->status) {
664         case 0:
665                 break;
666         case -ESHUTDOWN:
667                 /* fall through */
668                 pr_info("%s: rx urb %p - link shutdown %d\n",
669                         __func__, urb, urb->status);
670                 goto err_exit;
671         case -EPROTO:
672                 pr_info("%s: rx urb %p - link shutdown %d EPROTO\n",
673                         __func__, urb, urb->status);
674                 goto err_exit;
675         default:
676                 pr_info("%s: rx urb %p - status %d\n",
677                         __func__, urb, urb->status);
678                 break;
679         }
680
681         /* put rx urb data in rx buffer */
682         if (urb->actual_length > 0) {
683                 pr_debug("usb_net_raw_ip_rx_urb_comp - "
684                         "urb->actual_length %d\n", urb->actual_length);
685                 /* allocate skb with space for
686                  * - dummy ethernet header
687                  * - rx IP packet from modem
688                  */
689                 skb = netdev_alloc_skb(usb_net_raw_ip_dev[i],
690                         NET_IP_ALIGN + 14 + urb->actual_length);
691                 if (skb) {
692                         /* generate a dummy ethernet header
693                          * since modem sends IP packets without
694                          * any ethernet headers
695                          */
696                         memcpy(ethernet_header + 0,
697                                 usb_net_raw_ip_dev[i]->dev_addr, 6);
698                         memcpy(ethernet_header + 6,
699                                 "0x01\0x02\0x03\0x04\0x05\0x06", 6);
700                         /* fill skb with
701                          * - dummy ethernet header
702                          * - rx IP packet from modem
703                          */
704                         skb_reserve(skb, NET_IP_ALIGN);
705                         dst = skb_put(skb, 14);
706                         memcpy(dst, ethernet_header, 14);
707                         if ((((unsigned char *) urb->transfer_buffer)[0]
708                                 & 0xf0) == 0x60) {
709                                 /* ipv6 ether type */
710                                 dst[12] = 0x86;
711                                 dst[13] = 0xdd;
712                         }
713                         dst = skb_put(skb, urb->actual_length);
714                         memcpy(dst, urb->transfer_buffer, urb->actual_length);
715                         skb->protocol = eth_type_trans(skb,
716                                 usb_net_raw_ip_dev[i]);
717                         pr_debug("%s: ntohs(skb->protocol) %04x (%s)\n",
718                                 __func__, ntohs(skb->protocol),
719                                 (ntohs(skb->protocol) == 0x0800)
720                                         ? "IPv4"
721                                         : (ntohs(skb->protocol) == 0x86dd)
722                                         ? "IPv6"
723                                         : "unknown");
724                         pr_debug("%s: %02x %02x %02x %02x\n", __func__,
725                                 ((unsigned char *)urb->transfer_buffer)[0],
726                                 ((unsigned char *)urb->transfer_buffer)[1],
727                                 ((unsigned char *)urb->transfer_buffer)[2],
728                                 ((unsigned char *)urb->transfer_buffer)[3]);
729                         /* pass skb to network stack */
730                         if (netif_rx(skb) < 0) {
731                                 pr_err("usb_net_raw_ip_rx_urb_comp_work - "
732                                         "netif_rx(%p) failed\n", skb);
733                                 kfree_skb(skb);
734                                 usb->stats.rx_errors++;
735                         } else {
736                                 usb->stats.rx_packets++;
737                                 usb->stats.rx_bytes +=
738                                     (14 + urb->actual_length);
739                         }
740                 } else {
741                         pr_err("usb_net_raw_ip_rx_urb_comp_work - "
742                                 "netdev_alloc_skb() failed\n");
743                 }
744         }
745
746         /* mark rx urb complete */
747         usb->usb.rx_urb = (struct urb *) 0;
748
749         /* do not submit urb if interface is suspending */
750         if (urb->status == -ENOENT)
751                 return;
752
753         /* submit next rx urb */
754         usb_net_raw_ip_rx_urb_submit(usb);
755         return;
756
757 err_exit:
758         /* mark rx urb complete */
759         usb->usb.rx_urb = (struct urb *) 0;
760
761         pr_debug("usb_net_raw_ip_rx_urb_comp }\n");
762         return;
763 }
764
765 static int usb_net_raw_ip_setup_rx_urb( struct baseband_usb *usb)
766 {
767         pr_debug("usb_net_raw_ip_setup_rx_urb {\n");
768
769         /* check input */
770         if (!usb) {
771                 pr_err("%s: !usb\n", __func__);
772                 return -EINVAL;
773         }
774         if (usb->urb_r) {
775                 pr_err("%s: reusable rx urb already allocated\n", __func__);
776                 return -EINVAL;
777         }
778
779         /* allocate reusable rx urb */
780         usb->urb_r = usb_alloc_urb(0, GFP_ATOMIC);
781         if (!usb->urb_r) {
782                 pr_err("usb_alloc_urb() failed\n");
783                 return -ENOMEM;
784         }
785         usb->buff = kzalloc(USB_NET_BUFSIZ, GFP_ATOMIC);
786         if (!usb->buff) {
787                 pr_err("usb buffer kzalloc() failed\n");
788                 usb_free_urb(usb->urb_r);
789                 usb->urb_r = (struct urb *) 0;
790                 return -ENOMEM;
791         }
792
793         pr_debug("usb_net_raw_setup_ip_rx_urb }\n");
794         return 0;
795 }
796
797 static void usb_net_raw_ip_free_rx_urb(struct baseband_usb *usb)
798 {
799         pr_debug("usb_net_raw_ip_free_rx_urb {\n");
800
801         /* check input */
802         if (!usb) {
803                 pr_err("%s: !usb\n", __func__);
804                 return;
805         }
806
807         /* free reusable rx urb */
808         if (usb->urb_r) {
809                 usb_free_urb(usb->urb_r);
810                 usb->urb_r = (struct urb *) 0;
811         }
812         if (usb->buff) {
813                 kfree(usb->buff);
814                 usb->buff = (void *) 0;
815         }
816
817         pr_debug("usb_net_raw_ip_free_rx_urb }\n");
818 }
819
820 static int usb_net_raw_ip_tx_urb_submit(struct baseband_usb *usb,
821         struct sk_buff *skb)
822 {
823         struct urb *urb;
824         unsigned char *buf;
825         int err;
826
827         pr_debug("usb_net_raw_ip_tx_urb_submit {\n");
828
829         /* check input */
830         if (!usb) {
831                 pr_err("%s: !usb\n", __func__);
832                 return -EINVAL;
833         }
834         if (!usb->usb.interface) {
835                 pr_err("usb interface disconnected - not submitting tx urb\n");
836                 return -EINVAL;
837         }
838         if (!skb) {
839                 pr_err("%s: !skb\n", __func__);
840                 usb_autopm_put_interface_async(usb->usb.interface);
841                 return -EINVAL;
842         }
843
844         /* allocate urb */
845         urb = usb_alloc_urb(0, GFP_ATOMIC);
846         if (!urb) {
847                 pr_err("usb_alloc_urb() failed\n");
848                 usb_autopm_put_interface_async(usb->usb.interface);
849                 return -ENOMEM;
850         }
851         buf = kzalloc(skb->len - 14, GFP_ATOMIC);
852         if (!buf) {
853                 pr_err("usb buffer kzalloc() failed\n");
854                 usb_free_urb(urb);
855                 usb_autopm_put_interface_async(usb->usb.interface);
856                 return -ENOMEM;
857         }
858         err = skb_copy_bits(skb, 14, buf, skb->len - 14);
859         if (err < 0) {
860                 pr_err("skb_copy_bits() failed - %d\n", err);
861                 kfree(buf);
862                 usb_free_urb(urb);
863                 usb_autopm_put_interface_async(usb->usb.interface);
864                 return err;
865         }
866         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.out,
867                 buf, skb->len - 14,
868                 usb_net_raw_ip_tx_urb_comp,
869                 usb);
870         urb->transfer_flags = URB_ZERO_PACKET;
871         pr_debug("%s: ntohs(skb->protocol) %04x (%s)\n",
872                 __func__, ntohs(skb->protocol),
873                 (ntohs(skb->protocol) == 0x0800)
874                         ? "IPv4"
875                         : (ntohs(skb->protocol) == 0x86dd)
876                         ? "IPv6"
877                         : "unknown");
878         pr_debug("%s: %02x %02x %02x %02x\n", __func__,
879                 ((unsigned char *)urb->transfer_buffer)[0],
880                 ((unsigned char *)urb->transfer_buffer)[1],
881                 ((unsigned char *)urb->transfer_buffer)[2],
882                 ((unsigned char *)urb->transfer_buffer)[3]);
883
884         /* queue tx urb work */
885         usb_anchor_urb(urb, &usb->usb.tx_urb_deferred);
886         queue_work(usb->usb.tx_workqueue, &usb->usb.tx_work);
887
888         /* free skb */
889         consume_skb(skb);
890
891         pr_debug("usb_net_raw_ip_tx_urb_submit }\n");
892         return 0;
893 }
894
895 static void usb_net_raw_ip_tx_urb_work(struct work_struct *work)
896 {
897         struct baseband_usb *usb
898                 = container_of(work, struct baseband_usb, usb.tx_work);
899         struct urb *urb;
900         int err;
901
902         pr_debug("usb_net_raw_ip_tx_urb_work {\n");
903
904         /* check if tx urb(s) queued */
905         if (usb == NULL ||
906                 (!usb->usb.tx_urb &&
907                 usb_anchor_empty(&usb->usb.tx_urb_deferred))) {
908                 pr_debug("%s: nothing to do!\n", __func__);
909                 return;
910         }
911
912         /* check if usb interface disconnected */
913         if (!usb->usb.interface) {
914                 pr_err("%s: not submitting tx urb -interface disconnected\n",
915                         __func__);
916                 return;
917         }
918
919         /* check if suspended */
920         if (usb->susp_count > 0) {
921                 pr_info("%s: usb->susp_count %d > 0 (suspended)\n",
922                         __func__, usb->susp_count);
923                 return;
924         }
925
926         /* submit queued tx urb(s) */
927         while ((urb = usb_get_from_anchor(&usb->usb.tx_urb_deferred))
928                 != (struct urb *) 0) {
929                 /* decrement count from usb_get_from_anchor() */
930                 usb_free_urb(urb);
931                 /* check if usb interface disconnected */
932                 if (!usb->usb.interface) {
933                         pr_err("%s: not submitting tx urb %p"
934                                 " - interface disconnected\n",
935                                 __func__, urb);
936                         if (urb->transfer_buffer) {
937                                 kfree(urb->transfer_buffer);
938                                 urb->transfer_buffer = (void *) 0;
939                         }
940                         usb_free_urb(urb);
941                         usb->stats.tx_errors++;
942                         continue;
943                 }
944                 /* autoresume before tx */
945                 usb_mark_last_busy(usb->usb.device);
946                 /* submit tx urb */
947                 err = usb_submit_urb(urb, GFP_ATOMIC);
948                 if (err < 0) {
949                         pr_err("%s: usb_submit_urb(%p) failed - err %d\n",
950                                 __func__, urb, err);
951                         usb_autopm_put_interface_async(usb->usb.interface);
952                         if (urb->transfer_buffer) {
953                                 kfree(urb->transfer_buffer);
954                                 urb->transfer_buffer = (void *) 0;
955                         }
956                         usb_free_urb(urb);
957                         usb->stats.tx_errors++;
958                         continue;
959                 }
960                 /* free tx urb
961                  * - actual urb free occurs when refcnt which was incremented
962                  *   in usb_submit_urb is decremented to 0 (usually after urb
963                  *   completion function returns)
964                  * - tx urb transfer buffer will be freed in urb completion
965                  *   function
966                  */
967                 usb_free_urb(urb);
968         }
969
970         pr_debug("usb_net_raw_ip_tx_urb_work }\n");
971 }
972
973 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb)
974 {
975         struct baseband_usb *usb;
976
977         pr_debug("usb_net_raw_ip_tx_urb_comp {\n");
978
979         /* check input */
980         if (!urb) {
981                 pr_err("no urb\n");
982                 return;
983         }
984         usb = (struct baseband_usb *)urb->context;
985         switch (urb->status) {
986         case 0:
987                 break;
988         case -ENOENT:
989                 /* fall through */
990         case -ESHUTDOWN:
991                 /* fall through */
992         case -EPROTO:
993                 pr_info("%s: tx urb %p - link shutdown %d\n",
994                         __func__, urb, urb->status);
995                 usb_autopm_put_interface_async(usb->usb.interface);
996                 goto err_exit;
997         default:
998                 pr_info("%s: tx urb %p - status %d\n",
999                         __func__, urb, urb->status);
1000                 break;
1001         }
1002         if (urb->status)
1003                 usb->stats.tx_errors++;
1004         else {
1005                 usb->stats.tx_packets++;
1006                 usb->stats.tx_bytes += urb->transfer_buffer_length;
1007         }
1008
1009         /* autosuspend after tx completed */
1010         if (!usb->usb.interface) {
1011                 pr_err("%s: usb interface disconnected"
1012                         " before tx urb completed!\n",
1013                         __func__);
1014                 goto err_exit;
1015         }
1016         usb_autopm_put_interface_async(usb->usb.interface);
1017
1018 err_exit:
1019         /* free tx urb transfer buffer */
1020         if (urb->transfer_buffer) {
1021                 kfree(urb->transfer_buffer);
1022                 urb->transfer_buffer = (void *) 0;
1023         }
1024         pr_debug("usb_net_raw_ip_tx_urb_comp }\n");
1025 }
1026
1027 static int usb_net_raw_ip_init(void)
1028 {
1029         int i;
1030         int err;
1031         char name[32];
1032
1033         pr_debug("usb_net_raw_ip_init {\n");
1034
1035         err = usb_register(&baseband_usb_driver);
1036         if (err < 0) {
1037                 pr_err("cannot open usb driver - err %d\n", err);
1038                 return err;
1039         }
1040
1041         /* create multiple raw-ip network devices */
1042         for (i = 0; i < max_intfs; i++) {
1043                 /* open baseband usb */
1044                 g_i = i;
1045                 baseband_usb_net[i] = baseband_usb_open(i,
1046                                                 usb_net_raw_ip_intf[i]);
1047                 if (!baseband_usb_net[i]) {
1048                         pr_err("cannot open baseband usb net\n");
1049                         err = -1;
1050                         goto error_exit;
1051                 }
1052                 init_usb_anchor(&baseband_usb_net[i]->usb.tx_urb_deferred);
1053                 /* register network device */
1054                 usb_net_raw_ip_dev[i] = alloc_netdev(0,
1055                         BASEBAND_USB_NET_DEV_NAME,
1056                         ether_setup);
1057                 if (!usb_net_raw_ip_dev[i]) {
1058                         pr_err("alloc_netdev() failed\n");
1059                         err = -ENOMEM;
1060                         goto error_exit;
1061                 }
1062                 usb_net_raw_ip_dev[i]->netdev_ops = &usb_net_raw_ip_ops;
1063                 usb_net_raw_ip_dev[i]->watchdog_timeo = TX_TIMEOUT;
1064                 random_ether_addr(usb_net_raw_ip_dev[i]->dev_addr);
1065                 err = register_netdev(usb_net_raw_ip_dev[i]);
1066                 if (err < 0) {
1067                         pr_err("cannot register network device - %d\n", err);
1068                         goto error_exit;
1069                 }
1070                 pr_debug("registered baseband usb network device"
1071                                 " - dev %p name %s\n", usb_net_raw_ip_dev[i],
1072                                  BASEBAND_USB_NET_DEV_NAME);
1073                 /* start usb rx */
1074                 err = usb_net_raw_ip_setup_rx_urb(baseband_usb_net[i]);
1075                 if (err < 0) {
1076                         pr_err("setup reusable rx urb failed - err %d\n", err);
1077                         goto error_exit;
1078                 }
1079                 err = usb_net_raw_ip_rx_urb_submit(baseband_usb_net[i]);
1080                 if (err < 0) {
1081                         pr_err("submit rx failed - err %d\n", err);
1082                         goto error_exit;
1083                 }
1084                 /* start usb tx */
1085                 sprintf(name, "raw_ip_tx_wq-%d",
1086                         baseband_usb_net[i]->baseband_index);
1087                 baseband_usb_net[i]->usb.tx_workqueue
1088                         = create_singlethread_workqueue(name);
1089                 if (!baseband_usb_net[i]->usb.tx_workqueue) {
1090                         pr_err("cannot create workqueue\n");
1091                         goto error_exit;
1092                 }
1093                 INIT_WORK(&baseband_usb_net[i]->usb.tx_work,
1094                         usb_net_raw_ip_tx_urb_work);
1095         }
1096
1097         pr_debug("usb_net_raw_ip_init }\n");
1098         return 0;
1099
1100 error_exit:
1101         /* destroy multiple raw-ip network devices */
1102         for (i = 0; i < max_intfs; i++) {
1103                 /* unregister network device */
1104                 if (usb_net_raw_ip_dev[i]) {
1105                         unregister_netdev(usb_net_raw_ip_dev[i]);
1106                         free_netdev(usb_net_raw_ip_dev[i]);
1107                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
1108                 }
1109                 /* close baseband usb */
1110                 if (baseband_usb_net[i]) {
1111                         /* stop usb tx */
1112                         if (baseband_usb_net[i]->usb.tx_workqueue) {
1113                                 destroy_workqueue(baseband_usb_net[i]
1114                                         ->usb.tx_workqueue);
1115                                 baseband_usb_net[i]->usb.tx_workqueue
1116                                         = (struct workqueue_struct *) 0;
1117                         }
1118                         if (baseband_usb_net[i]->usb.tx_urb) {
1119                                 usb_kill_urb(baseband_usb_net[i]->usb.tx_urb);
1120                                 baseband_usb_net[i]->usb.tx_urb
1121                                         = (struct urb *) 0;
1122                         }
1123                         /* stop usb rx */
1124                         if (baseband_usb_net[i]->usb.rx_urb) {
1125                                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
1126                                 baseband_usb_net[i]->usb.rx_urb
1127                                         = (struct urb *) 0;
1128                         }
1129                         usb_net_raw_ip_free_rx_urb(baseband_usb_net[i]);
1130                         /* close usb */
1131                         baseband_usb_close(baseband_usb_net[i]);
1132                         baseband_usb_net[i] = (struct baseband_usb *) 0;
1133                 }
1134         }
1135         usb_deregister(&baseband_usb_driver);
1136
1137         return err;
1138 }
1139
1140 static void usb_net_raw_ip_exit(void)
1141 {
1142         int i;
1143
1144         pr_debug("usb_net_raw_ip_exit {\n");
1145
1146         /* destroy multiple raw-ip network devices */
1147         for (i = 0; i < max_intfs; i++) {
1148                 /* unregister network device */
1149                 if (usb_net_raw_ip_dev[i]) {
1150                         unregister_netdev(usb_net_raw_ip_dev[i]);
1151                         free_netdev(usb_net_raw_ip_dev[i]);
1152                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
1153                 }
1154                 /* close baseband usb */
1155                 if (baseband_usb_net[i]) {
1156                         /* stop usb tx */
1157                         if (baseband_usb_net[i]->usb.tx_workqueue) {
1158                                 destroy_workqueue(baseband_usb_net[i]
1159                                         ->usb.tx_workqueue);
1160                                 baseband_usb_net[i]->usb.tx_workqueue
1161                                         = (struct workqueue_struct *) 0;
1162                         }
1163                         if (baseband_usb_net[i]->usb.tx_urb) {
1164                                 usb_kill_urb(baseband_usb_net[i]->usb.tx_urb);
1165                                 baseband_usb_net[i]->usb.tx_urb
1166                                         = (struct urb *) 0;
1167                         }
1168                         /* stop usb rx */
1169                         if (baseband_usb_net[i]->usb.rx_urb) {
1170                                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
1171                                 baseband_usb_net[i]->usb.rx_urb
1172                                         = (struct urb *) 0;
1173                         }
1174                         usb_net_raw_ip_free_rx_urb(baseband_usb_net[i]);
1175                         /* close usb */
1176                         baseband_usb_close(baseband_usb_net[i]);
1177                         baseband_usb_net[i] = (struct baseband_usb *) 0;
1178                 }
1179         }
1180
1181         pr_debug("close usb driver {\n");
1182         usb_deregister(&baseband_usb_driver);
1183         pr_debug("close usb driver }\n");
1184
1185         pr_debug("usb_net_raw_ip_exit }\n");
1186 }
1187
1188 module_init(usb_net_raw_ip_init)
1189 module_exit(usb_net_raw_ip_exit)
1190