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