net: ptp: do not reimplement PTP/BPF classifier
[linux/fpc-iii.git] / drivers / net / wireless / rsi / rsi_91x_usb.c
blobbb1bf96670ebd382a1789c58f98f4afb91d6c9eb
1 /**
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>
19 #include "rsi_usb.h"
21 /**
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,
31 void *buf,
32 u16 len,
33 u8 endpoint)
35 struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
36 int status;
37 s32 transfer;
39 status = usb_bulk_msg(dev->usbdev,
40 usb_sndbulkpipe(dev->usbdev,
41 dev->bulkout_endpoint_addr[endpoint - 1]),
42 buf,
43 len,
44 &transfer,
45 HZ * 5);
47 if (status < 0) {
48 rsi_dbg(ERR_ZONE,
49 "Card write failed with error code :%10d\n", status);
50 dev->write_fail = 1;
52 return status;
55 /**
56 * rsi_write_multiple() - This function writes multiple bytes of information
57 * to the USB card.
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,
66 u8 endpoint,
67 u8 *data,
68 u32 count)
70 struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
71 u8 *seg = dev->tx_buffer;
73 if (dev->write_fail)
74 return 0;
76 if (endpoint == MGMT_EP) {
77 memset(seg, 0, RSI_USB_TX_HEAD_ROOM);
78 memcpy(seg + RSI_USB_TX_HEAD_ROOM, data, count);
79 } else {
80 seg = ((u8 *)data - RSI_USB_TX_HEAD_ROOM);
83 return rsi_usb_card_write(adapter,
84 seg,
85 count + RSI_USB_TX_HEAD_ROOM,
86 endpoint);
89 /**
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;
103 __le16 buffer_size;
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;
130 bep_found++;
133 if (bep_found >= MAX_BULK_EP)
134 break;
137 if (!(dev->bulkin_endpoint_addr) &&
138 (dev->bulkout_endpoint_addr[0]))
139 return -EINVAL;
141 return 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,
153 u32 reg,
154 u16 *value,
155 u16 len)
157 u8 temp_buf[4];
158 int status = 0;
160 status = usb_control_msg(usbdev,
161 usb_rcvctrlpipe(usbdev, 0),
162 USB_VENDOR_REGISTER_READ,
163 USB_TYPE_VENDOR,
164 ((reg & 0xffff0000) >> 16), (reg & 0xffff),
165 (void *)temp_buf,
166 len,
167 HZ * 5);
169 *value = (temp_buf[0] | (temp_buf[1] << 8));
170 if (status < 0) {
171 rsi_dbg(ERR_ZONE,
172 "%s: Reg read failed with error code :%d\n",
173 __func__, status);
175 return status;
179 * rsi_usb_reg_write() - This function writes the given data into the given
180 * register address.
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,
189 u32 reg,
190 u16 value,
191 u16 len)
193 u8 usb_reg_buf[4];
194 int status = 0;
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,
204 USB_TYPE_VENDOR,
205 ((reg & 0xffff0000) >> 16),
206 (reg & 0xffff),
207 (void *)usb_reg_buf,
208 len,
209 HZ * 5);
210 if (status < 0) {
211 rsi_dbg(ERR_ZONE,
212 "%s: Reg write failed with error code :%d\n",
213 __func__, status);
215 return status;
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.
223 * Return: None.
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;
230 if (urb->status)
231 return;
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];
246 int status;
248 usb_fill_bulk_urb(urb,
249 dev->usbdev,
250 usb_rcvbulkpipe(dev->usbdev,
251 dev->bulkin_endpoint_addr),
252 urb->transfer_buffer,
253 3000,
254 rsi_rx_done_handler,
255 adapter);
257 status = usb_submit_urb(urb, GFP_KERNEL);
258 if (status)
259 rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
261 return status;
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,
275 u32 addr,
276 u8 *data,
277 u32 count)
279 struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
280 u8 *buf;
281 u8 transfer;
282 int status = 0;
284 buf = kzalloc(4096, GFP_KERNEL);
285 if (!buf)
286 return -ENOMEM;
288 while (count) {
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,
294 USB_TYPE_VENDOR,
295 ((addr & 0xffff0000) >> 16),
296 (addr & 0xffff),
297 (void *)buf,
298 transfer,
299 HZ * 5);
300 if (status < 0) {
301 rsi_dbg(ERR_ZONE,
302 "Reg write failed with error code :%d\n",
303 status);
304 } else {
305 count -= transfer;
306 data += transfer;
307 addr += transfer;
311 kfree(buf);
312 return 0;
316 *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
317 * USB card.
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,
325 u8 *pkt,
326 u32 len)
328 u32 queueno = ((pkt[1] >> 4) & 0xf);
329 u8 endpoint;
331 endpoint = ((queueno == RSI_WIFI_MGMT_Q) ? MGMT_EP : DATA_EP);
333 return rsi_write_multiple(adapter,
334 endpoint,
335 (u8 *)pkt,
336 len);
340 * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
341 * @adapter: Pointer to the adapter structure.
343 * Return: None.
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;
366 int status;
368 rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
369 if (!rsi_dev)
370 return -ENOMEM;
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))
376 return -EINVAL;
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",
384 __func__);
385 return -ENOMEM;
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");
402 if (status) {
403 rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
404 goto fail;
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);
410 #endif
412 rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
413 return 0;
415 fail:
416 kfree(rsi_dev->tx_buffer);
417 kfree(common->rx_data_pkt);
418 return status;
422 * rsi_probe() - This function is called by kernel when the driver provided
423 * Vendor and device IDs are matched. All the initialization
424 * work is done here.
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;
435 u16 fw_status;
437 rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
439 adapter = rsi_91x_init();
440 if (!adapter) {
441 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
442 __func__);
443 return 1;
446 if (rsi_init_usb_interface(adapter, pfunction)) {
447 rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
448 __func__);
449 goto err;
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)
457 goto err1;
458 else
459 fw_status &= 1;
461 if (!fw_status) {
462 if (rsi_usb_device_init(adapter->priv)) {
463 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
464 __func__);
465 goto err1;
468 if (rsi_usb_reg_write(dev->usbdev,
469 USB_INTERNAL_REG_1,
470 RSI_USB_READY_MAGIC_NUM, 1) < 0)
471 goto err1;
472 rsi_dbg(INIT_ZONE, "%s: Performed device init\n", __func__);
475 if (rsi_rx_urb_submit(adapter))
476 goto err1;
478 return 0;
479 err1:
480 rsi_deinit_usb_interface(adapter);
481 err:
482 rsi_91x_deinit(adapter);
483 rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
484 return 1;
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.
492 * Return: None.
494 static void rsi_disconnect(struct usb_interface *pfunction)
496 struct rsi_hw *adapter = usb_get_intfdata(pfunction);
498 if (!adapter)
499 return;
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__);
508 #ifdef CONFIG_PM
509 static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
511 /* Not yet implemented */
512 return -ENOSYS;
515 static int rsi_resume(struct usb_interface *intf)
517 /* Not yet implemented */
518 return -ENOSYS;
520 #endif
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) },
527 { /* Blank */},
530 static struct usb_driver rsi_driver = {
531 .name = "RSI-USB WLAN",
532 .probe = rsi_probe,
533 .disconnect = rsi_disconnect,
534 .id_table = rsi_dev_table,
535 #ifdef CONFIG_PM
536 .suspend = rsi_suspend,
537 .resume = rsi_resume,
538 #endif
542 * rsi_module_init() - This function registers the client driver.
543 * @void: Void.
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__);
551 return 0;
555 * rsi_module_exit() - This function unregisters the client driver.
556 * @void: Void.
558 * Return: None.
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");