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>
22 * rsi_usb_card_write() - This function writes to the USB Card.
23 * @adapter: Pointer to the adapter structure.
24 * @buf: Pointer to the buffer from where the data has to be taken.
25 * @len: Length to be written.
26 * @endpoint: Type of endpoint.
28 * Return: status: 0 on success, a negative error code on failure.
30 static int rsi_usb_card_write(struct rsi_hw
*adapter
,
35 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
39 status
= usb_bulk_msg(dev
->usbdev
,
40 usb_sndbulkpipe(dev
->usbdev
,
41 dev
->bulkout_endpoint_addr
[endpoint
- 1]),
49 "Card write failed with error code :%10d\n", status
);
56 * rsi_write_multiple() - This function writes multiple bytes of information
58 * @adapter: Pointer to the adapter structure.
59 * @addr: Address of the register.
60 * @data: Pointer to the data that has to be written.
61 * @count: Number of multiple bytes to be written.
63 * Return: 0 on success, a negative error code on failure.
65 static int rsi_write_multiple(struct rsi_hw
*adapter
,
70 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
71 u8
*seg
= dev
->tx_buffer
;
76 if (endpoint
== MGMT_EP
) {
77 memset(seg
, 0, RSI_USB_TX_HEAD_ROOM
);
78 memcpy(seg
+ RSI_USB_TX_HEAD_ROOM
, data
, count
);
80 seg
= ((u8
*)data
- RSI_USB_TX_HEAD_ROOM
);
83 return rsi_usb_card_write(adapter
,
85 count
+ RSI_USB_TX_HEAD_ROOM
,
90 * rsi_find_bulk_in_and_out_endpoints() - This function initializes the bulk
91 * endpoints to the device.
92 * @interface: Pointer to the USB interface structure.
93 * @adapter: Pointer to the adapter structure.
95 * Return: ret_val: 0 on success, -ENOMEM on failure.
97 static int rsi_find_bulk_in_and_out_endpoints(struct usb_interface
*interface
,
98 struct rsi_hw
*adapter
)
100 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
101 struct usb_host_interface
*iface_desc
;
102 struct usb_endpoint_descriptor
*endpoint
;
104 int ii
, bep_found
= 0;
106 iface_desc
= &(interface
->altsetting
[0]);
108 for (ii
= 0; ii
< iface_desc
->desc
.bNumEndpoints
; ++ii
) {
109 endpoint
= &(iface_desc
->endpoint
[ii
].desc
);
111 if ((!(dev
->bulkin_endpoint_addr
)) &&
112 (endpoint
->bEndpointAddress
& USB_DIR_IN
) &&
113 ((endpoint
->bmAttributes
&
114 USB_ENDPOINT_XFERTYPE_MASK
) ==
115 USB_ENDPOINT_XFER_BULK
)) {
116 buffer_size
= endpoint
->wMaxPacketSize
;
117 dev
->bulkin_size
= buffer_size
;
118 dev
->bulkin_endpoint_addr
=
119 endpoint
->bEndpointAddress
;
122 if (!dev
->bulkout_endpoint_addr
[bep_found
] &&
123 !(endpoint
->bEndpointAddress
& USB_DIR_IN
) &&
124 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
125 USB_ENDPOINT_XFER_BULK
)) {
126 dev
->bulkout_endpoint_addr
[bep_found
] =
127 endpoint
->bEndpointAddress
;
128 buffer_size
= endpoint
->wMaxPacketSize
;
129 dev
->bulkout_size
[bep_found
] = buffer_size
;
133 if (bep_found
>= MAX_BULK_EP
)
137 if (!(dev
->bulkin_endpoint_addr
) &&
138 (dev
->bulkout_endpoint_addr
[0]))
144 /* rsi_usb_reg_read() - This function reads data from given register address.
145 * @usbdev: Pointer to the usb_device structure.
146 * @reg: Address of the register to be read.
147 * @value: Value to be read.
148 * @len: length of data to be read.
150 * Return: status: 0 on success, a negative error code on failure.
152 static int rsi_usb_reg_read(struct usb_device
*usbdev
,
158 int status
= -ENOMEM
;
160 buf
= kmalloc(0x04, GFP_KERNEL
);
164 status
= usb_control_msg(usbdev
,
165 usb_rcvctrlpipe(usbdev
, 0),
166 USB_VENDOR_REGISTER_READ
,
168 ((reg
& 0xffff0000) >> 16), (reg
& 0xffff),
173 *value
= (buf
[0] | (buf
[1] << 8));
176 "%s: Reg read failed with error code :%d\n",
185 * rsi_usb_reg_write() - This function writes the given data into the given
187 * @usbdev: Pointer to the usb_device structure.
188 * @reg: Address of the register.
189 * @value: Value to write.
190 * @len: Length of data to be written.
192 * Return: status: 0 on success, a negative error code on failure.
194 static int rsi_usb_reg_write(struct usb_device
*usbdev
,
200 int status
= -ENOMEM
;
202 usb_reg_buf
= kmalloc(0x04, GFP_KERNEL
);
206 usb_reg_buf
[0] = (value
& 0x00ff);
207 usb_reg_buf
[1] = (value
& 0xff00) >> 8;
208 usb_reg_buf
[2] = 0x0;
209 usb_reg_buf
[3] = 0x0;
211 status
= usb_control_msg(usbdev
,
212 usb_sndctrlpipe(usbdev
, 0),
213 USB_VENDOR_REGISTER_WRITE
,
215 ((reg
& 0xffff0000) >> 16),
222 "%s: Reg write failed with error code :%d\n",
231 * rsi_rx_done_handler() - This function is called when a packet is received
232 * from USB stack. This is callback to recieve done.
233 * @urb: Received URB.
237 static void rsi_rx_done_handler(struct urb
*urb
)
239 struct rsi_hw
*adapter
= urb
->context
;
240 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
245 rsi_set_event(&dev
->rx_thread
.event
);
249 * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
250 * @adapter: Pointer to the adapter structure.
252 * Return: 0 on success, a negative error code on failure.
254 static int rsi_rx_urb_submit(struct rsi_hw
*adapter
)
256 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
257 struct urb
*urb
= dev
->rx_usb_urb
[0];
260 usb_fill_bulk_urb(urb
,
262 usb_rcvbulkpipe(dev
->usbdev
,
263 dev
->bulkin_endpoint_addr
),
264 urb
->transfer_buffer
,
269 status
= usb_submit_urb(urb
, GFP_KERNEL
);
271 rsi_dbg(ERR_ZONE
, "%s: Failed in urb submission\n", __func__
);
277 * rsi_usb_write_register_multiple() - This function writes multiple bytes of
278 * information to multiple registers.
279 * @adapter: Pointer to the adapter structure.
280 * @addr: Address of the register.
281 * @data: Pointer to the data that has to be written.
282 * @count: Number of multiple bytes to be written on to the registers.
284 * Return: status: 0 on success, a negative error code on failure.
286 int rsi_usb_write_register_multiple(struct rsi_hw
*adapter
,
291 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
296 buf
= kzalloc(4096, GFP_KERNEL
);
301 transfer
= (u8
)(min_t(u32
, count
, 4096));
302 memcpy(buf
, data
, transfer
);
303 status
= usb_control_msg(dev
->usbdev
,
304 usb_sndctrlpipe(dev
->usbdev
, 0),
305 USB_VENDOR_REGISTER_WRITE
,
307 ((addr
& 0xffff0000) >> 16),
314 "Reg write failed with error code :%d\n",
328 *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
330 * @adapter: Pointer to the adapter structure.
331 * @pkt: Pointer to the data to be written on to the card.
332 * @len: Length of the data to be written on to the card.
334 * Return: 0 on success, a negative error code on failure.
336 static int rsi_usb_host_intf_write_pkt(struct rsi_hw
*adapter
,
340 u32 queueno
= ((pkt
[1] >> 4) & 0xf);
343 endpoint
= ((queueno
== RSI_WIFI_MGMT_Q
) ? MGMT_EP
: DATA_EP
);
345 return rsi_write_multiple(adapter
,
352 * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
353 * @adapter: Pointer to the adapter structure.
357 static void rsi_deinit_usb_interface(struct rsi_hw
*adapter
)
359 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
361 rsi_kill_thread(&dev
->rx_thread
);
362 usb_free_urb(dev
->rx_usb_urb
[0]);
363 kfree(adapter
->priv
->rx_data_pkt
);
364 kfree(dev
->tx_buffer
);
368 * rsi_init_usb_interface() - This function initializes the usb interface.
369 * @adapter: Pointer to the adapter structure.
370 * @pfunction: Pointer to USB interface structure.
372 * Return: 0 on success, a negative error code on failure.
374 static int rsi_init_usb_interface(struct rsi_hw
*adapter
,
375 struct usb_interface
*pfunction
)
377 struct rsi_91x_usbdev
*rsi_dev
;
378 struct rsi_common
*common
= adapter
->priv
;
381 rsi_dev
= kzalloc(sizeof(*rsi_dev
), GFP_KERNEL
);
385 adapter
->rsi_dev
= rsi_dev
;
386 rsi_dev
->usbdev
= interface_to_usbdev(pfunction
);
388 if (rsi_find_bulk_in_and_out_endpoints(pfunction
, adapter
))
391 adapter
->device
= &pfunction
->dev
;
392 usb_set_intfdata(pfunction
, adapter
);
394 common
->rx_data_pkt
= kmalloc(2048, GFP_KERNEL
);
395 if (!common
->rx_data_pkt
) {
396 rsi_dbg(ERR_ZONE
, "%s: Failed to allocate memory\n",
401 rsi_dev
->tx_buffer
= kmalloc(2048, GFP_KERNEL
);
402 if (!rsi_dev
->tx_buffer
) {
406 rsi_dev
->rx_usb_urb
[0] = usb_alloc_urb(0, GFP_KERNEL
);
407 if (!rsi_dev
->rx_usb_urb
[0]) {
411 rsi_dev
->rx_usb_urb
[0]->transfer_buffer
= adapter
->priv
->rx_data_pkt
;
412 rsi_dev
->tx_blk_size
= 252;
414 /* Initializing function callbacks */
415 adapter
->rx_urb_submit
= rsi_rx_urb_submit
;
416 adapter
->host_intf_write_pkt
= rsi_usb_host_intf_write_pkt
;
417 adapter
->check_hw_queue_status
= rsi_usb_check_queue_status
;
418 adapter
->determine_event_timeout
= rsi_usb_event_timeout
;
420 rsi_init_event(&rsi_dev
->rx_thread
.event
);
421 status
= rsi_create_kthread(common
, &rsi_dev
->rx_thread
,
422 rsi_usb_rx_thread
, "RX-Thread");
424 rsi_dbg(ERR_ZONE
, "%s: Unable to init rx thrd\n", __func__
);
428 #ifdef CONFIG_RSI_DEBUGFS
429 /* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
430 adapter
->num_debugfs_entries
= (MAX_DEBUGFS_ENTRIES
- 1);
433 rsi_dbg(INIT_ZONE
, "%s: Enabled the interface\n", __func__
);
437 usb_free_urb(rsi_dev
->rx_usb_urb
[0]);
439 kfree(rsi_dev
->tx_buffer
);
441 kfree(common
->rx_data_pkt
);
446 * rsi_probe() - This function is called by kernel when the driver provided
447 * Vendor and device IDs are matched. All the initialization
449 * @pfunction: Pointer to the USB interface structure.
450 * @id: Pointer to the usb_device_id structure.
452 * Return: 0 on success, a negative error code on failure.
454 static int rsi_probe(struct usb_interface
*pfunction
,
455 const struct usb_device_id
*id
)
457 struct rsi_hw
*adapter
;
458 struct rsi_91x_usbdev
*dev
;
462 rsi_dbg(INIT_ZONE
, "%s: Init function called\n", __func__
);
464 adapter
= rsi_91x_init();
466 rsi_dbg(ERR_ZONE
, "%s: Failed to init os intf ops\n",
471 status
= rsi_init_usb_interface(adapter
, pfunction
);
473 rsi_dbg(ERR_ZONE
, "%s: Failed to init usb interface\n",
478 rsi_dbg(ERR_ZONE
, "%s: Initialized os intf ops\n", __func__
);
480 dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
482 status
= rsi_usb_reg_read(dev
->usbdev
, FW_STATUS_REG
, &fw_status
, 2);
489 status
= rsi_usb_device_init(adapter
->priv
);
491 rsi_dbg(ERR_ZONE
, "%s: Failed in device init\n",
496 status
= rsi_usb_reg_write(dev
->usbdev
,
498 RSI_USB_READY_MAGIC_NUM
, 1);
501 rsi_dbg(INIT_ZONE
, "%s: Performed device init\n", __func__
);
504 status
= rsi_rx_urb_submit(adapter
);
510 rsi_deinit_usb_interface(adapter
);
512 rsi_91x_deinit(adapter
);
513 rsi_dbg(ERR_ZONE
, "%s: Failed in probe...Exiting\n", __func__
);
518 * rsi_disconnect() - This function performs the reverse of the probe function,
519 * it deintialize the driver structure.
520 * @pfunction: Pointer to the USB interface structure.
524 static void rsi_disconnect(struct usb_interface
*pfunction
)
526 struct rsi_hw
*adapter
= usb_get_intfdata(pfunction
);
531 rsi_mac80211_detach(adapter
);
532 rsi_deinit_usb_interface(adapter
);
533 rsi_91x_deinit(adapter
);
535 rsi_dbg(INFO_ZONE
, "%s: Deinitialization completed\n", __func__
);
539 static int rsi_suspend(struct usb_interface
*intf
, pm_message_t message
)
541 /* Not yet implemented */
545 static int rsi_resume(struct usb_interface
*intf
)
547 /* Not yet implemented */
552 static const struct usb_device_id rsi_dev_table
[] = {
553 { USB_DEVICE(0x0303, 0x0100) },
554 { USB_DEVICE(0x041B, 0x0301) },
555 { USB_DEVICE(0x041B, 0x0201) },
556 { USB_DEVICE(0x041B, 0x9330) },
560 static struct usb_driver rsi_driver
= {
561 .name
= "RSI-USB WLAN",
563 .disconnect
= rsi_disconnect
,
564 .id_table
= rsi_dev_table
,
566 .suspend
= rsi_suspend
,
567 .resume
= rsi_resume
,
571 module_usb_driver(rsi_driver
);
573 MODULE_AUTHOR("Redpine Signals Inc");
574 MODULE_DESCRIPTION("Common USB layer for RSI drivers");
575 MODULE_SUPPORTED_DEVICE("RSI-91x");
576 MODULE_DEVICE_TABLE(usb
, rsi_dev_table
);
577 MODULE_FIRMWARE(FIRMWARE_RSI9113
);
578 MODULE_VERSION("0.1");
579 MODULE_LICENSE("Dual BSD/GPL");