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

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

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