unknown changes from android-tegra-nv-3.4
[linux-3.10.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         /* 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;
97         void *buff;
98         /* suspend count */
99         int susp_count;
100 };
101
102 static struct baseband_usb *baseband_usb_net[MAX_INTFS] = { 0, 0, 0};
103
104 static struct net_device *usb_net_raw_ip_dev[MAX_INTFS] = { 0, 0, 0};
105
106 static unsigned int g_usb_interface_index[MAX_INTFS];
107 static struct usb_interface *g_usb_interface[MAX_INTFS];
108
109 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb);
110 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb);
111
112 static int usb_net_raw_ip_tx_urb_submit(struct baseband_usb *usb,
113         struct sk_buff *skb);
114 static void usb_net_raw_ip_tx_urb_work(struct work_struct *work);
115 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb);
116
117 static int baseband_usb_driver_probe(struct usb_interface *intf,
118         const struct usb_device_id *id)
119 {
120         int i = g_i;
121
122         pr_debug("%s(%d) { intf %p id %p\n", __func__, __LINE__, intf, id);
123
124         pr_debug("i %d\n", i);
125
126         pr_debug("intf->cur_altsetting->desc.bInterfaceNumber %02x\n",
127                 intf->cur_altsetting->desc.bInterfaceNumber);
128         pr_debug("intf->cur_altsetting->desc.bAlternateSetting %02x\n",
129                 intf->cur_altsetting->desc.bAlternateSetting);
130         pr_debug("intf->cur_altsetting->desc.bNumEndpoints %02x\n",
131                 intf->cur_altsetting->desc.bNumEndpoints);
132         pr_debug("intf->cur_altsetting->desc.bInterfaceClass %02x\n",
133                 intf->cur_altsetting->desc.bInterfaceClass);
134         pr_debug("intf->cur_altsetting->desc.bInterfaceSubClass %02x\n",
135                 intf->cur_altsetting->desc.bInterfaceSubClass);
136         pr_debug("intf->cur_altsetting->desc.bInterfaceProtocol %02x\n",
137                 intf->cur_altsetting->desc.bInterfaceProtocol);
138         pr_debug("intf->cur_altsetting->desc.iInterface %02x\n",
139                 intf->cur_altsetting->desc.iInterface);
140
141         if (g_usb_interface_index[i] !=
142                 intf->cur_altsetting->desc.bInterfaceNumber) {
143                 pr_debug("%s(%d) } -ENODEV\n", __func__, __LINE__);
144                 return -ENODEV;
145         } else {
146                 g_usb_interface[i] = intf;
147         }
148
149         pr_debug("%s(%d) }\n", __func__, __LINE__);
150         return 0;
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[MAX_INTFS][2];
354
355 static char baseband_usb_driver_name[MAX_INTFS][32];
356
357 static struct usb_driver baseband_usb_driver[MAX_INTFS] = {
358         {
359                 .name = baseband_usb_driver_name[0],
360                 .probe = baseband_usb_driver_probe,
361                 .disconnect = baseband_usb_driver_disconnect,
362                 .id_table = baseband_usb_driver_id_table[0],
363 #ifdef CONFIG_PM
364                 .suspend = baseband_usb_driver_suspend,
365                 .resume = baseband_usb_driver_resume,
366                 .reset_resume = baseband_usb_driver_reset_resume,
367                 .supports_autosuspend = 1,
368 #endif
369         },
370         {
371                 .name = baseband_usb_driver_name[1],
372                 .probe = baseband_usb_driver_probe,
373                 .disconnect = baseband_usb_driver_disconnect,
374                 .id_table = baseband_usb_driver_id_table[1],
375 #ifdef CONFIG_PM
376                 .suspend = baseband_usb_driver_suspend,
377                 .resume = baseband_usb_driver_resume,
378                 .reset_resume = baseband_usb_driver_reset_resume,
379                 .supports_autosuspend = 1,
380 #endif
381         },
382         {
383                 .name = baseband_usb_driver_name[2],
384                 .probe = baseband_usb_driver_probe,
385                 .disconnect = baseband_usb_driver_disconnect,
386                 .id_table = baseband_usb_driver_id_table[2],
387 #ifdef CONFIG_PM
388                 .suspend = baseband_usb_driver_suspend,
389                 .resume = baseband_usb_driver_resume,
390                 .reset_resume = baseband_usb_driver_reset_resume,
391                 .supports_autosuspend = 1,
392 #endif
393         },
394 };
395
396 static void find_usb_pipe(struct baseband_usb *usb)
397 {
398         struct usb_device *usbdev = usb->usb.device;
399         struct usb_interface *intf = usb->usb.interface;
400         unsigned char numendpoint = intf->cur_altsetting->desc.bNumEndpoints;
401         struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
402         unsigned char n;
403
404         for (n = 0; n < numendpoint; n++) {
405                 if (usb_endpoint_is_isoc_in(&endpoint[n].desc)) {
406                         pr_debug("endpoint[%d] isochronous in\n", n);
407                         usb->usb.pipe.isoch.in = usb_rcvisocpipe(usbdev,
408                                 endpoint[n].desc.bEndpointAddress);
409                 } else if (usb_endpoint_is_isoc_out(&endpoint[n].desc)) {
410                         pr_debug("endpoint[%d] isochronous out\n", n);
411                         usb->usb.pipe.isoch.out = usb_sndisocpipe(usbdev,
412                                 endpoint[n].desc.bEndpointAddress);
413                 } else if (usb_endpoint_is_bulk_in(&endpoint[n].desc)) {
414                         pr_debug("endpoint[%d] bulk in\n", n);
415                         usb->usb.pipe.bulk.in = usb_rcvbulkpipe(usbdev,
416                                 endpoint[n].desc.bEndpointAddress);
417                 } else if (usb_endpoint_is_bulk_out(&endpoint[n].desc)) {
418                         pr_debug("endpoint[%d] bulk out\n", n);
419                         usb->usb.pipe.bulk.out = usb_sndbulkpipe(usbdev,
420                                 endpoint[n].desc.bEndpointAddress);
421                 } else if (usb_endpoint_is_int_in(&endpoint[n].desc)) {
422                         pr_debug("endpoint[%d] interrupt in\n", n);
423                         usb->usb.pipe.interrupt.in = usb_rcvintpipe(usbdev,
424                                 endpoint[n].desc.bEndpointAddress);
425                 } else if (usb_endpoint_is_int_out(&endpoint[n].desc)) {
426                         pr_debug("endpoint[%d] interrupt out\n", n);
427                         usb->usb.pipe.interrupt.out = usb_sndintpipe(usbdev,
428                                 endpoint[n].desc.bEndpointAddress);
429                 } else {
430                         pr_debug("endpoint[%d] skipped\n", n);
431                 }
432         }
433 }
434
435 void baseband_usb_close(struct baseband_usb *usb);
436
437 struct baseband_usb *baseband_usb_open(int index,
438         unsigned int vid,
439         unsigned int pid,
440         unsigned int intf)
441 {
442         struct baseband_usb *usb;
443         int err;
444
445         pr_debug("baseband_usb_open {\n");
446
447         /* allocate baseband usb structure */
448         usb = kzalloc(sizeof(struct baseband_usb),
449                 GFP_KERNEL);
450         if (!usb)
451                 return (struct baseband_usb *) 0;
452
453         /* create semaphores */
454         sema_init(&usb->sem, 1);
455
456         /* open usb driver */
457         sprintf(baseband_usb_driver_name[index],
458                 "baseband_usb_%x_%x_%x",
459                 vid, pid, intf);
460         baseband_usb_driver_id_table[index][0].match_flags =
461                 USB_DEVICE_ID_MATCH_DEVICE;
462         baseband_usb_driver_id_table[index][0].idVendor = vid;
463         baseband_usb_driver_id_table[index][0].idProduct = pid;
464         g_usb_interface_index[index] = intf;
465         g_usb_interface[index] = (struct usb_interface *) 0;
466         err = usb_register(&baseband_usb_driver[index]);
467         if (err < 0) {
468                 pr_err("cannot open usb driver - err %d\n", err);
469                 kfree(usb);
470                 return (struct baseband_usb *) 0;
471         }
472         usb->baseband_index = index;
473         usb->usb.driver = &baseband_usb_driver[index];
474         if (!g_usb_interface[index]) {
475                 pr_err("cannot open usb driver - !g_usb_interface[%d]\n",
476                         index);
477                 usb_deregister(usb->usb.driver);
478                 kfree(usb);
479                 return (struct baseband_usb *) 0;
480         }
481         usb->usb.device = interface_to_usbdev(g_usb_interface[index]);
482         usb->usb.interface = g_usb_interface[index];
483         find_usb_pipe(usb);
484         usb->usb.rx_urb = (struct urb *) 0;
485         usb->usb.tx_urb = (struct urb *) 0;
486         g_usb_interface_index[index] = ~0U;
487         g_usb_interface[index] = (struct usb_interface *) 0;
488         pr_debug("usb->usb.driver->name %s\n", usb->usb.driver->name);
489         pr_debug("usb->usb.device %p\n", usb->usb.device);
490         pr_debug("usb->usb.interface %p\n", usb->usb.interface);
491         pr_debug("usb->usb.pipe.isoch.in %x\n", usb->usb.pipe.isoch.in);
492         pr_debug("usb->usb.pipe.isoch.out %x\n", usb->usb.pipe.isoch.out);
493         pr_debug("usb->usb.pipe.bulk.in %x\n", usb->usb.pipe.bulk.in);
494         pr_debug("usb->usb.pipe.bulk.out %x\n", usb->usb.pipe.bulk.out);
495         pr_debug("usb->usb.pipe.interrupt.in %x\n", usb->usb.pipe.interrupt.in);
496         pr_debug("usb->usb.pipe.interrupt.out %x\n",
497                 usb->usb.pipe.interrupt.out);
498
499         pr_debug("baseband_usb_open }\n");
500         return usb;
501 }
502
503 void baseband_usb_close(struct baseband_usb *usb)
504 {
505         pr_debug("baseband_usb_close {\n");
506
507         /* check input */
508         if (!usb)
509                 return;
510
511         /* close usb driver */
512         if (usb->usb.driver) {
513                 pr_debug("close usb driver {\n");
514                 usb_deregister(usb->usb.driver);
515                 usb->usb.driver = (struct usb_driver *) 0;
516                 pr_debug("close usb driver }\n");
517         }
518
519         /* destroy semaphores */
520         memset(&usb->sem, 0, sizeof(usb->sem));
521
522         /* free baseband usb structure */
523         kfree(usb);
524
525         pr_debug("baseband_usb_close }\n");
526 }
527
528 static int baseband_usb_netdev_init(struct net_device *dev)
529 {
530         pr_debug("baseband_usb_netdev_init\n");
531         return 0;
532 }
533
534 static void baseband_usb_netdev_uninit(struct net_device *dev)
535 {
536         pr_debug("baseband_usb_netdev_uninit\n");
537 }
538
539 static int baseband_usb_netdev_open(struct net_device *dev)
540 {
541         pr_debug("baseband_usb_netdev_open\n");
542         netif_start_queue(dev);
543         return 0;
544 }
545
546 static int baseband_usb_netdev_stop(struct net_device *dev)
547 {
548         pr_debug("baseband_usb_netdev_stop\n");
549         netif_stop_queue(dev);
550         return 0;
551 }
552
553 static netdev_tx_t baseband_usb_netdev_start_xmit(
554         struct sk_buff *skb, struct net_device *dev)
555 {
556         int i;
557         struct baseband_usb *usb;
558         int err;
559
560         pr_debug("baseband_usb_netdev_start_xmit\n");
561
562         /* check input */
563         if (!skb) {
564                 pr_err("no skb\n");
565                 return NETDEV_TX_BUSY;
566         }
567         if (!dev) {
568                 pr_err("no net dev\n");
569                 return NETDEV_TX_BUSY;
570         }
571
572         /* find index of network device which is transmitting */
573         for (i = 0; i < max_intfs; i++) {
574                 if (usb_net_raw_ip_dev[i] == dev)
575                         break;
576         }
577         if (i >= max_intfs) {
578                 pr_err("unknown net dev %p\n", dev);
579                 return NETDEV_TX_BUSY;
580         }
581         usb = baseband_usb_net[i];
582
583         /* autoresume if suspended */
584         if (usb->usb.interface) {
585                 usb_autopm_get_interface_async(usb->usb.interface);
586         } else {
587                 pr_err("%s: tx get interface error\n", __func__);
588                 netif_stop_queue(dev);
589                 usb->stats.tx_errors++;
590                 return NETDEV_TX_BUSY;
591         }
592
593         /* submit tx urb */
594         err = usb_net_raw_ip_tx_urb_submit(usb, skb);
595         if (err < 0) {
596                 pr_err("%s: tx urb submit error\n", __func__);
597                 netif_stop_queue(dev);
598                 usb->stats.tx_errors++;
599                 return NETDEV_TX_BUSY;
600         }
601
602         return NETDEV_TX_OK;
603 }
604
605 static struct net_device_stats *baseband_usb_netdev_get_stats(
606                                 struct net_device *dev)
607 {
608         int i;
609         for (i = 0; i < max_intfs; i++) {
610                 if (dev == usb_net_raw_ip_dev[i]) {
611                         pr_debug("%s idx(%d)\n", __func__, i);
612                         return &baseband_usb_net[i]->stats;
613                 }
614         }
615         pr_debug("%s mismatch dev, default idx(0)\n", __func__);
616         return &baseband_usb_net[0]->stats;
617 }
618
619 static struct net_device_ops usb_net_raw_ip_ops = {
620         .ndo_init =             baseband_usb_netdev_init,
621         .ndo_uninit =           baseband_usb_netdev_uninit,
622         .ndo_open =             baseband_usb_netdev_open,
623         .ndo_stop =             baseband_usb_netdev_stop,
624         .ndo_start_xmit =       baseband_usb_netdev_start_xmit,
625         .ndo_get_stats = baseband_usb_netdev_get_stats,
626 };
627
628 static int usb_net_raw_ip_rx_urb_submit(struct baseband_usb *usb)
629 {
630         struct urb *urb;
631         void *buf;
632         int err;
633
634         pr_debug("usb_net_raw_ip_rx_urb_submit { usb %p\n", usb);
635
636         /* check input */
637         if (!usb) {
638                 pr_err("%s: !usb\n", __func__);
639                 return -EINVAL;
640         }
641         if (!usb->usb.interface) {
642                 pr_err("usb interface disconnected - not submitting rx urb\n");
643                 return -EINVAL;
644         }
645         if (usb->usb.rx_urb) {
646                 pr_err("previous urb still active\n");
647                 return -EBUSY;
648         }
649         if (!usb->urb_r || !usb->buff) {
650                 pr_err("no reusable rx urb found\n");
651                 return -ENOMEM;
652         }
653
654         /* reuse rx urb */
655         urb = usb->urb_r;
656         buf = usb->buff;
657         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.in,
658                 buf, USB_NET_BUFSIZ,
659                 usb_net_raw_ip_rx_urb_comp,
660                 usb);
661         urb->transfer_flags = 0;
662
663         /* submit rx urb */
664         usb_mark_last_busy(usb->usb.device);
665         usb->usb.rx_urb = urb;
666         err = usb_submit_urb(urb, GFP_ATOMIC);
667         if (err < 0) {
668                 pr_err("usb_submit_urb() failed - err %d\n", err);
669                 usb->usb.rx_urb = (struct urb *) 0;
670                 return err;
671         }
672
673         pr_debug("usb_net_raw_ip_rx_urb_submit }\n");
674         return err;
675 }
676
677 static void usb_net_raw_ip_rx_urb_comp(struct urb *urb)
678 {
679         struct baseband_usb *usb;
680         int i;
681         struct sk_buff *skb;
682         unsigned char *dst;
683         unsigned char ethernet_header[14] = {
684                 /* Destination MAC */
685                 0x00, 0x00,
686                 0x00, 0x00,
687                 0x00, 0x00,
688                 /* Source MAC */
689                 0x00, 0x00,
690                 0x00, 0x00,
691                 0x00, 0x00,
692                 /* EtherType */
693                 NET_IP_ETHERTYPE,
694         };
695
696         pr_debug("usb_net_raw_ip_rx_urb_comp { urb %p\n", urb);
697
698         /* check input */
699         if (!urb) {
700                 pr_err("no urb\n");
701                 return;
702         }
703         usb = (struct baseband_usb *)urb->context;
704         i = usb->baseband_index;
705         switch (urb->status) {
706         case 0:
707                 break;
708         case -ESHUTDOWN:
709                 /* fall through */
710                 pr_info("%s: rx urb %p - link shutdown %d\n",
711                         __func__, urb, urb->status);
712                 goto err_exit;
713         case -EPROTO:
714                 pr_info("%s: rx urb %p - link shutdown %d EPROTO\n",
715                         __func__, urb, urb->status);
716                 goto err_exit;
717         default:
718                 pr_info("%s: rx urb %p - status %d\n",
719                         __func__, urb, urb->status);
720                 break;
721         }
722
723         /* put rx urb data in rx buffer */
724         if (urb->actual_length > 0) {
725                 pr_debug("usb_net_raw_ip_rx_urb_comp - "
726                         "urb->actual_length %d\n", urb->actual_length);
727                 /* allocate skb with space for
728                  * - dummy ethernet header
729                  * - rx IP packet from modem
730                  */
731                 skb = netdev_alloc_skb(usb_net_raw_ip_dev[i],
732                         NET_IP_ALIGN + 14 + urb->actual_length);
733                 if (skb) {
734                         /* generate a dummy ethernet header
735                          * since modem sends IP packets without
736                          * any ethernet headers
737                          */
738                         memcpy(ethernet_header + 0,
739                                 usb_net_raw_ip_dev[i]->dev_addr, 6);
740                         memcpy(ethernet_header + 6,
741                                 "0x01\0x02\0x03\0x04\0x05\0x06", 6);
742                         /* fill skb with
743                          * - dummy ethernet header
744                          * - rx IP packet from modem
745                          */
746                         skb_reserve(skb, NET_IP_ALIGN);
747                         dst = skb_put(skb, 14);
748                         memcpy(dst, ethernet_header, 14);
749                         dst = skb_put(skb, urb->actual_length);
750                         memcpy(dst, urb->transfer_buffer, urb->actual_length);
751                         skb->protocol = eth_type_trans(skb,
752                                 usb_net_raw_ip_dev[i]);
753                         /* pass skb to network stack */
754                         if (netif_rx(skb) < 0) {
755                                 pr_err("usb_net_raw_ip_rx_urb_comp_work - "
756                                         "netif_rx(%p) failed\n", skb);
757                                 kfree_skb(skb);
758                                 usb->stats.rx_errors++;
759                         } else {
760                                 usb->stats.rx_packets++;
761                                 usb->stats.rx_bytes +=
762                                     (14 + urb->actual_length);
763                         }
764                 } else {
765                         pr_err("usb_net_raw_ip_rx_urb_comp_work - "
766                                 "netdev_alloc_skb() failed\n");
767                 }
768         }
769
770         /* mark rx urb complete */
771         usb->usb.rx_urb = (struct urb *) 0;
772
773         /* do not submit urb if interface is suspending */
774         if (urb->status == -ENOENT)
775                 return;
776
777         /* submit next rx urb */
778         usb_net_raw_ip_rx_urb_submit(usb);
779         return;
780
781 err_exit:
782         /* mark rx urb complete */
783         usb->usb.rx_urb = (struct urb *) 0;
784
785         pr_debug("usb_net_raw_ip_rx_urb_comp }\n");
786         return;
787 }
788
789 static int usb_net_raw_ip_setup_rx_urb( struct baseband_usb *usb)
790 {
791         pr_debug("usb_net_raw_ip_setup_rx_urb {\n");
792
793         /* check input */
794         if (!usb) {
795                 pr_err("%s: !usb\n", __func__);
796                 return -EINVAL;
797         }
798         if (usb->urb_r) {
799                 pr_err("%s: reusable rx urb already allocated\n", __func__);
800                 return -EINVAL;
801         }
802
803         /* allocate reusable rx urb */
804         usb->urb_r = usb_alloc_urb(0, GFP_ATOMIC);
805         if (!usb->urb_r) {
806                 pr_err("usb_alloc_urb() failed\n");
807                 return -ENOMEM;
808         }
809         usb->buff = kzalloc(USB_NET_BUFSIZ, GFP_ATOMIC);
810         if (!usb->buff) {
811                 pr_err("usb buffer kzalloc() failed\n");
812                 usb_free_urb(usb->urb_r);
813                 usb->urb_r = (struct urb *) 0;
814                 return -ENOMEM;
815         }
816
817         pr_debug("usb_net_raw_setup_ip_rx_urb }\n");
818         return 0;
819 }
820
821 static void usb_net_raw_ip_free_rx_urb(struct baseband_usb *usb)
822 {
823         pr_debug("usb_net_raw_ip_free_rx_urb {\n");
824
825         /* check input */
826         if (!usb) {
827                 pr_err("%s: !usb\n", __func__);
828                 return;
829         }
830
831         /* free reusable rx urb */
832         if (usb->urb_r) {
833                 usb_free_urb(usb->urb_r);
834                 usb->urb_r = (struct urb *) 0;
835         }
836         if (usb->buff) {
837                 kfree(usb->buff);
838                 usb->buff = (void *) 0;
839         }
840
841         pr_debug("usb_net_raw_ip_free_rx_urb }\n");
842 }
843
844 static int usb_net_raw_ip_tx_urb_submit(struct baseband_usb *usb,
845         struct sk_buff *skb)
846 {
847         struct urb *urb;
848         unsigned char *buf;
849         int err;
850
851         pr_debug("usb_net_raw_ip_tx_urb_submit {\n");
852
853         /* check input */
854         if (!usb) {
855                 pr_err("%s: !usb\n", __func__);
856                 return -EINVAL;
857         }
858         if (!usb->usb.interface) {
859                 pr_err("usb interface disconnected - not submitting tx urb\n");
860                 return -EINVAL;
861         }
862         if (!skb) {
863                 pr_err("%s: !skb\n", __func__);
864                 usb_autopm_put_interface_async(usb->usb.interface);
865                 return -EINVAL;
866         }
867
868         /* allocate urb */
869         urb = usb_alloc_urb(0, GFP_ATOMIC);
870         if (!urb) {
871                 pr_err("usb_alloc_urb() failed\n");
872                 usb_autopm_put_interface_async(usb->usb.interface);
873                 return -ENOMEM;
874         }
875         buf = kzalloc(skb->len - 14, GFP_ATOMIC);
876         if (!buf) {
877                 pr_err("usb buffer kzalloc() failed\n");
878                 usb_free_urb(urb);
879                 usb_autopm_put_interface_async(usb->usb.interface);
880                 return -ENOMEM;
881         }
882         err = skb_copy_bits(skb, 14, buf, skb->len - 14);
883         if (err < 0) {
884                 pr_err("skb_copy_bits() failed - %d\n", err);
885                 kfree(buf);
886                 usb_free_urb(urb);
887                 usb_autopm_put_interface_async(usb->usb.interface);
888                 return err;
889         }
890         usb_fill_bulk_urb(urb, usb->usb.device, usb->usb.pipe.bulk.out,
891                 buf, skb->len - 14,
892                 usb_net_raw_ip_tx_urb_comp,
893                 usb);
894         urb->transfer_flags = URB_ZERO_PACKET;
895
896         /* queue tx urb work */
897         usb_anchor_urb(urb, &usb->usb.tx_urb_deferred);
898         queue_work(usb->usb.tx_workqueue, &usb->usb.tx_work);
899
900         /* free skb */
901         consume_skb(skb);
902
903         pr_debug("usb_net_raw_ip_tx_urb_submit }\n");
904         return 0;
905 }
906
907 static void usb_net_raw_ip_tx_urb_work(struct work_struct *work)
908 {
909         struct baseband_usb *usb
910                 = container_of(work, struct baseband_usb, usb.tx_work);
911         struct urb *urb;
912         int err;
913
914         pr_debug("usb_net_raw_ip_tx_urb_work {\n");
915
916         /* check if tx urb(s) queued */
917         if (usb == NULL ||
918                 (!usb->usb.tx_urb &&
919                 usb_anchor_empty(&usb->usb.tx_urb_deferred))) {
920                 pr_debug("%s: nothing to do!\n", __func__);
921                 return;
922         }
923
924         /* check if usb interface disconnected */
925         if (!usb->usb.interface) {
926                 pr_err("%s: not submitting tx urb %p -interface disconnected\n",
927                         __func__, urb);
928                 return;
929         }
930
931         /* check if suspended */
932         if (usb->susp_count > 0) {
933                 pr_info("%s: usb->susp_count %d > 0 (suspended)\n",
934                         __func__, usb->susp_count);
935                 return;
936         }
937
938         /* submit queued tx urb(s) */
939         while ((urb = usb_get_from_anchor(&usb->usb.tx_urb_deferred))
940                 != (struct urb *) 0) {
941                 /* decrement count from usb_get_from_anchor() */
942                 usb_free_urb(urb);
943                 /* check if usb interface disconnected */
944                 if (!usb->usb.interface) {
945                         pr_err("%s: not submitting tx urb %p"
946                                 " - interface disconnected\n",
947                                 __func__, urb);
948                         if (urb->transfer_buffer) {
949                                 kfree(urb->transfer_buffer);
950                                 urb->transfer_buffer = (void *) 0;
951                         }
952                         usb_free_urb(urb);
953                         usb->stats.tx_errors++;
954                         continue;
955                 }
956                 /* autoresume before tx */
957                 usb_mark_last_busy(usb->usb.device);
958                 /* submit tx urb */
959                 err = usb_submit_urb(urb, GFP_ATOMIC);
960                 if (err < 0) {
961                         pr_err("%s: usb_submit_urb(%p) failed - err %d\n",
962                                 __func__, urb, err);
963                         usb_autopm_put_interface_async(usb->usb.interface);
964                         if (urb->transfer_buffer) {
965                                 kfree(urb->transfer_buffer);
966                                 urb->transfer_buffer = (void *) 0;
967                         }
968                         usb_free_urb(urb);
969                         usb->stats.tx_errors++;
970                         continue;
971                 }
972                 /* free tx urb
973                  * - actual urb free occurs when refcnt which was incremented
974                  *   in usb_submit_urb is decremented to 0 (usually after urb
975                  *   completion function returns)
976                  * - tx urb transfer buffer will be freed in urb completion
977                  *   function
978                  */
979                 usb_free_urb(urb);
980         }
981
982         pr_debug("usb_net_raw_ip_tx_urb_work }\n");
983 }
984
985 static void usb_net_raw_ip_tx_urb_comp(struct urb *urb)
986 {
987         struct baseband_usb *usb;
988
989         pr_debug("usb_net_raw_ip_tx_urb_comp {\n");
990
991         /* check input */
992         if (!urb) {
993                 pr_err("no urb\n");
994                 return;
995         }
996         usb = (struct baseband_usb *)urb->context;
997         switch (urb->status) {
998         case 0:
999                 break;
1000         case -ENOENT:
1001                 /* fall through */
1002         case -ESHUTDOWN:
1003                 /* fall through */
1004         case -EPROTO:
1005                 pr_info("%s: tx urb %p - link shutdown %d\n",
1006                         __func__, urb, urb->status);
1007                 usb_autopm_put_interface_async(usb->usb.interface);
1008                 goto err_exit;
1009         default:
1010                 pr_info("%s: tx urb %p - status %d\n",
1011                         __func__, urb, urb->status);
1012                 break;
1013         }
1014         if (urb->status)
1015                 usb->stats.tx_errors++;
1016         else {
1017                 usb->stats.tx_packets++;
1018                 usb->stats.tx_bytes += urb->transfer_buffer_length;
1019         }
1020
1021         /* autosuspend after tx completed */
1022         if (!usb->usb.interface) {
1023                 pr_err("%s: usb interface disconnected"
1024                         " before tx urb completed!\n",
1025                         __func__);
1026                 goto err_exit;
1027         }
1028         usb_autopm_put_interface_async(usb->usb.interface);
1029
1030 err_exit:
1031         /* free tx urb transfer buffer */
1032         if (urb->transfer_buffer) {
1033                 kfree(urb->transfer_buffer);
1034                 urb->transfer_buffer = (void *) 0;
1035         }
1036         pr_debug("usb_net_raw_ip_tx_urb_comp }\n");
1037 }
1038
1039 static int usb_net_raw_ip_init(void)
1040 {
1041         int i;
1042         int err;
1043         char name[32];
1044
1045         pr_debug("usb_net_raw_ip_init {\n");
1046
1047         /* create multiple raw-ip network devices */
1048         for (i = 0; i < max_intfs; i++) {
1049                 /* open baseband usb */
1050                 g_i = i;
1051                 baseband_usb_net[i] = baseband_usb_open(i, usb_net_raw_ip_vid,
1052                         usb_net_raw_ip_pid, usb_net_raw_ip_intf[i]);
1053                 if (!baseband_usb_net[i]) {
1054                         pr_err("cannot open baseband usb net\n");
1055                         err = -1;
1056                         goto error_exit;
1057                 }
1058                 init_usb_anchor(&baseband_usb_net[i]->usb.tx_urb_deferred);
1059                 /* register network device */
1060                 usb_net_raw_ip_dev[i] = alloc_netdev(0,
1061                         BASEBAND_USB_NET_DEV_NAME,
1062                         ether_setup);
1063                 if (!usb_net_raw_ip_dev[i]) {
1064                         pr_err("alloc_netdev() failed\n");
1065                         err = -ENOMEM;
1066                         goto error_exit;
1067                 }
1068                 usb_net_raw_ip_dev[i]->netdev_ops = &usb_net_raw_ip_ops;
1069                 usb_net_raw_ip_dev[i]->watchdog_timeo = TX_TIMEOUT;
1070                 random_ether_addr(usb_net_raw_ip_dev[i]->dev_addr);
1071                 err = register_netdev(usb_net_raw_ip_dev[i]);
1072                 if (err < 0) {
1073                         pr_err("cannot register network device - %d\n", err);
1074                         goto error_exit;
1075                 }
1076                 pr_debug("registered baseband usb network device"
1077                                 " - dev %p name %s\n", usb_net_raw_ip_dev[i],
1078                                  BASEBAND_USB_NET_DEV_NAME);
1079                 /* start usb rx */
1080                 err = usb_net_raw_ip_setup_rx_urb(baseband_usb_net[i]);
1081                 if (err < 0) {
1082                         pr_err("setup reusable rx urb failed - err %d\n", err);
1083                         goto error_exit;
1084                 }
1085                 err = usb_net_raw_ip_rx_urb_submit(baseband_usb_net[i]);
1086                 if (err < 0) {
1087                         pr_err("submit rx failed - err %d\n", err);
1088                         goto error_exit;
1089                 }
1090                 /* start usb tx */
1091                 sprintf(name, "raw_ip_tx_wq-%d",
1092                         baseband_usb_net[i]->baseband_index);
1093                 baseband_usb_net[i]->usb.tx_workqueue
1094                         = create_singlethread_workqueue(name);
1095                 if (!baseband_usb_net[i]->usb.tx_workqueue) {
1096                         pr_err("cannot create workqueue\n");
1097                         goto error_exit;
1098                 }
1099                 INIT_WORK(&baseband_usb_net[i]->usb.tx_work,
1100                         usb_net_raw_ip_tx_urb_work);
1101         }
1102
1103         pr_debug("usb_net_raw_ip_init }\n");
1104         return 0;
1105
1106 error_exit:
1107         /* destroy multiple raw-ip network devices */
1108         for (i = 0; i < max_intfs; i++) {
1109                 /* unregister network device */
1110                 if (usb_net_raw_ip_dev[i]) {
1111                         unregister_netdev(usb_net_raw_ip_dev[i]);
1112                         free_netdev(usb_net_raw_ip_dev[i]);
1113                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
1114                 }
1115                 /* close baseband usb */
1116                 if (baseband_usb_net[i]) {
1117                         /* stop usb tx */
1118                         if (baseband_usb_net[i]->usb.tx_workqueue) {
1119                                 destroy_workqueue(baseband_usb_net[i]
1120                                         ->usb.tx_workqueue);
1121                                 baseband_usb_net[i]->usb.tx_workqueue
1122                                         = (struct workqueue_struct *) 0;
1123                         }
1124                         if (baseband_usb_net[i]->usb.tx_urb) {
1125                                 usb_kill_urb(baseband_usb_net[i]->usb.tx_urb);
1126                                 baseband_usb_net[i]->usb.tx_urb
1127                                         = (struct urb *) 0;
1128                         }
1129                         /* stop usb rx */
1130                         if (baseband_usb_net[i]->usb.rx_urb) {
1131                                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
1132                                 baseband_usb_net[i]->usb.rx_urb
1133                                         = (struct urb *) 0;
1134                         }
1135                         usb_net_raw_ip_free_rx_urb(baseband_usb_net[i]);
1136                         /* close usb */
1137                         baseband_usb_close(baseband_usb_net[i]);
1138                         baseband_usb_net[i] = (struct baseband_usb *) 0;
1139                 }
1140         }
1141
1142         return err;
1143 }
1144
1145 static void usb_net_raw_ip_exit(void)
1146 {
1147         int i;
1148
1149         pr_debug("usb_net_raw_ip_exit {\n");
1150
1151         /* destroy multiple raw-ip network devices */
1152         for (i = 0; i < max_intfs; i++) {
1153                 /* unregister network device */
1154                 if (usb_net_raw_ip_dev[i]) {
1155                         unregister_netdev(usb_net_raw_ip_dev[i]);
1156                         free_netdev(usb_net_raw_ip_dev[i]);
1157                         usb_net_raw_ip_dev[i] = (struct net_device *) 0;
1158                 }
1159                 /* close baseband usb */
1160                 if (baseband_usb_net[i]) {
1161                         /* stop usb tx */
1162                         if (baseband_usb_net[i]->usb.tx_workqueue) {
1163                                 destroy_workqueue(baseband_usb_net[i]
1164                                         ->usb.tx_workqueue);
1165                                 baseband_usb_net[i]->usb.tx_workqueue
1166                                         = (struct workqueue_struct *) 0;
1167                         }
1168                         if (baseband_usb_net[i]->usb.tx_urb) {
1169                                 usb_kill_urb(baseband_usb_net[i]->usb.tx_urb);
1170                                 baseband_usb_net[i]->usb.tx_urb
1171                                         = (struct urb *) 0;
1172                         }
1173                         /* stop usb rx */
1174                         if (baseband_usb_net[i]->usb.rx_urb) {
1175                                 usb_kill_urb(baseband_usb_net[i]->usb.rx_urb);
1176                                 baseband_usb_net[i]->usb.rx_urb
1177                                         = (struct urb *) 0;
1178                         }
1179                         usb_net_raw_ip_free_rx_urb(baseband_usb_net[i]);
1180                         /* close usb */
1181                         baseband_usb_close(baseband_usb_net[i]);
1182                         baseband_usb_net[i] = (struct baseband_usb *) 0;
1183                 }
1184         }
1185
1186         pr_debug("usb_net_raw_ip_exit }\n");
1187 }
1188
1189 module_init(usb_net_raw_ip_init)
1190 module_exit(usb_net_raw_ip_exit)
1191