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