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

Linux/drivers/net/wireless/mwifiex/usb.c

  1 /*
  2  * Marvell Wireless LAN device driver: USB specific handling
  3  *
  4  * Copyright (C) 2012, 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 struct mwifiex_if_ops usb_ops;
 26 static struct semaphore add_remove_card_sem;
 27 static struct usb_card_rec *usb_card;
 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 
536         if (!card) {
537                 pr_err("%s: card is NULL\n", __func__);
538                 return;
539         }
540 
541         mwifiex_usb_free(card);
542 
543         if (card->adapter) {
544                 struct mwifiex_adapter *adapter = card->adapter;
545 
546                 if (!adapter->priv_num)
547                         return;
548 
549                 dev_dbg(adapter->dev, "%s: removing card\n", __func__);
550                 mwifiex_remove_card(adapter, &add_remove_card_sem);
551         }
552 
553         usb_set_intfdata(intf, NULL);
554         usb_put_dev(interface_to_usbdev(intf));
555         kfree(card);
556         usb_card = NULL;
557 
558         return;
559 }
560 
561 static struct usb_driver mwifiex_usb_driver = {
562         .name = "mwifiex_usb",
563         .probe = mwifiex_usb_probe,
564         .disconnect = mwifiex_usb_disconnect,
565         .id_table = mwifiex_usb_table,
566         .suspend = mwifiex_usb_suspend,
567         .resume = mwifiex_usb_resume,
568 };
569 
570 static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
571 {
572         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
573         int i;
574 
575         card->tx_cmd.adapter = adapter;
576         card->tx_cmd.ep = card->tx_cmd_ep;
577 
578         card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
579         if (!card->tx_cmd.urb) {
580                 dev_err(adapter->dev, "tx_cmd.urb allocation failed\n");
581                 return -ENOMEM;
582         }
583 
584         card->tx_data_ix = 0;
585 
586         for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
587                 card->tx_data_list[i].adapter = adapter;
588                 card->tx_data_list[i].ep = card->tx_data_ep;
589 
590                 card->tx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
591                 if (!card->tx_data_list[i].urb) {
592                         dev_err(adapter->dev,
593                                 "tx_data_list[] urb allocation failed\n");
594                         return -ENOMEM;
595                 }
596         }
597 
598         return 0;
599 }
600 
601 static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
602 {
603         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
604         int i;
605 
606         card->rx_cmd.adapter = adapter;
607         card->rx_cmd.ep = card->rx_cmd_ep;
608 
609         card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
610         if (!card->rx_cmd.urb) {
611                 dev_err(adapter->dev, "rx_cmd.urb allocation failed\n");
612                 return -ENOMEM;
613         }
614 
615         card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
616         if (!card->rx_cmd.skb) {
617                 dev_err(adapter->dev, "rx_cmd.skb allocation failed\n");
618                 return -ENOMEM;
619         }
620 
621         if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
622                 return -1;
623 
624         for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
625                 card->rx_data_list[i].adapter = adapter;
626                 card->rx_data_list[i].ep = card->rx_data_ep;
627 
628                 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
629                 if (!card->rx_data_list[i].urb) {
630                         dev_err(adapter->dev,
631                                 "rx_data_list[] urb allocation failed\n");
632                         return -1;
633                 }
634                 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
635                                               MWIFIEX_RX_DATA_BUF_SIZE))
636                         return -1;
637         }
638 
639         return 0;
640 }
641 
642 static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
643                                    u32 *len, u8 ep, u32 timeout)
644 {
645         struct usb_card_rec *card = adapter->card;
646         int actual_length, ret;
647 
648         if (!(*len % card->bulk_out_maxpktsize))
649                 (*len)++;
650 
651         /* Send the data block */
652         ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
653                            *len, &actual_length, timeout);
654         if (ret) {
655                 dev_err(adapter->dev, "usb_bulk_msg for tx failed: %d\n", ret);
656                 return ret;
657         }
658 
659         *len = actual_length;
660 
661         return ret;
662 }
663 
664 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
665                                   u32 *len, u8 ep, u32 timeout)
666 {
667         struct usb_card_rec *card = adapter->card;
668         int actual_length, ret;
669 
670         /* Receive the data response */
671         ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
672                            *len, &actual_length, timeout);
673         if (ret) {
674                 dev_err(adapter->dev, "usb_bulk_msg for rx failed: %d\n", ret);
675                 return ret;
676         }
677 
678         *len = actual_length;
679 
680         return ret;
681 }
682 
683 /* This function write a command/data packet to card. */
684 static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
685                                     struct sk_buff *skb,
686                                     struct mwifiex_tx_param *tx_param)
687 {
688         struct usb_card_rec *card = adapter->card;
689         struct urb_context *context;
690         u8 *data = (u8 *)skb->data;
691         struct urb *tx_urb;
692 
693         if (adapter->is_suspended) {
694                 dev_err(adapter->dev,
695                         "%s: not allowed while suspended\n", __func__);
696                 return -1;
697         }
698 
699         if (adapter->surprise_removed) {
700                 dev_err(adapter->dev, "%s: device removed\n", __func__);
701                 return -1;
702         }
703 
704         if (ep == card->tx_data_ep &&
705             atomic_read(&card->tx_data_urb_pending) >= MWIFIEX_TX_DATA_URB) {
706                 return -EBUSY;
707         }
708 
709         dev_dbg(adapter->dev, "%s: ep=%d\n", __func__, ep);
710 
711         if (ep == card->tx_cmd_ep) {
712                 context = &card->tx_cmd;
713         } else {
714                 if (card->tx_data_ix >= MWIFIEX_TX_DATA_URB)
715                         card->tx_data_ix = 0;
716                 context = &card->tx_data_list[card->tx_data_ix++];
717         }
718 
719         context->adapter = adapter;
720         context->ep = ep;
721         context->skb = skb;
722         tx_urb = context->urb;
723 
724         usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
725                           data, skb->len, mwifiex_usb_tx_complete,
726                           (void *)context);
727 
728         tx_urb->transfer_flags |= URB_ZERO_PACKET;
729 
730         if (ep == card->tx_cmd_ep)
731                 atomic_inc(&card->tx_cmd_urb_pending);
732         else
733                 atomic_inc(&card->tx_data_urb_pending);
734 
735         if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
736                 dev_err(adapter->dev, "%s: usb_submit_urb failed\n", __func__);
737                 if (ep == card->tx_cmd_ep) {
738                         atomic_dec(&card->tx_cmd_urb_pending);
739                 } else {
740                         atomic_dec(&card->tx_data_urb_pending);
741                         if (card->tx_data_ix)
742                                 card->tx_data_ix--;
743                         else
744                                 card->tx_data_ix = MWIFIEX_TX_DATA_URB;
745                 }
746 
747                 return -1;
748         } else {
749                 if (ep == card->tx_data_ep &&
750                     atomic_read(&card->tx_data_urb_pending) ==
751                                                         MWIFIEX_TX_DATA_URB)
752                         return -ENOSR;
753         }
754 
755         return -EINPROGRESS;
756 }
757 
758 /* This function register usb device and initialize parameter. */
759 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
760 {
761         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
762 
763         card->adapter = adapter;
764         adapter->dev = &card->udev->dev;
765         usb_card = card;
766 
767         switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
768         case USB8897_PID_1:
769         case USB8897_PID_2:
770                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
771                 strcpy(adapter->fw_name, USB8897_DEFAULT_FW_NAME);
772                 break;
773         case USB8797_PID_1:
774         case USB8797_PID_2:
775         default:
776                 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
777                 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
778                 break;
779         }
780 
781         return 0;
782 }
783 
784 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
785 {
786         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
787 
788         card->adapter = NULL;
789 }
790 
791 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
792                                     struct mwifiex_fw_image *fw)
793 {
794         int ret = 0;
795         u8 *firmware = fw->fw_buf, *recv_buff;
796         u32 retries = USB8XXX_FW_MAX_RETRY, dlen;
797         u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
798         struct fw_data *fwdata;
799         struct fw_sync_header sync_fw;
800         u8 check_winner = 1;
801 
802         if (!firmware) {
803                 dev_err(adapter->dev,
804                         "No firmware image found! Terminating download\n");
805                 ret = -1;
806                 goto fw_exit;
807         }
808 
809         /* Allocate memory for transmit */
810         fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
811         if (!fwdata)
812                 goto fw_exit;
813 
814         /* Allocate memory for receive */
815         recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
816         if (!recv_buff)
817                 goto cleanup;
818 
819         do {
820                 /* Send pseudo data to check winner status first */
821                 if (check_winner) {
822                         memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
823                         dlen = 0;
824                 } else {
825                         /* copy the header of the fw_data to get the length */
826                         memcpy(&fwdata->fw_hdr, &firmware[tlen],
827                                sizeof(struct fw_header));
828 
829                         dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
830                         dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
831                         tlen += sizeof(struct fw_header);
832 
833                         memcpy(fwdata->data, &firmware[tlen], dlen);
834 
835                         fwdata->seq_num = cpu_to_le32(fw_seqnum);
836                         tlen += dlen;
837                 }
838 
839                 /* If the send/receive fails or CRC occurs then retry */
840                 while (retries--) {
841                         u8 *buf = (u8 *)fwdata;
842                         u32 len = FW_DATA_XMIT_SIZE;
843 
844                         /* send the firmware block */
845                         ret = mwifiex_write_data_sync(adapter, buf, &len,
846                                                 MWIFIEX_USB_EP_CMD_EVENT,
847                                                 MWIFIEX_USB_TIMEOUT);
848                         if (ret) {
849                                 dev_err(adapter->dev,
850                                         "write_data_sync: failed: %d\n", ret);
851                                 continue;
852                         }
853 
854                         buf = recv_buff;
855                         len = FW_DNLD_RX_BUF_SIZE;
856 
857                         /* Receive the firmware block response */
858                         ret = mwifiex_read_data_sync(adapter, buf, &len,
859                                                 MWIFIEX_USB_EP_CMD_EVENT,
860                                                 MWIFIEX_USB_TIMEOUT);
861                         if (ret) {
862                                 dev_err(adapter->dev,
863                                         "read_data_sync: failed: %d\n", ret);
864                                 continue;
865                         }
866 
867                         memcpy(&sync_fw, recv_buff,
868                                sizeof(struct fw_sync_header));
869 
870                         /* check 1st firmware block resp for highest bit set */
871                         if (check_winner) {
872                                 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
873                                         dev_warn(adapter->dev,
874                                                  "USB is not the winner %#x\n",
875                                                  sync_fw.cmd);
876 
877                                         /* returning success */
878                                         ret = 0;
879                                         goto cleanup;
880                                 }
881 
882                                 dev_dbg(adapter->dev,
883                                         "USB is the winner, start to download FW\n");
884 
885                                 check_winner = 0;
886                                 break;
887                         }
888 
889                         /* check the firmware block response for CRC errors */
890                         if (sync_fw.cmd) {
891                                 dev_err(adapter->dev,
892                                         "FW received block with CRC %#x\n",
893                                         sync_fw.cmd);
894                                 ret = -1;
895                                 continue;
896                         }
897 
898                         retries = USB8XXX_FW_MAX_RETRY;
899                         break;
900                 }
901                 fw_seqnum++;
902         } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
903 
904 cleanup:
905         dev_dbg(adapter->dev, "%s: %d bytes downloaded\n", __func__, tlen);
906 
907         kfree(recv_buff);
908         kfree(fwdata);
909 
910         if (retries)
911                 ret = 0;
912 fw_exit:
913         return ret;
914 }
915 
916 static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
917                         struct mwifiex_fw_image *fw)
918 {
919         int ret;
920         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
921 
922         if (card->usb_boot_state == USB8XXX_FW_DNLD) {
923                 ret = mwifiex_prog_fw_w_helper(adapter, fw);
924                 if (ret)
925                         return -1;
926 
927                 /* Boot state changes after successful firmware download */
928                 if (card->usb_boot_state == USB8XXX_FW_DNLD)
929                         return -1;
930         }
931 
932         ret = mwifiex_usb_rx_init(adapter);
933         if (!ret)
934                 ret = mwifiex_usb_tx_init(adapter);
935 
936         return ret;
937 }
938 
939 static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
940 {
941         struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
942 
943         skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
944         if ((ep == card->rx_cmd_ep) &&
945             (!atomic_read(&card->rx_cmd_urb_pending)))
946                 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
947                                           MWIFIEX_RX_CMD_BUF_SIZE);
948 
949         return;
950 }
951 
952 static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
953                                        struct sk_buff *skb)
954 {
955         atomic_dec(&adapter->rx_pending);
956         mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
957 
958         return 0;
959 }
960 
961 static int mwifiex_usb_data_complete(struct mwifiex_adapter *adapter)
962 {
963         atomic_dec(&adapter->rx_pending);
964 
965         return 0;
966 }
967 
968 /* This function wakes up the card. */
969 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
970 {
971         /* Simulation of HS_AWAKE event */
972         adapter->pm_wakeup_fw_try = false;
973         adapter->pm_wakeup_card_req = false;
974         adapter->ps_state = PS_STATE_AWAKE;
975 
976         return 0;
977 }
978 
979 static struct mwifiex_if_ops usb_ops = {
980         .register_dev =         mwifiex_register_dev,
981         .unregister_dev =       mwifiex_unregister_dev,
982         .wakeup =               mwifiex_pm_wakeup_card,
983         .wakeup_complete =      mwifiex_pm_wakeup_card_complete,
984 
985         /* USB specific */
986         .dnld_fw =              mwifiex_usb_dnld_fw,
987         .cmdrsp_complete =      mwifiex_usb_cmd_event_complete,
988         .event_complete =       mwifiex_usb_cmd_event_complete,
989         .data_complete =        mwifiex_usb_data_complete,
990         .host_to_card =         mwifiex_usb_host_to_card,
991 };
992 
993 /* This function initializes the USB driver module.
994  *
995  * This initiates the semaphore and registers the device with
996  * USB bus.
997  */
998 static int mwifiex_usb_init_module(void)
999 {
1000         int ret;
1001 
1002         pr_debug("Marvell USB8797 Driver\n");
1003 
1004         sema_init(&add_remove_card_sem, 1);
1005 
1006         ret = usb_register(&mwifiex_usb_driver);
1007         if (ret)
1008                 pr_err("Driver register failed!\n");
1009         else
1010                 pr_debug("info: Driver registered successfully!\n");
1011 
1012         return ret;
1013 }
1014 
1015 /* This function cleans up the USB driver.
1016  *
1017  * The following major steps are followed in .disconnect for cleanup:
1018  *      - Resume the device if its suspended
1019  *      - Disconnect the device if connected
1020  *      - Shutdown the firmware
1021  *      - Unregister the device from USB bus.
1022  */
1023 static void mwifiex_usb_cleanup_module(void)
1024 {
1025         if (!down_interruptible(&add_remove_card_sem))
1026                 up(&add_remove_card_sem);
1027 
1028         if (usb_card && usb_card->adapter) {
1029                 struct mwifiex_adapter *adapter = usb_card->adapter;
1030 
1031                 /* In case driver is removed when asynchronous FW downloading is
1032                  * in progress
1033                  */
1034                 wait_for_completion(&adapter->fw_load);
1035 
1036 #ifdef CONFIG_PM
1037                 if (adapter->is_suspended)
1038                         mwifiex_usb_resume(usb_card->intf);
1039 #endif
1040 
1041                 mwifiex_deauthenticate_all(adapter);
1042 
1043                 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
1044                                                           MWIFIEX_BSS_ROLE_ANY),
1045                                          MWIFIEX_FUNC_SHUTDOWN);
1046         }
1047 
1048         usb_deregister(&mwifiex_usb_driver);
1049 }
1050 
1051 module_init(mwifiex_usb_init_module);
1052 module_exit(mwifiex_usb_cleanup_module);
1053 
1054 MODULE_AUTHOR("Marvell International Ltd.");
1055 MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1056 MODULE_VERSION(USB_VERSION);
1057 MODULE_LICENSE("GPL v2");
1058 MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1059 MODULE_FIRMWARE(USB8897_DEFAULT_FW_NAME);
1060 

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