Version:  2.0.40 2.2.26 2.4.37 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 3.18 3.19

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

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