GNU Linux-libre 4.4.294-gnu1
[releases.git] / drivers / net / wireless / mwifiex / usb.c
1 /*
2  * Marvell Wireless LAN device driver: USB specific handling
3  *
4  * Copyright (C) 2012-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "main.h"
21 #include "usb.h"
22
23 #define USB_VERSION     "1.0"
24
25 static u8 user_rmmod;
26 static struct mwifiex_if_ops usb_ops;
27 static struct semaphore add_remove_card_sem;
28
29 static struct usb_device_id mwifiex_usb_table[] = {
30         /* 8766 */
31         {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)},
32         {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2,
33                                        USB_CLASS_VENDOR_SPEC,
34                                        USB_SUBCLASS_VENDOR_SPEC, 0xff)},
35         /* 8797 */
36         {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)},
37         {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2,
38                                        USB_CLASS_VENDOR_SPEC,
39                                        USB_SUBCLASS_VENDOR_SPEC, 0xff)},
40         /* 8801 */
41         {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)},
42         {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2,
43                                        USB_CLASS_VENDOR_SPEC,
44                                        USB_SUBCLASS_VENDOR_SPEC, 0xff)},
45         /* 8997 */
46         {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)},
47         {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2,
48                                        USB_CLASS_VENDOR_SPEC,
49                                        USB_SUBCLASS_VENDOR_SPEC, 0xff)},
50         { }     /* Terminating entry */
51 };
52
53 MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
54
55 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
56
57 /* This function handles received packet. Necessary action is taken based on
58  * cmd/event/data.
59  */
60 static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
61                             struct sk_buff *skb, u8 ep)
62 {
63         u32 recv_type;
64         __le32 tmp;
65         int ret;
66
67         if (adapter->hs_activated)
68                 mwifiex_process_hs_config(adapter);
69
70         if (skb->len < INTF_HEADER_LEN) {
71                 mwifiex_dbg(adapter, ERROR,
72                             "%s: invalid skb->len\n", __func__);
73                 return -1;
74         }
75
76         switch (ep) {
77         case MWIFIEX_USB_EP_CMD_EVENT:
78                 mwifiex_dbg(adapter, EVENT,
79                             "%s: EP_CMD_EVENT\n", __func__);
80                 skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
81                 recv_type = le32_to_cpu(tmp);
82                 skb_pull(skb, INTF_HEADER_LEN);
83
84                 switch (recv_type) {
85                 case MWIFIEX_USB_TYPE_CMD:
86                         if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
87                                 mwifiex_dbg(adapter, ERROR,
88                                             "CMD: skb->len too large\n");
89                                 ret = -1;
90                                 goto exit_restore_skb;
91                         } else if (!adapter->curr_cmd) {
92                                 mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
93                                 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
94                                         mwifiex_process_sleep_confirm_resp(
95                                                         adapter, skb->data,
96                                                         skb->len);
97                                         ret = 0;
98                                         goto exit_restore_skb;
99                                 }
100                                 ret = -1;
101                                 goto exit_restore_skb;
102                         }
103
104                         adapter->curr_cmd->resp_skb = skb;
105                         adapter->cmd_resp_received = true;
106                         break;
107                 case MWIFIEX_USB_TYPE_EVENT:
108                         if (skb->len < sizeof(u32)) {
109                                 mwifiex_dbg(adapter, ERROR,
110                                             "EVENT: skb->len too small\n");
111                                 ret = -1;
112                                 goto exit_restore_skb;
113                         }
114                         skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
115                         adapter->event_cause = le32_to_cpu(tmp);
116                         mwifiex_dbg(adapter, EVENT,
117                                     "event_cause %#x\n", adapter->event_cause);
118
119                         if (skb->len > MAX_EVENT_SIZE) {
120                                 mwifiex_dbg(adapter, ERROR,
121                                             "EVENT: event body too large\n");
122                                 ret = -1;
123                                 goto exit_restore_skb;
124                         }
125
126                         memcpy(adapter->event_body, skb->data +
127                                MWIFIEX_EVENT_HEADER_LEN, skb->len);
128
129                         adapter->event_received = true;
130                         adapter->event_skb = skb;
131                         break;
132                 default:
133                         mwifiex_dbg(adapter, ERROR,
134                                     "unknown recv_type %#x\n", recv_type);
135                         return -1;
136                 }
137                 break;
138         case MWIFIEX_USB_EP_DATA:
139                 mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
140                 if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
141                         mwifiex_dbg(adapter, ERROR,
142                                     "DATA: skb->len too large\n");
143                         return -1;
144                 }
145
146                 skb_queue_tail(&adapter->rx_data_q, skb);
147                 adapter->data_received = true;
148                 atomic_inc(&adapter->rx_pending);
149                 break;
150         default:
151                 mwifiex_dbg(adapter, ERROR,
152                             "%s: unknown endport %#x\n", __func__, ep);
153                 return -1;
154         }
155
156         return -EINPROGRESS;
157
158 exit_restore_skb:
159         /* The buffer will be reused for further cmds/events */
160         skb_push(skb, INTF_HEADER_LEN);
161
162         return ret;
163 }
164
165 static void mwifiex_usb_rx_complete(struct urb *urb)
166 {
167         struct urb_context *context = (struct urb_context *)urb->context;
168         struct mwifiex_adapter *adapter = context->adapter;
169         struct sk_buff *skb = context->skb;
170         struct usb_card_rec *card;
171         int recv_length = urb->actual_length;
172         int size, status;
173
174         if (!adapter || !adapter->card) {
175                 pr_err("mwifiex adapter or card structure is not valid\n");
176                 return;
177         }
178
179         card = (struct usb_card_rec *)adapter->card;
180         if (card->rx_cmd_ep == context->ep)
181                 atomic_dec(&card->rx_cmd_urb_pending);
182         else
183                 atomic_dec(&card->rx_data_urb_pending);
184
185         if (recv_length) {
186                 if (urb->status || (adapter->surprise_removed)) {
187                         mwifiex_dbg(adapter, ERROR,
188                                     "URB status is failed: %d\n", urb->status);
189                         /* Do not free skb in case of command ep */
190                         if (card->rx_cmd_ep != context->ep)
191                                 dev_kfree_skb_any(skb);
192                         goto setup_for_next;
193                 }
194                 if (skb->len > recv_length)
195                         skb_trim(skb, recv_length);
196                 else
197                         skb_put(skb, recv_length - skb->len);
198
199                 status = mwifiex_usb_recv(adapter, skb, context->ep);
200
201                 mwifiex_dbg(adapter, INFO,
202                             "info: recv_length=%d, status=%d\n",
203                             recv_length, status);
204                 if (status == -EINPROGRESS) {
205                         mwifiex_queue_main_work(adapter);
206
207                         /* urb for data_ep is re-submitted now;
208                          * urb for cmd_ep will be re-submitted in callback
209                          * mwifiex_usb_recv_complete
210                          */
211                         if (card->rx_cmd_ep == context->ep)
212                                 return;
213                 } else {
214                         if (status == -1)
215                                 mwifiex_dbg(adapter, ERROR,
216                                             "received data processing failed!\n");
217
218                         /* Do not free skb in case of command ep */
219                         if (card->rx_cmd_ep != context->ep)
220                                 dev_kfree_skb_any(skb);
221                 }
222         } else if (urb->status) {
223                 if (!adapter->is_suspended) {
224                         mwifiex_dbg(adapter, FATAL,
225                                     "Card is removed: %d\n", urb->status);
226                         adapter->surprise_removed = true;
227                 }
228                 dev_kfree_skb_any(skb);
229                 return;
230         } else {
231                 /* Do not free skb in case of command ep */
232                 if (card->rx_cmd_ep != context->ep)
233                         dev_kfree_skb_any(skb);
234
235                 /* fall through setup_for_next */
236         }
237
238 setup_for_next:
239         if (card->rx_cmd_ep == context->ep)
240                 size = MWIFIEX_RX_CMD_BUF_SIZE;
241         else
242                 size = MWIFIEX_RX_DATA_BUF_SIZE;
243
244         if (card->rx_cmd_ep == context->ep) {
245                 mwifiex_usb_submit_rx_urb(context, size);
246         } else {
247                 if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING){
248                         mwifiex_usb_submit_rx_urb(context, size);
249                 }else{
250                         context->skb = NULL;
251                 }
252         }
253
254         return;
255 }
256
257 static void mwifiex_usb_tx_complete(struct urb *urb)
258 {
259         struct urb_context *context = (struct urb_context *)(urb->context);
260         struct mwifiex_adapter *adapter = context->adapter;
261         struct usb_card_rec *card = adapter->card;
262         struct usb_tx_data_port *port;
263         int i;
264
265         mwifiex_dbg(adapter, INFO,
266                     "%s: status: %d\n", __func__, urb->status);
267
268         if (context->ep == card->tx_cmd_ep) {
269                 mwifiex_dbg(adapter, CMD,
270                             "%s: CMD\n", __func__);
271                 atomic_dec(&card->tx_cmd_urb_pending);
272                 adapter->cmd_sent = false;
273         } else {
274                 mwifiex_dbg(adapter, DATA,
275                             "%s: DATA\n", __func__);
276                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
277                         port = &card->port[i];
278                         if (context->ep == port->tx_data_ep) {
279                                 atomic_dec(&port->tx_data_urb_pending);
280                                 port->block_status = false;
281                                 break;
282                         }
283                 }
284                 adapter->data_sent = false;
285                 mwifiex_write_data_complete(adapter, context->skb, 0,
286                                             urb->status ? -1 : 0);
287         }
288
289         if (card->mc_resync_flag)
290                 mwifiex_multi_chan_resync(adapter);
291
292         mwifiex_queue_main_work(adapter);
293
294         return;
295 }
296
297 static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
298 {
299         struct mwifiex_adapter *adapter = ctx->adapter;
300         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
301
302         if (card->rx_cmd_ep != ctx->ep) {
303                 ctx->skb = dev_alloc_skb(size);
304                 if (!ctx->skb) {
305                         mwifiex_dbg(adapter, ERROR,
306                                     "%s: dev_alloc_skb failed\n", __func__);
307                         return -ENOMEM;
308                 }
309         }
310
311         usb_fill_bulk_urb(ctx->urb, card->udev,
312                           usb_rcvbulkpipe(card->udev, ctx->ep), ctx->skb->data,
313                           size, mwifiex_usb_rx_complete, (void *)ctx);
314
315         if (card->rx_cmd_ep == ctx->ep)
316                 atomic_inc(&card->rx_cmd_urb_pending);
317         else
318                 atomic_inc(&card->rx_data_urb_pending);
319
320         if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
321                 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
322                 dev_kfree_skb_any(ctx->skb);
323                 ctx->skb = NULL;
324
325                 if (card->rx_cmd_ep == ctx->ep)
326                         atomic_dec(&card->rx_cmd_urb_pending);
327                 else
328                         atomic_dec(&card->rx_data_urb_pending);
329
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 static void mwifiex_usb_free(struct usb_card_rec *card)
337 {
338         struct usb_tx_data_port *port;
339         int i, j;
340
341         if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
342                 usb_kill_urb(card->rx_cmd.urb);
343
344         usb_free_urb(card->rx_cmd.urb);
345         card->rx_cmd.urb = NULL;
346
347         if (atomic_read(&card->rx_data_urb_pending))
348                 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
349                         if (card->rx_data_list[i].urb)
350                                 usb_kill_urb(card->rx_data_list[i].urb);
351
352         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
353                 usb_free_urb(card->rx_data_list[i].urb);
354                 card->rx_data_list[i].urb = NULL;
355         }
356
357         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
358                 port = &card->port[i];
359                 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
360                         usb_free_urb(port->tx_data_list[j].urb);
361                         port->tx_data_list[j].urb = NULL;
362                 }
363         }
364
365         usb_free_urb(card->tx_cmd.urb);
366         card->tx_cmd.urb = NULL;
367
368         return;
369 }
370
371 /* This function probes an mwifiex device and registers it. It allocates
372  * the card structure, initiates the device registration and initialization
373  * procedure by adding a logical interface.
374  */
375 static int mwifiex_usb_probe(struct usb_interface *intf,
376                              const struct usb_device_id *id)
377 {
378         struct usb_device *udev = interface_to_usbdev(intf);
379         struct usb_host_interface *iface_desc = intf->cur_altsetting;
380         struct usb_endpoint_descriptor *epd;
381         int ret, i;
382         struct usb_card_rec *card;
383         u16 id_vendor, id_product, bcd_device, bcd_usb;
384
385         card = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
386         if (!card)
387                 return -ENOMEM;
388
389         id_vendor = le16_to_cpu(udev->descriptor.idVendor);
390         id_product = le16_to_cpu(udev->descriptor.idProduct);
391         bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
392         bcd_usb = le16_to_cpu(udev->descriptor.bcdUSB);
393         pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
394                  id_vendor, id_product, bcd_device);
395
396         /* PID_1 is used for firmware downloading only */
397         switch (id_product) {
398         case USB8766_PID_1:
399         case USB8797_PID_1:
400         case USB8801_PID_1:
401         case USB8997_PID_1:
402                 card->usb_boot_state = USB8XXX_FW_DNLD;
403                 break;
404         case USB8766_PID_2:
405         case USB8797_PID_2:
406         case USB8801_PID_2:
407         case USB8997_PID_2:
408                 card->usb_boot_state = USB8XXX_FW_READY;
409                 break;
410         default:
411                 pr_warn("unknown id_product %#x\n", id_product);
412                 card->usb_boot_state = USB8XXX_FW_DNLD;
413                 break;
414         }
415
416         card->udev = udev;
417         card->intf = intf;
418
419         pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
420                  udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass,
421                  udev->descriptor.bDeviceSubClass,
422                  udev->descriptor.bDeviceProtocol);
423
424         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
425                 epd = &iface_desc->endpoint[i].desc;
426                 if (usb_endpoint_dir_in(epd) &&
427                     usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
428                     usb_endpoint_xfer_bulk(epd)) {
429                         pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
430                                  le16_to_cpu(epd->wMaxPacketSize),
431                                  epd->bEndpointAddress);
432                         card->rx_cmd_ep = usb_endpoint_num(epd);
433                         atomic_set(&card->rx_cmd_urb_pending, 0);
434                 }
435                 if (usb_endpoint_dir_in(epd) &&
436                     usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
437                     usb_endpoint_xfer_bulk(epd)) {
438                         pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
439                                  le16_to_cpu(epd->wMaxPacketSize),
440                                  epd->bEndpointAddress);
441                         card->rx_data_ep = usb_endpoint_num(epd);
442                         atomic_set(&card->rx_data_urb_pending, 0);
443                 }
444                 if (usb_endpoint_dir_out(epd) &&
445                     usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
446                     usb_endpoint_xfer_bulk(epd)) {
447                         pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
448                                  le16_to_cpu(epd->wMaxPacketSize),
449                                  epd->bEndpointAddress);
450                         card->port[0].tx_data_ep = usb_endpoint_num(epd);
451                         atomic_set(&card->port[0].tx_data_urb_pending, 0);
452                 }
453                 if (usb_endpoint_dir_out(epd) &&
454                     usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 &&
455                     usb_endpoint_xfer_bulk(epd)) {
456                         pr_debug("info: bulk OUT chan2:\t"
457                                  "max pkt size: %d, addr: %d\n",
458                                  le16_to_cpu(epd->wMaxPacketSize),
459                                  epd->bEndpointAddress);
460                         card->port[1].tx_data_ep = usb_endpoint_num(epd);
461                         atomic_set(&card->port[1].tx_data_urb_pending, 0);
462                 }
463                 if (usb_endpoint_dir_out(epd) &&
464                     usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
465                     usb_endpoint_xfer_bulk(epd)) {
466                         pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
467                                  le16_to_cpu(epd->wMaxPacketSize),
468                                  epd->bEndpointAddress);
469                         card->tx_cmd_ep = usb_endpoint_num(epd);
470                         atomic_set(&card->tx_cmd_urb_pending, 0);
471                         card->bulk_out_maxpktsize =
472                                         le16_to_cpu(epd->wMaxPacketSize);
473                 }
474         }
475
476         switch (card->usb_boot_state) {
477         case USB8XXX_FW_DNLD:
478                 /* Reject broken descriptors. */
479                 if (!card->rx_cmd_ep || !card->tx_cmd_ep)
480                         return -ENODEV;
481                 if (card->bulk_out_maxpktsize == 0)
482                         return -ENODEV;
483                 break;
484         case USB8XXX_FW_READY:
485                 /* Assume the driver can handle missing endpoints for now. */
486                 break;
487         default:
488                 WARN_ON(1);
489                 return -ENODEV;
490         }
491
492         usb_set_intfdata(intf, card);
493
494         ret = mwifiex_add_card(card, &add_remove_card_sem, &usb_ops,
495                                MWIFIEX_USB);
496         if (ret) {
497                 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
498                 usb_reset_device(udev);
499                 kfree(card);
500                 return ret;
501         }
502
503         usb_get_dev(udev);
504
505         return 0;
506 }
507
508 /* Kernel needs to suspend all functions separately. Therefore all
509  * registered functions must have drivers with suspend and resume
510  * methods. Failing that the kernel simply removes the whole card.
511  *
512  * If already not suspended, this function allocates and sends a
513  * 'host sleep activate' request to the firmware and turns off the traffic.
514  */
515 static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
516 {
517         struct usb_card_rec *card = usb_get_intfdata(intf);
518         struct mwifiex_adapter *adapter;
519         struct usb_tx_data_port *port;
520         int i, j;
521
522         if (!card || !card->adapter) {
523                 pr_err("%s: card or card->adapter is NULL\n", __func__);
524                 return 0;
525         }
526         adapter = card->adapter;
527
528         if (unlikely(adapter->is_suspended))
529                 mwifiex_dbg(adapter, WARN,
530                             "Device already suspended\n");
531
532         mwifiex_enable_hs(adapter);
533
534         /* 'is_suspended' flag indicates device is suspended.
535          * It must be set here before the usb_kill_urb() calls. Reason
536          * is in the complete handlers, urb->status(= -ENOENT) and
537          * this flag is used in combination to distinguish between a
538          * 'suspended' state and a 'disconnect' one.
539          */
540         adapter->is_suspended = true;
541         adapter->hs_enabling = false;
542
543         if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
544                 usb_kill_urb(card->rx_cmd.urb);
545
546         if (atomic_read(&card->rx_data_urb_pending))
547                 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
548                         if (card->rx_data_list[i].urb)
549                                 usb_kill_urb(card->rx_data_list[i].urb);
550
551         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
552                 port = &card->port[i];
553                 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
554                         if (port->tx_data_list[j].urb)
555                                 usb_kill_urb(port->tx_data_list[j].urb);
556                 }
557         }
558
559         if (card->tx_cmd.urb)
560                 usb_kill_urb(card->tx_cmd.urb);
561
562         return 0;
563 }
564
565 /* Kernel needs to suspend all functions separately. Therefore all
566  * registered functions must have drivers with suspend and resume
567  * methods. Failing that the kernel simply removes the whole card.
568  *
569  * If already not resumed, this function turns on the traffic and
570  * sends a 'host sleep cancel' request to the firmware.
571  */
572 static int mwifiex_usb_resume(struct usb_interface *intf)
573 {
574         struct usb_card_rec *card = usb_get_intfdata(intf);
575         struct mwifiex_adapter *adapter;
576         int i;
577
578         if (!card || !card->adapter) {
579                 pr_err("%s: card or card->adapter is NULL\n", __func__);
580                 return 0;
581         }
582         adapter = card->adapter;
583
584         if (unlikely(!adapter->is_suspended)) {
585                 mwifiex_dbg(adapter, WARN,
586                             "Device already resumed\n");
587                 return 0;
588         }
589
590         /* Indicate device resumed. The netdev queue will be resumed only
591          * after the urbs have been re-submitted
592          */
593         adapter->is_suspended = false;
594
595         if (!atomic_read(&card->rx_data_urb_pending))
596                 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
597                         mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
598                                                   MWIFIEX_RX_DATA_BUF_SIZE);
599
600         if (!atomic_read(&card->rx_cmd_urb_pending)) {
601                 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
602                 if (card->rx_cmd.skb)
603                         mwifiex_usb_submit_rx_urb(&card->rx_cmd,
604                                                   MWIFIEX_RX_CMD_BUF_SIZE);
605         }
606
607         /* Disable Host Sleep */
608         if (adapter->hs_activated)
609                 mwifiex_cancel_hs(mwifiex_get_priv(adapter,
610                                                    MWIFIEX_BSS_ROLE_ANY),
611                                   MWIFIEX_ASYNC_CMD);
612
613         return 0;
614 }
615
616 static void mwifiex_usb_disconnect(struct usb_interface *intf)
617 {
618         struct usb_card_rec *card = usb_get_intfdata(intf);
619         struct mwifiex_adapter *adapter;
620
621         if (!card || !card->adapter) {
622                 pr_err("%s: card or card->adapter is NULL\n", __func__);
623                 return;
624         }
625
626         adapter = card->adapter;
627         if (!adapter->priv_num)
628                 return;
629
630         if (user_rmmod) {
631 #ifdef CONFIG_PM
632                 if (adapter->is_suspended)
633                         mwifiex_usb_resume(intf);
634 #endif
635
636                 mwifiex_deauthenticate_all(adapter);
637
638                 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
639                                                           MWIFIEX_BSS_ROLE_ANY),
640                                          MWIFIEX_FUNC_SHUTDOWN);
641         }
642
643         if (adapter->workqueue)
644                 flush_workqueue(adapter->workqueue);
645
646         mwifiex_usb_free(card);
647
648         mwifiex_dbg(adapter, FATAL,
649                     "%s: removing card\n", __func__);
650         mwifiex_remove_card(adapter, &add_remove_card_sem);
651
652         usb_set_intfdata(intf, NULL);
653         usb_put_dev(interface_to_usbdev(intf));
654         kfree(card);
655
656         return;
657 }
658
659 static struct usb_driver mwifiex_usb_driver = {
660         .name = "mwifiex_usb",
661         .probe = mwifiex_usb_probe,
662         .disconnect = mwifiex_usb_disconnect,
663         .id_table = mwifiex_usb_table,
664         .suspend = mwifiex_usb_suspend,
665         .resume = mwifiex_usb_resume,
666         .soft_unbind = 1,
667 };
668
669 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
670 {
671         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
672         struct usb_tx_data_port *port;
673         int i, j;
674
675         card->tx_cmd.adapter = adapter;
676         card->tx_cmd.ep = card->tx_cmd_ep;
677
678         card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
679         if (!card->tx_cmd.urb) {
680                 mwifiex_dbg(adapter, ERROR,
681                             "tx_cmd.urb allocation failed\n");
682                 return -ENOMEM;
683         }
684
685         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
686                 port = &card->port[i];
687                 if (!port->tx_data_ep)
688                         continue;
689                 port->tx_data_ix = 0;
690                 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
691                         port->block_status = false;
692                 else
693                         port->block_status = true;
694                 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
695                         port->tx_data_list[j].adapter = adapter;
696                         port->tx_data_list[j].ep = port->tx_data_ep;
697                         port->tx_data_list[j].urb =
698                                         usb_alloc_urb(0, GFP_KERNEL);
699                         if (!port->tx_data_list[j].urb) {
700                                 mwifiex_dbg(adapter, ERROR,
701                                             "urb allocation failed\n");
702                                 return -ENOMEM;
703                         }
704                 }
705         }
706
707         return 0;
708 }
709
710 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
711 {
712         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
713         int i;
714
715         card->rx_cmd.adapter = adapter;
716         card->rx_cmd.ep = card->rx_cmd_ep;
717
718         card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
719         if (!card->rx_cmd.urb) {
720                 mwifiex_dbg(adapter, ERROR, "rx_cmd.urb allocation failed\n");
721                 return -ENOMEM;
722         }
723
724         card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
725         if (!card->rx_cmd.skb)
726                 return -ENOMEM;
727
728         if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
729                 return -1;
730
731         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
732                 card->rx_data_list[i].adapter = adapter;
733                 card->rx_data_list[i].ep = card->rx_data_ep;
734
735                 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
736                 if (!card->rx_data_list[i].urb) {
737                         mwifiex_dbg(adapter, ERROR,
738                                     "rx_data_list[] urb allocation failed\n");
739                         return -1;
740                 }
741                 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
742                                               MWIFIEX_RX_DATA_BUF_SIZE))
743                         return -1;
744         }
745
746         return 0;
747 }
748
749 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
750                                    u32 *len, u8 ep, u32 timeout)
751 {
752         struct usb_card_rec *card = adapter->card;
753         int actual_length, ret;
754
755         if (!(*len % card->bulk_out_maxpktsize))
756                 (*len)++;
757
758         /* Send the data block */
759         ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
760                            *len, &actual_length, timeout);
761         if (ret) {
762                 mwifiex_dbg(adapter, ERROR,
763                             "usb_bulk_msg for tx failed: %d\n", ret);
764                 return ret;
765         }
766
767         *len = actual_length;
768
769         return ret;
770 }
771
772 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
773                                   u32 *len, u8 ep, u32 timeout)
774 {
775         struct usb_card_rec *card = adapter->card;
776         int actual_length, ret;
777
778         /* Receive the data response */
779         ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
780                            *len, &actual_length, timeout);
781         if (ret) {
782                 mwifiex_dbg(adapter, ERROR,
783                             "usb_bulk_msg for rx failed: %d\n", ret);
784                 return ret;
785         }
786
787         *len = actual_length;
788
789         return ret;
790 }
791
792 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
793 {
794         struct usb_card_rec *card = adapter->card;
795         u8 active_port = MWIFIEX_USB_EP_DATA;
796         struct mwifiex_private *priv = NULL;
797         int i;
798
799         if (adapter->usb_mc_status) {
800                 for (i = 0; i < adapter->priv_num; i++) {
801                         priv = adapter->priv[i];
802                         if (!priv)
803                                 continue;
804                         if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
805                              !priv->bss_started) ||
806                             (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
807                              !priv->media_connected))
808                                 priv->usb_port = MWIFIEX_USB_EP_DATA;
809                 }
810                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
811                         card->port[i].block_status = false;
812         } else {
813                 for (i = 0; i < adapter->priv_num; i++) {
814                         priv = adapter->priv[i];
815                         if (!priv)
816                                 continue;
817                         if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
818                              priv->bss_started) ||
819                             (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
820                              priv->media_connected)) {
821                                 active_port = priv->usb_port;
822                                 break;
823                         }
824                 }
825                 for (i = 0; i < adapter->priv_num; i++) {
826                         priv = adapter->priv[i];
827                         if (priv)
828                                 priv->usb_port = active_port;
829                 }
830                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
831                         if (active_port == card->port[i].tx_data_ep)
832                                 card->port[i].block_status = false;
833                         else
834                                 card->port[i].block_status = true;
835                 }
836         }
837 }
838
839 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
840 {
841         struct usb_card_rec *card = priv->adapter->card;
842         int idx;
843
844         for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
845                 if (priv->usb_port == card->port[idx].tx_data_ep)
846                         return !card->port[idx].block_status;
847         }
848
849         return false;
850 }
851
852 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
853 {
854         struct usb_card_rec *card = adapter->card;
855         int i;
856
857         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
858                 if (!card->port[i].block_status)
859                         return false;
860
861         return true;
862 }
863
864 /* This function write a command/data packet to card. */
865 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
866                                     struct sk_buff *skb,
867                                     struct mwifiex_tx_param *tx_param)
868 {
869         struct usb_card_rec *card = adapter->card;
870         struct urb_context *context = NULL;
871         struct usb_tx_data_port *port = NULL;
872         u8 *data = (u8 *)skb->data;
873         struct urb *tx_urb;
874         int idx, ret;
875
876         if (adapter->is_suspended) {
877                 mwifiex_dbg(adapter, ERROR,
878                             "%s: not allowed while suspended\n", __func__);
879                 return -1;
880         }
881
882         if (adapter->surprise_removed) {
883                 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
884                 return -1;
885         }
886
887         mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
888
889         if (ep == card->tx_cmd_ep) {
890                 context = &card->tx_cmd;
891         } else {
892                 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
893                         if (ep == card->port[idx].tx_data_ep) {
894                                 port = &card->port[idx];
895                                 if (atomic_read(&port->tx_data_urb_pending)
896                                     >= MWIFIEX_TX_DATA_URB) {
897                                         port->block_status = true;
898                                         ret = -EBUSY;
899                                         goto done;
900                                 }
901                                 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
902                                         port->tx_data_ix = 0;
903                                 context =
904                                         &port->tx_data_list[port->tx_data_ix++];
905                                 break;
906                         }
907                 }
908                 if (!port) {
909                         mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
910                         return -1;
911                 }
912         }
913
914         context->adapter = adapter;
915         context->ep = ep;
916         context->skb = skb;
917         tx_urb = context->urb;
918
919         usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
920                           data, skb->len, mwifiex_usb_tx_complete,
921                           (void *)context);
922
923         tx_urb->transfer_flags |= URB_ZERO_PACKET;
924
925         if (ep == card->tx_cmd_ep)
926                 atomic_inc(&card->tx_cmd_urb_pending);
927         else
928                 atomic_inc(&port->tx_data_urb_pending);
929
930         if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
931                 mwifiex_dbg(adapter, ERROR,
932                             "%s: usb_submit_urb failed\n", __func__);
933                 if (ep == card->tx_cmd_ep) {
934                         atomic_dec(&card->tx_cmd_urb_pending);
935                 } else {
936                         atomic_dec(&port->tx_data_urb_pending);
937                         port->block_status = false;
938                         if (port->tx_data_ix)
939                                 port->tx_data_ix--;
940                         else
941                                 port->tx_data_ix = MWIFIEX_TX_DATA_URB;
942                 }
943
944                 return -1;
945         } else {
946                 if (ep != card->tx_cmd_ep &&
947                     atomic_read(&port->tx_data_urb_pending) ==
948                                                         MWIFIEX_TX_DATA_URB) {
949                         port->block_status = true;
950                         ret = -ENOSR;
951                         goto done;
952                 }
953         }
954
955         return -EINPROGRESS;
956
957 done:
958         if (ep != card->tx_cmd_ep)
959                 adapter->data_sent = mwifiex_usb_data_sent(adapter);
960
961         return ret;
962 }
963
964 /* This function register usb device and initialize parameter. */
965 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
966 {
967         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
968
969         card->adapter = adapter;
970         adapter->dev = &card->udev->dev;
971
972         switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
973         case USB8997_PID_1:
974         case USB8997_PID_2:
975                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
976                 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
977                 adapter->ext_scan = true;
978                 break;
979         case USB8766_PID_1:
980         case USB8766_PID_2:
981                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
982                 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
983                 adapter->ext_scan = true;
984                 break;
985         case USB8801_PID_1:
986         case USB8801_PID_2:
987                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
988                 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
989                 adapter->ext_scan = false;
990                 break;
991         case USB8797_PID_1:
992         case USB8797_PID_2:
993         default:
994                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
995                 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
996                 break;
997         }
998
999         adapter->usb_mc_status = false;
1000         adapter->usb_mc_setup = false;
1001
1002         return 0;
1003 }
1004
1005 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1006 {
1007         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1008
1009         card->adapter = NULL;
1010 }
1011
1012 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1013                                     struct mwifiex_fw_image *fw)
1014 {
1015         int ret = 0;
1016         u8 *firmware = fw->fw_buf, *recv_buff;
1017         u32 retries = USB8XXX_FW_MAX_RETRY, dlen;
1018         u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1019         struct fw_data *fwdata;
1020         struct fw_sync_header sync_fw;
1021         u8 check_winner = 1;
1022
1023         if (!firmware) {
1024                 mwifiex_dbg(adapter, ERROR,
1025                             "No firmware image found! Terminating download\n");
1026                 ret = -1;
1027                 goto fw_exit;
1028         }
1029
1030         /* Allocate memory for transmit */
1031         fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1032         if (!fwdata) {
1033                 ret = -ENOMEM;
1034                 goto fw_exit;
1035         }
1036
1037         /* Allocate memory for receive */
1038         recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1039         if (!recv_buff)
1040                 goto cleanup;
1041
1042         do {
1043                 /* Send pseudo data to check winner status first */
1044                 if (check_winner) {
1045                         memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1046                         dlen = 0;
1047                 } else {
1048                         /* copy the header of the fw_data to get the length */
1049                         memcpy(&fwdata->fw_hdr, &firmware[tlen],
1050                                sizeof(struct fw_header));
1051
1052                         dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1053                         dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1054                         tlen += sizeof(struct fw_header);
1055
1056                         memcpy(fwdata->data, &firmware[tlen], dlen);
1057
1058                         fwdata->seq_num = cpu_to_le32(fw_seqnum);
1059                         tlen += dlen;
1060                 }
1061
1062                 /* If the send/receive fails or CRC occurs then retry */
1063                 while (retries--) {
1064                         u8 *buf = (u8 *)fwdata;
1065                         u32 len = FW_DATA_XMIT_SIZE;
1066
1067                         /* send the firmware block */
1068                         ret = mwifiex_write_data_sync(adapter, buf, &len,
1069                                                 MWIFIEX_USB_EP_CMD_EVENT,
1070                                                 MWIFIEX_USB_TIMEOUT);
1071                         if (ret) {
1072                                 mwifiex_dbg(adapter, ERROR,
1073                                             "write_data_sync: failed: %d\n",
1074                                             ret);
1075                                 continue;
1076                         }
1077
1078                         buf = recv_buff;
1079                         len = FW_DNLD_RX_BUF_SIZE;
1080
1081                         /* Receive the firmware block response */
1082                         ret = mwifiex_read_data_sync(adapter, buf, &len,
1083                                                 MWIFIEX_USB_EP_CMD_EVENT,
1084                                                 MWIFIEX_USB_TIMEOUT);
1085                         if (ret) {
1086                                 mwifiex_dbg(adapter, ERROR,
1087                                             "read_data_sync: failed: %d\n",
1088                                             ret);
1089                                 continue;
1090                         }
1091
1092                         memcpy(&sync_fw, recv_buff,
1093                                sizeof(struct fw_sync_header));
1094
1095                         /* check 1st firmware block resp for highest bit set */
1096                         if (check_winner) {
1097                                 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1098                                         mwifiex_dbg(adapter, WARN,
1099                                                     "USB is not the winner %#x\n",
1100                                                     sync_fw.cmd);
1101
1102                                         /* returning success */
1103                                         ret = 0;
1104                                         goto cleanup;
1105                                 }
1106
1107                                 mwifiex_dbg(adapter, MSG,
1108                                             "start to download FW...\n");
1109
1110                                 check_winner = 0;
1111                                 break;
1112                         }
1113
1114                         /* check the firmware block response for CRC errors */
1115                         if (sync_fw.cmd) {
1116                                 mwifiex_dbg(adapter, ERROR,
1117                                             "FW received block with CRC %#x\n",
1118                                             sync_fw.cmd);
1119                                 ret = -1;
1120                                 continue;
1121                         }
1122
1123                         retries = USB8XXX_FW_MAX_RETRY;
1124                         break;
1125                 }
1126                 fw_seqnum++;
1127         } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1128
1129 cleanup:
1130         mwifiex_dbg(adapter, MSG,
1131                     "info: FW download over, size %d bytes\n", tlen);
1132
1133         kfree(recv_buff);
1134         kfree(fwdata);
1135
1136         if (retries)
1137                 ret = 0;
1138 fw_exit:
1139         return ret;
1140 }
1141
1142 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1143                         struct mwifiex_fw_image *fw)
1144 {
1145         int ret;
1146         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1147
1148         if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1149                 ret = mwifiex_prog_fw_w_helper(adapter, fw);
1150                 if (ret)
1151                         return -1;
1152
1153                 /* Boot state changes after successful firmware download */
1154                 if (card->usb_boot_state == USB8XXX_FW_DNLD)
1155                         return -1;
1156         }
1157
1158         ret = mwifiex_usb_rx_init(adapter);
1159         if (!ret)
1160                 ret = mwifiex_usb_tx_init(adapter);
1161
1162         return ret;
1163 }
1164
1165 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1166 {
1167         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1168
1169         skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1170         if ((ep == card->rx_cmd_ep) &&
1171             (!atomic_read(&card->rx_cmd_urb_pending)))
1172                 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1173                                           MWIFIEX_RX_CMD_BUF_SIZE);
1174
1175         return;
1176 }
1177
1178 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1179                                        struct sk_buff *skb)
1180 {
1181         mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1182
1183         return 0;
1184 }
1185
1186 /* This function wakes up the card. */
1187 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1188 {
1189         /* Simulation of HS_AWAKE event */
1190         adapter->pm_wakeup_fw_try = false;
1191         del_timer(&adapter->wakeup_timer);
1192         adapter->pm_wakeup_card_req = false;
1193         adapter->ps_state = PS_STATE_AWAKE;
1194
1195         return 0;
1196 }
1197
1198 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1199 {
1200         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1201         int i;
1202         struct urb_context *ctx;
1203
1204         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1205                 if (card->rx_data_list[i].skb)
1206                         continue;
1207                 ctx = &card->rx_data_list[i];
1208                 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1209         }
1210 }
1211
1212 /* This function is called after the card has woken up. */
1213 static inline int
1214 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1215 {
1216         return 0;
1217 }
1218
1219 static struct mwifiex_if_ops usb_ops = {
1220         .register_dev =         mwifiex_register_dev,
1221         .unregister_dev =       mwifiex_unregister_dev,
1222         .wakeup =               mwifiex_pm_wakeup_card,
1223         .wakeup_complete =      mwifiex_pm_wakeup_card_complete,
1224
1225         /* USB specific */
1226         .dnld_fw =              mwifiex_usb_dnld_fw,
1227         .cmdrsp_complete =      mwifiex_usb_cmd_event_complete,
1228         .event_complete =       mwifiex_usb_cmd_event_complete,
1229         .host_to_card =         mwifiex_usb_host_to_card,
1230         .submit_rem_rx_urbs =   mwifiex_usb_submit_rem_rx_urbs,
1231         .multi_port_resync =    mwifiex_usb_port_resync,
1232         .is_port_ready =        mwifiex_usb_is_port_ready,
1233 };
1234
1235 /* This function initializes the USB driver module.
1236  *
1237  * This initiates the semaphore and registers the device with
1238  * USB bus.
1239  */
1240 static int mwifiex_usb_init_module(void)
1241 {
1242         int ret;
1243
1244         pr_debug("Marvell USB8797 Driver\n");
1245
1246         sema_init(&add_remove_card_sem, 1);
1247
1248         ret = usb_register(&mwifiex_usb_driver);
1249         if (ret)
1250                 pr_err("Driver register failed!\n");
1251         else
1252                 pr_debug("info: Driver registered successfully!\n");
1253
1254         return ret;
1255 }
1256
1257 /* This function cleans up the USB driver.
1258  *
1259  * The following major steps are followed in .disconnect for cleanup:
1260  *      - Resume the device if its suspended
1261  *      - Disconnect the device if connected
1262  *      - Shutdown the firmware
1263  *      - Unregister the device from USB bus.
1264  */
1265 static void mwifiex_usb_cleanup_module(void)
1266 {
1267         if (!down_interruptible(&add_remove_card_sem))
1268                 up(&add_remove_card_sem);
1269
1270         /* set the flag as user is removing this module */
1271         user_rmmod = 1;
1272
1273         usb_deregister(&mwifiex_usb_driver);
1274 }
1275
1276 module_init(mwifiex_usb_init_module);
1277 module_exit(mwifiex_usb_cleanup_module);
1278
1279 MODULE_AUTHOR("Marvell International Ltd.");
1280 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1281 MODULE_VERSION(USB_VERSION);
1282 MODULE_LICENSE("GPL v2");
1283 /*(DEBLOBBED)*/