GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / net / wireless / marvell / 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                 mwifiex_write_data_complete(adapter, context->skb, 0,
278                                             urb->status ? -1 : 0);
279                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
280                         port = &card->port[i];
281                         if (context->ep == port->tx_data_ep) {
282                                 atomic_dec(&port->tx_data_urb_pending);
283                                 port->block_status = false;
284                                 break;
285                         }
286                 }
287                 adapter->data_sent = false;
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 && !adapter->mfg_mode) {
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                 return -ENOMEM;
682
683         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
684                 port = &card->port[i];
685                 if (!port->tx_data_ep)
686                         continue;
687                 port->tx_data_ix = 0;
688                 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
689                         port->block_status = false;
690                 else
691                         port->block_status = true;
692                 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
693                         port->tx_data_list[j].adapter = adapter;
694                         port->tx_data_list[j].ep = port->tx_data_ep;
695                         port->tx_data_list[j].urb =
696                                         usb_alloc_urb(0, GFP_KERNEL);
697                         if (!port->tx_data_list[j].urb)
698                                 return -ENOMEM;
699                 }
700         }
701
702         return 0;
703 }
704
705 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
706 {
707         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
708         int i;
709
710         card->rx_cmd.adapter = adapter;
711         card->rx_cmd.ep = card->rx_cmd_ep;
712
713         card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
714         if (!card->rx_cmd.urb)
715                 return -ENOMEM;
716
717         card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
718         if (!card->rx_cmd.skb)
719                 return -ENOMEM;
720
721         if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
722                 return -1;
723
724         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
725                 card->rx_data_list[i].adapter = adapter;
726                 card->rx_data_list[i].ep = card->rx_data_ep;
727
728                 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
729                 if (!card->rx_data_list[i].urb)
730                         return -1;
731                 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
732                                               MWIFIEX_RX_DATA_BUF_SIZE))
733                         return -1;
734         }
735
736         return 0;
737 }
738
739 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
740                                    u32 *len, u8 ep, u32 timeout)
741 {
742         struct usb_card_rec *card = adapter->card;
743         int actual_length, ret;
744
745         if (!(*len % card->bulk_out_maxpktsize))
746                 (*len)++;
747
748         /* Send the data block */
749         ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
750                            *len, &actual_length, timeout);
751         if (ret) {
752                 mwifiex_dbg(adapter, ERROR,
753                             "usb_bulk_msg for tx failed: %d\n", ret);
754                 return ret;
755         }
756
757         *len = actual_length;
758
759         return ret;
760 }
761
762 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
763                                   u32 *len, u8 ep, u32 timeout)
764 {
765         struct usb_card_rec *card = adapter->card;
766         int actual_length, ret;
767
768         /* Receive the data response */
769         ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
770                            *len, &actual_length, timeout);
771         if (ret) {
772                 mwifiex_dbg(adapter, ERROR,
773                             "usb_bulk_msg for rx failed: %d\n", ret);
774                 return ret;
775         }
776
777         *len = actual_length;
778
779         return ret;
780 }
781
782 static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
783 {
784         struct usb_card_rec *card = adapter->card;
785         u8 active_port = MWIFIEX_USB_EP_DATA;
786         struct mwifiex_private *priv = NULL;
787         int i;
788
789         if (adapter->usb_mc_status) {
790                 for (i = 0; i < adapter->priv_num; i++) {
791                         priv = adapter->priv[i];
792                         if (!priv)
793                                 continue;
794                         if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
795                              !priv->bss_started) ||
796                             (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
797                              !priv->media_connected))
798                                 priv->usb_port = MWIFIEX_USB_EP_DATA;
799                 }
800                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
801                         card->port[i].block_status = false;
802         } else {
803                 for (i = 0; i < adapter->priv_num; i++) {
804                         priv = adapter->priv[i];
805                         if (!priv)
806                                 continue;
807                         if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
808                              priv->bss_started) ||
809                             (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
810                              priv->media_connected)) {
811                                 active_port = priv->usb_port;
812                                 break;
813                         }
814                 }
815                 for (i = 0; i < adapter->priv_num; i++) {
816                         priv = adapter->priv[i];
817                         if (priv)
818                                 priv->usb_port = active_port;
819                 }
820                 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
821                         if (active_port == card->port[i].tx_data_ep)
822                                 card->port[i].block_status = false;
823                         else
824                                 card->port[i].block_status = true;
825                 }
826         }
827 }
828
829 static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
830 {
831         struct usb_card_rec *card = priv->adapter->card;
832         int idx;
833
834         for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
835                 if (priv->usb_port == card->port[idx].tx_data_ep)
836                         return !card->port[idx].block_status;
837         }
838
839         return false;
840 }
841
842 static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
843 {
844         struct usb_card_rec *card = adapter->card;
845         int i;
846
847         for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
848                 if (!card->port[i].block_status)
849                         return false;
850
851         return true;
852 }
853
854 /* This function write a command/data packet to card. */
855 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
856                                     struct sk_buff *skb,
857                                     struct mwifiex_tx_param *tx_param)
858 {
859         struct usb_card_rec *card = adapter->card;
860         struct urb_context *context = NULL;
861         struct usb_tx_data_port *port = NULL;
862         u8 *data = (u8 *)skb->data;
863         struct urb *tx_urb;
864         int idx, ret = -EINPROGRESS;
865
866         if (adapter->is_suspended) {
867                 mwifiex_dbg(adapter, ERROR,
868                             "%s: not allowed while suspended\n", __func__);
869                 return -1;
870         }
871
872         if (adapter->surprise_removed) {
873                 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
874                 return -1;
875         }
876
877         mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
878
879         if (ep == card->tx_cmd_ep) {
880                 context = &card->tx_cmd;
881         } else {
882                 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
883                         if (ep == card->port[idx].tx_data_ep) {
884                                 port = &card->port[idx];
885                                 if (atomic_read(&port->tx_data_urb_pending)
886                                     >= MWIFIEX_TX_DATA_URB) {
887                                         port->block_status = true;
888                                         adapter->data_sent =
889                                                 mwifiex_usb_data_sent(adapter);
890                                         return -EBUSY;
891                                 }
892                                 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
893                                         port->tx_data_ix = 0;
894                                 context =
895                                         &port->tx_data_list[port->tx_data_ix++];
896                                 break;
897                         }
898                 }
899                 if (!port) {
900                         mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
901                         return -1;
902                 }
903         }
904
905         context->adapter = adapter;
906         context->ep = ep;
907         context->skb = skb;
908         tx_urb = context->urb;
909
910         usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
911                           data, skb->len, mwifiex_usb_tx_complete,
912                           (void *)context);
913
914         tx_urb->transfer_flags |= URB_ZERO_PACKET;
915
916         if (ep == card->tx_cmd_ep)
917                 atomic_inc(&card->tx_cmd_urb_pending);
918         else
919                 atomic_inc(&port->tx_data_urb_pending);
920
921         if (ep != card->tx_cmd_ep &&
922             atomic_read(&port->tx_data_urb_pending) ==
923                                         MWIFIEX_TX_DATA_URB) {
924                 port->block_status = true;
925                 adapter->data_sent = mwifiex_usb_data_sent(adapter);
926                 ret = -ENOSR;
927         }
928
929         if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
930                 mwifiex_dbg(adapter, ERROR,
931                             "%s: usb_submit_urb failed\n", __func__);
932                 if (ep == card->tx_cmd_ep) {
933                         atomic_dec(&card->tx_cmd_urb_pending);
934                 } else {
935                         atomic_dec(&port->tx_data_urb_pending);
936                         port->block_status = false;
937                         adapter->data_sent = 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                 ret = -1;
944         }
945
946         return ret;
947 }
948
949 /* This function register usb device and initialize parameter. */
950 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
951 {
952         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
953
954         card->adapter = adapter;
955         adapter->dev = &card->udev->dev;
956
957         switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
958         case USB8997_PID_1:
959         case USB8997_PID_2:
960                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
961                 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
962                 adapter->ext_scan = true;
963                 break;
964         case USB8766_PID_1:
965         case USB8766_PID_2:
966                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
967                 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
968                 adapter->ext_scan = true;
969                 break;
970         case USB8801_PID_1:
971         case USB8801_PID_2:
972                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
973                 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
974                 adapter->ext_scan = false;
975                 break;
976         case USB8797_PID_1:
977         case USB8797_PID_2:
978         default:
979                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
980                 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
981                 break;
982         }
983
984         adapter->usb_mc_status = false;
985         adapter->usb_mc_setup = false;
986
987         return 0;
988 }
989
990 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
991 {
992         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
993
994         card->adapter = NULL;
995 }
996
997 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
998                                     struct mwifiex_fw_image *fw)
999 {
1000         int ret = 0;
1001         u8 *firmware = fw->fw_buf, *recv_buff;
1002         u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1003         u32 dlen;
1004         u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1005         struct fw_data *fwdata;
1006         struct fw_sync_header sync_fw;
1007         u8 check_winner = 1;
1008
1009         if (!firmware) {
1010                 mwifiex_dbg(adapter, ERROR,
1011                             "No firmware image found! Terminating download\n");
1012                 ret = -1;
1013                 goto fw_exit;
1014         }
1015
1016         /* Allocate memory for transmit */
1017         fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1018         if (!fwdata) {
1019                 ret = -ENOMEM;
1020                 goto fw_exit;
1021         }
1022
1023         /* Allocate memory for receive */
1024         recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1025         if (!recv_buff) {
1026                 ret = -ENOMEM;
1027                 goto cleanup;
1028         }
1029
1030         do {
1031                 /* Send pseudo data to check winner status first */
1032                 if (check_winner) {
1033                         memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1034                         dlen = 0;
1035                 } else {
1036                         /* copy the header of the fw_data to get the length */
1037                         memcpy(&fwdata->fw_hdr, &firmware[tlen],
1038                                sizeof(struct fw_header));
1039
1040                         dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1041                         dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1042                         tlen += sizeof(struct fw_header);
1043
1044                         /* Command 7 doesn't have data length field */
1045                         if (dnld_cmd == FW_CMD_7)
1046                                 dlen = 0;
1047
1048                         memcpy(fwdata->data, &firmware[tlen], dlen);
1049
1050                         fwdata->seq_num = cpu_to_le32(fw_seqnum);
1051                         tlen += dlen;
1052                 }
1053
1054                 /* If the send/receive fails or CRC occurs then retry */
1055                 while (--retries) {
1056                         u8 *buf = (u8 *)fwdata;
1057                         u32 len = FW_DATA_XMIT_SIZE;
1058
1059                         /* send the firmware block */
1060                         ret = mwifiex_write_data_sync(adapter, buf, &len,
1061                                                 MWIFIEX_USB_EP_CMD_EVENT,
1062                                                 MWIFIEX_USB_TIMEOUT);
1063                         if (ret) {
1064                                 mwifiex_dbg(adapter, ERROR,
1065                                             "write_data_sync: failed: %d\n",
1066                                             ret);
1067                                 continue;
1068                         }
1069
1070                         buf = recv_buff;
1071                         len = FW_DNLD_RX_BUF_SIZE;
1072
1073                         /* Receive the firmware block response */
1074                         ret = mwifiex_read_data_sync(adapter, buf, &len,
1075                                                 MWIFIEX_USB_EP_CMD_EVENT,
1076                                                 MWIFIEX_USB_TIMEOUT);
1077                         if (ret) {
1078                                 mwifiex_dbg(adapter, ERROR,
1079                                             "read_data_sync: failed: %d\n",
1080                                             ret);
1081                                 continue;
1082                         }
1083
1084                         memcpy(&sync_fw, recv_buff,
1085                                sizeof(struct fw_sync_header));
1086
1087                         /* check 1st firmware block resp for highest bit set */
1088                         if (check_winner) {
1089                                 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1090                                         mwifiex_dbg(adapter, WARN,
1091                                                     "USB is not the winner %#x\n",
1092                                                     sync_fw.cmd);
1093
1094                                         /* returning success */
1095                                         ret = 0;
1096                                         goto cleanup;
1097                                 }
1098
1099                                 mwifiex_dbg(adapter, MSG,
1100                                             "start to download FW...\n");
1101
1102                                 check_winner = 0;
1103                                 break;
1104                         }
1105
1106                         /* check the firmware block response for CRC errors */
1107                         if (sync_fw.cmd) {
1108                                 mwifiex_dbg(adapter, ERROR,
1109                                             "FW received block with CRC %#x\n",
1110                                             sync_fw.cmd);
1111                                 ret = -1;
1112                                 continue;
1113                         }
1114
1115                         retries = USB8XXX_FW_MAX_RETRY + 1;
1116                         break;
1117                 }
1118                 fw_seqnum++;
1119         } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1120
1121 cleanup:
1122         mwifiex_dbg(adapter, MSG,
1123                     "info: FW download over, size %d bytes\n", tlen);
1124
1125         kfree(recv_buff);
1126         kfree(fwdata);
1127
1128         if (retries)
1129                 ret = 0;
1130 fw_exit:
1131         return ret;
1132 }
1133
1134 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1135                         struct mwifiex_fw_image *fw)
1136 {
1137         int ret;
1138         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1139
1140         if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1141                 ret = mwifiex_prog_fw_w_helper(adapter, fw);
1142                 if (ret)
1143                         return -1;
1144
1145                 /* Boot state changes after successful firmware download */
1146                 if (card->usb_boot_state == USB8XXX_FW_DNLD)
1147                         return -1;
1148         }
1149
1150         ret = mwifiex_usb_rx_init(adapter);
1151         if (!ret)
1152                 ret = mwifiex_usb_tx_init(adapter);
1153
1154         return ret;
1155 }
1156
1157 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1158 {
1159         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1160
1161         skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1162         if ((ep == card->rx_cmd_ep) &&
1163             (!atomic_read(&card->rx_cmd_urb_pending)))
1164                 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1165                                           MWIFIEX_RX_CMD_BUF_SIZE);
1166
1167         return;
1168 }
1169
1170 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1171                                        struct sk_buff *skb)
1172 {
1173         mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1174
1175         return 0;
1176 }
1177
1178 /* This function wakes up the card. */
1179 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1180 {
1181         /* Simulation of HS_AWAKE event */
1182         adapter->pm_wakeup_fw_try = false;
1183         del_timer(&adapter->wakeup_timer);
1184         adapter->pm_wakeup_card_req = false;
1185         adapter->ps_state = PS_STATE_AWAKE;
1186
1187         return 0;
1188 }
1189
1190 static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1191 {
1192         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1193         int i;
1194         struct urb_context *ctx;
1195
1196         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1197                 if (card->rx_data_list[i].skb)
1198                         continue;
1199                 ctx = &card->rx_data_list[i];
1200                 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1201         }
1202 }
1203
1204 /* This function is called after the card has woken up. */
1205 static inline int
1206 mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1207 {
1208         return 0;
1209 }
1210
1211 static struct mwifiex_if_ops usb_ops = {
1212         .register_dev =         mwifiex_register_dev,
1213         .unregister_dev =       mwifiex_unregister_dev,
1214         .wakeup =               mwifiex_pm_wakeup_card,
1215         .wakeup_complete =      mwifiex_pm_wakeup_card_complete,
1216
1217         /* USB specific */
1218         .dnld_fw =              mwifiex_usb_dnld_fw,
1219         .cmdrsp_complete =      mwifiex_usb_cmd_event_complete,
1220         .event_complete =       mwifiex_usb_cmd_event_complete,
1221         .host_to_card =         mwifiex_usb_host_to_card,
1222         .submit_rem_rx_urbs =   mwifiex_usb_submit_rem_rx_urbs,
1223         .multi_port_resync =    mwifiex_usb_port_resync,
1224         .is_port_ready =        mwifiex_usb_is_port_ready,
1225 };
1226
1227 /* This function initializes the USB driver module.
1228  *
1229  * This initiates the semaphore and registers the device with
1230  * USB bus.
1231  */
1232 static int mwifiex_usb_init_module(void)
1233 {
1234         int ret;
1235
1236         pr_debug("Marvell USB8797 Driver\n");
1237
1238         sema_init(&add_remove_card_sem, 1);
1239
1240         ret = usb_register(&mwifiex_usb_driver);
1241         if (ret)
1242                 pr_err("Driver register failed!\n");
1243         else
1244                 pr_debug("info: Driver registered successfully!\n");
1245
1246         return ret;
1247 }
1248
1249 /* This function cleans up the USB driver.
1250  *
1251  * The following major steps are followed in .disconnect for cleanup:
1252  *      - Resume the device if its suspended
1253  *      - Disconnect the device if connected
1254  *      - Shutdown the firmware
1255  *      - Unregister the device from USB bus.
1256  */
1257 static void mwifiex_usb_cleanup_module(void)
1258 {
1259         if (!down_interruptible(&add_remove_card_sem))
1260                 up(&add_remove_card_sem);
1261
1262         /* set the flag as user is removing this module */
1263         user_rmmod = 1;
1264
1265         usb_deregister(&mwifiex_usb_driver);
1266 }
1267
1268 module_init(mwifiex_usb_init_module);
1269 module_exit(mwifiex_usb_cleanup_module);
1270
1271 MODULE_AUTHOR("Marvell International Ltd.");
1272 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1273 MODULE_VERSION(USB_VERSION);
1274 MODULE_LICENSE("GPL v2");
1275 /*(DEBLOBBED)*/