Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

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

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