arm: tegra: comms: Enable autopm for RAW-IP network driver.
[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, NVIDIA Corporation.
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       3
46
47 MODULE_LICENSE("GPL");
48
49 int g_i;
50
51 int max_intfs = MAX_INTFS;
52 unsigned long usb_net_raw_ip_vid = 0x1519;
53 unsigned long usb_net_raw_ip_pid = 0x0020;
54 unsigned long usb_net_raw_ip_intf[MAX_INTFS] = { 0x03, 0x05, 0x07 };
55 unsigned long usb_net_raw_ip_rx_debug;
56 unsigned long usb_net_raw_ip_tx_debug;
57
58 module_param(max_intfs, int, 0644);
59 MODULE_PARM_DESC(max_intfs, "usb net (raw-ip) - max. interfaces supported");
60 module_param(usb_net_raw_ip_vid, ulong, 0644);
61 MODULE_PARM_DESC(usb_net_raw_ip_vid, "usb net (raw-ip) - USB VID");
62 module_param(usb_net_raw_ip_pid, ulong, 0644);
63 MODULE_PARM_DESC(usb_net_raw_ip_pid, "usb net (raw-ip) - USB PID");
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         int baseband_index;
71         struct {
72                 struct usb_driver *driver;
73                 struct usb_device *device;
74                 struct usb_interface *interface;
75                 struct {
76                         struct {
77                                 unsigned int in;
78                                 unsigned int out;
79                         } isoch, bulk, interrupt;
80                 } pipe;
81                 /* currently active rx urb */
82                 struct urb *rx_urb;
83                 /* currently active tx urb */
84                 struct urb *tx_urb;
85         } usb;
86 };
87
88 static struct baseband_usb *baseband_usb_net[MAX_INTFS] = { 0, 0, 0};
89
90 static struct net_device *usb_net_raw_ip_dev[MAX_INTFS] = { 0, 0, 0};
91
92 static unsigned int g_usb_interface_index[MAX_INTFS];
93 static struct usb_interface *g_usb_interface[MAX_INTFS];
94
95 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb);
96 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb);
97 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb);
98
99 static int baseband_usb_driver_probe(struct usb_interface *intf,
100         const struct usb_device_id *id)
101 {
102         int i = g_i;
103
104         pr_debug("%s(%d) { intf %p id %p\n", __func__, __LINE__, intf, id);
105
106         pr_debug("i %d\n", i);
107
108         pr_debug("intf->cur_altsetting->desc.bInterfaceNumber %02x\n",
109                 intf->cur_altsetting->desc.bInterfaceNumber);
110         pr_debug("intf->cur_altsetting->desc.bAlternateSetting %02x\n",
111                 intf->cur_altsetting->desc.bAlternateSetting);
112         pr_debug("intf->cur_altsetting->desc.bNumEndpoints %02x\n",
113                 intf->cur_altsetting->desc.bNumEndpoints);
114         pr_debug("intf->cur_altsetting->desc.bInterfaceClass %02x\n",
115                 intf->cur_altsetting->desc.bInterfaceClass);
116         pr_debug("intf->cur_altsetting->desc.bInterfaceSubClass %02x\n",
117                 intf->cur_altsetting->desc.bInterfaceSubClass);
118         pr_debug("intf->cur_altsetting->desc.bInterfaceProtocol %02x\n",
119                 intf->cur_altsetting->desc.bInterfaceProtocol);
120         pr_debug("intf->cur_altsetting->desc.iInterface %02x\n",
121                 intf->cur_altsetting->desc.iInterface);
122
123         if (g_usb_interface_index[i] !=
124                 intf->cur_altsetting->desc.bInterfaceNumber) {
125                 pr_debug("%s(%d) } -ENODEV\n", __func__, __LINE__);
126                 return -ENODEV;
127         } else {
128                 g_usb_interface[i] = intf;
129         }
130
131         pr_debug("%s(%d) }\n", __func__, __LINE__);
132         return 0;
133 }
134
135 static void baseband_usb_driver_disconnect(struct usb_interface *intf)
136 {
137         pr_debug("%s intf %p\n", __func__, intf);
138 }
139
140 #ifdef CONFIG_PM
141 static int baseband_usb_driver_suspend(struct usb_interface *intf,
142         pm_message_t message)
143 {
144         int i;
145
146         pr_debug("%s intf %p\n", __func__, intf);
147
148         for (i = 0; i < max_intfs; i++) {
149                 pr_debug("[%d]\n", i);
150                 if (!baseband_usb_net[i])
151                         continue;
152                 if (baseband_usb_net[i]->usb.interface != intf) {
153                         pr_debug("%p != %p\n",
154                                 baseband_usb_net[i]->usb.interface, intf);
155                         continue;
156                 }
157                 if (!baseband_usb_net[i]->usb.rx_urb) {
158                         pr_debug("rx_usb already killed\n");
159                         continue;
160                 }
161                 /* kill usb rx */
162                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
163                 baseband_usb_net[i]->usb.rx_urb = (struct urb *) 0;
164         }
165
166         return 0;
167 }
168
169 static int baseband_usb_driver_resume(struct usb_interface *intf)
170 {
171         int i, err;
172
173         pr_debug("%s intf %p\n", __func__, intf);
174
175         for (i = 0; i < max_intfs; i++) {
176                 pr_debug("[%d]\n", i);
177                 if (!baseband_usb_net[i])
178                         continue;
179                 if (baseband_usb_net[i]->usb.interface != intf) {
180                         pr_debug("%p != %p\n",
181                                 baseband_usb_net[i]->usb.interface, intf);
182                         continue;
183                 }
184                 if (baseband_usb_net[i]->usb.rx_urb) {
185                         pr_debug("rx_usb already exists\n");
186                         continue;
187                 }
188                 /* start usb rx */
189                 err = usb_net_raw_ip_rx_urb_submit(baseband_usb_net[i]);
190                 if (err < 0) {
191                         pr_err("submit rx failed - err %d\n", err);
192                         continue;
193                 }
194         }
195
196         return 0;
197 }
198 static int baseband_usb_driver_reset_resume(struct usb_interface *intf)
199 {
200         pr_debug("%s intf %p\n", __func__, intf);
201         return baseband_usb_driver_resume(intf);
202 }
203 #endif /* CONFIG_PM */
204
205 static struct usb_device_id baseband_usb_driver_id_table[MAX_INTFS][2];
206
207 static char baseband_usb_driver_name[MAX_INTFS][32];
208
209 static struct usb_driver baseband_usb_driver[MAX_INTFS] = {
210         {
211                 .name = baseband_usb_driver_name[0],
212                 .probe = baseband_usb_driver_probe,
213                 .disconnect = baseband_usb_driver_disconnect,
214                 .id_table = baseband_usb_driver_id_table[0],
215 #ifdef CONFIG_PM
216                 .suspend = baseband_usb_driver_suspend,
217                 .resume = baseband_usb_driver_resume,
218                 .reset_resume = baseband_usb_driver_reset_resume,
219                 .supports_autosuspend = 1,
220 #endif
221         },
222         {
223                 .name = baseband_usb_driver_name[1],
224                 .probe = baseband_usb_driver_probe,
225                 .disconnect = baseband_usb_driver_disconnect,
226                 .id_table = baseband_usb_driver_id_table[1],
227 #ifdef CONFIG_PM
228                 .suspend = baseband_usb_driver_suspend,
229                 .resume = baseband_usb_driver_resume,
230                 .reset_resume = baseband_usb_driver_reset_resume,
231                 .supports_autosuspend = 1,
232 #endif
233         },
234         {
235                 .name = baseband_usb_driver_name[2],
236                 .probe = baseband_usb_driver_probe,
237                 .disconnect = baseband_usb_driver_disconnect,
238                 .id_table = baseband_usb_driver_id_table[2],
239 #ifdef CONFIG_PM
240                 .suspend = baseband_usb_driver_suspend,
241                 .resume = baseband_usb_driver_resume,
242                 .reset_resume = baseband_usb_driver_reset_resume,
243                 .supports_autosuspend = 1,
244 #endif
245         },
246 };
247
248 static void find_usb_pipe(struct baseband_usb *usb)
249 {
250         struct usb_device *usbdev = usb->usb.device;
251         struct usb_interface *intf = usb->usb.interface;
252         unsigned char numendpoint = intf->cur_altsetting->desc.bNumEndpoints;
253         struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
254         unsigned char n;
255
256         for (n = 0; n < numendpoint; n++) {
257                 if (usb_endpoint_is_isoc_in(&endpoint[n].desc)) {
258                         pr_debug("endpoint[%d] isochronous in\n", n);
259                         usb->usb.pipe.isoch.in = usb_rcvisocpipe(usbdev,
260                                 endpoint[n].desc.bEndpointAddress);
261                 } else if (usb_endpoint_is_isoc_out(&endpoint[n].desc)) {
262                         pr_debug("endpoint[%d] isochronous out\n", n);
263                         usb->usb.pipe.isoch.out = usb_sndisocpipe(usbdev,
264                                 endpoint[n].desc.bEndpointAddress);
265                 } else if (usb_endpoint_is_bulk_in(&endpoint[n].desc)) {
266                         pr_debug("endpoint[%d] bulk in\n", n);
267                         usb->usb.pipe.bulk.in = usb_rcvbulkpipe(usbdev,
268                                 endpoint[n].desc.bEndpointAddress);
269                 } else if (usb_endpoint_is_bulk_out(&endpoint[n].desc)) {
270                         pr_debug("endpoint[%d] bulk out\n", n);
271                         usb->usb.pipe.bulk.out = usb_sndbulkpipe(usbdev,
272                                 endpoint[n].desc.bEndpointAddress);
273                 } else if (usb_endpoint_is_int_in(&endpoint[n].desc)) {
274                         pr_debug("endpoint[%d] interrupt in\n", n);
275                         usb->usb.pipe.interrupt.in = usb_rcvintpipe(usbdev,
276                                 endpoint[n].desc.bEndpointAddress);
277                 } else if (usb_endpoint_is_int_out(&endpoint[n].desc)) {
278                         pr_debug("endpoint[%d] interrupt out\n", n);
279                         usb->usb.pipe.interrupt.out = usb_sndintpipe(usbdev,
280                                 endpoint[n].desc.bEndpointAddress);
281                 } else {
282                         pr_debug("endpoint[%d] skipped\n", n);
283                 }
284         }
285 }
286
287 void baseband_usb_close(struct baseband_usb *usb);
288
289 struct baseband_usb *baseband_usb_open(int index,
290         unsigned int vid,
291         unsigned int pid,
292         unsigned int intf)
293 {
294         struct baseband_usb *usb;
295         int err;
296
297         pr_debug("baseband_usb_open {\n");
298
299         /* allocate baseband usb structure */
300         usb = kzalloc(sizeof(struct baseband_usb),
301                 GFP_KERNEL);
302         if (!usb)
303                 return (struct baseband_usb *) 0;
304
305         /* open usb driver */
306         sprintf(baseband_usb_driver_name[index],
307                 "baseband_usb_%x_%x_%x",
308                 vid, pid, intf);
309         baseband_usb_driver_id_table[index][0].match_flags =
310                 USB_DEVICE_ID_MATCH_DEVICE;
311         baseband_usb_driver_id_table[index][0].idVendor = vid;
312         baseband_usb_driver_id_table[index][0].idProduct = pid;
313         g_usb_interface_index[index] = intf;
314         g_usb_interface[index] = (struct usb_interface *) 0;
315         err = usb_register(&baseband_usb_driver[index]);
316         if (err < 0) {
317                 pr_err("cannot open usb driver - err %d\n", err);
318                 goto error_exit;
319         }
320         usb->baseband_index = index;
321         usb->usb.driver = &baseband_usb_driver[index];
322         if (!g_usb_interface[index]) {
323                 pr_err("cannot open usb driver - !g_usb_interface[%d]\n",
324                         index);
325                 goto error_exit;
326         }
327         usb->usb.device = interface_to_usbdev(g_usb_interface[index]);
328         usb->usb.interface = g_usb_interface[index];
329         find_usb_pipe(usb);
330         usb->usb.rx_urb = (struct urb *) 0;
331         usb->usb.tx_urb = (struct urb *) 0;
332         g_usb_interface_index[index] = ~0U;
333         g_usb_interface[index] = (struct usb_interface *) 0;
334         pr_debug("usb->usb.driver->name %s\n", usb->usb.driver->name);
335         pr_debug("usb->usb.device %p\n", usb->usb.device);
336         pr_debug("usb->usb.interface %p\n", usb->usb.interface);
337         pr_debug("usb->usb.pipe.isoch.in %x\n", usb->usb.pipe.isoch.in);
338         pr_debug("usb->usb.pipe.isoch.out %x\n", usb->usb.pipe.isoch.out);
339         pr_debug("usb->usb.pipe.bulk.in %x\n", usb->usb.pipe.bulk.in);
340         pr_debug("usb->usb.pipe.bulk.out %x\n", usb->usb.pipe.bulk.out);
341         pr_debug("usb->usb.pipe.interrupt.in %x\n", usb->usb.pipe.interrupt.in);
342         pr_debug("usb->usb.pipe.interrupt.out %x\n",
343                 usb->usb.pipe.interrupt.out);
344
345         pr_debug("baseband_usb_open }\n");
346         return usb;
347
348 error_exit:
349         return (struct baseband_usb *) 0;
350 }
351
352 void baseband_usb_close(struct baseband_usb *usb)
353 {
354         pr_debug("baseband_usb_close {\n");
355
356         /* check input */
357         if (!usb)
358                 return;
359
360         /* close usb driver */
361         if (usb->usb.driver) {
362                 pr_debug("close usb driver {\n");
363                 usb_deregister(usb->usb.driver);
364                 usb->usb.driver = (struct usb_driver *) 0;
365                 pr_debug("close usb driver }\n");
366         }
367
368         /* free baseband usb structure */
369         kfree(usb);
370
371         pr_debug("baseband_usb_close }\n");
372 }
373
374 static int baseband_usb_netdev_init(struct net_device *dev)
375 {
376         pr_debug("baseband_usb_netdev_init\n");
377         return 0;
378 }
379
380 static void baseband_usb_netdev_uninit(struct net_device *dev)
381 {
382         pr_debug("baseband_usb_netdev_uninit\n");
383 }
384
385 static int baseband_usb_netdev_open(struct net_device *dev)
386 {
387         pr_debug("baseband_usb_netdev_open\n");
388         netif_start_queue(dev);
389         return 0;
390 }
391
392 static int baseband_usb_netdev_stop(struct net_device *dev)
393 {
394         pr_debug("baseband_usb_netdev_stop\n");
395         netif_stop_queue(dev);
396         return 0;
397 }
398
399 static netdev_tx_t baseband_usb_netdev_start_xmit(
400         struct sk_buff *skb, struct net_device *dev)
401 {
402         int i = 0;
403         struct baseband_usb *usb = baseband_usb_net[i];
404         struct urb *urb;
405         unsigned char *buf;
406         int err;
407
408         pr_debug("baseband_usb_netdev_start_xmit\n");
409
410         /* check input */
411         if (!skb) {
412                 pr_err("no skb\n");
413                 return -EINVAL;
414         }
415
416         /* allocate urb */
417         urb = usb_alloc_urb(0, GFP_ATOMIC);
418         if (!urb) {
419                 pr_err("usb_alloc_urb() failed\n");
420                 kfree_skb(skb);
421                 return -ENOMEM;
422         }
423         buf = kzalloc(skb->len - 14, GFP_ATOMIC);
424         if (!buf) {
425                 pr_err("usb buffer kzalloc() failed\n");
426                 usb_free_urb(urb);
427                 kfree_skb(skb);
428                 return -ENOMEM;
429         }
430         err = skb_copy_bits(skb, 14, buf, skb->len - 14);
431         if (err < 0) {
432                 pr_err("skb_copy_bits() failed - %d\n", err);
433                 kfree(buf);
434                 usb_free_urb(urb);
435                 kfree_skb(skb);
436                 return err;
437         }
438         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.out,
439                 buf, skb->len - 14,
440                 usb_net_raw_ip_tx_urb_comp,
441                 usb);
442         urb->transfer_flags = URB_ZERO_PACKET;
443
444         /* autoresume before tx */
445         err = usb_autopm_get_interface(usb->usb.interface);
446         if (err < 0) {
447                 pr_err("%s: usb_autopm_get_interface(%p) failed %d\n",
448                         __func__, usb->usb.interface, err);
449                 kfree(urb->transfer_buffer);
450                 usb_free_urb(urb);
451                 kfree_skb(skb);
452                 return err;
453         }
454
455         /* submit tx urb */
456         usb_mark_last_busy(usb->usb.device);
457         usb->usb.tx_urb = urb;
458         err = usb_submit_urb(urb, GFP_ATOMIC);
459         if (err < 0) {
460                 pr_err("usb_submit_urb() failed - err %d\n", err);
461                 usb_autopm_put_interface(usb->usb.interface);
462                 usb->usb.tx_urb = (struct urb *) 0;
463                 kfree(urb->transfer_buffer);
464                 usb_free_urb(urb);
465                 kfree_skb(skb);
466                 return err;
467         }
468
469         /* free skb */
470         consume_skb(skb);
471
472         return NETDEV_TX_OK;
473 }
474
475 static struct net_device_ops usb_net_raw_ip_ops = {
476         .ndo_init =             baseband_usb_netdev_init,
477         .ndo_uninit =           baseband_usb_netdev_uninit,
478         .ndo_open =             baseband_usb_netdev_open,
479         .ndo_stop =             baseband_usb_netdev_stop,
480         .ndo_start_xmit =       baseband_usb_netdev_start_xmit,
481 };
482
483 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb)
484 {
485         struct urb *urb;
486         void *buf;
487         int err;
488
489         pr_debug("usb_net_raw_ip_rx_urb_submit { usb %p\n", usb);
490
491         /* check input */
492         if (usb->usb.rx_urb) {
493                 pr_err("previous urb still active\n");
494                 return -1;
495         }
496
497         /* allocate rx urb */
498         urb = usb_alloc_urb(0, GFP_ATOMIC);
499         if (!urb) {
500                 pr_err("usb_alloc_urb() failed\n");
501                 return -ENOMEM;
502         }
503         buf = kzalloc(USB_NET_BUFSIZ, GFP_ATOMIC);
504         if (!buf) {
505                 pr_err("usb buffer kzalloc() failed\n");
506                 usb_free_urb(urb);
507                 return -ENOMEM;
508         }
509         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.in,
510                 buf, USB_NET_BUFSIZ,
511                 usb_net_raw_ip_rx_urb_comp,
512                 usb);
513         urb->transfer_flags = 0;
514
515         /* submit rx urb */
516         usb_mark_last_busy(usb->usb.device);
517         usb->usb.rx_urb = urb;
518         err = usb_submit_urb(urb, GFP_ATOMIC);
519         if (err < 0) {
520                 pr_err("usb_submit_urb() failed - err %d\n", err);
521                 usb->usb.rx_urb = (struct urb *) 0;
522                 kfree(urb->transfer_buffer);
523                 usb_free_urb(urb);
524                 return err;
525         }
526
527         pr_debug("usb_net_raw_ip_rx_urb_submit }\n");
528         return err;
529 }
530
531 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb)
532 {
533         struct baseband_usb *usb = (struct baseband_usb *) urb->context;
534         int i = usb->baseband_index;
535         struct sk_buff *skb;
536         unsigned char *dst;
537         unsigned char ethernet_header[14] = {
538                 /* Destination MAC */
539                 0x00, 0x00,
540                 0x00, 0x00,
541                 0x00, 0x00,
542                 /* Source MAC */
543                 0x00, 0x00,
544                 0x00, 0x00,
545                 0x00, 0x00,
546                 /* EtherType */
547                 NET_IP_ETHERTYPE,
548         };
549
550         pr_debug("usb_net_raw_ip_rx_urb_comp { urb %p\n", urb);
551
552         /* check input */
553         if (!urb) {
554                 pr_err("no urb\n");
555                 return;
556         }
557         if (urb->status == -ENOENT) {
558                 pr_info("rx urb killed\n");
559                 return;
560         }
561         if (urb->status) {
562                 pr_info("rx urb status %d\n", urb->status);
563         }
564
565         /* put rx urb data in rx buffer */
566         if (urb->actual_length) {
567                 pr_debug("usb_net_raw_ip_rx_urb_comp - "
568                         "urb->actual_length %d\n", urb->actual_length);
569                 /* allocate skb with space for
570                  * - dummy ethernet header
571                  * - rx IP packet from modem
572                  */
573                 skb = netdev_alloc_skb(usb_net_raw_ip_dev[i],
574                         NET_IP_ALIGN + 14 + urb->actual_length);
575                 if (skb) {
576                         /* generate a dummy ethernet header
577                          * since modem sends IP packets without
578                          * any ethernet headers
579                          */
580                         memcpy(ethernet_header + 0,
581                                 usb_net_raw_ip_dev[i]->dev_addr, 6);
582                         memcpy(ethernet_header + 6,
583                                 "0x01\0x02\0x03\0x04\0x05\0x06", 6);
584                         /* fill skb with
585                          * - dummy ethernet header
586                          * - rx IP packet from modem
587                          */
588                         skb_reserve(skb, NET_IP_ALIGN);
589                         dst = skb_put(skb, 14);
590                         memcpy(dst, ethernet_header, 14);
591                         dst = skb_put(skb, urb->actual_length);
592                         memcpy(dst, urb->transfer_buffer, urb->actual_length);
593                         skb->protocol = eth_type_trans(skb,
594                                 usb_net_raw_ip_dev[i]);
595                         /* pass skb to network stack */
596                         if (netif_rx(skb) < 0) {
597                                 pr_err("usb_net_raw_ip_rx_urb_comp_work - "
598                                         "netif_rx(%p) failed\n", skb);
599                                 kfree_skb(skb);
600                         }
601                 } else {
602                         pr_err("usb_net_raw_ip_rx_urb_comp_work - "
603                                 "netdev_alloc_skb() failed\n");
604                 }
605         }
606
607         /* free rx urb */
608         if (urb->transfer_buffer) {
609                 kfree(urb->transfer_buffer);
610                 urb->transfer_buffer = (void *) 0;
611         }
612         usb_free_urb(urb);
613         usb->usb.rx_urb = (struct urb *) 0;
614
615         /* submit next rx urb */
616         usb_net_raw_ip_rx_urb_submit(usb);
617
618         pr_debug("usb_net_raw_ip_rx_urb_comp }\n");
619 }
620
621 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb)
622 {
623         struct baseband_usb *usb = (struct baseband_usb *) urb->context;
624
625         pr_debug("usb_net_raw_ip_tx_urb_comp {\n");
626
627         /* free tx urb */
628         if (urb->transfer_buffer) {
629                 kfree(urb->transfer_buffer);
630                 urb->transfer_buffer = (void *) 0;
631         }
632         usb_free_urb(urb);
633         usb->usb.tx_urb = (struct urb *) 0;
634
635         /* autosuspend after tx completed */
636         usb_autopm_put_interface_async(usb->usb.interface);
637
638         pr_debug("usb_net_raw_ip_tx_urb_comp }\n");
639 }
640
641 static int usb_net_raw_ip_init(void)
642 {
643         int i;
644         int err;
645
646         pr_debug("usb_net_raw_ip_init {\n");
647
648         /* create multiple raw-ip network devices */
649         for (i = 0; i < max_intfs; i++) {
650                 /* open baseband usb */
651                 g_i = i;
652                 baseband_usb_net[i] = baseband_usb_open(i, usb_net_raw_ip_vid,
653                         usb_net_raw_ip_pid, usb_net_raw_ip_intf[i]);
654                 if (!baseband_usb_net[i]) {
655                         pr_err("cannot open baseband usb net\n");
656                         err = -1;
657                         goto error_exit;
658                 }
659                 /* register network device */
660                 usb_net_raw_ip_dev[i] = alloc_netdev(0,
661                         BASEBAND_USB_NET_DEV_NAME,
662                         ether_setup);
663                 if (!usb_net_raw_ip_dev[i]) {
664                         pr_err("alloc_netdev() failed\n");
665                         err = -ENOMEM;
666                         goto error_exit;
667                 }
668                 usb_net_raw_ip_dev[i]->netdev_ops = &usb_net_raw_ip_ops;
669                 usb_net_raw_ip_dev[i]->watchdog_timeo = TX_TIMEOUT;
670                 random_ether_addr(usb_net_raw_ip_dev[i]->dev_addr);
671                 err = register_netdev(usb_net_raw_ip_dev[i]);
672                 if (err < 0) {
673                         pr_err("cannot register network device - %d\n", err);
674                         goto error_exit;
675                 }
676                 pr_debug("registered baseband usb network device"
677                                 " - dev %p name %s\n", usb_net_raw_ip_dev[i],
678                                  BASEBAND_USB_NET_DEV_NAME);
679                 /* start usb rx */
680                 err = usb_net_raw_ip_rx_urb_submit(baseband_usb_net[i]);
681                 if (err < 0) {
682                         pr_err("submit rx failed - err %d\n", err);
683                         goto error_exit;
684                 }
685         }
686
687         pr_debug("usb_net_raw_ip_init }\n");
688         return 0;
689
690 error_exit:
691         /* destroy multiple raw-ip network devices */
692         for (i = 0; i < max_intfs; i++) {
693                 /* unregister network device */
694                 if (usb_net_raw_ip_dev[i]) {
695                         unregister_netdev(usb_net_raw_ip_dev[i]);
696                         free_netdev(usb_net_raw_ip_dev[i]);
697                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
698                 }
699                 /* close baseband usb */
700                 if (baseband_usb_net[i]) {
701                         baseband_usb_close(baseband_usb_net[i]);
702                         baseband_usb_net[i] = (struct baseband_usb *) 0;
703                 }
704         }
705
706         return err;
707 }
708
709 static void usb_net_raw_ip_exit(void)
710 {
711         int i;
712
713         pr_debug("usb_net_raw_ip_exit {\n");
714
715         /* destroy multiple raw-ip network devices */
716         for (i = 0; i < max_intfs; i++) {
717                 /* unregister network device */
718                 if (usb_net_raw_ip_dev[i]) {
719                         unregister_netdev(usb_net_raw_ip_dev[i]);
720                         free_netdev(usb_net_raw_ip_dev[i]);
721                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
722                 }
723                 /* close baseband usb */
724                 if (baseband_usb_net[i]) {
725                         baseband_usb_close(baseband_usb_net[i]);
726                         baseband_usb_net[i] = (struct baseband_usb *) 0;
727                 }
728         }
729
730         pr_debug("usb_net_raw_ip_exit }\n");
731 }
732
733 module_init(usb_net_raw_ip_init)
734 module_exit(usb_net_raw_ip_exit)
735