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