Version:  2.6.34 2.6.35 2.6.36 2.6.37 2.6.38 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

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

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