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, a negative error code 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, a negative error code 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, a negative error code 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, a negative error code 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, a negative error code 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, a negative error code 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, a negative error code 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        usb_free_urb(dev->rx_usb_urb[0]);
 363        kfree(adapter->priv->rx_data_pkt);
 364        kfree(dev->tx_buffer);
 365}
 366
 367/**
 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.
 371 *
 372 * Return: 0 on success, a negative error code on failure.
 373 */
 374static int rsi_init_usb_interface(struct rsi_hw *adapter,
 375                                  struct usb_interface *pfunction)
 376{
 377        struct rsi_91x_usbdev *rsi_dev;
 378        struct rsi_common *common = adapter->priv;
 379        int status;
 380
 381        rsi_dev = kzalloc(sizeof(*rsi_dev), GFP_KERNEL);
 382        if (!rsi_dev)
 383                return -ENOMEM;
 384
 385        adapter->rsi_dev = rsi_dev;
 386        rsi_dev->usbdev = interface_to_usbdev(pfunction);
 387
 388        if (rsi_find_bulk_in_and_out_endpoints(pfunction, adapter))
 389                return -EINVAL;
 390
 391        adapter->device = &pfunction->dev;
 392        usb_set_intfdata(pfunction, adapter);
 393
 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",
 397                        __func__);
 398                return -ENOMEM;
 399        }
 400
 401        rsi_dev->tx_buffer = kmalloc(2048, GFP_KERNEL);
 402        if (!rsi_dev->tx_buffer) {
 403                status = -ENOMEM;
 404                goto fail_tx;
 405        }
 406        rsi_dev->rx_usb_urb[0] = usb_alloc_urb(0, GFP_KERNEL);
 407        if (!rsi_dev->rx_usb_urb[0]) {
 408                status = -ENOMEM;
 409                goto fail_rx;
 410        }
 411        rsi_dev->rx_usb_urb[0]->transfer_buffer = adapter->priv->rx_data_pkt;
 412        rsi_dev->tx_blk_size = 252;
 413
 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;
 419
 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");
 423        if (status) {
 424                rsi_dbg(ERR_ZONE, "%s: Unable to init rx thrd\n", __func__);
 425                goto fail_thread;
 426        }
 427
 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);
 431#endif
 432
 433        rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
 434        return 0;
 435
 436fail_thread:
 437        usb_free_urb(rsi_dev->rx_usb_urb[0]);
 438fail_rx:
 439        kfree(rsi_dev->tx_buffer);
 440fail_tx:
 441        kfree(common->rx_data_pkt);
 442        return status;
 443}
 444
 445/**
 446 * rsi_probe() - This function is called by kernel when the driver provided
 447 *               Vendor and device IDs are matched. All the initialization
 448 *               work is done here.
 449 * @pfunction: Pointer to the USB interface structure.
 450 * @id: Pointer to the usb_device_id structure.
 451 *
 452 * Return: 0 on success, a negative error code on failure.
 453 */
 454static int rsi_probe(struct usb_interface *pfunction,
 455                     const struct usb_device_id *id)
 456{
 457        struct rsi_hw *adapter;
 458        struct rsi_91x_usbdev *dev;
 459        u16 fw_status;
 460        int status;
 461
 462        rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
 463
 464        adapter = rsi_91x_init();
 465        if (!adapter) {
 466                rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
 467                        __func__);
 468                return -ENOMEM;
 469        }
 470
 471        status = rsi_init_usb_interface(adapter, pfunction);
 472        if (status) {
 473                rsi_dbg(ERR_ZONE, "%s: Failed to init usb interface\n",
 474                        __func__);
 475                goto err;
 476        }
 477
 478        rsi_dbg(ERR_ZONE, "%s: Initialized os intf ops\n", __func__);
 479
 480        dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
 481
 482        status = rsi_usb_reg_read(dev->usbdev, FW_STATUS_REG, &fw_status, 2);
 483        if (status)
 484                goto err1;
 485        else
 486                fw_status &= 1;
 487
 488        if (!fw_status) {
 489                status = rsi_usb_device_init(adapter->priv);
 490                if (status) {
 491                        rsi_dbg(ERR_ZONE, "%s: Failed in device init\n",
 492                                __func__);
 493                        goto err1;
 494                }
 495
 496                status = rsi_usb_reg_write(dev->usbdev,
 497                                           USB_INTERNAL_REG_1,
 498                                           RSI_USB_READY_MAGIC_NUM, 1);
 499                if (status)
 500                        goto err1;
 501                rsi_dbg(INIT_ZONE, "%s: Performed device init\n", __func__);
 502        }
 503
 504        status = rsi_rx_urb_submit(adapter);
 505        if (status)
 506                goto err1;
 507
 508        return 0;
 509err1:
 510        rsi_deinit_usb_interface(adapter);
 511err:
 512        rsi_91x_deinit(adapter);
 513        rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
 514        return status;
 515}
 516
 517/**
 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.
 521 *
 522 * Return: None.
 523 */
 524static void rsi_disconnect(struct usb_interface *pfunction)
 525{
 526        struct rsi_hw *adapter = usb_get_intfdata(pfunction);
 527
 528        if (!adapter)
 529                return;
 530
 531        rsi_mac80211_detach(adapter);
 532        rsi_deinit_usb_interface(adapter);
 533        rsi_91x_deinit(adapter);
 534
 535        rsi_dbg(INFO_ZONE, "%s: Deinitialization completed\n", __func__);
 536}
 537
 538#ifdef CONFIG_PM
 539static int rsi_suspend(struct usb_interface *intf, pm_message_t message)
 540{
 541        /* Not yet implemented */
 542        return -ENOSYS;
 543}
 544
 545static int rsi_resume(struct usb_interface *intf)
 546{
 547        /* Not yet implemented */
 548        return -ENOSYS;
 549}
 550#endif
 551
 552static 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) },
 557        { /* Blank */},
 558};
 559
 560static struct usb_driver rsi_driver = {
 561        .name       = "RSI-USB WLAN",
 562        .probe      = rsi_probe,
 563        .disconnect = rsi_disconnect,
 564        .id_table   = rsi_dev_table,
 565#ifdef CONFIG_PM
 566        .suspend    = rsi_suspend,
 567        .resume     = rsi_resume,
 568#endif
 569};
 570
 571module_usb_driver(rsi_driver);
 572
 573MODULE_AUTHOR("Redpine Signals Inc");
 574MODULE_DESCRIPTION("Common USB layer for RSI drivers");
 575MODULE_SUPPORTED_DEVICE("RSI-91x");
 576MODULE_DEVICE_TABLE(usb, rsi_dev_table);
 577MODULE_FIRMWARE(FIRMWARE_RSI9113);
 578MODULE_VERSION("0.1");
 579MODULE_LICENSE("Dual BSD/GPL");
 580