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, -1 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, -1 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, -1 on failure.
152 static int rsi_usb_reg_read(struct usb_device
*usbdev
,
160 status
= usb_control_msg(usbdev
,
161 usb_rcvctrlpipe(usbdev
, 0),
162 USB_VENDOR_REGISTER_READ
,
164 ((reg
& 0xffff0000) >> 16), (reg
& 0xffff),
169 *value
= (temp_buf
[0] | (temp_buf
[1] << 8));
172 "%s: Reg read failed with error code :%d\n",
179 * rsi_usb_reg_write() - This function writes the given data into the given
181 * @usbdev: Pointer to the usb_device structure.
182 * @reg: Address of the register.
183 * @value: Value to write.
184 * @len: Length of data to be written.
186 * Return: status: 0 on success, -1 on failure.
188 static int rsi_usb_reg_write(struct usb_device
*usbdev
,
196 usb_reg_buf
[0] = (value
& 0x00ff);
197 usb_reg_buf
[1] = (value
& 0xff00) >> 8;
198 usb_reg_buf
[2] = 0x0;
199 usb_reg_buf
[3] = 0x0;
201 status
= usb_control_msg(usbdev
,
202 usb_sndctrlpipe(usbdev
, 0),
203 USB_VENDOR_REGISTER_WRITE
,
205 ((reg
& 0xffff0000) >> 16),
212 "%s: Reg write failed with error code :%d\n",
219 * rsi_rx_done_handler() - This function is called when a packet is received
220 * from USB stack. This is callback to recieve done.
221 * @urb: Received URB.
225 static void rsi_rx_done_handler(struct urb
*urb
)
227 struct rsi_hw
*adapter
= urb
->context
;
228 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
233 rsi_set_event(&dev
->rx_thread
.event
);
237 * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
238 * @adapter: Pointer to the adapter structure.
240 * Return: 0 on success, -1 on failure.
242 static int rsi_rx_urb_submit(struct rsi_hw
*adapter
)
244 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
245 struct urb
*urb
= dev
->rx_usb_urb
[0];
248 usb_fill_bulk_urb(urb
,
250 usb_rcvbulkpipe(dev
->usbdev
,
251 dev
->bulkin_endpoint_addr
),
252 urb
->transfer_buffer
,
257 status
= usb_submit_urb(urb
, GFP_KERNEL
);
259 rsi_dbg(ERR_ZONE
, "%s: Failed in urb submission\n", __func__
);
265 * rsi_usb_write_register_multiple() - This function writes multiple bytes of
266 * information to multiple registers.
267 * @adapter: Pointer to the adapter structure.
268 * @addr: Address of the register.
269 * @data: Pointer to the data that has to be written.
270 * @count: Number of multiple bytes to be written on to the registers.
272 * Return: status: 0 on success, -1 on failure.
274 int rsi_usb_write_register_multiple(struct rsi_hw
*adapter
,
279 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
284 buf
= kzalloc(4096, GFP_KERNEL
);
289 transfer
= min_t(int, count
, 4096);
290 memcpy(buf
, data
, transfer
);
291 status
= usb_control_msg(dev
->usbdev
,
292 usb_sndctrlpipe(dev
->usbdev
, 0),
293 USB_VENDOR_REGISTER_WRITE
,
295 ((addr
& 0xffff0000) >> 16),
302 "Reg write failed with error code :%d\n",
316 *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
318 * @adapter: Pointer to the adapter structure.
319 * @pkt: Pointer to the data to be written on to the card.
320 * @len: Length of the data to be written on to the card.
322 * Return: 0 on success, -1 on failure.
324 static int rsi_usb_host_intf_write_pkt(struct rsi_hw
*adapter
,
328 u32 queueno
= ((pkt
[1] >> 4) & 0xf);
331 endpoint
= ((queueno
== RSI_WIFI_MGMT_Q
) ? MGMT_EP
: DATA_EP
);
333 return rsi_write_multiple(adapter
,
340 * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
341 * @adapter: Pointer to the adapter structure.
345 static void rsi_deinit_usb_interface(struct rsi_hw
*adapter
)
347 struct rsi_91x_usbdev
*dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
349 rsi_kill_thread(&dev
->rx_thread
);
350 kfree(adapter
->priv
->rx_data_pkt
);
351 kfree(dev
->tx_buffer
);
355 * rsi_init_usb_interface() - This function initializes the usb interface.
356 * @adapter: Pointer to the adapter structure.
357 * @pfunction: Pointer to USB interface structure.
359 * Return: 0 on success, -1 on failure.
361 static int rsi_init_usb_interface(struct rsi_hw
*adapter
,
362 struct usb_interface
*pfunction
)
364 struct rsi_91x_usbdev
*rsi_dev
;
365 struct rsi_common
*common
= adapter
->priv
;
368 rsi_dev
= kzalloc(sizeof(*rsi_dev
), GFP_KERNEL
);
372 adapter
->rsi_dev
= rsi_dev
;
373 rsi_dev
->usbdev
= interface_to_usbdev(pfunction
);
375 if (rsi_find_bulk_in_and_out_endpoints(pfunction
, adapter
))
378 adapter
->device
= &pfunction
->dev
;
379 usb_set_intfdata(pfunction
, adapter
);
381 common
->rx_data_pkt
= kmalloc(2048, GFP_KERNEL
);
382 if (!common
->rx_data_pkt
) {
383 rsi_dbg(ERR_ZONE
, "%s: Failed to allocate memory\n",
388 rsi_dev
->tx_buffer
= kmalloc(2048, GFP_ATOMIC
);
389 rsi_dev
->rx_usb_urb
[0] = usb_alloc_urb(0, GFP_KERNEL
);
390 rsi_dev
->rx_usb_urb
[0]->transfer_buffer
= adapter
->priv
->rx_data_pkt
;
391 rsi_dev
->tx_blk_size
= 252;
393 /* Initializing function callbacks */
394 adapter
->rx_urb_submit
= rsi_rx_urb_submit
;
395 adapter
->host_intf_write_pkt
= rsi_usb_host_intf_write_pkt
;
396 adapter
->check_hw_queue_status
= rsi_usb_check_queue_status
;
397 adapter
->determine_event_timeout
= rsi_usb_event_timeout
;
399 rsi_init_event(&rsi_dev
->rx_thread
.event
);
400 status
= rsi_create_kthread(common
, &rsi_dev
->rx_thread
,
401 rsi_usb_rx_thread
, "RX-Thread");
403 rsi_dbg(ERR_ZONE
, "%s: Unable to init rx thrd\n", __func__
);
407 #ifdef CONFIG_RSI_DEBUGFS
408 /* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
409 adapter
->num_debugfs_entries
= (MAX_DEBUGFS_ENTRIES
- 1);
412 rsi_dbg(INIT_ZONE
, "%s: Enabled the interface\n", __func__
);
416 kfree(rsi_dev
->tx_buffer
);
417 kfree(common
->rx_data_pkt
);
422 * rsi_probe() - This function is called by kernel when the driver provided
423 * Vendor and device IDs are matched. All the initialization
425 * @pfunction: Pointer to the USB interface structure.
426 * @id: Pointer to the usb_device_id structure.
428 * Return: 0 on success, -1 on failure.
430 static int rsi_probe(struct usb_interface
*pfunction
,
431 const struct usb_device_id
*id
)
433 struct rsi_hw
*adapter
;
434 struct rsi_91x_usbdev
*dev
;
437 rsi_dbg(INIT_ZONE
, "%s: Init function called\n", __func__
);
439 adapter
= rsi_91x_init();
441 rsi_dbg(ERR_ZONE
, "%s: Failed to init os intf ops\n",
446 if (rsi_init_usb_interface(adapter
, pfunction
)) {
447 rsi_dbg(ERR_ZONE
, "%s: Failed to init usb interface\n",
452 rsi_dbg(ERR_ZONE
, "%s: Initialized os intf ops\n", __func__
);
454 dev
= (struct rsi_91x_usbdev
*)adapter
->rsi_dev
;
456 if (rsi_usb_reg_read(dev
->usbdev
, FW_STATUS_REG
, &fw_status
, 2) < 0)
462 if (rsi_usb_device_init(adapter
->priv
)) {
463 rsi_dbg(ERR_ZONE
, "%s: Failed in device init\n",
468 if (rsi_usb_reg_write(dev
->usbdev
,
470 RSI_USB_READY_MAGIC_NUM
, 1) < 0)
472 rsi_dbg(INIT_ZONE
, "%s: Performed device init\n", __func__
);
475 if (rsi_rx_urb_submit(adapter
))
480 rsi_deinit_usb_interface(adapter
);
482 rsi_91x_deinit(adapter
);
483 rsi_dbg(ERR_ZONE
, "%s: Failed in probe...Exiting\n", __func__
);
488 * rsi_disconnect() - This function performs the reverse of the probe function,
489 * it deintialize the driver structure.
490 * @pfunction: Pointer to the USB interface structure.
494 static void rsi_disconnect(struct usb_interface
*pfunction
)
496 struct rsi_hw
*adapter
= usb_get_intfdata(pfunction
);
501 rsi_mac80211_detach(adapter
);
502 rsi_deinit_usb_interface(adapter
);
503 rsi_91x_deinit(adapter
);
505 rsi_dbg(INFO_ZONE
, "%s: Deinitialization completed\n", __func__
);
509 static int rsi_suspend(struct usb_interface
*intf
, pm_message_t message
)
511 /* Not yet implemented */
515 static int rsi_resume(struct usb_interface
*intf
)
517 /* Not yet implemented */
522 static const struct usb_device_id rsi_dev_table
[] = {
523 { USB_DEVICE(0x0303, 0x0100) },
524 { USB_DEVICE(0x041B, 0x0301) },
525 { USB_DEVICE(0x041B, 0x0201) },
526 { USB_DEVICE(0x041B, 0x9330) },
530 static struct usb_driver rsi_driver
= {
531 .name
= "RSI-USB WLAN",
533 .disconnect
= rsi_disconnect
,
534 .id_table
= rsi_dev_table
,
536 .suspend
= rsi_suspend
,
537 .resume
= rsi_resume
,
542 * rsi_module_init() - This function registers the client driver.
545 * Return: 0 on success.
547 static int rsi_module_init(void)
549 usb_register(&rsi_driver
);
550 rsi_dbg(INIT_ZONE
, "%s: Registering driver\n", __func__
);
555 * rsi_module_exit() - This function unregisters the client driver.
560 static void rsi_module_exit(void)
562 usb_deregister(&rsi_driver
);
563 rsi_dbg(INFO_ZONE
, "%s: Unregistering driver\n", __func__
);
566 module_init(rsi_module_init
);
567 module_exit(rsi_module_exit
);
569 MODULE_AUTHOR("Redpine Signals Inc");
570 MODULE_DESCRIPTION("Common USB layer for RSI drivers");
571 MODULE_SUPPORTED_DEVICE("RSI-91x");
572 MODULE_DEVICE_TABLE(usb
, rsi_dev_table
);
573 MODULE_FIRMWARE(FIRMWARE_RSI9113
);
574 MODULE_VERSION("0.1");
575 MODULE_LICENSE("Dual BSD/GPL");