Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
0004     Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
0005     <http://rt2x00.serialmonkey.com>
0006 
0007  */
0008 
0009 /*
0010     Module: rt2x00usb
0011     Abstract: rt2x00 generic usb device routines.
0012  */
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 #include <linux/usb.h>
0018 #include <linux/bug.h>
0019 
0020 #include "rt2x00.h"
0021 #include "rt2x00usb.h"
0022 
0023 static bool rt2x00usb_check_usb_error(struct rt2x00_dev *rt2x00dev, int status)
0024 {
0025     if (status == -ENODEV || status == -ENOENT)
0026         return true;
0027 
0028     if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
0029         return false;
0030 
0031     if (status == -EPROTO || status == -ETIMEDOUT)
0032         rt2x00dev->num_proto_errs++;
0033     else
0034         rt2x00dev->num_proto_errs = 0;
0035 
0036     if (rt2x00dev->num_proto_errs > 3)
0037         return true;
0038 
0039     return false;
0040 }
0041 
0042 /*
0043  * Interfacing with the HW.
0044  */
0045 int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
0046                  const u8 request, const u8 requesttype,
0047                  const u16 offset, const u16 value,
0048                  void *buffer, const u16 buffer_length,
0049                  const int timeout)
0050 {
0051     struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
0052     int status;
0053     unsigned int pipe =
0054         (requesttype == USB_VENDOR_REQUEST_IN) ?
0055         usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
0056     unsigned long expire = jiffies + msecs_to_jiffies(timeout);
0057 
0058     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0059         return -ENODEV;
0060 
0061     do {
0062         status = usb_control_msg(usb_dev, pipe, request, requesttype,
0063                      value, offset, buffer, buffer_length,
0064                      timeout / 2);
0065         if (status >= 0)
0066             return 0;
0067 
0068         if (rt2x00usb_check_usb_error(rt2x00dev, status)) {
0069             /* Device has disappeared. */
0070             clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
0071             break;
0072         }
0073     } while (time_before(jiffies, expire));
0074 
0075     rt2x00_err(rt2x00dev,
0076            "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
0077            request, offset, status);
0078 
0079     return status;
0080 }
0081 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
0082 
0083 int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
0084                    const u8 request, const u8 requesttype,
0085                    const u16 offset, void *buffer,
0086                    const u16 buffer_length, const int timeout)
0087 {
0088     int status;
0089 
0090     BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
0091 
0092     /*
0093      * Check for Cache availability.
0094      */
0095     if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
0096         rt2x00_err(rt2x00dev, "CSR cache not available\n");
0097         return -ENOMEM;
0098     }
0099 
0100     if (requesttype == USB_VENDOR_REQUEST_OUT)
0101         memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
0102 
0103     status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
0104                       offset, 0, rt2x00dev->csr.cache,
0105                       buffer_length, timeout);
0106 
0107     if (!status && requesttype == USB_VENDOR_REQUEST_IN)
0108         memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
0109 
0110     return status;
0111 }
0112 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
0113 
0114 int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
0115                   const u8 request, const u8 requesttype,
0116                   const u16 offset, void *buffer,
0117                   const u16 buffer_length)
0118 {
0119     int status = 0;
0120     unsigned char *tb;
0121     u16 off, len, bsize;
0122 
0123     mutex_lock(&rt2x00dev->csr_mutex);
0124 
0125     tb  = (char *)buffer;
0126     off = offset;
0127     len = buffer_length;
0128     while (len && !status) {
0129         bsize = min_t(u16, CSR_CACHE_SIZE, len);
0130         status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
0131                             requesttype, off, tb,
0132                             bsize, REGISTER_TIMEOUT);
0133 
0134         tb  += bsize;
0135         len -= bsize;
0136         off += bsize;
0137     }
0138 
0139     mutex_unlock(&rt2x00dev->csr_mutex);
0140 
0141     return status;
0142 }
0143 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
0144 
0145 int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
0146                const unsigned int offset,
0147                const struct rt2x00_field32 field,
0148                u32 *reg)
0149 {
0150     unsigned int i;
0151 
0152     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0153         return -ENODEV;
0154 
0155     for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
0156         *reg = rt2x00usb_register_read_lock(rt2x00dev, offset);
0157         if (!rt2x00_get_field32(*reg, field))
0158             return 1;
0159         udelay(REGISTER_BUSY_DELAY);
0160     }
0161 
0162     rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
0163            offset, *reg);
0164     *reg = ~0;
0165 
0166     return 0;
0167 }
0168 EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
0169 
0170 
0171 struct rt2x00_async_read_data {
0172     __le32 reg;
0173     struct usb_ctrlrequest cr;
0174     struct rt2x00_dev *rt2x00dev;
0175     bool (*callback)(struct rt2x00_dev *, int, u32);
0176 };
0177 
0178 static void rt2x00usb_register_read_async_cb(struct urb *urb)
0179 {
0180     struct rt2x00_async_read_data *rd = urb->context;
0181     if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
0182         usb_anchor_urb(urb, rd->rt2x00dev->anchor);
0183         if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
0184             usb_unanchor_urb(urb);
0185             kfree(rd);
0186         }
0187     } else
0188         kfree(rd);
0189 }
0190 
0191 void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
0192                    const unsigned int offset,
0193                    bool (*callback)(struct rt2x00_dev*, int, u32))
0194 {
0195     struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
0196     struct urb *urb;
0197     struct rt2x00_async_read_data *rd;
0198 
0199     rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
0200     if (!rd)
0201         return;
0202 
0203     urb = usb_alloc_urb(0, GFP_ATOMIC);
0204     if (!urb) {
0205         kfree(rd);
0206         return;
0207     }
0208 
0209     rd->rt2x00dev = rt2x00dev;
0210     rd->callback = callback;
0211     rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
0212     rd->cr.bRequest = USB_MULTI_READ;
0213     rd->cr.wValue = 0;
0214     rd->cr.wIndex = cpu_to_le16(offset);
0215     rd->cr.wLength = cpu_to_le16(sizeof(u32));
0216 
0217     usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
0218                  (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
0219                  rt2x00usb_register_read_async_cb, rd);
0220     usb_anchor_urb(urb, rt2x00dev->anchor);
0221     if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
0222         usb_unanchor_urb(urb);
0223         kfree(rd);
0224     }
0225     usb_free_urb(urb);
0226 }
0227 EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
0228 
0229 /*
0230  * TX data handlers.
0231  */
0232 static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
0233 {
0234     /*
0235      * If the transfer to hardware succeeded, it does not mean the
0236      * frame was send out correctly. It only means the frame
0237      * was successfully pushed to the hardware, we have no
0238      * way to determine the transmission status right now.
0239      * (Only indirectly by looking at the failed TX counters
0240      * in the register).
0241      */
0242     if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
0243         rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
0244     else
0245         rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
0246 }
0247 
0248 static void rt2x00usb_work_txdone(struct work_struct *work)
0249 {
0250     struct rt2x00_dev *rt2x00dev =
0251         container_of(work, struct rt2x00_dev, txdone_work);
0252     struct data_queue *queue;
0253     struct queue_entry *entry;
0254 
0255     tx_queue_for_each(rt2x00dev, queue) {
0256         while (!rt2x00queue_empty(queue)) {
0257             entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
0258 
0259             if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
0260                 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
0261                 break;
0262 
0263             rt2x00usb_work_txdone_entry(entry);
0264         }
0265     }
0266 }
0267 
0268 static void rt2x00usb_interrupt_txdone(struct urb *urb)
0269 {
0270     struct queue_entry *entry = (struct queue_entry *)urb->context;
0271     struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
0272 
0273     if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
0274         return;
0275     /*
0276      * Check if the frame was correctly uploaded
0277      */
0278     if (urb->status)
0279         set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
0280     /*
0281      * Report the frame as DMA done
0282      */
0283     rt2x00lib_dmadone(entry);
0284 
0285     if (rt2x00dev->ops->lib->tx_dma_done)
0286         rt2x00dev->ops->lib->tx_dma_done(entry);
0287     /*
0288      * Schedule the delayed work for reading the TX status
0289      * from the device.
0290      */
0291     if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
0292         !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
0293         queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
0294 }
0295 
0296 static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
0297 {
0298     struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
0299     struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
0300     struct queue_entry_priv_usb *entry_priv = entry->priv_data;
0301     u32 length;
0302     int status;
0303 
0304     if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
0305         test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
0306         return false;
0307 
0308     /*
0309      * USB devices require certain padding at the end of each frame
0310      * and urb. Those paddings are not included in skbs. Pass entry
0311      * to the driver to determine what the overall length should be.
0312      */
0313     length = rt2x00dev->ops->lib->get_tx_data_len(entry);
0314 
0315     status = skb_padto(entry->skb, length);
0316     if (unlikely(status)) {
0317         /* TODO: report something more appropriate than IO_FAILED. */
0318         rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
0319         set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
0320         rt2x00lib_dmadone(entry);
0321 
0322         return false;
0323     }
0324 
0325     usb_fill_bulk_urb(entry_priv->urb, usb_dev,
0326               usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
0327               entry->skb->data, length,
0328               rt2x00usb_interrupt_txdone, entry);
0329 
0330     status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
0331     if (status) {
0332         if (rt2x00usb_check_usb_error(rt2x00dev, status))
0333             clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
0334         set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
0335         rt2x00lib_dmadone(entry);
0336     }
0337 
0338     return false;
0339 }
0340 
0341 /*
0342  * RX data handlers.
0343  */
0344 static void rt2x00usb_work_rxdone(struct work_struct *work)
0345 {
0346     struct rt2x00_dev *rt2x00dev =
0347         container_of(work, struct rt2x00_dev, rxdone_work);
0348     struct queue_entry *entry;
0349     struct skb_frame_desc *skbdesc;
0350     u8 rxd[32];
0351 
0352     while (!rt2x00queue_empty(rt2x00dev->rx)) {
0353         entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
0354 
0355         if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
0356             break;
0357 
0358         /*
0359          * Fill in desc fields of the skb descriptor
0360          */
0361         skbdesc = get_skb_frame_desc(entry->skb);
0362         skbdesc->desc = rxd;
0363         skbdesc->desc_len = entry->queue->desc_size;
0364 
0365         /*
0366          * Send the frame to rt2x00lib for further processing.
0367          */
0368         rt2x00lib_rxdone(entry, GFP_KERNEL);
0369     }
0370 }
0371 
0372 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
0373 {
0374     struct queue_entry *entry = (struct queue_entry *)urb->context;
0375     struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
0376 
0377     if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
0378         return;
0379 
0380     /*
0381      * Check if the received data is simply too small
0382      * to be actually valid, or if the urb is signaling
0383      * a problem.
0384      */
0385     if (urb->actual_length < entry->queue->desc_size || urb->status)
0386         set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
0387 
0388     /*
0389      * Report the frame as DMA done
0390      */
0391     rt2x00lib_dmadone(entry);
0392 
0393     /*
0394      * Schedule the delayed work for processing RX data
0395      */
0396     queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
0397 }
0398 
0399 static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
0400 {
0401     struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
0402     struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
0403     struct queue_entry_priv_usb *entry_priv = entry->priv_data;
0404     int status;
0405 
0406     if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
0407         return false;
0408 
0409     rt2x00lib_dmastart(entry);
0410 
0411     usb_fill_bulk_urb(entry_priv->urb, usb_dev,
0412               usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
0413               entry->skb->data, entry->skb->len,
0414               rt2x00usb_interrupt_rxdone, entry);
0415 
0416     status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
0417     if (status) {
0418         if (rt2x00usb_check_usb_error(rt2x00dev, status))
0419             clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
0420         set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
0421         rt2x00lib_dmadone(entry);
0422     }
0423 
0424     return false;
0425 }
0426 
0427 void rt2x00usb_kick_queue(struct data_queue *queue)
0428 {
0429     switch (queue->qid) {
0430     case QID_AC_VO:
0431     case QID_AC_VI:
0432     case QID_AC_BE:
0433     case QID_AC_BK:
0434         if (!rt2x00queue_empty(queue))
0435             rt2x00queue_for_each_entry(queue,
0436                            Q_INDEX_DONE,
0437                            Q_INDEX,
0438                            NULL,
0439                            rt2x00usb_kick_tx_entry);
0440         break;
0441     case QID_RX:
0442         if (!rt2x00queue_full(queue))
0443             rt2x00queue_for_each_entry(queue,
0444                            Q_INDEX,
0445                            Q_INDEX_DONE,
0446                            NULL,
0447                            rt2x00usb_kick_rx_entry);
0448         break;
0449     default:
0450         break;
0451     }
0452 }
0453 EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
0454 
0455 static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
0456 {
0457     struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
0458     struct queue_entry_priv_usb *entry_priv = entry->priv_data;
0459     struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
0460 
0461     if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
0462         return false;
0463 
0464     usb_kill_urb(entry_priv->urb);
0465 
0466     /*
0467      * Kill guardian urb (if required by driver).
0468      */
0469     if ((entry->queue->qid == QID_BEACON) &&
0470         (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
0471         usb_kill_urb(bcn_priv->guardian_urb);
0472 
0473     return false;
0474 }
0475 
0476 void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
0477 {
0478     struct work_struct *completion;
0479     unsigned int i;
0480 
0481     if (drop)
0482         rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
0483                        rt2x00usb_flush_entry);
0484 
0485     /*
0486      * Obtain the queue completion handler
0487      */
0488     switch (queue->qid) {
0489     case QID_AC_VO:
0490     case QID_AC_VI:
0491     case QID_AC_BE:
0492     case QID_AC_BK:
0493         completion = &queue->rt2x00dev->txdone_work;
0494         break;
0495     case QID_RX:
0496         completion = &queue->rt2x00dev->rxdone_work;
0497         break;
0498     default:
0499         return;
0500     }
0501 
0502     for (i = 0; i < 10; i++) {
0503         /*
0504          * Check if the driver is already done, otherwise we
0505          * have to sleep a little while to give the driver/hw
0506          * the oppurtunity to complete interrupt process itself.
0507          */
0508         if (rt2x00queue_empty(queue))
0509             break;
0510 
0511         /*
0512          * Schedule the completion handler manually, when this
0513          * worker function runs, it should cleanup the queue.
0514          */
0515         queue_work(queue->rt2x00dev->workqueue, completion);
0516 
0517         /*
0518          * Wait for a little while to give the driver
0519          * the oppurtunity to recover itself.
0520          */
0521         msleep(50);
0522     }
0523 }
0524 EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
0525 
0526 static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
0527 {
0528     rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced reset\n",
0529             queue->qid);
0530 
0531     rt2x00queue_stop_queue(queue);
0532     rt2x00queue_flush_queue(queue, true);
0533     rt2x00queue_start_queue(queue);
0534 }
0535 
0536 static int rt2x00usb_dma_timeout(struct data_queue *queue)
0537 {
0538     struct queue_entry *entry;
0539 
0540     entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
0541     return rt2x00queue_dma_timeout(entry);
0542 }
0543 
0544 void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
0545 {
0546     struct data_queue *queue;
0547 
0548     tx_queue_for_each(rt2x00dev, queue) {
0549         if (!rt2x00queue_empty(queue)) {
0550             if (rt2x00usb_dma_timeout(queue))
0551                 rt2x00usb_watchdog_tx_dma(queue);
0552         }
0553     }
0554 }
0555 EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
0556 
0557 /*
0558  * Radio handlers
0559  */
0560 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
0561 {
0562     rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
0563                     REGISTER_TIMEOUT);
0564 }
0565 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
0566 
0567 /*
0568  * Device initialization handlers.
0569  */
0570 void rt2x00usb_clear_entry(struct queue_entry *entry)
0571 {
0572     entry->flags = 0;
0573 
0574     if (entry->queue->qid == QID_RX)
0575         rt2x00usb_kick_rx_entry(entry, NULL);
0576 }
0577 EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
0578 
0579 static void rt2x00usb_assign_endpoint(struct data_queue *queue,
0580                       struct usb_endpoint_descriptor *ep_desc)
0581 {
0582     struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
0583     int pipe;
0584 
0585     queue->usb_endpoint = usb_endpoint_num(ep_desc);
0586 
0587     if (queue->qid == QID_RX) {
0588         pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
0589         queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe);
0590     } else {
0591         pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
0592         queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe);
0593     }
0594 
0595     if (!queue->usb_maxpacket)
0596         queue->usb_maxpacket = 1;
0597 }
0598 
0599 static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
0600 {
0601     struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
0602     struct usb_host_interface *intf_desc = intf->cur_altsetting;
0603     struct usb_endpoint_descriptor *ep_desc;
0604     struct data_queue *queue = rt2x00dev->tx;
0605     struct usb_endpoint_descriptor *tx_ep_desc = NULL;
0606     unsigned int i;
0607 
0608     /*
0609      * Walk through all available endpoints to search for "bulk in"
0610      * and "bulk out" endpoints. When we find such endpoints collect
0611      * the information we need from the descriptor and assign it
0612      * to the queue.
0613      */
0614     for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
0615         ep_desc = &intf_desc->endpoint[i].desc;
0616 
0617         if (usb_endpoint_is_bulk_in(ep_desc)) {
0618             rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
0619         } else if (usb_endpoint_is_bulk_out(ep_desc) &&
0620                (queue != queue_end(rt2x00dev))) {
0621             rt2x00usb_assign_endpoint(queue, ep_desc);
0622             queue = queue_next(queue);
0623 
0624             tx_ep_desc = ep_desc;
0625         }
0626     }
0627 
0628     /*
0629      * At least 1 endpoint for RX and 1 endpoint for TX must be available.
0630      */
0631     if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
0632         rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
0633         return -EPIPE;
0634     }
0635 
0636     /*
0637      * It might be possible not all queues have a dedicated endpoint.
0638      * Loop through all TX queues and copy the endpoint information
0639      * which we have gathered from already assigned endpoints.
0640      */
0641     txall_queue_for_each(rt2x00dev, queue) {
0642         if (!queue->usb_endpoint)
0643             rt2x00usb_assign_endpoint(queue, tx_ep_desc);
0644     }
0645 
0646     return 0;
0647 }
0648 
0649 static int rt2x00usb_alloc_entries(struct data_queue *queue)
0650 {
0651     struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
0652     struct queue_entry_priv_usb *entry_priv;
0653     struct queue_entry_priv_usb_bcn *bcn_priv;
0654     unsigned int i;
0655 
0656     for (i = 0; i < queue->limit; i++) {
0657         entry_priv = queue->entries[i].priv_data;
0658         entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
0659         if (!entry_priv->urb)
0660             return -ENOMEM;
0661     }
0662 
0663     /*
0664      * If this is not the beacon queue or
0665      * no guardian byte was required for the beacon,
0666      * then we are done.
0667      */
0668     if (queue->qid != QID_BEACON ||
0669         !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
0670         return 0;
0671 
0672     for (i = 0; i < queue->limit; i++) {
0673         bcn_priv = queue->entries[i].priv_data;
0674         bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
0675         if (!bcn_priv->guardian_urb)
0676             return -ENOMEM;
0677     }
0678 
0679     return 0;
0680 }
0681 
0682 static void rt2x00usb_free_entries(struct data_queue *queue)
0683 {
0684     struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
0685     struct queue_entry_priv_usb *entry_priv;
0686     struct queue_entry_priv_usb_bcn *bcn_priv;
0687     unsigned int i;
0688 
0689     if (!queue->entries)
0690         return;
0691 
0692     for (i = 0; i < queue->limit; i++) {
0693         entry_priv = queue->entries[i].priv_data;
0694         usb_kill_urb(entry_priv->urb);
0695         usb_free_urb(entry_priv->urb);
0696     }
0697 
0698     /*
0699      * If this is not the beacon queue or
0700      * no guardian byte was required for the beacon,
0701      * then we are done.
0702      */
0703     if (queue->qid != QID_BEACON ||
0704         !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
0705         return;
0706 
0707     for (i = 0; i < queue->limit; i++) {
0708         bcn_priv = queue->entries[i].priv_data;
0709         usb_kill_urb(bcn_priv->guardian_urb);
0710         usb_free_urb(bcn_priv->guardian_urb);
0711     }
0712 }
0713 
0714 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
0715 {
0716     struct data_queue *queue;
0717     int status;
0718 
0719     /*
0720      * Find endpoints for each queue
0721      */
0722     status = rt2x00usb_find_endpoints(rt2x00dev);
0723     if (status)
0724         goto exit;
0725 
0726     /*
0727      * Allocate DMA
0728      */
0729     queue_for_each(rt2x00dev, queue) {
0730         status = rt2x00usb_alloc_entries(queue);
0731         if (status)
0732             goto exit;
0733     }
0734 
0735     return 0;
0736 
0737 exit:
0738     rt2x00usb_uninitialize(rt2x00dev);
0739 
0740     return status;
0741 }
0742 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
0743 
0744 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
0745 {
0746     struct data_queue *queue;
0747 
0748     usb_kill_anchored_urbs(rt2x00dev->anchor);
0749     hrtimer_cancel(&rt2x00dev->txstatus_timer);
0750     cancel_work_sync(&rt2x00dev->rxdone_work);
0751     cancel_work_sync(&rt2x00dev->txdone_work);
0752 
0753     queue_for_each(rt2x00dev, queue)
0754         rt2x00usb_free_entries(queue);
0755 }
0756 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
0757 
0758 /*
0759  * USB driver handlers.
0760  */
0761 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
0762 {
0763     kfree(rt2x00dev->rf);
0764     rt2x00dev->rf = NULL;
0765 
0766     kfree(rt2x00dev->eeprom);
0767     rt2x00dev->eeprom = NULL;
0768 
0769     kfree(rt2x00dev->csr.cache);
0770     rt2x00dev->csr.cache = NULL;
0771 }
0772 
0773 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
0774 {
0775     rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
0776     if (!rt2x00dev->csr.cache)
0777         goto exit;
0778 
0779     rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
0780     if (!rt2x00dev->eeprom)
0781         goto exit;
0782 
0783     rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
0784     if (!rt2x00dev->rf)
0785         goto exit;
0786 
0787     return 0;
0788 
0789 exit:
0790     rt2x00_probe_err("Failed to allocate registers\n");
0791 
0792     rt2x00usb_free_reg(rt2x00dev);
0793 
0794     return -ENOMEM;
0795 }
0796 
0797 int rt2x00usb_probe(struct usb_interface *usb_intf,
0798             const struct rt2x00_ops *ops)
0799 {
0800     struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
0801     struct ieee80211_hw *hw;
0802     struct rt2x00_dev *rt2x00dev;
0803     int retval;
0804 
0805     usb_dev = usb_get_dev(usb_dev);
0806     usb_reset_device(usb_dev);
0807 
0808     hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
0809     if (!hw) {
0810         rt2x00_probe_err("Failed to allocate hardware\n");
0811         retval = -ENOMEM;
0812         goto exit_put_device;
0813     }
0814 
0815     usb_set_intfdata(usb_intf, hw);
0816 
0817     rt2x00dev = hw->priv;
0818     rt2x00dev->dev = &usb_intf->dev;
0819     rt2x00dev->ops = ops;
0820     rt2x00dev->hw = hw;
0821 
0822     rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
0823 
0824     INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
0825     INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
0826     hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
0827              HRTIMER_MODE_REL);
0828 
0829     retval = rt2x00usb_alloc_reg(rt2x00dev);
0830     if (retval)
0831         goto exit_free_device;
0832 
0833     rt2x00dev->anchor = devm_kmalloc(&usb_dev->dev,
0834                     sizeof(struct usb_anchor),
0835                     GFP_KERNEL);
0836     if (!rt2x00dev->anchor) {
0837         retval = -ENOMEM;
0838         goto exit_free_reg;
0839     }
0840     init_usb_anchor(rt2x00dev->anchor);
0841 
0842     retval = rt2x00lib_probe_dev(rt2x00dev);
0843     if (retval)
0844         goto exit_free_anchor;
0845 
0846     return 0;
0847 
0848 exit_free_anchor:
0849     usb_kill_anchored_urbs(rt2x00dev->anchor);
0850 
0851 exit_free_reg:
0852     rt2x00usb_free_reg(rt2x00dev);
0853 
0854 exit_free_device:
0855     ieee80211_free_hw(hw);
0856 
0857 exit_put_device:
0858     usb_put_dev(usb_dev);
0859 
0860     usb_set_intfdata(usb_intf, NULL);
0861 
0862     return retval;
0863 }
0864 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
0865 
0866 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
0867 {
0868     struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
0869     struct rt2x00_dev *rt2x00dev = hw->priv;
0870 
0871     /*
0872      * Free all allocated data.
0873      */
0874     rt2x00lib_remove_dev(rt2x00dev);
0875     rt2x00usb_free_reg(rt2x00dev);
0876     ieee80211_free_hw(hw);
0877 
0878     /*
0879      * Free the USB device data.
0880      */
0881     usb_set_intfdata(usb_intf, NULL);
0882     usb_put_dev(interface_to_usbdev(usb_intf));
0883 }
0884 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
0885 
0886 #ifdef CONFIG_PM
0887 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
0888 {
0889     struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
0890     struct rt2x00_dev *rt2x00dev = hw->priv;
0891 
0892     return rt2x00lib_suspend(rt2x00dev);
0893 }
0894 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
0895 
0896 int rt2x00usb_resume(struct usb_interface *usb_intf)
0897 {
0898     struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
0899     struct rt2x00_dev *rt2x00dev = hw->priv;
0900 
0901     return rt2x00lib_resume(rt2x00dev);
0902 }
0903 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
0904 #endif /* CONFIG_PM */
0905 
0906 /*
0907  * rt2x00usb module information.
0908  */
0909 MODULE_AUTHOR(DRV_PROJECT);
0910 MODULE_VERSION(DRV_VERSION);
0911 MODULE_DESCRIPTION("rt2x00 usb library");
0912 MODULE_LICENSE("GPL");