linux/drivers/net/wireless/ralink/rt2x00/rt2x00usb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3        Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
   4        Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
   5        <http://rt2x00.serialmonkey.com>
   6
   7 */
   8
   9/*
  10        Module: rt2x00usb
  11        Abstract: rt2x00 generic usb device routines.
  12 */
  13
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/usb.h>
  18#include <linux/bug.h>
  19
  20#include "rt2x00.h"
  21#include "rt2x00usb.h"
  22
  23static bool rt2x00usb_check_usb_error(struct rt2x00_dev *rt2x00dev, int status)
  24{
  25        if (status == -ENODEV || status == -ENOENT)
  26                return true;
  27
  28        if (status == -EPROTO || status == -ETIMEDOUT)
  29                rt2x00dev->num_proto_errs++;
  30        else
  31                rt2x00dev->num_proto_errs = 0;
  32
  33        if (rt2x00dev->num_proto_errs > 3)
  34                return true;
  35
  36        return false;
  37}
  38
  39/*
  40 * Interfacing with the HW.
  41 */
  42int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  43                             const u8 request, const u8 requesttype,
  44                             const u16 offset, const u16 value,
  45                             void *buffer, const u16 buffer_length,
  46                             const int timeout)
  47{
  48        struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  49        int status;
  50        unsigned int pipe =
  51            (requesttype == USB_VENDOR_REQUEST_IN) ?
  52            usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
  53        unsigned long expire = jiffies + msecs_to_jiffies(timeout);
  54
  55        if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  56                return -ENODEV;
  57
  58        do {
  59                status = usb_control_msg(usb_dev, pipe, request, requesttype,
  60                                         value, offset, buffer, buffer_length,
  61                                         timeout / 2);
  62                if (status >= 0)
  63                        return 0;
  64
  65                if (rt2x00usb_check_usb_error(rt2x00dev, status)) {
  66                        /* Device has disappeared. */
  67                        clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  68                        break;
  69                }
  70        } while (time_before(jiffies, expire));
  71
  72        rt2x00_err(rt2x00dev,
  73                   "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
  74                   request, offset, status);
  75
  76        return status;
  77}
  78EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
  79
  80int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
  81                                   const u8 request, const u8 requesttype,
  82                                   const u16 offset, void *buffer,
  83                                   const u16 buffer_length, const int timeout)
  84{
  85        int status;
  86
  87        BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
  88
  89        /*
  90         * Check for Cache availability.
  91         */
  92        if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
  93                rt2x00_err(rt2x00dev, "CSR cache not available\n");
  94                return -ENOMEM;
  95        }
  96
  97        if (requesttype == USB_VENDOR_REQUEST_OUT)
  98                memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
  99
 100        status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
 101                                          offset, 0, rt2x00dev->csr.cache,
 102                                          buffer_length, timeout);
 103
 104        if (!status && requesttype == USB_VENDOR_REQUEST_IN)
 105                memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
 106
 107        return status;
 108}
 109EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
 110
 111int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
 112                                  const u8 request, const u8 requesttype,
 113                                  const u16 offset, void *buffer,
 114                                  const u16 buffer_length)
 115{
 116        int status = 0;
 117        unsigned char *tb;
 118        u16 off, len, bsize;
 119
 120        mutex_lock(&rt2x00dev->csr_mutex);
 121
 122        tb  = (char *)buffer;
 123        off = offset;
 124        len = buffer_length;
 125        while (len && !status) {
 126                bsize = min_t(u16, CSR_CACHE_SIZE, len);
 127                status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
 128                                                        requesttype, off, tb,
 129                                                        bsize, REGISTER_TIMEOUT);
 130
 131                tb  += bsize;
 132                len -= bsize;
 133                off += bsize;
 134        }
 135
 136        mutex_unlock(&rt2x00dev->csr_mutex);
 137
 138        return status;
 139}
 140EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
 141
 142int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
 143                           const unsigned int offset,
 144                           const struct rt2x00_field32 field,
 145                           u32 *reg)
 146{
 147        unsigned int i;
 148
 149        if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
 150                return -ENODEV;
 151
 152        for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
 153                *reg = rt2x00usb_register_read_lock(rt2x00dev, offset);
 154                if (!rt2x00_get_field32(*reg, field))
 155                        return 1;
 156                udelay(REGISTER_BUSY_DELAY);
 157        }
 158
 159        rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
 160                   offset, *reg);
 161        *reg = ~0;
 162
 163        return 0;
 164}
 165EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
 166
 167
 168struct rt2x00_async_read_data {
 169        __le32 reg;
 170        struct usb_ctrlrequest cr;
 171        struct rt2x00_dev *rt2x00dev;
 172        bool (*callback)(struct rt2x00_dev *, int, u32);
 173};
 174
 175static void rt2x00usb_register_read_async_cb(struct urb *urb)
 176{
 177        struct rt2x00_async_read_data *rd = urb->context;
 178        if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
 179                usb_anchor_urb(urb, rd->rt2x00dev->anchor);
 180                if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
 181                        usb_unanchor_urb(urb);
 182                        kfree(rd);
 183                }
 184        } else
 185                kfree(rd);
 186}
 187
 188void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
 189                                   const unsigned int offset,
 190                                   bool (*callback)(struct rt2x00_dev*, int, u32))
 191{
 192        struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
 193        struct urb *urb;
 194        struct rt2x00_async_read_data *rd;
 195
 196        rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
 197        if (!rd)
 198                return;
 199
 200        urb = usb_alloc_urb(0, GFP_ATOMIC);
 201        if (!urb) {
 202                kfree(rd);
 203                return;
 204        }
 205
 206        rd->rt2x00dev = rt2x00dev;
 207        rd->callback = callback;
 208        rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
 209        rd->cr.bRequest = USB_MULTI_READ;
 210        rd->cr.wValue = 0;
 211        rd->cr.wIndex = cpu_to_le16(offset);
 212        rd->cr.wLength = cpu_to_le16(sizeof(u32));
 213
 214        usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
 215                             (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
 216                             rt2x00usb_register_read_async_cb, rd);
 217        usb_anchor_urb(urb, rt2x00dev->anchor);
 218        if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
 219                usb_unanchor_urb(urb);
 220                kfree(rd);
 221        }
 222        usb_free_urb(urb);
 223}
 224EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
 225
 226/*
 227 * TX data handlers.
 228 */
 229static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
 230{
 231        /*
 232         * If the transfer to hardware succeeded, it does not mean the
 233         * frame was send out correctly. It only means the frame
 234         * was successfully pushed to the hardware, we have no
 235         * way to determine the transmission status right now.
 236         * (Only indirectly by looking at the failed TX counters
 237         * in the register).
 238         */
 239        if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
 240                rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
 241        else
 242                rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
 243}
 244
 245static void rt2x00usb_work_txdone(struct work_struct *work)
 246{
 247        struct rt2x00_dev *rt2x00dev =
 248            container_of(work, struct rt2x00_dev, txdone_work);
 249        struct data_queue *queue;
 250        struct queue_entry *entry;
 251
 252        tx_queue_for_each(rt2x00dev, queue) {
 253                while (!rt2x00queue_empty(queue)) {
 254                        entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
 255
 256                        if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
 257                            !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
 258                                break;
 259
 260                        rt2x00usb_work_txdone_entry(entry);
 261                }
 262        }
 263}
 264
 265static void rt2x00usb_interrupt_txdone(struct urb *urb)
 266{
 267        struct queue_entry *entry = (struct queue_entry *)urb->context;
 268        struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
 269
 270        if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
 271                return;
 272        /*
 273         * Check if the frame was correctly uploaded
 274         */
 275        if (urb->status)
 276                set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
 277        /*
 278         * Report the frame as DMA done
 279         */
 280        rt2x00lib_dmadone(entry);
 281
 282        if (rt2x00dev->ops->lib->tx_dma_done)
 283                rt2x00dev->ops->lib->tx_dma_done(entry);
 284        /*
 285         * Schedule the delayed work for reading the TX status
 286         * from the device.
 287         */
 288        if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
 289            !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
 290                queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
 291}
 292
 293static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
 294{
 295        struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
 296        struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
 297        struct queue_entry_priv_usb *entry_priv = entry->priv_data;
 298        u32 length;
 299        int status;
 300
 301        if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
 302            test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
 303                return false;
 304
 305        /*
 306         * USB devices require certain padding at the end of each frame
 307         * and urb. Those paddings are not included in skbs. Pass entry
 308         * to the driver to determine what the overall length should be.
 309         */
 310        length = rt2x00dev->ops->lib->get_tx_data_len(entry);
 311
 312        status = skb_padto(entry->skb, length);
 313        if (unlikely(status)) {
 314                /* TODO: report something more appropriate than IO_FAILED. */
 315                rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
 316                set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
 317                rt2x00lib_dmadone(entry);
 318
 319                return false;
 320        }
 321
 322        usb_fill_bulk_urb(entry_priv->urb, usb_dev,
 323                          usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
 324                          entry->skb->data, length,
 325                          rt2x00usb_interrupt_txdone, entry);
 326
 327        status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
 328        if (status) {
 329                if (rt2x00usb_check_usb_error(rt2x00dev, status))
 330                        clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
 331                set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
 332                rt2x00lib_dmadone(entry);
 333        }
 334
 335        return false;
 336}
 337
 338/*
 339 * RX data handlers.
 340 */
 341static void rt2x00usb_work_rxdone(struct work_struct *work)
 342{
 343        struct rt2x00_dev *rt2x00dev =
 344            container_of(work, struct rt2x00_dev, rxdone_work);
 345        struct queue_entry *entry;
 346        struct skb_frame_desc *skbdesc;
 347        u8 rxd[32];
 348
 349        while (!rt2x00queue_empty(rt2x00dev->rx)) {
 350                entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
 351
 352                if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
 353                    !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
 354                        break;
 355
 356                /*
 357                 * Fill in desc fields of the skb descriptor
 358                 */
 359                skbdesc = get_skb_frame_desc(entry->skb);
 360                skbdesc->desc = rxd;
 361                skbdesc->desc_len = entry->queue->desc_size;
 362
 363                /*
 364                 * Send the frame to rt2x00lib for further processing.
 365                 */
 366                rt2x00lib_rxdone(entry, GFP_KERNEL);
 367        }
 368}
 369
 370static void rt2x00usb_interrupt_rxdone(struct urb *urb)
 371{
 372        struct queue_entry *entry = (struct queue_entry *)urb->context;
 373        struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
 374
 375        if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
 376                return;
 377
 378        /*
 379         * Report the frame as DMA done
 380         */
 381        rt2x00lib_dmadone(entry);
 382
 383        /*
 384         * Check if the received data is simply too small
 385         * to be actually valid, or if the urb is signaling
 386         * a problem.
 387         */
 388        if (urb->actual_length < entry->queue->desc_size || urb->status)
 389                set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
 390
 391        /*
 392         * Schedule the delayed work for reading the RX status
 393         * from the device.
 394         */
 395        queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
 396}
 397
 398static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
 399{
 400        struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
 401        struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
 402        struct queue_entry_priv_usb *entry_priv = entry->priv_data;
 403        int status;
 404
 405        if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
 406            test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
 407                return false;
 408
 409        rt2x00lib_dmastart(entry);
 410
 411        usb_fill_bulk_urb(entry_priv->urb, usb_dev,
 412                          usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
 413                          entry->skb->data, entry->skb->len,
 414                          rt2x00usb_interrupt_rxdone, entry);
 415
 416        status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
 417        if (status) {
 418                if (rt2x00usb_check_usb_error(rt2x00dev, status))
 419                        clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
 420                set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
 421                rt2x00lib_dmadone(entry);
 422        }
 423
 424        return false;
 425}
 426
 427void rt2x00usb_kick_queue(struct data_queue *queue)
 428{
 429        switch (queue->qid) {
 430        case QID_AC_VO:
 431        case QID_AC_VI:
 432        case QID_AC_BE:
 433        case QID_AC_BK:
 434                if (!rt2x00queue_empty(queue))
 435                        rt2x00queue_for_each_entry(queue,
 436                                                   Q_INDEX_DONE,
 437                                                   Q_INDEX,
 438                                                   NULL,
 439                                                   rt2x00usb_kick_tx_entry);
 440                break;
 441        case QID_RX:
 442                if (!rt2x00queue_full(queue))
 443                        rt2x00queue_for_each_entry(queue,
 444                                                   Q_INDEX,
 445                                                   Q_INDEX_DONE,
 446                                                   NULL,
 447                                                   rt2x00usb_kick_rx_entry);
 448                break;
 449        default:
 450                break;
 451        }
 452}
 453EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
 454
 455static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
 456{
 457        struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
 458        struct queue_entry_priv_usb *entry_priv = entry->priv_data;
 459        struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
 460
 461        if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
 462                return false;
 463
 464        usb_kill_urb(entry_priv->urb);
 465
 466        /*
 467         * Kill guardian urb (if required by driver).
 468         */
 469        if ((entry->queue->qid == QID_BEACON) &&
 470            (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
 471                usb_kill_urb(bcn_priv->guardian_urb);
 472
 473        return false;
 474}
 475
 476void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
 477{
 478        struct work_struct *completion;
 479        unsigned int i;
 480
 481        if (drop)
 482                rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
 483                                           rt2x00usb_flush_entry);
 484
 485        /*
 486         * Obtain the queue completion handler
 487         */
 488        switch (queue->qid) {
 489        case QID_AC_VO:
 490        case QID_AC_VI:
 491        case QID_AC_BE:
 492        case QID_AC_BK:
 493                completion = &queue->rt2x00dev->txdone_work;
 494                break;
 495        case QID_RX:
 496                completion = &queue->rt2x00dev->rxdone_work;
 497                break;
 498        default:
 499                return;
 500        }
 501
 502        for (i = 0; i < 10; i++) {
 503                /*
 504                 * Check if the driver is already done, otherwise we
 505                 * have to sleep a little while to give the driver/hw
 506                 * the oppurtunity to complete interrupt process itself.
 507                 */
 508                if (rt2x00queue_empty(queue))
 509                        break;
 510
 511                /*
 512                 * Schedule the completion handler manually, when this
 513                 * worker function runs, it should cleanup the queue.
 514                 */
 515                queue_work(queue->rt2x00dev->workqueue, completion);
 516
 517                /*
 518                 * Wait for a little while to give the driver
 519                 * the oppurtunity to recover itself.
 520                 */
 521                msleep(50);
 522        }
 523}
 524EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
 525
 526static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
 527{
 528        rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n",
 529                    queue->qid);
 530
 531        rt2x00queue_stop_queue(queue);
 532        rt2x00queue_flush_queue(queue, true);
 533        rt2x00queue_start_queue(queue);
 534}
 535
 536static int rt2x00usb_dma_timeout(struct data_queue *queue)
 537{
 538        struct queue_entry *entry;
 539
 540        entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
 541        return rt2x00queue_dma_timeout(entry);
 542}
 543
 544void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
 545{
 546        struct data_queue *queue;
 547
 548        tx_queue_for_each(rt2x00dev, queue) {
 549                if (!rt2x00queue_empty(queue)) {
 550                        if (rt2x00usb_dma_timeout(queue))
 551                                rt2x00usb_watchdog_tx_dma(queue);
 552                }
 553        }
 554}
 555EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
 556
 557/*
 558 * Radio handlers
 559 */
 560void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
 561{
 562        rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
 563                                    REGISTER_TIMEOUT);
 564}
 565EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
 566
 567/*
 568 * Device initialization handlers.
 569 */
 570void rt2x00usb_clear_entry(struct queue_entry *entry)
 571{
 572        entry->flags = 0;
 573
 574        if (entry->queue->qid == QID_RX)
 575                rt2x00usb_kick_rx_entry(entry, NULL);
 576}
 577EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
 578
 579static void rt2x00usb_assign_endpoint(struct data_queue *queue,
 580                                      struct usb_endpoint_descriptor *ep_desc)
 581{
 582        struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
 583        int pipe;
 584
 585        queue->usb_endpoint = usb_endpoint_num(ep_desc);
 586
 587        if (queue->qid == QID_RX) {
 588                pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
 589                queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
 590        } else {
 591                pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
 592                queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
 593        }
 594
 595        if (!queue->usb_maxpacket)
 596                queue->usb_maxpacket = 1;
 597}
 598
 599static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
 600{
 601        struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
 602        struct usb_host_interface *intf_desc = intf->cur_altsetting;
 603        struct usb_endpoint_descriptor *ep_desc;
 604        struct data_queue *queue = rt2x00dev->tx;
 605        struct usb_endpoint_descriptor *tx_ep_desc = NULL;
 606        unsigned int i;
 607
 608        /*
 609         * Walk through all available endpoints to search for "bulk in"
 610         * and "bulk out" endpoints. When we find such endpoints collect
 611         * the information we need from the descriptor and assign it
 612         * to the queue.
 613         */
 614        for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
 615                ep_desc = &intf_desc->endpoint[i].desc;
 616
 617                if (usb_endpoint_is_bulk_in(ep_desc)) {
 618                        rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
 619                } else if (usb_endpoint_is_bulk_out(ep_desc) &&
 620                           (queue != queue_end(rt2x00dev))) {
 621                        rt2x00usb_assign_endpoint(queue, ep_desc);
 622                        queue = queue_next(queue);
 623
 624                        tx_ep_desc = ep_desc;
 625                }
 626        }
 627
 628        /*
 629         * At least 1 endpoint for RX and 1 endpoint for TX must be available.
 630         */
 631        if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
 632                rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
 633                return -EPIPE;
 634        }
 635
 636        /*
 637         * It might be possible not all queues have a dedicated endpoint.
 638         * Loop through all TX queues and copy the endpoint information
 639         * which we have gathered from already assigned endpoints.
 640         */
 641        txall_queue_for_each(rt2x00dev, queue) {
 642                if (!queue->usb_endpoint)
 643                        rt2x00usb_assign_endpoint(queue, tx_ep_desc);
 644        }
 645
 646        return 0;
 647}
 648
 649static int rt2x00usb_alloc_entries(struct data_queue *queue)
 650{
 651        struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
 652        struct queue_entry_priv_usb *entry_priv;
 653        struct queue_entry_priv_usb_bcn *bcn_priv;
 654        unsigned int i;
 655
 656        for (i = 0; i < queue->limit; i++) {
 657                entry_priv = queue->entries[i].priv_data;
 658                entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
 659                if (!entry_priv->urb)
 660                        return -ENOMEM;
 661        }
 662
 663        /*
 664         * If this is not the beacon queue or
 665         * no guardian byte was required for the beacon,
 666         * then we are done.
 667         */
 668        if (queue->qid != QID_BEACON ||
 669            !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
 670                return 0;
 671
 672        for (i = 0; i < queue->limit; i++) {
 673                bcn_priv = queue->entries[i].priv_data;
 674                bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
 675                if (!bcn_priv->guardian_urb)
 676                        return -ENOMEM;
 677        }
 678
 679        return 0;
 680}
 681
 682static void rt2x00usb_free_entries(struct data_queue *queue)
 683{
 684        struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
 685        struct queue_entry_priv_usb *entry_priv;
 686        struct queue_entry_priv_usb_bcn *bcn_priv;
 687        unsigned int i;
 688
 689        if (!queue->entries)
 690                return;
 691
 692        for (i = 0; i < queue->limit; i++) {
 693                entry_priv = queue->entries[i].priv_data;
 694                usb_kill_urb(entry_priv->urb);
 695                usb_free_urb(entry_priv->urb);
 696        }
 697
 698        /*
 699         * If this is not the beacon queue or
 700         * no guardian byte was required for the beacon,
 701         * then we are done.
 702         */
 703        if (queue->qid != QID_BEACON ||
 704            !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
 705                return;
 706
 707        for (i = 0; i < queue->limit; i++) {
 708                bcn_priv = queue->entries[i].priv_data;
 709                usb_kill_urb(bcn_priv->guardian_urb);
 710                usb_free_urb(bcn_priv->guardian_urb);
 711        }
 712}
 713
 714int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
 715{
 716        struct data_queue *queue;
 717        int status;
 718
 719        /*
 720         * Find endpoints for each queue
 721         */
 722        status = rt2x00usb_find_endpoints(rt2x00dev);
 723        if (status)
 724                goto exit;
 725
 726        /*
 727         * Allocate DMA
 728         */
 729        queue_for_each(rt2x00dev, queue) {
 730                status = rt2x00usb_alloc_entries(queue);
 731                if (status)
 732                        goto exit;
 733        }
 734
 735        return 0;
 736
 737exit:
 738        rt2x00usb_uninitialize(rt2x00dev);
 739
 740        return status;
 741}
 742EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
 743
 744void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
 745{
 746        struct data_queue *queue;
 747
 748        usb_kill_anchored_urbs(rt2x00dev->anchor);
 749        hrtimer_cancel(&rt2x00dev->txstatus_timer);
 750        cancel_work_sync(&rt2x00dev->rxdone_work);
 751        cancel_work_sync(&rt2x00dev->txdone_work);
 752
 753        queue_for_each(rt2x00dev, queue)
 754                rt2x00usb_free_entries(queue);
 755}
 756EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
 757
 758/*
 759 * USB driver handlers.
 760 */
 761static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
 762{
 763        kfree(rt2x00dev->rf);
 764        rt2x00dev->rf = NULL;
 765
 766        kfree(rt2x00dev->eeprom);
 767        rt2x00dev->eeprom = NULL;
 768
 769        kfree(rt2x00dev->csr.cache);
 770        rt2x00dev->csr.cache = NULL;
 771}
 772
 773static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
 774{
 775        rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
 776        if (!rt2x00dev->csr.cache)
 777                goto exit;
 778
 779        rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
 780        if (!rt2x00dev->eeprom)
 781                goto exit;
 782
 783        rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
 784        if (!rt2x00dev->rf)
 785                goto exit;
 786
 787        return 0;
 788
 789exit:
 790        rt2x00_probe_err("Failed to allocate registers\n");
 791
 792        rt2x00usb_free_reg(rt2x00dev);
 793
 794        return -ENOMEM;
 795}
 796
 797int rt2x00usb_probe(struct usb_interface *usb_intf,
 798                    const struct rt2x00_ops *ops)
 799{
 800        struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
 801        struct ieee80211_hw *hw;
 802        struct rt2x00_dev *rt2x00dev;
 803        int retval;
 804
 805        usb_dev = usb_get_dev(usb_dev);
 806        usb_reset_device(usb_dev);
 807
 808        hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
 809        if (!hw) {
 810                rt2x00_probe_err("Failed to allocate hardware\n");
 811                retval = -ENOMEM;
 812                goto exit_put_device;
 813        }
 814
 815        usb_set_intfdata(usb_intf, hw);
 816
 817        rt2x00dev = hw->priv;
 818        rt2x00dev->dev = &usb_intf->dev;
 819        rt2x00dev->ops = ops;
 820        rt2x00dev->hw = hw;
 821
 822        rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
 823
 824        INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
 825        INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
 826        hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
 827                     HRTIMER_MODE_REL);
 828
 829        retval = rt2x00usb_alloc_reg(rt2x00dev);
 830        if (retval)
 831                goto exit_free_device;
 832
 833        rt2x00dev->anchor = devm_kmalloc(&usb_dev->dev,
 834                                        sizeof(struct usb_anchor),
 835                                        GFP_KERNEL);
 836        if (!rt2x00dev->anchor) {
 837                retval = -ENOMEM;
 838                goto exit_free_reg;
 839        }
 840        init_usb_anchor(rt2x00dev->anchor);
 841
 842        retval = rt2x00lib_probe_dev(rt2x00dev);
 843        if (retval)
 844                goto exit_free_anchor;
 845
 846        return 0;
 847
 848exit_free_anchor:
 849        usb_kill_anchored_urbs(rt2x00dev->anchor);
 850
 851exit_free_reg:
 852        rt2x00usb_free_reg(rt2x00dev);
 853
 854exit_free_device:
 855        ieee80211_free_hw(hw);
 856
 857exit_put_device:
 858        usb_put_dev(usb_dev);
 859
 860        usb_set_intfdata(usb_intf, NULL);
 861
 862        return retval;
 863}
 864EXPORT_SYMBOL_GPL(rt2x00usb_probe);
 865
 866void rt2x00usb_disconnect(struct usb_interface *usb_intf)
 867{
 868        struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
 869        struct rt2x00_dev *rt2x00dev = hw->priv;
 870
 871        /*
 872         * Free all allocated data.
 873         */
 874        rt2x00lib_remove_dev(rt2x00dev);
 875        rt2x00usb_free_reg(rt2x00dev);
 876        ieee80211_free_hw(hw);
 877
 878        /*
 879         * Free the USB device data.
 880         */
 881        usb_set_intfdata(usb_intf, NULL);
 882        usb_put_dev(interface_to_usbdev(usb_intf));
 883}
 884EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
 885
 886#ifdef CONFIG_PM
 887int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
 888{
 889        struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
 890        struct rt2x00_dev *rt2x00dev = hw->priv;
 891
 892        return rt2x00lib_suspend(rt2x00dev, state);
 893}
 894EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
 895
 896int rt2x00usb_resume(struct usb_interface *usb_intf)
 897{
 898        struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
 899        struct rt2x00_dev *rt2x00dev = hw->priv;
 900
 901        return rt2x00lib_resume(rt2x00dev);
 902}
 903EXPORT_SYMBOL_GPL(rt2x00usb_resume);
 904#endif /* CONFIG_PM */
 905
 906/*
 907 * rt2x00usb module information.
 908 */
 909MODULE_AUTHOR(DRV_PROJECT);
 910MODULE_VERSION(DRV_VERSION);
 911MODULE_DESCRIPTION("rt2x00 usb library");
 912MODULE_LICENSE("GPL");
 913