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