2 * Copyright (c) 2014 Redpine Signals Inc.
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.
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.
18 #include <linux/module.h>
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.
29 * Return: status: 0 on success, a negative error code on failure.
31 static int rsi_usb_card_write(struct rsi_hw
*adapter
,
36 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
38 u8
*seg
= dev
->tx_buffer
;
40 int ep
= dev
->bulkout_endpoint_addr
[endpoint
- 1];
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
;
46 status
= usb_bulk_msg(dev
->usbdev
,
47 usb_sndbulkpipe(dev
->usbdev
, ep
),
55 "Card write failed with error code :%10d\n", status
);
62 * rsi_write_multiple() - This function writes multiple bytes of information
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.
69 * Return: 0 on success, a negative error code on failure.
71 static int rsi_write_multiple(struct rsi_hw
*adapter
,
76 struct rsi_91x_usbdev
*dev
;
84 dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
88 return rsi_usb_card_write(adapter
, data
, count
, endpoint
);
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.
97 * Return: ret_val: 0 on success, -ENOMEM on failure.
99 static int rsi_find_bulk_in_and_out_endpoints(struct usb_interface
*interface
,
100 struct rsi_hw
*adapter
)
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
;
106 int ii
, bep_found
= 0;
108 iface_desc
= &(interface
->altsetting
[0]);
110 for (ii
= 0; ii
< iface_desc
->desc
.bNumEndpoints
; ++ii
) {
111 endpoint
= &(iface_desc
->endpoint
[ii
].desc
);
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
;
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
;
135 if (bep_found
>= MAX_BULK_EP
)
139 if (!(dev
->bulkin_endpoint_addr
) &&
140 (dev
->bulkout_endpoint_addr
[0]))
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)
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.
155 * Return: status: 0 on success, a negative error code on failure.
157 static int rsi_usb_reg_read(struct usb_device
*usbdev
,
163 int status
= -ENOMEM
;
165 if (len
> RSI_USB_CTRL_BUF_SIZE
)
168 buf
= kmalloc(RSI_USB_CTRL_BUF_SIZE
, GFP_KERNEL
);
172 status
= usb_control_msg(usbdev
,
173 usb_rcvctrlpipe(usbdev
, 0),
174 USB_VENDOR_REGISTER_READ
,
176 ((reg
& 0xffff0000) >> 16), (reg
& 0xffff),
179 USB_CTRL_GET_TIMEOUT
);
181 *value
= (buf
[0] | (buf
[1] << 8));
184 "%s: Reg read failed with error code :%d\n",
193 * rsi_usb_reg_write() - This function writes the given data into the given
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.
200 * Return: status: 0 on success, a negative error code on failure.
202 static int rsi_usb_reg_write(struct usb_device
*usbdev
,
208 int status
= -ENOMEM
;
210 if (len
> RSI_USB_CTRL_BUF_SIZE
)
213 usb_reg_buf
= kmalloc(RSI_USB_CTRL_BUF_SIZE
, GFP_KERNEL
);
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;
222 status
= usb_control_msg(usbdev
,
223 usb_sndctrlpipe(usbdev
, 0),
224 USB_VENDOR_REGISTER_WRITE
,
226 ((reg
& 0xffff0000) >> 16),
230 USB_CTRL_SET_TIMEOUT
);
233 "%s: Reg write failed with error code :%d\n",
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.
248 static void rsi_rx_done_handler(struct urb
*urb
)
250 struct rsi_hw
*adapter
= urb
->context
;
251 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
256 rsi_set_event(&dev
->rx_thread
.event
);
260 * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
261 * @adapter: Pointer to the adapter structure.
263 * Return: 0 on success, a negative error code on failure.
265 static int rsi_rx_urb_submit(struct rsi_hw
*adapter
)
267 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
268 struct urb
*urb
= dev
->rx_usb_urb
[0];
271 usb_fill_bulk_urb(urb
,
273 usb_rcvbulkpipe(dev
->usbdev
,
274 dev
->bulkin_endpoint_addr
),
275 urb
->transfer_buffer
,
280 status
= usb_submit_urb(urb
, GFP_KERNEL
);
282 rsi_dbg(ERR_ZONE
, "%s: Failed in urb submission\n", __func__
);
287 static int rsi_usb_read_register_multiple(struct rsi_hw
*adapter
, u32 addr
,
290 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
298 buf
= kzalloc(RSI_USB_BUF_SIZE
, GFP_KERNEL
);
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
,
308 ((addr
& 0xffff0000) >> 16),
309 (addr
& 0xffff), (void *)buf
,
310 transfer
, USB_CTRL_GET_TIMEOUT
);
313 "Reg read failed with error code :%d\n",
318 memcpy(data
, buf
, transfer
);
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.
335 * Return: status: 0 on success, a negative error code on failure.
337 static int rsi_usb_write_register_multiple(struct rsi_hw
*adapter
, u32 addr
,
340 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
345 buf
= kzalloc(RSI_USB_BUF_SIZE
, GFP_KERNEL
);
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
,
356 ((addr
& 0xffff0000) >> 16),
360 USB_CTRL_SET_TIMEOUT
);
363 "Reg write failed with error code :%d\n",
378 *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
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.
384 * Return: 0 on success, a negative error code on failure.
386 static int rsi_usb_host_intf_write_pkt(struct rsi_hw
*adapter
,
390 u32 queueno
= ((pkt
[1] >> 4) & 0x7);
393 endpoint
= ((queueno
== RSI_WIFI_MGMT_Q
|| queueno
== RSI_WIFI_DATA_Q
||
394 queueno
== RSI_COEX_Q
) ? WLAN_EP
: BT_EP
);
396 return rsi_write_multiple(adapter
,
402 static int rsi_usb_master_reg_read(struct rsi_hw
*adapter
, u32 reg
,
405 struct usb_device
*usbdev
=
406 ((struct rsi_91x_usbdev
*)adapter
->rsi_dev
)->usbdev
;
410 ret
= rsi_usb_reg_read(usbdev
, reg
, &temp
, len
);
418 static int rsi_usb_master_reg_write(struct rsi_hw
*adapter
,
420 unsigned long value
, u16 len
)
422 struct usb_device
*usbdev
=
423 ((struct rsi_91x_usbdev
*)adapter
->rsi_dev
)->usbdev
;
425 return rsi_usb_reg_write(usbdev
, reg
, value
, len
);
428 static int rsi_usb_load_data_master_write(struct rsi_hw
*adapter
,
430 u32 instructions_sz
, u16 block_size
,
438 num_blocks
= instructions_sz
/ block_size
;
439 rsi_dbg(INFO_ZONE
, "num_blocks: %d\n", num_blocks
);
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
,
449 rsi_dbg(INFO_ZONE
, "%s: loading block: %d\n", __func__
, i
);
450 base_address
+= block_size
;
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
,
460 instructions_sz
% block_size
);
464 "Written Last Block in Address 0x%x Successfully\n",
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
,
480 * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
481 * @adapter: Pointer to the adapter structure.
485 static void rsi_deinit_usb_interface(struct rsi_hw
*adapter
)
487 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
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
);
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.
500 * Return: 0 on success, a negative error code on failure.
502 static int rsi_init_usb_interface(struct rsi_hw
*adapter
,
503 struct usb_interface
*pfunction
)
505 struct rsi_91x_usbdev
*rsi_dev
;
506 struct rsi_common
*common
= adapter
->priv
;
509 rsi_dev
= kzalloc(sizeof(*rsi_dev
), GFP_KERNEL
);
513 adapter
->rsi_dev
= rsi_dev
;
514 rsi_dev
->usbdev
= interface_to_usbdev(pfunction
);
516 if (rsi_find_bulk_in_and_out_endpoints(pfunction
, adapter
))
519 adapter
->device
= &pfunction
->dev
;
520 usb_set_intfdata(pfunction
, adapter
);
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",
529 rsi_dev
->tx_buffer
= kmalloc(2048, GFP_KERNEL
);
530 if (!rsi_dev
->tx_buffer
) {
534 rsi_dev
->rx_usb_urb
[0] = usb_alloc_urb(0, GFP_KERNEL
);
535 if (!rsi_dev
->rx_usb_urb
[0]) {
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
;
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
;
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");
554 rsi_dbg(ERR_ZONE
, "%s: Unable to init rx thrd\n", __func__
);
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);
563 rsi_dbg(INIT_ZONE
, "%s: Enabled the interface\n", __func__
);
567 usb_free_urb(rsi_dev
->rx_usb_urb
[0]);
569 kfree(rsi_dev
->tx_buffer
);
571 kfree(common
->rx_data_pkt
);
575 static int usb_ulp_read_write(struct rsi_hw
*adapter
, u16 addr
, u32 data
,
580 ret
= rsi_usb_master_reg_write
581 (adapter
, RSI_GSPI_DATA_REG1
,
582 ((addr
<< 6) | ((data
>> 16) & 0xffff)), 2);
586 ret
= rsi_usb_master_reg_write(adapter
, RSI_GSPI_DATA_REG0
,
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);
595 ret
= rsi_usb_master_reg_write(adapter
, RSI_GSPI_CTRL_REG1
,
596 ((len_in_bits
- 1) | RSI_GSPI_TRIG
), 2);
605 static int rsi_reset_card(struct rsi_hw
*adapter
)
609 rsi_dbg(INFO_ZONE
, "Resetting Card...\n");
610 rsi_usb_master_reg_write(adapter
, RSI_TA_HOLD_REG
, 0xE, 4);
612 /* This msleep will ensure Thread-Arch processor to go to hold
613 * and any pending dma transfers to rf in device to finish.
617 ret
= usb_ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_1
,
618 RSI_ULP_WRITE_2
, 32);
621 ret
= usb_ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_2
,
622 RSI_ULP_WRITE_0
, 32);
625 ret
= usb_ulp_read_write(adapter
, RSI_WATCH_DOG_DELAY_TIMER_1
,
626 RSI_ULP_WRITE_50
, 32);
629 ret
= usb_ulp_read_write(adapter
, RSI_WATCH_DOG_DELAY_TIMER_2
,
630 RSI_ULP_WRITE_0
, 32);
633 ret
= usb_ulp_read_write(adapter
, RSI_WATCH_DOG_TIMER_ENABLE
,
634 RSI_ULP_TIMER_ENABLE
, 32);
638 rsi_dbg(INFO_ZONE
, "Reset card done\n");
642 rsi_dbg(ERR_ZONE
, "Reset card failed\n");
647 * rsi_probe() - This function is called by kernel when the driver provided
648 * Vendor and device IDs are matched. All the initialization
650 * @pfunction: Pointer to the USB interface structure.
651 * @id: Pointer to the usb_device_id structure.
653 * Return: 0 on success, a negative error code on failure.
655 static int rsi_probe(struct usb_interface
*pfunction
,
656 const struct usb_device_id
*id
)
658 struct rsi_hw
*adapter
;
659 struct rsi_91x_usbdev
*dev
;
663 rsi_dbg(INIT_ZONE
, "%s: Init function called\n", __func__
);
665 adapter
= rsi_91x_init();
667 rsi_dbg(ERR_ZONE
, "%s: Failed to init os intf ops\n",
671 adapter
->rsi_host_intf
= RSI_HOST_INTF_USB
;
673 status
= rsi_init_usb_interface(adapter
, pfunction
);
675 rsi_dbg(ERR_ZONE
, "%s: Failed to init usb interface\n",
680 rsi_dbg(ERR_ZONE
, "%s: Initialized os intf ops\n", __func__
);
682 dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
684 status
= rsi_usb_reg_read(dev
->usbdev
, FW_STATUS_REG
, &fw_status
, 2);
691 rsi_dbg(INIT_ZONE
, "Loading firmware...\n");
692 status
= rsi_hal_device_init(adapter
);
694 rsi_dbg(ERR_ZONE
, "%s: Failed in device init\n",
698 rsi_dbg(INIT_ZONE
, "%s: Device Init Done\n", __func__
);
701 status
= rsi_rx_urb_submit(adapter
);
707 rsi_deinit_usb_interface(adapter
);
709 rsi_91x_deinit(adapter
);
710 rsi_dbg(ERR_ZONE
, "%s: Failed in probe...Exiting\n", __func__
);
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.
721 static void rsi_disconnect(struct usb_interface
*pfunction
)
723 struct rsi_hw
*adapter
= usb_get_intfdata(pfunction
);
728 rsi_mac80211_detach(adapter
);
729 rsi_reset_card(adapter
);
730 rsi_deinit_usb_interface(adapter
);
731 rsi_91x_deinit(adapter
);
733 rsi_dbg(INFO_ZONE
, "%s: Deinitialization completed\n", __func__
);
737 static int rsi_suspend(struct usb_interface
*intf
, pm_message_t message
)
739 /* Not yet implemented */
743 static int rsi_resume(struct usb_interface
*intf
)
745 /* Not yet implemented */
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) },
759 static struct usb_driver rsi_driver
= {
760 .name
= "RSI-USB WLAN",
762 .disconnect
= rsi_disconnect
,
763 .id_table
= rsi_dev_table
,
765 .suspend
= rsi_suspend
,
766 .resume
= rsi_resume
,
770 module_usb_driver(rsi_driver
);
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");