blob: debd6216366ab16155ab4f004e81b26b6f8f9ca1 [file] [log] [blame]
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001/*
2 * Marvell Wireless LAN device driver: USB specific handling
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2012-2014, Marvell International Ltd.
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07005 *
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
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070025static struct mwifiex_if_ops usb_ops;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070026
27static struct usb_device_id mwifiex_usb_table[] = {
Kristian Evensen9e6f3f42014-11-20 16:52:58 +010028 /* 8766 */
29 {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)},
30 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2,
31 USB_CLASS_VENDOR_SPEC,
32 USB_SUBCLASS_VENDOR_SPEC, 0xff)},
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -080033 /* 8797 */
34 {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)},
35 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2,
36 USB_CLASS_VENDOR_SPEC,
37 USB_SUBCLASS_VENDOR_SPEC, 0xff)},
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +053038 /* 8801 */
39 {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)},
40 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2,
41 USB_CLASS_VENDOR_SPEC,
42 USB_SUBCLASS_VENDOR_SPEC, 0xff)},
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -070043 /* 8997 */
44 {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)},
45 {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2,
46 USB_CLASS_VENDOR_SPEC,
47 USB_SUBCLASS_VENDOR_SPEC, 0xff)},
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070048 { } /* Terminating entry */
49};
50
51MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
52
53static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
54
55/* This function handles received packet. Necessary action is taken based on
56 * cmd/event/data.
57 */
58static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
59 struct sk_buff *skb, u8 ep)
60{
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070061 u32 recv_type;
62 __le32 tmp;
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070063 int ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070064
65 if (adapter->hs_activated)
66 mwifiex_process_hs_config(adapter);
67
68 if (skb->len < INTF_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053069 mwifiex_dbg(adapter, ERROR,
70 "%s: invalid skb->len\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070071 return -1;
72 }
73
74 switch (ep) {
75 case MWIFIEX_USB_EP_CMD_EVENT:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053076 mwifiex_dbg(adapter, EVENT,
77 "%s: EP_CMD_EVENT\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070078 skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
79 recv_type = le32_to_cpu(tmp);
80 skb_pull(skb, INTF_HEADER_LEN);
81
82 switch (recv_type) {
83 case MWIFIEX_USB_TYPE_CMD:
84 if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053085 mwifiex_dbg(adapter, ERROR,
86 "CMD: skb->len too large\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070087 ret = -1;
88 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070089 } else if (!adapter->curr_cmd) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053090 mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070091 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
92 mwifiex_process_sleep_confirm_resp(
93 adapter, skb->data,
94 skb->len);
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070095 ret = 0;
96 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070097 }
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -070098 ret = -1;
99 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700100 }
101
102 adapter->curr_cmd->resp_skb = skb;
103 adapter->cmd_resp_received = true;
104 break;
105 case MWIFIEX_USB_TYPE_EVENT:
106 if (skb->len < sizeof(u32)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530107 mwifiex_dbg(adapter, ERROR,
108 "EVENT: skb->len too small\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700109 ret = -1;
110 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700111 }
112 skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
113 adapter->event_cause = le32_to_cpu(tmp);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530114 mwifiex_dbg(adapter, EVENT,
115 "event_cause %#x\n", adapter->event_cause);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700116
117 if (skb->len > MAX_EVENT_SIZE) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530118 mwifiex_dbg(adapter, ERROR,
119 "EVENT: event body too large\n");
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700120 ret = -1;
121 goto exit_restore_skb;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700122 }
123
Amitkumar Karware80c81d2012-06-20 20:21:12 -0700124 memcpy(adapter->event_body, skb->data +
125 MWIFIEX_EVENT_HEADER_LEN, skb->len);
126
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700127 adapter->event_received = true;
128 adapter->event_skb = skb;
129 break;
130 default:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530131 mwifiex_dbg(adapter, ERROR,
132 "unknown recv_type %#x\n", recv_type);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700133 return -1;
134 }
135 break;
136 case MWIFIEX_USB_EP_DATA:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530137 mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700138 if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530139 mwifiex_dbg(adapter, ERROR,
140 "DATA: skb->len too large\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700141 return -1;
142 }
Avinash Patilec4a16b2014-11-05 17:04:27 +0530143
144 skb_queue_tail(&adapter->rx_data_q, skb);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700145 adapter->data_received = true;
Avinash Patilec4a16b2014-11-05 17:04:27 +0530146 atomic_inc(&adapter->rx_pending);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700147 break;
148 default:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530149 mwifiex_dbg(adapter, ERROR,
150 "%s: unknown endport %#x\n", __func__, ep);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700151 return -1;
152 }
153
154 return -EINPROGRESS;
Amitkumar Karwar8311f0d2012-06-20 20:21:13 -0700155
156exit_restore_skb:
157 /* The buffer will be reused for further cmds/events */
158 skb_push(skb, INTF_HEADER_LEN);
159
160 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700161}
162
163static void mwifiex_usb_rx_complete(struct urb *urb)
164{
165 struct urb_context *context = (struct urb_context *)urb->context;
166 struct mwifiex_adapter *adapter = context->adapter;
167 struct sk_buff *skb = context->skb;
168 struct usb_card_rec *card;
169 int recv_length = urb->actual_length;
170 int size, status;
171
172 if (!adapter || !adapter->card) {
173 pr_err("mwifiex adapter or card structure is not valid\n");
174 return;
175 }
176
177 card = (struct usb_card_rec *)adapter->card;
178 if (card->rx_cmd_ep == context->ep)
179 atomic_dec(&card->rx_cmd_urb_pending);
180 else
181 atomic_dec(&card->rx_data_urb_pending);
182
183 if (recv_length) {
184 if (urb->status || (adapter->surprise_removed)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530185 mwifiex_dbg(adapter, ERROR,
186 "URB status is failed: %d\n", urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700187 /* Do not free skb in case of command ep */
188 if (card->rx_cmd_ep != context->ep)
189 dev_kfree_skb_any(skb);
190 goto setup_for_next;
191 }
192 if (skb->len > recv_length)
193 skb_trim(skb, recv_length);
194 else
195 skb_put(skb, recv_length - skb->len);
196
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700197 status = mwifiex_usb_recv(adapter, skb, context->ep);
198
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530199 mwifiex_dbg(adapter, INFO,
200 "info: recv_length=%d, status=%d\n",
201 recv_length, status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700202 if (status == -EINPROGRESS) {
Shengzhen Lib2713f62015-03-13 17:37:54 +0530203 mwifiex_queue_main_work(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700204
205 /* urb for data_ep is re-submitted now;
206 * urb for cmd_ep will be re-submitted in callback
207 * mwifiex_usb_recv_complete
208 */
209 if (card->rx_cmd_ep == context->ep)
210 return;
211 } else {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700212 if (status == -1)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530213 mwifiex_dbg(adapter, ERROR,
214 "received data processing failed!\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700215
216 /* Do not free skb in case of command ep */
217 if (card->rx_cmd_ep != context->ep)
218 dev_kfree_skb_any(skb);
219 }
220 } else if (urb->status) {
221 if (!adapter->is_suspended) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530222 mwifiex_dbg(adapter, FATAL,
223 "Card is removed: %d\n", urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700224 adapter->surprise_removed = true;
225 }
226 dev_kfree_skb_any(skb);
227 return;
228 } else {
229 /* Do not free skb in case of command ep */
230 if (card->rx_cmd_ep != context->ep)
231 dev_kfree_skb_any(skb);
232
233 /* fall through setup_for_next */
234 }
235
236setup_for_next:
237 if (card->rx_cmd_ep == context->ep)
238 size = MWIFIEX_RX_CMD_BUF_SIZE;
239 else
240 size = MWIFIEX_RX_DATA_BUF_SIZE;
241
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530242 if (card->rx_cmd_ep == context->ep) {
243 mwifiex_usb_submit_rx_urb(context, size);
244 } else {
Ujjal Roy354a1942016-02-25 00:49:56 +0530245 if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530246 mwifiex_usb_submit_rx_urb(context, size);
Ujjal Roy354a1942016-02-25 00:49:56 +0530247 } else {
Reyad Attiyat11bdc442015-06-28 20:07:38 -0500248 context->skb = NULL;
249 }
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530250 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700251
252 return;
253}
254
255static void mwifiex_usb_tx_complete(struct urb *urb)
256{
257 struct urb_context *context = (struct urb_context *)(urb->context);
258 struct mwifiex_adapter *adapter = context->adapter;
259 struct usb_card_rec *card = adapter->card;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700260 struct usb_tx_data_port *port;
261 int i;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700262
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530263 mwifiex_dbg(adapter, INFO,
264 "%s: status: %d\n", __func__, urb->status);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700265
266 if (context->ep == card->tx_cmd_ep) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530267 mwifiex_dbg(adapter, CMD,
268 "%s: CMD\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700269 atomic_dec(&card->tx_cmd_urb_pending);
270 adapter->cmd_sent = false;
271 } else {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530272 mwifiex_dbg(adapter, DATA,
273 "%s: DATA\n", __func__);
Cathy Luo1afac192016-09-20 20:49:02 +0530274 mwifiex_write_data_complete(adapter, context->skb, 0,
275 urb->status ? -1 : 0);
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700276 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
277 port = &card->port[i];
278 if (context->ep == port->tx_data_ep) {
279 atomic_dec(&port->tx_data_urb_pending);
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700280 port->block_status = false;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700281 break;
282 }
283 }
Zhaoyang Liud9414442015-09-18 06:32:14 -0700284 adapter->data_sent = false;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700285 }
286
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700287 if (card->mc_resync_flag)
288 mwifiex_multi_chan_resync(adapter);
289
Shengzhen Lib2713f62015-03-13 17:37:54 +0530290 mwifiex_queue_main_work(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700291
292 return;
293}
294
295static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
296{
297 struct mwifiex_adapter *adapter = ctx->adapter;
298 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
299
300 if (card->rx_cmd_ep != ctx->ep) {
301 ctx->skb = dev_alloc_skb(size);
302 if (!ctx->skb) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530303 mwifiex_dbg(adapter, ERROR,
304 "%s: dev_alloc_skb failed\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700305 return -ENOMEM;
306 }
307 }
308
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530309 if (card->rx_cmd_ep == ctx->ep &&
310 card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
311 usb_fill_int_urb(ctx->urb, card->udev,
312 usb_rcvintpipe(card->udev, ctx->ep),
313 ctx->skb->data, size, mwifiex_usb_rx_complete,
314 (void *)ctx, card->rx_cmd_interval);
315 else
316 usb_fill_bulk_urb(ctx->urb, card->udev,
317 usb_rcvbulkpipe(card->udev, ctx->ep),
318 ctx->skb->data, size, mwifiex_usb_rx_complete,
319 (void *)ctx);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700320
321 if (card->rx_cmd_ep == ctx->ep)
322 atomic_inc(&card->rx_cmd_urb_pending);
323 else
324 atomic_inc(&card->rx_data_urb_pending);
325
326 if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530327 mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700328 dev_kfree_skb_any(ctx->skb);
329 ctx->skb = NULL;
330
331 if (card->rx_cmd_ep == ctx->ep)
332 atomic_dec(&card->rx_cmd_urb_pending);
333 else
334 atomic_dec(&card->rx_data_urb_pending);
335
336 return -1;
337 }
338
339 return 0;
340}
341
342static void mwifiex_usb_free(struct usb_card_rec *card)
343{
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700344 struct usb_tx_data_port *port;
345 int i, j;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700346
347 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
348 usb_kill_urb(card->rx_cmd.urb);
349
350 usb_free_urb(card->rx_cmd.urb);
351 card->rx_cmd.urb = NULL;
352
353 if (atomic_read(&card->rx_data_urb_pending))
354 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
355 if (card->rx_data_list[i].urb)
356 usb_kill_urb(card->rx_data_list[i].urb);
357
358 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
359 usb_free_urb(card->rx_data_list[i].urb);
360 card->rx_data_list[i].urb = NULL;
361 }
362
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700363 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
364 port = &card->port[i];
365 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
366 usb_free_urb(port->tx_data_list[j].urb);
367 port->tx_data_list[j].urb = NULL;
368 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700369 }
370
371 usb_free_urb(card->tx_cmd.urb);
372 card->tx_cmd.urb = NULL;
373
374 return;
375}
376
377/* This function probes an mwifiex device and registers it. It allocates
378 * the card structure, initiates the device registration and initialization
379 * procedure by adding a logical interface.
380 */
381static int mwifiex_usb_probe(struct usb_interface *intf,
382 const struct usb_device_id *id)
383{
384 struct usb_device *udev = interface_to_usbdev(intf);
385 struct usb_host_interface *iface_desc = intf->cur_altsetting;
386 struct usb_endpoint_descriptor *epd;
387 int ret, i;
388 struct usb_card_rec *card;
Kirtika Ruchandani41338282016-11-23 17:27:10 -0800389 u16 id_vendor, id_product, bcd_device;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700390
Brian Norris66b9c182016-10-26 16:29:20 -0700391 card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700392 if (!card)
393 return -ENOMEM;
394
Brian Norris4a79aa12016-11-18 19:30:26 +0530395 init_completion(&card->fw_done);
396
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700397 id_vendor = le16_to_cpu(udev->descriptor.idVendor);
398 id_product = le16_to_cpu(udev->descriptor.idProduct);
399 bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700400 pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
401 id_vendor, id_product, bcd_device);
402
403 /* PID_1 is used for firmware downloading only */
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800404 switch (id_product) {
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100405 case USB8766_PID_1:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800406 case USB8797_PID_1:
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530407 case USB8801_PID_1:
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -0700408 case USB8997_PID_1:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800409 card->usb_boot_state = USB8XXX_FW_DNLD;
410 break;
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100411 case USB8766_PID_2:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800412 case USB8797_PID_2:
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530413 case USB8801_PID_2:
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -0700414 case USB8997_PID_2:
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800415 card->usb_boot_state = USB8XXX_FW_READY;
416 break;
417 default:
Joe Perchesfe3881c2014-09-09 20:27:44 -0700418 pr_warn("unknown id_product %#x\n", id_product);
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800419 card->usb_boot_state = USB8XXX_FW_DNLD;
420 break;
421 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700422
423 card->udev = udev;
424 card->intf = intf;
425
Masanari Iida69797832012-11-30 22:08:31 +0900426 pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
Johan Hovolda1ad7192017-05-12 12:15:55 +0200427 le16_to_cpu(udev->descriptor.bcdUSB),
428 udev->descriptor.bDeviceClass,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700429 udev->descriptor.bDeviceSubClass,
430 udev->descriptor.bDeviceProtocol);
431
432 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
433 epd = &iface_desc->endpoint[i].desc;
434 if (usb_endpoint_dir_in(epd) &&
435 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530436 (usb_endpoint_xfer_bulk(epd) ||
437 usb_endpoint_xfer_int(epd))) {
438 card->rx_cmd_ep_type = usb_endpoint_type(epd);
439 card->rx_cmd_interval = epd->bInterval;
440 pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n",
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700441 le16_to_cpu(epd->wMaxPacketSize),
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530442 epd->bEndpointAddress, card->rx_cmd_ep_type);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700443 card->rx_cmd_ep = usb_endpoint_num(epd);
444 atomic_set(&card->rx_cmd_urb_pending, 0);
445 }
446 if (usb_endpoint_dir_in(epd) &&
447 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
448 usb_endpoint_xfer_bulk(epd)) {
449 pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
450 le16_to_cpu(epd->wMaxPacketSize),
451 epd->bEndpointAddress);
452 card->rx_data_ep = usb_endpoint_num(epd);
453 atomic_set(&card->rx_data_urb_pending, 0);
454 }
455 if (usb_endpoint_dir_out(epd) &&
456 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
457 usb_endpoint_xfer_bulk(epd)) {
458 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
459 le16_to_cpu(epd->wMaxPacketSize),
460 epd->bEndpointAddress);
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700461 card->port[0].tx_data_ep = usb_endpoint_num(epd);
462 atomic_set(&card->port[0].tx_data_urb_pending, 0);
463 }
464 if (usb_endpoint_dir_out(epd) &&
465 usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 &&
466 usb_endpoint_xfer_bulk(epd)) {
467 pr_debug("info: bulk OUT chan2:\t"
468 "max pkt size: %d, addr: %d\n",
469 le16_to_cpu(epd->wMaxPacketSize),
470 epd->bEndpointAddress);
471 card->port[1].tx_data_ep = usb_endpoint_num(epd);
472 atomic_set(&card->port[1].tx_data_urb_pending, 0);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700473 }
474 if (usb_endpoint_dir_out(epd) &&
475 usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530476 (usb_endpoint_xfer_bulk(epd) ||
477 usb_endpoint_xfer_int(epd))) {
478 card->tx_cmd_ep_type = usb_endpoint_type(epd);
479 card->tx_cmd_interval = epd->bInterval;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700480 pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
481 le16_to_cpu(epd->wMaxPacketSize),
482 epd->bEndpointAddress);
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530483 pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n",
484 le16_to_cpu(epd->wMaxPacketSize),
485 epd->bEndpointAddress, card->tx_cmd_ep_type);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700486 card->tx_cmd_ep = usb_endpoint_num(epd);
487 atomic_set(&card->tx_cmd_urb_pending, 0);
488 card->bulk_out_maxpktsize =
489 le16_to_cpu(epd->wMaxPacketSize);
490 }
491 }
492
493 usb_set_intfdata(intf, card);
494
Brian Norris4a79aa12016-11-18 19:30:26 +0530495 ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
Rajat Jain2e02b582016-11-15 19:06:02 +0530496 MWIFIEX_USB, &card->udev->dev);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700497 if (ret) {
498 pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
499 usb_reset_device(udev);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700500 return ret;
501 }
502
503 usb_get_dev(udev);
504
505 return 0;
506}
507
508/* Kernel needs to suspend all functions separately. Therefore all
509 * registered functions must have drivers with suspend and resume
510 * methods. Failing that the kernel simply removes the whole card.
511 *
512 * If already not suspended, this function allocates and sends a
513 * 'host sleep activate' request to the firmware and turns off the traffic.
514 */
515static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
516{
517 struct usb_card_rec *card = usb_get_intfdata(intf);
518 struct mwifiex_adapter *adapter;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700519 struct usb_tx_data_port *port;
520 int i, j;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700521
Brian Norrisb42dbb22016-11-18 19:30:29 +0530522 /* Might still be loading firmware */
523 wait_for_completion(&card->fw_done);
524
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700525 adapter = card->adapter;
Brian Norrisb42dbb22016-11-18 19:30:29 +0530526 if (!adapter) {
527 dev_err(&intf->dev, "card is not valid\n");
528 return 0;
529 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700530
531 if (unlikely(adapter->is_suspended))
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530532 mwifiex_dbg(adapter, WARN,
533 "Device already suspended\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700534
Brian Norris58b70332016-11-18 19:30:31 +0530535 /* Enable the Host Sleep */
536 if (!mwifiex_enable_hs(adapter)) {
537 mwifiex_dbg(adapter, ERROR,
538 "cmd: failed to suspend\n");
539 adapter->hs_enabling = false;
540 return -EFAULT;
541 }
542
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700543
544 /* 'is_suspended' flag indicates device is suspended.
545 * It must be set here before the usb_kill_urb() calls. Reason
546 * is in the complete handlers, urb->status(= -ENOENT) and
547 * this flag is used in combination to distinguish between a
548 * 'suspended' state and a 'disconnect' one.
549 */
550 adapter->is_suspended = true;
Amitkumar Karwarc0dbba62014-03-25 19:01:20 -0700551 adapter->hs_enabling = false;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700552
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700553 if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
554 usb_kill_urb(card->rx_cmd.urb);
555
556 if (atomic_read(&card->rx_data_urb_pending))
557 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
558 if (card->rx_data_list[i].urb)
559 usb_kill_urb(card->rx_data_list[i].urb);
560
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700561 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
562 port = &card->port[i];
563 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
564 if (port->tx_data_list[j].urb)
565 usb_kill_urb(port->tx_data_list[j].urb);
566 }
567 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700568
569 if (card->tx_cmd.urb)
570 usb_kill_urb(card->tx_cmd.urb);
571
572 return 0;
573}
574
575/* Kernel needs to suspend all functions separately. Therefore all
576 * registered functions must have drivers with suspend and resume
577 * methods. Failing that the kernel simply removes the whole card.
578 *
579 * If already not resumed, this function turns on the traffic and
580 * sends a 'host sleep cancel' request to the firmware.
581 */
582static int mwifiex_usb_resume(struct usb_interface *intf)
583{
584 struct usb_card_rec *card = usb_get_intfdata(intf);
585 struct mwifiex_adapter *adapter;
586 int i;
587
Brian Norrise98fb112016-11-18 19:30:33 +0530588 if (!card->adapter) {
589 dev_err(&intf->dev, "%s: card->adapter is NULL\n",
590 __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700591 return 0;
592 }
593 adapter = card->adapter;
594
595 if (unlikely(!adapter->is_suspended)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530596 mwifiex_dbg(adapter, WARN,
597 "Device already resumed\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700598 return 0;
599 }
600
601 /* Indicate device resumed. The netdev queue will be resumed only
602 * after the urbs have been re-submitted
603 */
604 adapter->is_suspended = false;
605
606 if (!atomic_read(&card->rx_data_urb_pending))
607 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
608 mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
609 MWIFIEX_RX_DATA_BUF_SIZE);
610
611 if (!atomic_read(&card->rx_cmd_urb_pending)) {
612 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
613 if (card->rx_cmd.skb)
614 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
615 MWIFIEX_RX_CMD_BUF_SIZE);
616 }
617
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700618 /* Disable Host Sleep */
619 if (adapter->hs_activated)
620 mwifiex_cancel_hs(mwifiex_get_priv(adapter,
621 MWIFIEX_BSS_ROLE_ANY),
622 MWIFIEX_ASYNC_CMD);
623
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700624 return 0;
625}
626
627static void mwifiex_usb_disconnect(struct usb_interface *intf)
628{
629 struct usb_card_rec *card = usb_get_intfdata(intf);
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700630 struct mwifiex_adapter *adapter;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700631
Brian Norris4a79aa12016-11-18 19:30:26 +0530632 wait_for_completion(&card->fw_done);
633
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700634 adapter = card->adapter;
Brian Norris4a79aa12016-11-18 19:30:26 +0530635 if (!adapter || !adapter->priv_num)
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700636 return;
637
Xinming Hu045f0c12016-11-30 20:22:17 +0530638 if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700639 mwifiex_deauthenticate_all(adapter);
640
641 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
642 MWIFIEX_BSS_ROLE_ANY),
643 MWIFIEX_FUNC_SHUTDOWN);
644 }
645
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700646 mwifiex_usb_free(card);
647
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530648 mwifiex_dbg(adapter, FATAL,
649 "%s: removing card\n", __func__);
Brian Norris4a79aa12016-11-18 19:30:26 +0530650 mwifiex_remove_card(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700651
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700652 usb_put_dev(interface_to_usbdev(intf));
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700653}
654
655static struct usb_driver mwifiex_usb_driver = {
Bing Zhaoe4ceb0f2014-02-21 12:23:00 -0800656 .name = "mwifiex_usb",
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700657 .probe = mwifiex_usb_probe,
658 .disconnect = mwifiex_usb_disconnect,
659 .id_table = mwifiex_usb_table,
660 .suspend = mwifiex_usb_suspend,
661 .resume = mwifiex_usb_resume,
Amitkumar Karwar3fffd7c2014-04-14 15:32:54 -0700662 .soft_unbind = 1,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700663};
664
665static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
666{
667 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700668 struct usb_tx_data_port *port;
669 int i, j;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700670
671 card->tx_cmd.adapter = adapter;
672 card->tx_cmd.ep = card->tx_cmd_ep;
673
674 card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangdbea99d2016-08-11 23:13:07 +0200675 if (!card->tx_cmd.urb)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700676 return -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700677
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700678 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
679 port = &card->port[i];
680 if (!port->tx_data_ep)
681 continue;
682 port->tx_data_ix = 0;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700683 if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
684 port->block_status = false;
685 else
686 port->block_status = true;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700687 for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
688 port->tx_data_list[j].adapter = adapter;
689 port->tx_data_list[j].ep = port->tx_data_ep;
690 port->tx_data_list[j].urb =
691 usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangdbea99d2016-08-11 23:13:07 +0200692 if (!port->tx_data_list[j].urb)
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700693 return -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700694 }
695 }
696
697 return 0;
698}
699
700static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
701{
702 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
703 int i;
704
705 card->rx_cmd.adapter = adapter;
706 card->rx_cmd.ep = card->rx_cmd_ep;
707
708 card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangdbea99d2016-08-11 23:13:07 +0200709 if (!card->rx_cmd.urb)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700710 return -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700711
712 card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530713 if (!card->rx_cmd.skb)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700714 return -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700715
716 if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
717 return -1;
718
719 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
720 card->rx_data_list[i].adapter = adapter;
721 card->rx_data_list[i].ep = card->rx_data_ep;
722
723 card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangdbea99d2016-08-11 23:13:07 +0200724 if (!card->rx_data_list[i].urb)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700725 return -1;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700726 if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
727 MWIFIEX_RX_DATA_BUF_SIZE))
728 return -1;
729 }
730
731 return 0;
732}
733
734static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
735 u32 *len, u8 ep, u32 timeout)
736{
737 struct usb_card_rec *card = adapter->card;
738 int actual_length, ret;
739
740 if (!(*len % card->bulk_out_maxpktsize))
741 (*len)++;
742
743 /* Send the data block */
744 ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
745 *len, &actual_length, timeout);
746 if (ret) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530747 mwifiex_dbg(adapter, ERROR,
748 "usb_bulk_msg for tx failed: %d\n", ret);
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800749 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700750 }
751
752 *len = actual_length;
753
754 return ret;
755}
756
757static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
758 u32 *len, u8 ep, u32 timeout)
759{
760 struct usb_card_rec *card = adapter->card;
761 int actual_length, ret;
762
763 /* Receive the data response */
764 ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
765 *len, &actual_length, timeout);
766 if (ret) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530767 mwifiex_dbg(adapter, ERROR,
768 "usb_bulk_msg for rx failed: %d\n", ret);
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800769 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700770 }
771
772 *len = actual_length;
773
774 return ret;
775}
776
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700777static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
778{
779 struct usb_card_rec *card = adapter->card;
780 u8 active_port = MWIFIEX_USB_EP_DATA;
781 struct mwifiex_private *priv = NULL;
782 int i;
783
784 if (adapter->usb_mc_status) {
785 for (i = 0; i < adapter->priv_num; i++) {
786 priv = adapter->priv[i];
787 if (!priv)
788 continue;
789 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
790 !priv->bss_started) ||
791 (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
792 !priv->media_connected))
793 priv->usb_port = MWIFIEX_USB_EP_DATA;
794 }
795 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
796 card->port[i].block_status = false;
797 } else {
798 for (i = 0; i < adapter->priv_num; i++) {
799 priv = adapter->priv[i];
800 if (!priv)
801 continue;
802 if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
803 priv->bss_started) ||
804 (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
805 priv->media_connected)) {
806 active_port = priv->usb_port;
807 break;
808 }
809 }
810 for (i = 0; i < adapter->priv_num; i++) {
811 priv = adapter->priv[i];
812 if (priv)
813 priv->usb_port = active_port;
814 }
815 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
816 if (active_port == card->port[i].tx_data_ep)
817 card->port[i].block_status = false;
818 else
819 card->port[i].block_status = true;
820 }
821 }
822}
823
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700824static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
825{
826 struct usb_card_rec *card = priv->adapter->card;
827 int idx;
828
829 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
830 if (priv->usb_port == card->port[idx].tx_data_ep)
831 return !card->port[idx].block_status;
832 }
833
834 return false;
835}
836
837static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
838{
839 struct usb_card_rec *card = adapter->card;
840 int i;
841
842 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
843 if (!card->port[i].block_status)
844 return false;
845
846 return true;
847}
848
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700849/* This function write a command/data packet to card. */
850static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
851 struct sk_buff *skb,
852 struct mwifiex_tx_param *tx_param)
853{
854 struct usb_card_rec *card = adapter->card;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700855 struct urb_context *context = NULL;
856 struct usb_tx_data_port *port = NULL;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700857 u8 *data = (u8 *)skb->data;
858 struct urb *tx_urb;
Amitkumar Karwarac3b5612016-09-20 20:49:04 +0530859 int idx, ret = -EINPROGRESS;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700860
861 if (adapter->is_suspended) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530862 mwifiex_dbg(adapter, ERROR,
863 "%s: not allowed while suspended\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700864 return -1;
865 }
866
867 if (adapter->surprise_removed) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530868 mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700869 return -1;
870 }
871
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530872 mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700873
874 if (ep == card->tx_cmd_ep) {
875 context = &card->tx_cmd;
876 } else {
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700877 for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
878 if (ep == card->port[idx].tx_data_ep) {
879 port = &card->port[idx];
880 if (atomic_read(&port->tx_data_urb_pending)
881 >= MWIFIEX_TX_DATA_URB) {
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700882 port->block_status = true;
Amitkumar Karwarac3b5612016-09-20 20:49:04 +0530883 adapter->data_sent =
884 mwifiex_usb_data_sent(adapter);
885 return -EBUSY;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700886 }
887 if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
888 port->tx_data_ix = 0;
889 context =
890 &port->tx_data_list[port->tx_data_ix++];
891 break;
892 }
893 }
894 if (!port) {
895 mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
896 return -1;
897 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700898 }
899
900 context->adapter = adapter;
901 context->ep = ep;
902 context->skb = skb;
903 tx_urb = context->urb;
904
Ganapathi Bhat182f5692017-03-27 18:27:44 +0530905 if (ep == card->tx_cmd_ep &&
906 card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
907 usb_fill_int_urb(tx_urb, card->udev,
908 usb_sndintpipe(card->udev, ep), data,
909 skb->len, mwifiex_usb_tx_complete,
910 (void *)context, card->tx_cmd_interval);
911 else
912 usb_fill_bulk_urb(tx_urb, card->udev,
913 usb_sndbulkpipe(card->udev, ep), data,
914 skb->len, mwifiex_usb_tx_complete,
915 (void *)context);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700916
917 tx_urb->transfer_flags |= URB_ZERO_PACKET;
918
919 if (ep == card->tx_cmd_ep)
920 atomic_inc(&card->tx_cmd_urb_pending);
921 else
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700922 atomic_inc(&port->tx_data_urb_pending);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700923
Cathy Luo5476f802016-09-20 20:49:03 +0530924 if (ep != card->tx_cmd_ep &&
925 atomic_read(&port->tx_data_urb_pending) ==
926 MWIFIEX_TX_DATA_URB) {
927 port->block_status = true;
928 adapter->data_sent = mwifiex_usb_data_sent(adapter);
Amitkumar Karwarac3b5612016-09-20 20:49:04 +0530929 ret = -ENOSR;
Cathy Luo5476f802016-09-20 20:49:03 +0530930 }
931
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700932 if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530933 mwifiex_dbg(adapter, ERROR,
934 "%s: usb_submit_urb failed\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700935 if (ep == card->tx_cmd_ep) {
936 atomic_dec(&card->tx_cmd_urb_pending);
937 } else {
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700938 atomic_dec(&port->tx_data_urb_pending);
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700939 port->block_status = false;
Cathy Luo5476f802016-09-20 20:49:03 +0530940 adapter->data_sent = false;
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700941 if (port->tx_data_ix)
942 port->tx_data_ix--;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700943 else
Zhaoyang Liu308fe292015-09-18 06:32:16 -0700944 port->tx_data_ix = MWIFIEX_TX_DATA_URB;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700945 }
Amitkumar Karwarac3b5612016-09-20 20:49:04 +0530946 ret = -1;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700947 }
948
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -0700949 return ret;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700950}
951
952/* This function register usb device and initialize parameter. */
953static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
954{
955 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
956
957 card->adapter = adapter;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700958
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800959 switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -0700960 case USB8997_PID_1:
961 case USB8997_PID_2:
962 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
963 strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
964 adapter->ext_scan = true;
965 break;
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100966 case USB8766_PID_1:
967 case USB8766_PID_2:
968 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
969 strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
Avinash Patil1fe192d2015-01-23 17:09:19 +0530970 adapter->ext_scan = true;
Kristian Evensen9e6f3f42014-11-20 16:52:58 +0100971 break;
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530972 case USB8801_PID_1:
973 case USB8801_PID_2:
974 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
975 strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
Avinash Patil1fe192d2015-01-23 17:09:19 +0530976 adapter->ext_scan = false;
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +0530977 break;
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800978 case USB8797_PID_1:
979 case USB8797_PID_2:
980 default:
Amitkumar Karwar828cf222014-02-27 19:35:13 -0800981 adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -0800982 strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
983 break;
984 }
985
Zhaoyang Liu2b0f9972015-09-18 06:32:15 -0700986 adapter->usb_mc_status = false;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700987 adapter->usb_mc_setup = false;
Zhaoyang Liu2b0f9972015-09-18 06:32:15 -0700988
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700989 return 0;
990}
991
Amitkumar Karwar2739a952013-07-22 19:17:51 -0700992static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
993{
994 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
995
Ujjal Roy353d2a62013-11-14 19:10:42 -0800996 card->adapter = NULL;
Amitkumar Karwar2739a952013-07-22 19:17:51 -0700997}
998
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700999static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1000 struct mwifiex_fw_image *fw)
1001{
1002 int ret = 0;
1003 u8 *firmware = fw->fw_buf, *recv_buff;
Dan Carpenter394f0ed2016-04-19 07:23:44 -07001004 u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1005 u32 dlen;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001006 u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1007 struct fw_data *fwdata;
1008 struct fw_sync_header sync_fw;
1009 u8 check_winner = 1;
1010
1011 if (!firmware) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301012 mwifiex_dbg(adapter, ERROR,
1013 "No firmware image found! Terminating download\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001014 ret = -1;
1015 goto fw_exit;
1016 }
1017
1018 /* Allocate memory for transmit */
1019 fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
Dan Carpenter3b1f0e82015-08-04 10:49:27 +03001020 if (!fwdata) {
1021 ret = -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001022 goto fw_exit;
Dan Carpenter3b1f0e82015-08-04 10:49:27 +03001023 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001024
1025 /* Allocate memory for receive */
1026 recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
Dan Carpentere0bdef02016-04-19 07:21:58 -07001027 if (!recv_buff) {
1028 ret = -ENOMEM;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001029 goto cleanup;
Dan Carpentere0bdef02016-04-19 07:21:58 -07001030 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001031
1032 do {
1033 /* Send pseudo data to check winner status first */
1034 if (check_winner) {
1035 memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1036 dlen = 0;
1037 } else {
1038 /* copy the header of the fw_data to get the length */
Sasha Levinfc40ca92013-01-02 16:23:01 -08001039 memcpy(&fwdata->fw_hdr, &firmware[tlen],
1040 sizeof(struct fw_header));
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001041
1042 dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1043 dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1044 tlen += sizeof(struct fw_header);
1045
Ganapathi Bhat78776462016-09-12 18:55:27 +05301046 /* Command 7 doesn't have data length field */
1047 if (dnld_cmd == FW_CMD_7)
1048 dlen = 0;
1049
Sasha Levinfc40ca92013-01-02 16:23:01 -08001050 memcpy(fwdata->data, &firmware[tlen], dlen);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001051
1052 fwdata->seq_num = cpu_to_le32(fw_seqnum);
1053 tlen += dlen;
1054 }
1055
1056 /* If the send/receive fails or CRC occurs then retry */
Dan Carpenter394f0ed2016-04-19 07:23:44 -07001057 while (--retries) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001058 u8 *buf = (u8 *)fwdata;
1059 u32 len = FW_DATA_XMIT_SIZE;
1060
1061 /* send the firmware block */
1062 ret = mwifiex_write_data_sync(adapter, buf, &len,
1063 MWIFIEX_USB_EP_CMD_EVENT,
1064 MWIFIEX_USB_TIMEOUT);
1065 if (ret) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301066 mwifiex_dbg(adapter, ERROR,
1067 "write_data_sync: failed: %d\n",
1068 ret);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001069 continue;
1070 }
1071
1072 buf = recv_buff;
1073 len = FW_DNLD_RX_BUF_SIZE;
1074
1075 /* Receive the firmware block response */
1076 ret = mwifiex_read_data_sync(adapter, buf, &len,
1077 MWIFIEX_USB_EP_CMD_EVENT,
1078 MWIFIEX_USB_TIMEOUT);
1079 if (ret) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301080 mwifiex_dbg(adapter, ERROR,
1081 "read_data_sync: failed: %d\n",
1082 ret);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001083 continue;
1084 }
1085
1086 memcpy(&sync_fw, recv_buff,
1087 sizeof(struct fw_sync_header));
1088
1089 /* check 1st firmware block resp for highest bit set */
1090 if (check_winner) {
1091 if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301092 mwifiex_dbg(adapter, WARN,
1093 "USB is not the winner %#x\n",
1094 sync_fw.cmd);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001095
1096 /* returning success */
1097 ret = 0;
1098 goto cleanup;
1099 }
1100
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301101 mwifiex_dbg(adapter, MSG,
1102 "start to download FW...\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001103
1104 check_winner = 0;
1105 break;
1106 }
1107
1108 /* check the firmware block response for CRC errors */
1109 if (sync_fw.cmd) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301110 mwifiex_dbg(adapter, ERROR,
1111 "FW received block with CRC %#x\n",
1112 sync_fw.cmd);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001113 ret = -1;
1114 continue;
1115 }
1116
Dan Carpenter394f0ed2016-04-19 07:23:44 -07001117 retries = USB8XXX_FW_MAX_RETRY + 1;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001118 break;
1119 }
1120 fw_seqnum++;
1121 } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1122
1123cleanup:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301124 mwifiex_dbg(adapter, MSG,
1125 "info: FW download over, size %d bytes\n", tlen);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001126
1127 kfree(recv_buff);
1128 kfree(fwdata);
1129
1130 if (retries)
1131 ret = 0;
1132fw_exit:
1133 return ret;
1134}
1135
1136static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1137 struct mwifiex_fw_image *fw)
1138{
1139 int ret;
1140 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1141
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001142 if (card->usb_boot_state == USB8XXX_FW_DNLD) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001143 ret = mwifiex_prog_fw_w_helper(adapter, fw);
1144 if (ret)
1145 return -1;
1146
1147 /* Boot state changes after successful firmware download */
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001148 if (card->usb_boot_state == USB8XXX_FW_DNLD)
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001149 return -1;
1150 }
1151
1152 ret = mwifiex_usb_rx_init(adapter);
1153 if (!ret)
1154 ret = mwifiex_usb_tx_init(adapter);
1155
1156 return ret;
1157}
1158
1159static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1160{
1161 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1162
1163 skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1164 if ((ep == card->rx_cmd_ep) &&
1165 (!atomic_read(&card->rx_cmd_urb_pending)))
1166 mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1167 MWIFIEX_RX_CMD_BUF_SIZE);
1168
1169 return;
1170}
1171
1172static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1173 struct sk_buff *skb)
1174{
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001175 mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1176
1177 return 0;
1178}
1179
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001180/* This function wakes up the card. */
1181static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1182{
1183 /* Simulation of HS_AWAKE event */
1184 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -07001185 del_timer(&adapter->wakeup_timer);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001186 adapter->pm_wakeup_card_req = false;
1187 adapter->ps_state = PS_STATE_AWAKE;
1188
1189 return 0;
1190}
1191
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +05301192static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1193{
1194 struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1195 int i;
1196 struct urb_context *ctx;
1197
1198 for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1199 if (card->rx_data_list[i].skb)
1200 continue;
1201 ctx = &card->rx_data_list[i];
1202 mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1203 }
1204}
1205
Xinming Hu61754302014-12-23 19:14:08 +05301206/* This function is called after the card has woken up. */
1207static inline int
1208mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1209{
1210 return 0;
1211}
1212
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001213static struct mwifiex_if_ops usb_ops = {
1214 .register_dev = mwifiex_register_dev,
Amitkumar Karwar2739a952013-07-22 19:17:51 -07001215 .unregister_dev = mwifiex_unregister_dev,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001216 .wakeup = mwifiex_pm_wakeup_card,
1217 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1218
1219 /* USB specific */
1220 .dnld_fw = mwifiex_usb_dnld_fw,
1221 .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
1222 .event_complete = mwifiex_usb_cmd_event_complete,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001223 .host_to_card = mwifiex_usb_host_to_card,
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +05301224 .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs,
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -07001225 .multi_port_resync = mwifiex_usb_port_resync,
Zhaoyang Liu735ab6b2015-09-18 06:32:18 -07001226 .is_port_ready = mwifiex_usb_is_port_ready,
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001227};
1228
Amitkumar Karwarc0e6aa42016-12-01 18:38:20 +05301229module_usb_driver(mwifiex_usb_driver);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001230
1231MODULE_AUTHOR("Marvell International Ltd.");
1232MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1233MODULE_VERSION(USB_VERSION);
1234MODULE_LICENSE("GPL v2");
Kristian Evensen9e6f3f42014-11-20 16:52:58 +01001235MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
Yogesh Ashok Powar68458de2014-01-09 19:36:55 -08001236MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
Yogesh Ashok Powareaa3d9f2015-01-23 17:09:18 +05301237MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
Zhaoyang Liu6d85ef02015-08-05 06:09:40 -07001238MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);