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