Version:  2.0.40 2.2.26 2.4.37 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us