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