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