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