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