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