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 			     u16 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] = (value & 0x00ff);
230 	usb_reg_buf[1] = (value & 0xff00) >> 8;
231 	usb_reg_buf[2] = 0x0;
232 	usb_reg_buf[3] = 0x0;
233 
234 	status = usb_control_msg(usbdev,
235 				 usb_sndctrlpipe(usbdev, 0),
236 				 USB_VENDOR_REGISTER_WRITE,
237 				 RSI_USB_REQ_OUT,
238 				 ((reg & 0xffff0000) >> 16),
239 				 (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 recieve 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 		goto out;
268 
269 	if (urb->actual_length <= 0) {
270 		rsi_dbg(INFO_ZONE, "%s: Zero length packet\n", __func__);
271 		goto out;
272 	}
273 	if (skb_queue_len(&dev->rx_q) >= RSI_MAX_RX_PKTS) {
274 		rsi_dbg(INFO_ZONE, "Max RX packets reached\n");
275 		goto out;
276 	}
277 	skb_put(rx_cb->rx_skb, urb->actual_length);
278 	skb_queue_tail(&dev->rx_q, rx_cb->rx_skb);
279 
280 	rsi_set_event(&dev->rx_thread.event);
281 	status = 0;
282 
283 out:
284 	if (rsi_rx_urb_submit(dev->priv, rx_cb->ep_num))
285 		rsi_dbg(ERR_ZONE, "%s: Failed in urb submission", __func__);
286 
287 	if (status)
288 		dev_kfree_skb(rx_cb->rx_skb);
289 }
290 
291 /**
292  * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
293  * @adapter: Pointer to the adapter structure.
294  *
295  * Return: 0 on success, a negative error code on failure.
296  */
297 static int rsi_rx_urb_submit(struct rsi_hw *adapter, u8 ep_num)
298 {
299 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
300 	struct rx_usb_ctrl_block *rx_cb = &dev->rx_cb[ep_num - 1];
301 	struct urb *urb = rx_cb->rx_urb;
302 	int status;
303 	struct sk_buff *skb;
304 	u8 dword_align_bytes = 0;
305 
306 #define RSI_MAX_RX_USB_PKT_SIZE	3000
307 	skb = dev_alloc_skb(RSI_MAX_RX_USB_PKT_SIZE);
308 	if (!skb)
309 		return -ENOMEM;
310 	skb_reserve(skb, MAX_DWORD_ALIGN_BYTES);
311 	dword_align_bytes = (unsigned long)skb->data & 0x3f;
312 	if (dword_align_bytes > 0)
313 		skb_push(skb, dword_align_bytes);
314 	urb->transfer_buffer = skb->data;
315 	rx_cb->rx_skb = skb;
316 
317 	usb_fill_bulk_urb(urb,
318 			  dev->usbdev,
319 			  usb_rcvbulkpipe(dev->usbdev,
320 			  dev->bulkin_endpoint_addr[ep_num - 1]),
321 			  urb->transfer_buffer,
322 			  RSI_MAX_RX_USB_PKT_SIZE,
323 			  rsi_rx_done_handler,
324 			  rx_cb);
325 
326 	status = usb_submit_urb(urb, GFP_KERNEL);
327 	if (status)
328 		rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
329 
330 	return status;
331 }
332 
333 static int rsi_usb_read_register_multiple(struct rsi_hw *adapter, u32 addr,
334 					  u8 *data, u16 count)
335 {
336 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
337 	u8 *buf;
338 	u16 transfer;
339 	int status;
340 
341 	if (!addr)
342 		return -EINVAL;
343 
344 	buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
345 	if (!buf)
346 		return -ENOMEM;
347 
348 	while (count) {
349 		transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
350 		status = usb_control_msg(dev->usbdev,
351 					 usb_rcvctrlpipe(dev->usbdev, 0),
352 					 USB_VENDOR_REGISTER_READ,
353 					 RSI_USB_REQ_IN,
354 					 ((addr & 0xffff0000) >> 16),
355 					 (addr & 0xffff), (void *)buf,
356 					 transfer, USB_CTRL_GET_TIMEOUT);
357 		if (status < 0) {
358 			rsi_dbg(ERR_ZONE,
359 				"Reg read failed with error code :%d\n",
360 				 status);
361 			kfree(buf);
362 			return status;
363 		}
364 		memcpy(data, buf, transfer);
365 		count -= transfer;
366 		data += transfer;
367 		addr += transfer;
368 	}
369 	kfree(buf);
370 	return 0;
371 }
372 
373 /**
374  * rsi_usb_write_register_multiple() - This function writes multiple bytes of
375  *				       information to multiple registers.
376  * @adapter: Pointer to the adapter structure.
377  * @addr: Address of the register.
378  * @data: Pointer to the data that has to be written.
379  * @count: Number of multiple bytes to be written on to the registers.
380  *
381  * Return: status: 0 on success, a negative error code on failure.
382  */
383 static int rsi_usb_write_register_multiple(struct rsi_hw *adapter, u32 addr,
384 					   u8 *data, u16 count)
385 {
386 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
387 	u8 *buf;
388 	u16 transfer;
389 	int status = 0;
390 
391 	buf = kzalloc(RSI_USB_BUF_SIZE, GFP_KERNEL);
392 	if (!buf)
393 		return -ENOMEM;
394 
395 	while (count) {
396 		transfer = min_t(u16, count, RSI_USB_BUF_SIZE);
397 		memcpy(buf, data, transfer);
398 		status = usb_control_msg(dev->usbdev,
399 					 usb_sndctrlpipe(dev->usbdev, 0),
400 					 USB_VENDOR_REGISTER_WRITE,
401 					 RSI_USB_REQ_OUT,
402 					 ((addr & 0xffff0000) >> 16),
403 					 (addr & 0xffff),
404 					 (void *)buf,
405 					 transfer,
406 					 USB_CTRL_SET_TIMEOUT);
407 		if (status < 0) {
408 			rsi_dbg(ERR_ZONE,
409 				"Reg write failed with error code :%d\n",
410 				status);
411 			kfree(buf);
412 			return status;
413 		}
414 		count -= transfer;
415 		data += transfer;
416 		addr += transfer;
417 	}
418 
419 	kfree(buf);
420 	return 0;
421 }
422 
423 /**
424  *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
425  *				   USB card.
426  * @adapter: Pointer to the adapter structure.
427  * @pkt: Pointer to the data to be written on to the card.
428  * @len: Length of the data to be written on to the card.
429  *
430  * Return: 0 on success, a negative error code on failure.
431  */
432 static int rsi_usb_host_intf_write_pkt(struct rsi_hw *adapter,
433 				       u8 *pkt,
434 				       u32 len)
435 {
436 	u32 queueno = ((pkt[1] >> 4) & 0x7);
437 	u8 endpoint;
438 
439 	endpoint = ((queueno == RSI_WIFI_MGMT_Q || queueno == RSI_WIFI_DATA_Q ||
440 		     queueno == RSI_COEX_Q) ? WLAN_EP : BT_EP);
441 
442 	return rsi_write_multiple(adapter,
443 				  endpoint,
444 				  (u8 *)pkt,
445 				  len);
446 }
447 
448 static int rsi_usb_master_reg_read(struct rsi_hw *adapter, u32 reg,
449 				   u32 *value, u16 len)
450 {
451 	struct usb_device *usbdev =
452 		((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
453 	u16 temp;
454 	int ret;
455 
456 	ret = rsi_usb_reg_read(usbdev, reg, &temp, len);
457 	if (ret < 0)
458 		return ret;
459 	*value = temp;
460 
461 	return 0;
462 }
463 
464 static int rsi_usb_master_reg_write(struct rsi_hw *adapter,
465 				    unsigned long reg,
466 				    unsigned long value, u16 len)
467 {
468 	struct usb_device *usbdev =
469 		((struct rsi_91x_usbdev *)adapter->rsi_dev)->usbdev;
470 
471 	return rsi_usb_reg_write(usbdev, reg, value, len);
472 }
473 
474 static int rsi_usb_load_data_master_write(struct rsi_hw *adapter,
475 					  u32 base_address,
476 					  u32 instructions_sz, u16 block_size,
477 					  u8 *ta_firmware)
478 {
479 	u16 num_blocks;
480 	u32 cur_indx, i;
481 	u8 temp_buf[256];
482 	int status;
483 
484 	num_blocks = instructions_sz / block_size;
485 	rsi_dbg(INFO_ZONE, "num_blocks: %d\n", num_blocks);
486 
487 	for (cur_indx = 0, i = 0; i < num_blocks; i++, cur_indx += block_size) {
488 		memcpy(temp_buf, ta_firmware + cur_indx, block_size);
489 		status = rsi_usb_write_register_multiple(adapter, base_address,
490 							 (u8 *)(temp_buf),
491 							 block_size);
492 		if (status < 0)
493 			return status;
494 
495 		rsi_dbg(INFO_ZONE, "%s: loading block: %d\n", __func__, i);
496 		base_address += block_size;
497 	}
498 
499 	if (instructions_sz % block_size) {
500 		memset(temp_buf, 0, block_size);
501 		memcpy(temp_buf, ta_firmware + cur_indx,
502 		       instructions_sz % block_size);
503 		status = rsi_usb_write_register_multiple
504 						(adapter, base_address,
505 						 (u8 *)temp_buf,
506 						 instructions_sz % block_size);
507 		if (status < 0)
508 			return status;
509 		rsi_dbg(INFO_ZONE,
510 			"Written Last Block in Address 0x%x Successfully\n",
511 			cur_indx);
512 	}
513 	return 0;
514 }
515 
516 static struct rsi_host_intf_ops usb_host_intf_ops = {
517 	.write_pkt		= rsi_usb_host_intf_write_pkt,
518 	.read_reg_multiple	= rsi_usb_read_register_multiple,
519 	.write_reg_multiple	= rsi_usb_write_register_multiple,
520 	.master_reg_read	= rsi_usb_master_reg_read,
521 	.master_reg_write	= rsi_usb_master_reg_write,
522 	.load_data_master_write	= rsi_usb_load_data_master_write,
523 };
524 
525 /**
526  * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
527  * @adapter: Pointer to the adapter structure.
528  *
529  * Return: None.
530  */
531 static void rsi_deinit_usb_interface(struct rsi_hw *adapter)
532 {
533 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
534 
535 	rsi_kill_thread(&dev->rx_thread);
536 
537 	usb_free_urb(dev->rx_cb[0].rx_urb);
538 	if (adapter->priv->coex_mode > 1)
539 		usb_free_urb(dev->rx_cb[1].rx_urb);
540 
541 	kfree(dev->tx_buffer);
542 }
543 
544 static int rsi_usb_init_rx(struct rsi_hw *adapter)
545 {
546 	struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
547 	struct rx_usb_ctrl_block *rx_cb;
548 	u8 idx, num_rx_cb;
549 
550 	num_rx_cb = (adapter->priv->coex_mode > 1 ? 2 : 1);
551 
552 	for (idx = 0; idx < num_rx_cb; idx++) {
553 		rx_cb = &dev->rx_cb[idx];
554 
555 		rx_cb->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
556 		if (!rx_cb->rx_urb) {
557 			rsi_dbg(ERR_ZONE, "Failed alloc rx urb[%d]\n", idx);
558 			goto err;
559 		}
560 		rx_cb->ep_num = idx + 1;
561 		rx_cb->data = (void *)dev;
562 	}
563 	skb_queue_head_init(&dev->rx_q);
564 	rsi_init_event(&dev->rx_thread.event);
565 	if (rsi_create_kthread(adapter->priv, &dev->rx_thread,
566 			       rsi_usb_rx_thread, "RX-Thread")) {
567 		rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
568 		goto err;
569 	}
570 
571 	return 0;
572 
573 err:
574 	usb_free_urb(dev->rx_cb[0].rx_urb);
575 	if (adapter->priv->coex_mode > 1)
576 		usb_free_urb(dev->rx_cb[1].rx_urb);
577 
578 	return -1;
579 }
580 
581 /**
582  * rsi_init_usb_interface() - This function initializes the usb interface.
583  * @adapter: Pointer to the adapter structure.
584  * @pfunction: Pointer to USB interface structure.
585  *
586  * Return: 0 on success, a negative error code on failure.
587  */
588 static int rsi_init_usb_interface(struct rsi_hw *adapter,
589 				  struct usb_interface *pfunction)
590 {
591 	struct rsi_91x_usbdev *rsi_dev;
592 	int status;
593 
594 	rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
595 	if (!rsi_dev)
596 		return -ENOMEM;
597 
598 	adapter->rsi_dev = rsi_dev;
599 	rsi_dev->usbdev = interface_to_usbdev(pfunction);
600 	rsi_dev->priv = (void *)adapter;
601 
602 	if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter)) {
603 		status = -EINVAL;
604 		goto fail_eps;
605 	}
606 
607 	adapter->device = &pfunction->dev;
608 	usb_set_intfdata(pfunction, adapter);
609 
610 	rsi_dev->tx_buffer = kmalloc(2048, GFP_KERNEL);
611 	if (!rsi_dev->tx_buffer) {
612 		status = -ENOMEM;
613 		goto fail_eps;
614 	}
615 
616 	if (rsi_usb_init_rx(adapter)) {
617 		rsi_dbg(ERR_ZONE, "Failed to init RX handle\n");
618 		status = -ENOMEM;
619 		goto fail_rx;
620 	}
621 
622 	rsi_dev->tx_blk_size = 252;
623 	adapter->block_size = rsi_dev->tx_blk_size;
624 
625 	/* Initializing function callbacks */
626 	adapter->check_hw_queue_status = rsi_usb_check_queue_status;
627 	adapter->determine_event_timeout = rsi_usb_event_timeout;
628 	adapter->rsi_host_intf = RSI_HOST_INTF_USB;
629 	adapter->host_intf_ops = &usb_host_intf_ops;
630 
631 #ifdef CONFIG_RSI_DEBUGFS
632 	/* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
633 	adapter->num_debugfs_entries = (MAX_DEBUGFS_ENTRIES - 1);
634 #endif
635 
636 	rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
637 	return 0;
638 
639 fail_rx:
640 	kfree(rsi_dev->tx_buffer);
641 
642 fail_eps:
643 	kfree(rsi_dev);
644 
645 	return status;
646 }
647 
648 static int usb_ulp_read_write(struct rsi_hw *adapter, u16 addr, u32 data,
649 			      u16 len_in_bits)
650 {
651 	int ret;
652 
653 	ret = rsi_usb_master_reg_write
654 			(adapter, RSI_GSPI_DATA_REG1,
655 			 ((addr << 6) | ((data >> 16) & 0xffff)), 2);
656 	if (ret < 0)
657 		return ret;
658 
659 	ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_DATA_REG0,
660 				       (data & 0xffff), 2);
661 	if (ret < 0)
662 		return ret;
663 
664 	/* Initializing GSPI for ULP read/writes */
665 	rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG0,
666 				 RSI_GSPI_CTRL_REG0_VALUE, 2);
667 
668 	ret = rsi_usb_master_reg_write(adapter, RSI_GSPI_CTRL_REG1,
669 				       ((len_in_bits - 1) | RSI_GSPI_TRIG), 2);
670 	if (ret < 0)
671 		return ret;
672 
673 	msleep(20);
674 
675 	return 0;
676 }
677 
678 static int rsi_reset_card(struct rsi_hw *adapter)
679 {
680 	int ret;
681 
682 	rsi_dbg(INFO_ZONE, "Resetting Card...\n");
683 	rsi_usb_master_reg_write(adapter, RSI_TA_HOLD_REG, 0xE, 4);
684 
685 	/* This msleep will ensure Thread-Arch processor to go to hold
686 	 * and any pending dma transfers to rf in device to finish.
687 	 */
688 	msleep(100);
689 
690 	ret = rsi_usb_master_reg_write(adapter, SWBL_REGOUT,
691 				       RSI_FW_WDT_DISABLE_REQ,
692 				       RSI_COMMON_REG_SIZE);
693 	if (ret < 0) {
694 		rsi_dbg(ERR_ZONE, "Disabling firmware watchdog timer failed\n");
695 		goto fail;
696 	}
697 
698 	ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_1,
699 				 RSI_ULP_WRITE_2, 32);
700 	if (ret < 0)
701 		goto fail;
702 	ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_2,
703 				 RSI_ULP_WRITE_0, 32);
704 	if (ret < 0)
705 		goto fail;
706 	ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_1,
707 				 RSI_ULP_WRITE_50, 32);
708 	if (ret < 0)
709 		goto fail;
710 	ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_DELAY_TIMER_2,
711 				 RSI_ULP_WRITE_0, 32);
712 	if (ret < 0)
713 		goto fail;
714 	ret = usb_ulp_read_write(adapter, RSI_WATCH_DOG_TIMER_ENABLE,
715 				 RSI_ULP_TIMER_ENABLE, 32);
716 	if (ret < 0)
717 		goto fail;
718 
719 	rsi_dbg(INFO_ZONE, "Reset card done\n");
720 	return ret;
721 
722 fail:
723 	rsi_dbg(ERR_ZONE, "Reset card failed\n");
724 	return ret;
725 }
726 
727 /**
728  * rsi_probe() - This function is called by kernel when the driver provided
729  *		 Vendor and device IDs are matched. All the initialization
730  *		 work is done here.
731  * @pfunction: Pointer to the USB interface structure.
732  * @id: Pointer to the usb_device_id structure.
733  *
734  * Return: 0 on success, a negative error code on failure.
735  */
736 static int rsi_probe(struct usb_interface *pfunction,
737 		     const struct usb_device_id *id)
738 {
739 	struct rsi_hw *adapter;
740 	struct rsi_91x_usbdev *dev;
741 	u16 fw_status;
742 	int status;
743 
744 	rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
745 
746 	adapter = rsi_91x_init(dev_oper_mode);
747 	if (!adapter) {
748 		rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
749 			__func__);
750 		return -ENOMEM;
751 	}
752 	adapter->rsi_host_intf = RSI_HOST_INTF_USB;
753 
754 	status = rsi_init_usb_interface(adapter, pfunction);
755 	if (status) {
756 		rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
757 			__func__);
758 		goto err;
759 	}
760 
761 	rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
762 
763 	dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
764 
765 	status = rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2);
766 	if (status < 0)
767 		goto err1;
768 	else
769 		fw_status &= 1;
770 
771 	if (!fw_status) {
772 		rsi_dbg(INIT_ZONE, "Loading firmware...\n");
773 		status = rsi_hal_device_init(adapter);
774 		if (status) {
775 			rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
776 				__func__);
777 			goto err1;
778 		}
779 		rsi_dbg(INIT_ZONE, "%s: Device Init Done\n", __func__);
780 	}
781 
782 	status = rsi_rx_urb_submit(adapter, WLAN_EP);
783 	if (status)
784 		goto err1;
785 
786 	if (adapter->priv->coex_mode > 1) {
787 		status = rsi_rx_urb_submit(adapter, BT_EP);
788 		if (status)
789 			goto err1;
790 	}
791 
792 	return 0;
793 err1:
794 	rsi_deinit_usb_interface(adapter);
795 err:
796 	rsi_91x_deinit(adapter);
797 	rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
798 	return status;
799 }
800 
801 /**
802  * rsi_disconnect() - This function performs the reverse of the probe function,
803  *		      it deinitialize the driver structure.
804  * @pfunction: Pointer to the USB interface structure.
805  *
806  * Return: None.
807  */
808 static void rsi_disconnect(struct usb_interface *pfunction)
809 {
810 	struct rsi_hw *adapter = usb_get_intfdata(pfunction);
811 
812 	if (!adapter)
813 		return;
814 
815 	rsi_mac80211_detach(adapter);
816 	rsi_reset_card(adapter);
817 	rsi_deinit_usb_interface(adapter);
818 	rsi_91x_deinit(adapter);
819 
820 	rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
821 }
822 
823 #ifdef CONFIG_PM
824 static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
825 {
826 	/* Not yet implemented */
827 	return -ENOSYS;
828 }
829 
830 static int rsi_resume(struct usb_interface *intf)
831 {
832 	/* Not yet implemented */
833 	return -ENOSYS;
834 }
835 #endif
836 
837 static const struct usb_device_id rsi_dev_table[] = {
838 	{ USB_DEVICE(RSI_USB_VID_9113, RSI_USB_PID_9113) },
839 	{ /* Blank */},
840 };
841 
842 static struct usb_driver rsi_driver = {
843 	.name       = "RSI-USB WLAN",
844 	.probe      = rsi_probe,
845 	.disconnect = rsi_disconnect,
846 	.id_table   = rsi_dev_table,
847 #ifdef CONFIG_PM
848 	.suspend    = rsi_suspend,
849 	.resume     = rsi_resume,
850 #endif
851 };
852 
853 module_usb_driver(rsi_driver);
854 
855 MODULE_AUTHOR("Redpine Signals Inc");
856 MODULE_DESCRIPTION("Common USB layer for RSI drivers");
857 MODULE_SUPPORTED_DEVICE("RSI-91x");
858 MODULE_DEVICE_TABLE(usb, rsi_dev_table);
859 MODULE_FIRMWARE(FIRMWARE_RSI9113);
860 MODULE_VERSION("0.1");
861 MODULE_LICENSE("Dual BSD/GPL");
862