1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  */
17 
18 #include <linux/module.h>
19 #include <net/rsi_91x.h>
20 #include "rsi_usb.h"
21 #include "rsi_hal.h"
22 #include "rsi_coex.h"
23 
24 /* Default operating mode is wlan STA + BT */
25 static u16 dev_oper_mode = DEV_OPMODE_STA_BT_DUAL;
26 module_param(dev_oper_mode, ushort, 0444);
27 MODULE_PARM_DESC(dev_oper_mode,
28 		 "1[Wi-Fi], 4[BT], 8[BT LE], 5[Wi-Fi STA + BT classic]\n"
29 		 "9[Wi-Fi STA + BT LE], 13[Wi-Fi STA + BT classic + BT LE]\n"
30 		 "6[AP + BT classic], 14[AP + BT classic + BT LE]");
31 
32 static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num);
33 
34 /**
35  * rsi_usb_card_write() - This function writes to the USB Card.
36  * @adapter: Pointer to the adapter structure.
37  * @buf: Pointer to the buffer from where the data has to be taken.
38  * @len: Length to be written.
39  * @endpoint: Type of endpoint.
40  *
41  * Return: status: 0 on success, a negative error code on failure.
42  */
43 static int rsi_usb_card_write(struct rsi_hw *adapter,
44 			      u8 *buf,
45 			      u16 len,
46 			      u8 endpoint)
47 {
48 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
49 	int status;
50 	u8 *seg = dev->tx_buffer;
51 	int transfer;
52 	int ep = dev->bulkout_endpoint_addr[endpoint - 1];
53 
54 	memset(seg, 0, len + RSI_USB_TX_HEAD_ROOM);
55 	memcpy(seg + RSI_USB_TX_HEAD_ROOM, buf, len);
56 	len += RSI_USB_TX_HEAD_ROOM;
57 	transfer = len;
58 	status = usb_bulk_msg(dev->usbdev,
59 			      usb_sndbulkpipe(dev->usbdev, ep),
60 			      (void *)seg,
61 			      (int)len,
62 			      &transfer,
63 			      HZ * 5);
64 
65 	if (status < 0) {
66 		rsi_dbg(ERR_ZONE,
67 			"Card write failed with error code :%10d\n", status);
68 		dev->write_fail = 1;
69 	}
70 	return status;
71 }
72 
73 /**
74  * rsi_write_multiple() - This function writes multiple bytes of information
75  *			  to the USB card.
76  * @adapter: Pointer to the adapter structure.
77  * @addr: Address of the register.
78  * @data: Pointer to the data that has to be written.
79  * @count: Number of multiple bytes to be written.
80  *
81  * Return: 0 on success, a negative error code on failure.
82  */
83 static int rsi_write_multiple(struct rsi_hw *adapter,
84 			      u8 endpoint,
85 			      u8 *data,
86 			      u32 count)
87 {
88 	struct rsi_91x_usbdev *dev;
89 
90 	if (!adapter)
91 		return -ENODEV;
92 
93 	if (endpoint == 0)
94 		return -EINVAL;
95 
96 	dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
97 	if (dev->write_fail)
98 		return -ENETDOWN;
99 
100 	return rsi_usb_card_write(adapter, data, count, endpoint);
101 }
102 
103 /**
104  * rsi_find_bulk_in_and_out_endpoints() - This function initializes the bulk
105  *					  endpoints to the device.
106  * @interface: Pointer to the USB interface structure.
107  * @adapter: Pointer to the adapter structure.
108  *
109  * Return: ret_val: 0 on success, -ENOMEM on failure.
110  */
111 static int rsi_find_bulk_in_and_out_endpoints(struct usb_interface *interface,
112 					      struct rsi_hw *adapter)
113 {
114 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
115 	struct usb_host_interface *iface_desc;
116 	struct usb_endpoint_descriptor *endpoint;
117 	__le16 buffer_size;
118 	int ii, bin_found = 0, bout_found = 0;
119 
120 	iface_desc = &(interface->altsetting[0]);
121 
122 	for (ii = 0; ii < iface_desc->desc.bNumEndpoints; ++ii) {
123 		endpoint = &(iface_desc->endpoint[ii].desc);
124 
125 		if (!dev->bulkin_endpoint_addr[bin_found] &&
126 		    (endpoint->bEndpointAddress & USB_DIR_IN) &&
127 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
128 		    USB_ENDPOINT_XFER_BULK)) {
129 			buffer_size = endpoint->wMaxPacketSize;
130 			dev->bulkin_size[bin_found] = buffer_size;
131 			dev->bulkin_endpoint_addr[bin_found] =
132 				endpoint->bEndpointAddress;
133 			bin_found++;
134 		}
135 
136 		if (!dev->bulkout_endpoint_addr[bout_found] &&
137 		    !(endpoint->bEndpointAddress & USB_DIR_IN) &&
138 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
139 		    USB_ENDPOINT_XFER_BULK)) {
140 			buffer_size = endpoint->wMaxPacketSize;
141 			dev->bulkout_endpoint_addr[bout_found] =
142 				endpoint->bEndpointAddress;
143 			dev->bulkout_size[bout_found] = buffer_size;
144 			bout_found++;
145 		}
146 
147 		if (bin_found >= MAX_BULK_EP || bout_found >= MAX_BULK_EP)
148 			break;
149 	}
150 
151 	if (!(dev->bulkin_endpoint_addr[0]) &&
152 	    dev->bulkout_endpoint_addr[0])
153 		return -EINVAL;
154 
155 	return 0;
156 }
157 
158 #define RSI_USB_REQ_OUT	(USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE)
159 #define RSI_USB_REQ_IN	(USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE)
160 
161 /* rsi_usb_reg_read() - This function reads data from given register address.
162  * @usbdev: Pointer to the usb_device structure.
163  * @reg: Address of the register to be read.
164  * @value: Value to be read.
165  * @len: length of data to be read.
166  *
167  * Return: status: 0 on success, a negative error code on failure.
168  */
169 static int rsi_usb_reg_read(struct usb_device *usbdev,
170 			    u32 reg,
171 			    u16 *value,
172 			    u16 len)
173 {
174 	u8 *buf;
175 	int status = -ENOMEM;
176 
177 	if (len > RSI_USB_CTRL_BUF_SIZE)
178 		return -EINVAL;
179 
180 	buf  = kmalloc(RSI_USB_CTRL_BUF_SIZE, GFP_KERNEL);
181 	if (!buf)
182 		return status;
183 
184 	status = usb_control_msg(usbdev,
185 				 usb_rcvctrlpipe(usbdev, 0),
186 				 USB_VENDOR_REGISTER_READ,
187 				 RSI_USB_REQ_IN,
188 				 ((reg & 0xffff0000) >> 16), (reg & 0xffff),
189 				 (void *)buf,
190 				 len,
191 				 USB_CTRL_GET_TIMEOUT);
192 
193 	*value = (buf[0] | (buf[1] << 8));
194 	if (status < 0) {
195 		rsi_dbg(ERR_ZONE,
196 			"%s: Reg read failed with error code :%d\n",
197 			__func__, status);
198 	}
199 	kfree(buf);
200 
201 	return status;
202 }
203 
204 /**
205  * rsi_usb_reg_write() - This function writes the given data into the given
206  *			 register address.
207  * @usbdev: Pointer to the usb_device structure.
208  * @reg: Address of the register.
209  * @value: Value to write.
210  * @len: Length of data to be written.
211  *
212  * Return: status: 0 on success, a negative error code on failure.
213  */
214 static int rsi_usb_reg_write(struct usb_device *usbdev,
215 			     u32 reg,
216 			     u32 value,
217 			     u16 len)
218 {
219 	u8 *usb_reg_buf;
220 	int status = -ENOMEM;
221 
222 	if (len > RSI_USB_CTRL_BUF_SIZE)
223 		return -EINVAL;
224 
225 	usb_reg_buf  = kmalloc(RSI_USB_CTRL_BUF_SIZE, GFP_KERNEL);
226 	if (!usb_reg_buf)
227 		return status;
228 
229 	usb_reg_buf[0] = (cpu_to_le32(value) & 0x00ff);
230 	usb_reg_buf[1] = (cpu_to_le32(value) & 0xff00) >> 8;
231 	usb_reg_buf[2] = (cpu_to_le32(value) & 0x00ff0000) >> 16;
232 	usb_reg_buf[3] = (cpu_to_le32(value) & 0xff000000) >> 24;
233 
234 	status = usb_control_msg(usbdev,
235 				 usb_sndctrlpipe(usbdev, 0),
236 				 USB_VENDOR_REGISTER_WRITE,
237 				 RSI_USB_REQ_OUT,
238 				 ((cpu_to_le32(reg) & 0xffff0000) >> 16),
239 				 (cpu_to_le32(reg) & 0xffff),
240 				 (void *)usb_reg_buf,
241 				 len,
242 				 USB_CTRL_SET_TIMEOUT);
243 	if (status < 0) {
244 		rsi_dbg(ERR_ZONE,
245 			"%s: Reg write failed with error code :%d\n",
246 			__func__, status);
247 	}
248 	kfree(usb_reg_buf);
249 
250 	return status;
251 }
252 
253 /**
254  * rsi_rx_done_handler() - This function is called when a packet is received
255  *			   from USB stack. This is callback to receive done.
256  * @urb: Received URB.
257  *
258  * Return: None.
259  */
260 static void rsi_rx_done_handler(struct urb *urb)
261 {
262 	struct rx_usb_ctrl_block *rx_cb = urb->context;
263 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)rx_cb->data;
264 	int status = -EINVAL;
265 
266 	if (urb->status) {
267 		dev_kfree_skb(rx_cb->rx_skb);
268 		return;
269 	}
270 
271 	if (urb->actual_length <= 0 ||
272 	    urb->actual_length > rx_cb->rx_skb->len) {
273 		rsi_dbg(INFO_ZONE, "%s: Invalid packet length = %d\n",
274 			__func__, urb->actual_length);
275 		goto out;
276 	}
277 	if (skb_queue_len(&dev->rx_q) >= RSI_MAX_RX_PKTS) {
278 		rsi_dbg(INFO_ZONE, "Max RX packets reached\n");
279 		goto out;
280 	}
281 	skb_trim(rx_cb->rx_skb, urb->actual_length);
282 	skb_queue_tail(&dev->rx_q, rx_cb->rx_skb);
283 
284 	rsi_set_event(&dev->rx_thread.event);
285 	status = 0;
286 
287 out:
288 	if (rsi_rx_urb_submit(dev->priv, rx_cb->ep_num))
289 		rsi_dbg(ERR_ZONE, "%s: Failed in urb submission", __func__);
290 
291 	if (status)
292 		dev_kfree_skb(rx_cb->rx_skb);
293 }
294 
295 /**
296  * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
297  * @adapter: Pointer to the adapter structure.
298  *
299  * Return: 0 on success, a negative error code on failure.
300  */
301 static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num)
302 {
303 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
304 	struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
305 	struct urb *urb = rx_cb->rx_urb;
306 	int status;
307 	struct sk_buff *skb;
308 	u8 dword_align_bytes = 0;
309 
310 #define RSI_MAX_RX_USB_PKT_SIZE	3000
311 	skb = dev_alloc_skb(RSI_MAX_RX_USB_PKT_SIZE);
312 	if (!skb)
313 		return -ENOMEM;
314 	skb_reserve(skb, MAX_DWORD_ALIGN_BYTES);
315 	skb_put(skb, RSI_MAX_RX_USB_PKT_SIZE - MAX_DWORD_ALIGN_BYTES);
316 	dword_align_bytes = (unsigned long)skb->data & 0x3f;
317 	if (dword_align_bytes > 0)
318 		skb_push(skb, dword_align_bytes);
319 	urb->transfer_buffer = skb->data;
320 	rx_cb->rx_skb = skb;
321 
322 	usb_fill_bulk_urb(urb,
323 			  dev->usbdev,
324 			  usb_rcvbulkpipe(dev->usbdev,
325 			  dev->bulkin_endpoint_addr[ep_num - 1]),
326 			  urb->transfer_buffer,
327 			  skb->len,
328 			  rsi_rx_done_handler,
329 			  rx_cb);
330 
331 	status = usb_submit_urb(urb, GFP_KERNEL);
332 	if (status)
333 		rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
334 
335 	return status;
336 }
337 
338 static int rsi_usb_read_register_multiple(struct rsi_hw *adapter, u32 addr,
339 					  u8 *data, u16 count)
340 {
341 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
342 	u8 *buf;
343 	u16 transfer;
344 	int status;
345 
346 	if (!addr)
347 		return -EINVAL;
348 
349 	buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
350 	if (!buf)
351 		return -ENOMEM;
352 
353 	while (count) {
354 		transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
355 		status = usb_control_msg(dev->usbdev,
356 					 usb_rcvctrlpipe(dev->usbdev, 0),
357 					 USB_VENDOR_REGISTER_READ,
358 					 RSI_USB_REQ_IN,
359 					 ((addr & 0xffff0000) >> 16),
360 					 (addr & 0xffff), (void *)buf,
361 					 transfer, USB_CTRL_GET_TIMEOUT);
362 		if (status < 0) {
363 			rsi_dbg(ERR_ZONE,
364 				"Reg read failed with error code :%d\n",
365 				 status);
366 			kfree(buf);
367 			return status;
368 		}
369 		memcpy(data, buf, transfer);
370 		count -= transfer;
371 		data += transfer;
372 		addr += transfer;
373 	}
374 	kfree(buf);
375 	return 0;
376 }
377 
378 /**
379  * rsi_usb_write_register_multiple() - This function writes multiple bytes of
380  *				       information to multiple registers.
381  * @adapter: Pointer to the adapter structure.
382  * @addr: Address of the register.
383  * @data: Pointer to the data that has to be written.
384  * @count: Number of multiple bytes to be written on to the registers.
385  *
386  * Return: status: 0 on success, a negative error code on failure.
387  */
388 static int rsi_usb_write_register_multiple(struct rsi_hw *adapter, u32 addr,
389 					   u8 *data, u16 count)
390 {
391 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
392 	u8 *buf;
393 	u16 transfer;
394 	int status = 0;
395 
396 	buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
397 	if (!buf)
398 		return -ENOMEM;
399 
400 	while (count) {
401 		transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
402 		memcpy(buf, data, transfer);
403 		status = usb_control_msg(dev->usbdev,
404 					 usb_sndctrlpipe(dev->usbdev, 0),
405 					 USB_VENDOR_REGISTER_WRITE,
406 					 RSI_USB_REQ_OUT,
407 					 ((addr & 0xffff0000) >> 16),
408 					 (addr & 0xffff),
409 					 (void *)buf,
410 					 transfer,
411 					 USB_CTRL_SET_TIMEOUT);
412 		if (status < 0) {
413 			rsi_dbg(ERR_ZONE,
414 				"Reg write failed with error code :%d\n",
415 				status);
416 			kfree(buf);
417 			return status;
418 		}
419 		count -= transfer;
420 		data += transfer;
421 		addr += transfer;
422 	}
423 
424 	kfree(buf);
425 	return 0;
426 }
427 
428 /**
429  *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
430  *				   USB card.
431  * @adapter: Pointer to the adapter structure.
432  * @pkt: Pointer to the data to be written on to the card.
433  * @len: Length of the data to be written on to the card.
434  *
435  * Return: 0 on success, a negative error code on failure.
436  */
437 static int rsi_usb_host_intf_write_pkt(struct rsi_hw *adapter,
438 				       u8 *pkt,
439 				       u32 len)
440 {
441 	u32 queueno = ((pkt[1] >> 4) & 0x7);
442 	u8 endpoint;
443 
444 	endpoint = ((queueno == RSI_WIFI_MGMT_Q || queueno == RSI_WIFI_DATA_Q ||
445 		     queueno == RSI_COEX_Q) ? WLAN_EP : BT_EP);
446 
447 	return rsi_write_multiple(adapter,
448 				  endpoint,
449 				  (u8 *)pkt,
450 				  len);
451 }
452 
453 static int rsi_usb_master_reg_read(struct rsi_hw *adapter, u32 reg,
454 				   u32 *value, u16 len)
455 {
456 	struct usb_device *usbdev =
457 		((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
458 	u16 temp;
459 	int ret;
460 
461 	ret = rsi_usb_reg_read(usbdev, reg, &temp, len);
462 	if (ret < 0)
463 		return ret;
464 	*value = temp;
465 
466 	return 0;
467 }
468 
469 static int rsi_usb_master_reg_write(struct rsi_hw *adapter,
470 				    unsigned long reg,
471 				    unsigned long value, u16 len)
472 {
473 	struct usb_device *usbdev =
474 		((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
475 
476 	return rsi_usb_reg_write(usbdev, reg, value, len);
477 }
478 
479 static int rsi_usb_load_data_master_write(struct rsi_hw *adapter,
480 					  u32 base_address,
481 					  u32 instructions_sz, u16 block_size,
482 					  u8 *ta_firmware)
483 {
484 	u16 num_blocks;
485 	u32 cur_indx, i;
486 	u8 temp_buf[256];
487 	int status;
488 
489 	num_blocks = instructions_sz / block_size;
490 	rsi_dbg(INFO_ZONE, "num_blocks: %d\n", num_blocks);
491 
492 	for (cur_indx = 0, i = 0; i < num_blocks; i++, cur_indx += block_size) {
493 		memcpy(temp_buf, ta_firmware + cur_indx, block_size);
494 		status = rsi_usb_write_register_multiple(adapter, base_address,
495 							 (u8 *)(temp_buf),
496 							 block_size);
497 		if (status < 0)
498 			return status;
499 
500 		rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
501 		base_address += block_size;
502 	}
503 
504 	if (instructions_sz % block_size) {
505 		memset(temp_buf, 0, block_size);
506 		memcpy(temp_buf, ta_firmware + cur_indx,
507 		       instructions_sz % block_size);
508 		status = rsi_usb_write_register_multiple
509 						(adapter, base_address,
510 						 (u8 *)temp_buf,
511 						 instructions_sz % block_size);
512 		if (status < 0)
513 			return status;
514 		rsi_dbg(INFO_ZONE,
515 			"Written Last Block in Address 0x%x Successfully\n",
516 			cur_indx);
517 	}
518 	return 0;
519 }
520 
521 static struct rsi_host_intf_ops usb_host_intf_ops = {
522 	.write_pkt		= rsi_usb_host_intf_write_pkt,
523 	.read_reg_multiple	= rsi_usb_read_register_multiple,
524 	.write_reg_multiple	= rsi_usb_write_register_multiple,
525 	.master_reg_read	= rsi_usb_master_reg_read,
526 	.master_reg_write	= rsi_usb_master_reg_write,
527 	.load_data_master_write	= rsi_usb_load_data_master_write,
528 };
529 
530 /**
531  * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
532  * @adapter: Pointer to the adapter structure.
533  *
534  * Return: None.
535  */
536 static void rsi_deinit_usb_interface(struct rsi_hw *adapter)
537 {
538 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
539 
540 	rsi_kill_thread(&dev->rx_thread);
541 
542 	usb_free_urb(dev->rx_cb[0].rx_urb);
543 	if (adapter->priv->coex_mode > 1)
544 		usb_free_urb(dev->rx_cb[1].rx_urb);
545 
546 	kfree(dev->tx_buffer);
547 }
548 
549 static int rsi_usb_init_rx(struct rsi_hw *adapter)
550 {
551 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
552 	struct rx_usb_ctrl_block *rx_cb;
553 	u8 idx, num_rx_cb;
554 
555 	num_rx_cb = (adapter->priv->coex_mode > 1 ? 2 : 1);
556 
557 	for (idx = 0; idx < num_rx_cb; idx++) {
558 		rx_cb = &dev->rx_cb[idx];
559 
560 		rx_cb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
561 		if (!rx_cb->rx_urb) {
562 			rsi_dbg(ERR_ZONE, "Failed alloc rx urb[%d]\n", idx);
563 			goto err;
564 		}
565 		rx_cb->ep_num = idx + 1;
566 		rx_cb->data = (void *)dev;
567 	}
568 	skb_queue_head_init(&dev->rx_q);
569 	rsi_init_event(&dev->rx_thread.event);
570 	if (rsi_create_kthread(adapter->priv, &dev->rx_thread,
571 			       rsi_usb_rx_thread, "RX-Thread")) {
572 		rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
573 		goto err;
574 	}
575 
576 	return 0;
577 
578 err:
579 	usb_free_urb(dev->rx_cb[0].rx_urb);
580 	if (adapter->priv->coex_mode > 1)
581 		usb_free_urb(dev->rx_cb[1].rx_urb);
582 
583 	return -1;
584 }
585 
586 /**
587  * rsi_init_usb_interface() - This function initializes the usb interface.
588  * @adapter: Pointer to the adapter structure.
589  * @pfunction: Pointer to USB interface structure.
590  *
591  * Return: 0 on success, a negative error code on failure.
592  */
593 static int rsi_init_usb_interface(struct rsi_hw *adapter,
594 				  struct usb_interface *pfunction)
595 {
596 	struct rsi_91x_usbdev *rsi_dev;
597 	int status;
598 
599 	rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
600 	if (!rsi_dev)
601 		return -ENOMEM;
602 
603 	adapter->rsi_dev = rsi_dev;
604 	rsi_dev->usbdev = interface_to_usbdev(pfunction);
605 	rsi_dev->priv = (void *)adapter;
606 
607 	if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter)) {
608 		status = -EINVAL;
609 		goto fail_eps;
610 	}
611 
612 	adapter->device = &pfunction->dev;
613 	usb_set_intfdata(pfunction, adapter);
614 
615 	rsi_dev->tx_buffer = kmalloc(2048, GFP_KERNEL);
616 	if (!rsi_dev->tx_buffer) {
617 		status = -ENOMEM;
618 		goto fail_eps;
619 	}
620 
621 	if (rsi_usb_init_rx(adapter)) {
622 		rsi_dbg(ERR_ZONE, "Failed to init RX handle\n");
623 		status = -ENOMEM;
624 		goto fail_rx;
625 	}
626 
627 	rsi_dev->tx_blk_size = 252;
628 	adapter->block_size = rsi_dev->tx_blk_size;
629 
630 	/* Initializing function callbacks */
631 	adapter->check_hw_queue_status = rsi_usb_check_queue_status;
632 	adapter->determine_event_timeout = rsi_usb_event_timeout;
633 	adapter->rsi_host_intf = RSI_HOST_INTF_USB;
634 	adapter->host_intf_ops = &usb_host_intf_ops;
635 
636 #ifdef CONFIG_RSI_DEBUGFS
637 	/* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
638 	adapter->num_debugfs_entries = (MAX_DEBUGFS_ENTRIES - 1);
639 #endif
640 
641 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
642 	return 0;
643 
644 fail_rx:
645 	kfree(rsi_dev->tx_buffer);
646 
647 fail_eps:
648 
649 	return status;
650 }
651 
652 static int usb_ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
653 			      u16 len_in_bits)
654 {
655 	int ret;
656 
657 	ret = rsi_usb_master_reg_write
658 			(adapter, RSI_GSPI_DATA_REG1,
659 			 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
660 	if (ret < 0)
661 		return ret;
662 
663 	ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
664 				       (data & 0xffff), 2);
665 	if (ret < 0)
666 		return ret;
667 
668 	/* Initializing GSPI for ULP read/writes */
669 	rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
670 				 RSI_GSPI_CTRL_REG0_VALUE, 2);
671 
672 	ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
673 				       ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
674 	if (ret < 0)
675 		return ret;
676 
677 	msleep(20);
678 
679 	return 0;
680 }
681 
682 static int rsi_reset_card(struct rsi_hw *adapter)
683 {
684 	int ret;
685 
686 	rsi_dbg(INFO_ZONE, "Resetting Card...\n");
687 	rsi_usb_master_reg_write(adapter, RSI_TA_HOLD_REG, 0xE, 4);
688 
689 	/* This msleep will ensure Thread-Arch processor to go to hold
690 	 * and any pending dma transfers to rf in device to finish.
691 	 */
692 	msleep(100);
693 
694 	ret = rsi_usb_master_reg_write(adapter, SWBL_REGOUT,
695 				       RSI_FW_WDT_DISABLE_REQ,
696 				       RSI_COMMON_REG_SIZE);
697 	if (ret < 0) {
698 		rsi_dbg(ERR_ZONE, "Disabling firmware watchdog timer failed\n");
699 		goto fail;
700 	}
701 
702 	if (adapter->device_model != RSI_DEV_9116) {
703 		ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1,
704 					 RSI_ULP_WRITE_2, 32);
705 		if (ret < 0)
706 			goto fail;
707 		ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2,
708 					 RSI_ULP_WRITE_0, 32);
709 		if (ret < 0)
710 			goto fail;
711 		ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
712 					 RSI_ULP_WRITE_50, 32);
713 		if (ret < 0)
714 			goto fail;
715 		ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
716 					 RSI_ULP_WRITE_0, 32);
717 		if (ret < 0)
718 			goto fail;
719 		ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
720 					 RSI_ULP_TIMER_ENABLE, 32);
721 		if (ret < 0)
722 			goto fail;
723 	} else {
724 		if ((rsi_usb_master_reg_write(adapter,
725 					      NWP_WWD_INTERRUPT_TIMER,
726 					      NWP_WWD_INT_TIMER_CLKS,
727 					      RSI_9116_REG_SIZE)) < 0) {
728 			goto fail;
729 		}
730 		if ((rsi_usb_master_reg_write(adapter,
731 					      NWP_WWD_SYSTEM_RESET_TIMER,
732 					      NWP_WWD_SYS_RESET_TIMER_CLKS,
733 					      RSI_9116_REG_SIZE)) < 0) {
734 			goto fail;
735 		}
736 		if ((rsi_usb_master_reg_write(adapter,
737 					      NWP_WWD_MODE_AND_RSTART,
738 					      NWP_WWD_TIMER_DISABLE,
739 					      RSI_9116_REG_SIZE)) < 0) {
740 			goto fail;
741 		}
742 	}
743 
744 	rsi_dbg(INFO_ZONE, "Reset card done\n");
745 	return ret;
746 
747 fail:
748 	rsi_dbg(ERR_ZONE, "Reset card failed\n");
749 	return ret;
750 }
751 
752 /**
753  * rsi_probe() - This function is called by kernel when the driver provided
754  *		 Vendor and device IDs are matched. All the initialization
755  *		 work is done here.
756  * @pfunction: Pointer to the USB interface structure.
757  * @id: Pointer to the usb_device_id structure.
758  *
759  * Return: 0 on success, a negative error code on failure.
760  */
761 static int rsi_probe(struct usb_interface *pfunction,
762 		     const struct usb_device_id *id)
763 {
764 	struct rsi_hw *adapter;
765 	struct rsi_91x_usbdev *dev;
766 	u16 fw_status;
767 	int status;
768 
769 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
770 
771 	adapter = rsi_91x_init(dev_oper_mode);
772 	if (!adapter) {
773 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
774 			__func__);
775 		return -ENOMEM;
776 	}
777 	adapter->rsi_host_intf = RSI_HOST_INTF_USB;
778 
779 	status = rsi_init_usb_interface(adapter, pfunction);
780 	if (status) {
781 		rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
782 			__func__);
783 		goto err;
784 	}
785 
786 	rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
787 
788 	if (id && id->idProduct == RSI_USB_PID_9113) {
789 		rsi_dbg(INIT_ZONE, "%s: 9113 module detected\n", __func__);
790 		adapter->device_model = RSI_DEV_9113;
791 	} else if (id && id->idProduct == RSI_USB_PID_9116) {
792 		rsi_dbg(INIT_ZONE, "%s: 9116 module detected\n", __func__);
793 		adapter->device_model = RSI_DEV_9116;
794 	} else {
795 		rsi_dbg(ERR_ZONE, "%s: Unsupported RSI device id 0x%x\n",
796 			__func__, id->idProduct);
797 		goto err1;
798 	}
799 
800 	dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
801 
802 	status = rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2);
803 	if (status < 0)
804 		goto err1;
805 	else
806 		fw_status &= 1;
807 
808 	if (!fw_status) {
809 		rsi_dbg(INIT_ZONE, "Loading firmware...\n");
810 		status = rsi_hal_device_init(adapter);
811 		if (status) {
812 			rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
813 				__func__);
814 			goto err1;
815 		}
816 		rsi_dbg(INIT_ZONE, "%s: Device Init Done\n", __func__);
817 	}
818 
819 	status = rsi_rx_urb_submit(adapter, WLAN_EP);
820 	if (status)
821 		goto err1;
822 
823 	if (adapter->priv->coex_mode > 1) {
824 		status = rsi_rx_urb_submit(adapter, BT_EP);
825 		if (status)
826 			goto err1;
827 	}
828 
829 	return 0;
830 err1:
831 	rsi_deinit_usb_interface(adapter);
832 err:
833 	rsi_91x_deinit(adapter);
834 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
835 	return status;
836 }
837 
838 /**
839  * rsi_disconnect() - This function performs the reverse of the probe function,
840  *		      it deinitialize the driver structure.
841  * @pfunction: Pointer to the USB interface structure.
842  *
843  * Return: None.
844  */
845 static void rsi_disconnect(struct usb_interface *pfunction)
846 {
847 	struct rsi_hw *adapter = usb_get_intfdata(pfunction);
848 
849 	if (!adapter)
850 		return;
851 
852 	rsi_mac80211_detach(adapter);
853 
854 	if (IS_ENABLED(CONFIG_RSI_COEX) && adapter->priv->coex_mode > 1 &&
855 	    adapter->priv->bt_adapter) {
856 		rsi_bt_ops.detach(adapter->priv->bt_adapter);
857 		adapter->priv->bt_adapter = NULL;
858 	}
859 
860 	rsi_reset_card(adapter);
861 	rsi_deinit_usb_interface(adapter);
862 	rsi_91x_deinit(adapter);
863 
864 	rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
865 }
866 
867 #ifdef CONFIG_PM
868 static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
869 {
870 	/* Not yet implemented */
871 	return -ENOSYS;
872 }
873 
874 static int rsi_resume(struct usb_interface *intf)
875 {
876 	/* Not yet implemented */
877 	return -ENOSYS;
878 }
879 #endif
880 
881 static const struct usb_device_id rsi_dev_table[] = {
882 	{ USB_DEVICE(RSI_USB_VENDOR_ID, RSI_USB_PID_9113) },
883 	{ USB_DEVICE(RSI_USB_VENDOR_ID, RSI_USB_PID_9116) },
884 	{ /* Blank */},
885 };
886 
887 static struct usb_driver rsi_driver = {
888 	.name       = "RSI-USB WLAN",
889 	.probe      = rsi_probe,
890 	.disconnect = rsi_disconnect,
891 	.id_table   = rsi_dev_table,
892 #ifdef CONFIG_PM
893 	.suspend    = rsi_suspend,
894 	.resume     = rsi_resume,
895 #endif
896 };
897 
898 module_usb_driver(rsi_driver);
899 
900 MODULE_AUTHOR("Redpine Signals Inc");
901 MODULE_DESCRIPTION("Common USB layer for RSI drivers");
902 MODULE_SUPPORTED_DEVICE("RSI-91x");
903 MODULE_DEVICE_TABLE(usb, rsi_dev_table);
904 MODULE_FIRMWARE(FIRMWARE_RSI9113);
905 MODULE_VERSION("0.1");
906 MODULE_LICENSE("Dual BSD/GPL");
907