linux/drivers/net/wireless/rsi/rsi_91x_usb.c
<<
>>
Prefs
   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
  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.
  27 *
  28 * Return: status: 0 on success, -1 on failure.
  29 */
  30static int rsi_usb_card_write(struct rsi_hw *adapter,
  31                              void *buf,
  32                              u16 len,
  33                              u8 endpoint)
  34{
  35        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  36        int status;
  37        s32 transfer;
  38
  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);
  46
  47        if (status < 0) {
  48                rsi_dbg(ERR_ZONE,
  49                        "Card write failed with error code :%10d\n", status);
  50                dev->write_fail = 1;
  51        }
  52        return status;
  53}
  54
  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.
  62 *
  63 * Return: 0 on success, -1 on failure.
  64 */
  65static int rsi_write_multiple(struct rsi_hw *adapter,
  66                              u8 endpoint,
  67                              u8 *data,
  68                              u32 count)
  69{
  70        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  71        u8 *seg = dev->tx_buffer;
  72
  73        if (dev->write_fail)
  74                return 0;
  75
  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);
  81        }
  82
  83        return rsi_usb_card_write(adapter,
  84                                  seg,
  85                                  count + RSI_USB_TX_HEAD_ROOM,
  86                                  endpoint);
  87}
  88
  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.
  94 *
  95 * Return: ret_val: 0 on success, -ENOMEM on failure.
  96 */
  97static int rsi_find_bulk_in_and_out_endpoints(struct usb_interface *interface,
  98                                              struct rsi_hw *adapter)
  99{
 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;
 105
 106        iface_desc = &(interface->altsetting[0]);
 107
 108        for (ii = 0; ii < iface_desc->desc.bNumEndpoints; ++ii) {
 109                endpoint = &(iface_desc->endpoint[ii].desc);
 110
 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;
 120                }
 121
 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++;
 131                }
 132
 133                if (bep_found >= MAX_BULK_EP)
 134                        break;
 135        }
 136
 137        if (!(dev->bulkin_endpoint_addr) &&
 138            (dev->bulkout_endpoint_addr[0]))
 139                return -EINVAL;
 140
 141        return 0;
 142}
 143
 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.
 149 *
 150 * Return: status: 0 on success, -1 on failure.
 151 */
 152static int rsi_usb_reg_read(struct usb_device *usbdev,
 153                            u32 reg,
 154                            u16 *value,
 155                            u16 len)
 156{
 157        u8 *buf;
 158        int status = -ENOMEM;
 159
 160        buf  = kmalloc(0x04, GFP_KERNEL);
 161        if (!buf)
 162                return status;
 163
 164        status = usb_control_msg(usbdev,
 165                                 usb_rcvctrlpipe(usbdev, 0),
 166                                 USB_VENDOR_REGISTER_READ,
 167                                 USB_TYPE_VENDOR,
 168                                 ((reg & 0xffff0000) >> 16), (reg & 0xffff),
 169                                 (void *)buf,
 170                                 len,
 171                                 HZ * 5);
 172
 173        *value = (buf[0] | (buf[1] << 8));
 174        if (status < 0) {
 175                rsi_dbg(ERR_ZONE,
 176                        "%s: Reg read failed with error code :%d\n",
 177                        __func__, status);
 178        }
 179        kfree(buf);
 180
 181        return status;
 182}
 183
 184/**
 185 * rsi_usb_reg_write() - This function writes the given data into the given
 186 *                       register address.
 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.
 191 *
 192 * Return: status: 0 on success, -1 on failure.
 193 */
 194static int rsi_usb_reg_write(struct usb_device *usbdev,
 195                             u32 reg,
 196                             u16 value,
 197                             u16 len)
 198{
 199        u8 *usb_reg_buf;
 200        int status = -ENOMEM;
 201
 202        usb_reg_buf  = kmalloc(0x04, GFP_KERNEL);
 203        if (!usb_reg_buf)
 204                return status;
 205
 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;
 210
 211        status = usb_control_msg(usbdev,
 212                                 usb_sndctrlpipe(usbdev, 0),
 213                                 USB_VENDOR_REGISTER_WRITE,
 214                                 USB_TYPE_VENDOR,
 215                                 ((reg & 0xffff0000) >> 16),
 216                                 (reg & 0xffff),
 217                                 (void *)usb_reg_buf,
 218                                 len,
 219                                 HZ * 5);
 220        if (status < 0) {
 221                rsi_dbg(ERR_ZONE,
 222                        "%s: Reg write failed with error code :%d\n",
 223                        __func__, status);
 224        }
 225        kfree(usb_reg_buf);
 226
 227        return status;
 228}
 229
 230/**
 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.
 234 *
 235 * Return: None.
 236 */
 237static void rsi_rx_done_handler(struct urb *urb)
 238{
 239        struct rsi_hw *adapter = urb->context;
 240        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 241
 242        if (urb->status)
 243                return;
 244
 245        rsi_set_event(&dev->rx_thread.event);
 246}
 247
 248/**
 249 * rsi_rx_urb_submit() - This function submits the given URB to the USB stack.
 250 * @adapter: Pointer to the adapter structure.
 251 *
 252 * Return: 0 on success, -1 on failure.
 253 */
 254static int rsi_rx_urb_submit(struct rsi_hw *adapter)
 255{
 256        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 257        struct urb *urb = dev->rx_usb_urb[0];
 258        int status;
 259
 260        usb_fill_bulk_urb(urb,
 261                          dev->usbdev,
 262                          usb_rcvbulkpipe(dev->usbdev,
 263                                dev->bulkin_endpoint_addr),
 264                          urb->transfer_buffer,
 265                          3000,
 266                          rsi_rx_done_handler,
 267                          adapter);
 268
 269        status = usb_submit_urb(urb, GFP_KERNEL);
 270        if (status)
 271                rsi_dbg(ERR_ZONE, "%s: Failed in urb submission\n", __func__);
 272
 273        return status;
 274}
 275
 276/**
 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.
 283 *
 284 * Return: status: 0 on success, -1 on failure.
 285 */
 286int rsi_usb_write_register_multiple(struct rsi_hw *adapter,
 287                                    u32 addr,
 288                                    u8 *data,
 289                                    u32 count)
 290{
 291        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 292        u8 *buf;
 293        u8 transfer;
 294        int status = 0;
 295
 296        buf = kzalloc(4096, GFP_KERNEL);
 297        if (!buf)
 298                return -ENOMEM;
 299
 300        while (count) {
 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,
 306                                         USB_TYPE_VENDOR,
 307                                         ((addr & 0xffff0000) >> 16),
 308                                         (addr & 0xffff),
 309                                         (void *)buf,
 310                                         transfer,
 311                                         HZ * 5);
 312                if (status < 0) {
 313                        rsi_dbg(ERR_ZONE,
 314                                "Reg write failed with error code :%d\n",
 315                                status);
 316                } else {
 317                        count -= transfer;
 318                        data += transfer;
 319                        addr += transfer;
 320                }
 321        }
 322
 323        kfree(buf);
 324        return 0;
 325}
 326
 327/**
 328 *rsi_usb_host_intf_write_pkt() - This function writes the packet to the
 329 *                                 USB card.
 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.
 333 *
 334 * Return: 0 on success, -1 on failure.
 335 */
 336static int rsi_usb_host_intf_write_pkt(struct rsi_hw *adapter,
 337                                       u8 *pkt,
 338                                       u32 len)
 339{
 340        u32 queueno = ((pkt[1] >> 4) & 0xf);
 341        u8 endpoint;
 342
 343        endpoint = ((queueno == RSI_WIFI_MGMT_Q) ? MGMT_EP : DATA_EP);
 344
 345        return rsi_write_multiple(adapter,
 346                                  endpoint,
 347                                  (u8 *)pkt,
 348                                  len);
 349}
 350
 351/**
 352 * rsi_deinit_usb_interface() - This function deinitializes the usb interface.
 353 * @adapter: Pointer to the adapter structure.
 354 *
 355 * Return: None.
 356 */
 357static void rsi_deinit_usb_interface(struct rsi_hw *adapter)
 358{
 359        struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 360
 361        rsi_kill_thread(&dev->rx_thread);
 362        kfree(adapter->priv->rx_data_pkt);
 363        kfree(dev->tx_buffer);
 364}
 365
 366/**
 367 * rsi_init_usb_interface() - This function initializes the usb interface.
 368 * @adapter: Pointer to the adapter structure.
 369 * @pfunction: Pointer to USB interface structure.
 370 *
 371 * Return: 0 on success, -1 on failure.
 372 */
 373static int rsi_init_usb_interface(struct rsi_hw *adapter,
 374                                  struct usb_interface *pfunction)
 375{
 376        struct rsi_91x_usbdev *rsi_dev;
 377        struct rsi_common *common = adapter->priv;
 378        int status;
 379
 380        rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
 381        if (!rsi_dev)
 382                return -ENOMEM;
 383
 384        adapter->rsi_dev = rsi_dev;
 385        rsi_dev->usbdev = interface_to_usbdev(pfunction);
 386
 387        if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter))
 388                return -EINVAL;
 389
 390        adapter->device = &pfunction->dev;
 391        usb_set_intfdata(pfunction, adapter);
 392
 393        common->rx_data_pkt = kmalloc(2048, GFP_KERNEL);
 394        if (!common->rx_data_pkt) {
 395                rsi_dbg(ERR_ZONE, "%s: Failed to allocate memory\n",
 396                        __func__);
 397                return -ENOMEM;
 398        }
 399
 400        rsi_dev->tx_buffer = kmalloc(2048, GFP_ATOMIC);
 401        rsi_dev->rx_usb_urb[0] = usb_alloc_urb(0, GFP_KERNEL);
 402        rsi_dev->rx_usb_urb[0]->transfer_buffer = adapter->priv->rx_data_pkt;
 403        rsi_dev->tx_blk_size = 252;
 404
 405        /* Initializing function callbacks */
 406        adapter->rx_urb_submit = rsi_rx_urb_submit;
 407        adapter->host_intf_write_pkt = rsi_usb_host_intf_write_pkt;
 408        adapter->check_hw_queue_status = rsi_usb_check_queue_status;
 409        adapter->determine_event_timeout = rsi_usb_event_timeout;
 410
 411        rsi_init_event(&rsi_dev->rx_thread.event);
 412        status = rsi_create_kthread(common, &rsi_dev->rx_thread,
 413                                    rsi_usb_rx_thread, "RX-Thread");
 414        if (status) {
 415                rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
 416                goto fail;
 417        }
 418
 419#ifdef CONFIG_RSI_DEBUGFS
 420        /* In USB, one less than the MAX_DEBUGFS_ENTRIES entries is required */
 421        adapter->num_debugfs_entries = (MAX_DEBUGFS_ENTRIES - 1);
 422#endif
 423
 424        rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
 425        return 0;
 426
 427fail:
 428        kfree(rsi_dev->tx_buffer);
 429        kfree(common->rx_data_pkt);
 430        return status;
 431}
 432
 433/**
 434 * rsi_probe() - This function is called by kernel when the driver provided
 435 *               Vendor and device IDs are matched. All the initialization
 436 *               work is done here.
 437 * @pfunction: Pointer to the USB interface structure.
 438 * @id: Pointer to the usb_device_id structure.
 439 *
 440 * Return: 0 on success, -1 on failure.
 441 */
 442static int rsi_probe(struct usb_interface *pfunction,
 443                     const struct usb_device_id *id)
 444{
 445        struct rsi_hw *adapter;
 446        struct rsi_91x_usbdev *dev;
 447        u16 fw_status;
 448
 449        rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
 450
 451        adapter = rsi_91x_init();
 452        if (!adapter) {
 453                rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
 454                        __func__);
 455                return 1;
 456        }
 457
 458        if (rsi_init_usb_interface(adapter, pfunction)) {
 459                rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
 460                        __func__);
 461                goto err;
 462        }
 463
 464        rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
 465
 466        dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 467
 468        if (rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2) < 0)
 469                goto err1;
 470        else
 471                fw_status &= 1;
 472
 473        if (!fw_status) {
 474                if (rsi_usb_device_init(adapter->priv)) {
 475                        rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
 476                                __func__);
 477                        goto err1;
 478                }
 479
 480                if (rsi_usb_reg_write(dev->usbdev,
 481                                      USB_INTERNAL_REG_1,
 482                                      RSI_USB_READY_MAGIC_NUM, 1) < 0)
 483                        goto err1;
 484                rsi_dbg(INIT_ZONE, "%s: Performed device init\n", __func__);
 485        }
 486
 487        if (rsi_rx_urb_submit(adapter))
 488                goto err1;
 489
 490        return 0;
 491err1:
 492        rsi_deinit_usb_interface(adapter);
 493err:
 494        rsi_91x_deinit(adapter);
 495        rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
 496        return 1;
 497}
 498
 499/**
 500 * rsi_disconnect() - This function performs the reverse of the probe function,
 501 *                    it deintialize the driver structure.
 502 * @pfunction: Pointer to the USB interface structure.
 503 *
 504 * Return: None.
 505 */
 506static void rsi_disconnect(struct usb_interface *pfunction)
 507{
 508        struct rsi_hw *adapter = usb_get_intfdata(pfunction);
 509
 510        if (!adapter)
 511                return;
 512
 513        rsi_mac80211_detach(adapter);
 514        rsi_deinit_usb_interface(adapter);
 515        rsi_91x_deinit(adapter);
 516
 517        rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
 518}
 519
 520#ifdef CONFIG_PM
 521static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
 522{
 523        /* Not yet implemented */
 524        return -ENOSYS;
 525}
 526
 527static int rsi_resume(struct usb_interface *intf)
 528{
 529        /* Not yet implemented */
 530        return -ENOSYS;
 531}
 532#endif
 533
 534static const struct usb_device_id rsi_dev_table[] = {
 535        { USB_DEVICE(0x0303, 0x0100) },
 536        { USB_DEVICE(0x041B, 0x0301) },
 537        { USB_DEVICE(0x041B, 0x0201) },
 538        { USB_DEVICE(0x041B, 0x9330) },
 539        { /* Blank */},
 540};
 541
 542static struct usb_driver rsi_driver = {
 543        .name       = "RSI-USB WLAN",
 544        .probe      = rsi_probe,
 545        .disconnect = rsi_disconnect,
 546        .id_table   = rsi_dev_table,
 547#ifdef CONFIG_PM
 548        .suspend    = rsi_suspend,
 549        .resume     = rsi_resume,
 550#endif
 551};
 552
 553/**
 554 * rsi_module_init() - This function registers the client driver.
 555 * @void: Void.
 556 *
 557 * Return: 0 on success.
 558 */
 559static int rsi_module_init(void)
 560{
 561        usb_register(&rsi_driver);
 562        rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
 563        return 0;
 564}
 565
 566/**
 567 * rsi_module_exit() - This function unregisters the client driver.
 568 * @void: Void.
 569 *
 570 * Return: None.
 571 */
 572static void rsi_module_exit(void)
 573{
 574        usb_deregister(&rsi_driver);
 575        rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
 576}
 577
 578module_init(rsi_module_init);
 579module_exit(rsi_module_exit);
 580
 581MODULE_AUTHOR("Redpine Signals Inc");
 582MODULE_DESCRIPTION("Common USB layer for RSI drivers");
 583MODULE_SUPPORTED_DEVICE("RSI-91x");
 584MODULE_DEVICE_TABLE(usb, rsi_dev_table);
 585MODULE_FIRMWARE(FIRMWARE_RSI9113);
 586MODULE_VERSION("0.1");
 587MODULE_LICENSE("Dual BSD/GPL");
 588
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.