Commit | Line | Data |
---|---|---|
95ea3627 | 1 | /* |
7e613e16 ID |
2 | Copyright (C) 2010 Willow Garage <http://www.willowgarage.com> |
3 | Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com> | |
95ea3627 ID |
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 | |
a05b8c58 | 17 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
95ea3627 ID |
18 | */ |
19 | ||
20 | /* | |
21 | Module: rt2x00usb | |
22 | Abstract: rt2x00 generic usb device routines. | |
23 | */ | |
24 | ||
95ea3627 ID |
25 | #include <linux/kernel.h> |
26 | #include <linux/module.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
95ea3627 | 28 | #include <linux/usb.h> |
3d82346c | 29 | #include <linux/bug.h> |
95ea3627 ID |
30 | |
31 | #include "rt2x00.h" | |
32 | #include "rt2x00usb.h" | |
33 | ||
34 | /* | |
35 | * Interfacing with the HW. | |
36 | */ | |
0e14f6d3 | 37 | int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev, |
95ea3627 ID |
38 | const u8 request, const u8 requesttype, |
39 | const u16 offset, const u16 value, | |
40 | void *buffer, const u16 buffer_length, | |
e9136550 | 41 | const int timeout) |
95ea3627 | 42 | { |
c1d35dfa | 43 | struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev); |
95ea3627 | 44 | int status; |
95ea3627 ID |
45 | unsigned int pipe = |
46 | (requesttype == USB_VENDOR_REQUEST_IN) ? | |
47 | usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0); | |
ad92bc9e | 48 | unsigned long expire = jiffies + msecs_to_jiffies(timeout); |
95ea3627 | 49 | |
66f84d65 SC |
50 | if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
51 | return -ENODEV; | |
3d82346c | 52 | |
ad92bc9e | 53 | do { |
95ea3627 ID |
54 | status = usb_control_msg(usb_dev, pipe, request, requesttype, |
55 | value, offset, buffer, buffer_length, | |
ad92bc9e | 56 | timeout / 2); |
95ea3627 ID |
57 | if (status >= 0) |
58 | return 0; | |
59 | ||
ad92bc9e SG |
60 | if (status == -ENODEV) { |
61 | /* Device has disappeared. */ | |
66f84d65 | 62 | clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags); |
95ea3627 | 63 | break; |
66f84d65 | 64 | } |
ad92bc9e | 65 | } while (time_before(jiffies, expire)); |
95ea3627 | 66 | |
ec9c4989 JP |
67 | rt2x00_err(rt2x00dev, |
68 | "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n", | |
69 | request, offset, status); | |
95ea3627 ID |
70 | |
71 | return status; | |
72 | } | |
73 | EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request); | |
74 | ||
3d82346c AB |
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) | |
95ea3627 ID |
79 | { |
80 | int status; | |
81 | ||
8ff48a8b | 82 | BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex)); |
3d82346c | 83 | |
95ea3627 ID |
84 | /* |
85 | * Check for Cache availability. | |
86 | */ | |
21795094 | 87 | if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) { |
ec9c4989 | 88 | rt2x00_err(rt2x00dev, "CSR cache not available\n"); |
95ea3627 ID |
89 | return -ENOMEM; |
90 | } | |
91 | ||
92 | if (requesttype == USB_VENDOR_REQUEST_OUT) | |
21795094 | 93 | memcpy(rt2x00dev->csr.cache, buffer, buffer_length); |
95ea3627 ID |
94 | |
95 | status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype, | |
21795094 | 96 | offset, 0, rt2x00dev->csr.cache, |
95ea3627 ID |
97 | buffer_length, timeout); |
98 | ||
99 | if (!status && requesttype == USB_VENDOR_REQUEST_IN) | |
21795094 | 100 | memcpy(buffer, rt2x00dev->csr.cache, buffer_length); |
95ea3627 ID |
101 | |
102 | return status; | |
103 | } | |
3d82346c AB |
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, | |
e9dc51aa | 109 | const u16 buffer_length) |
ed0dbeeb IM |
110 | { |
111 | int status = 0; | |
112 | unsigned char *tb; | |
113 | u16 off, len, bsize; | |
114 | ||
8ff48a8b | 115 | mutex_lock(&rt2x00dev->csr_mutex); |
ed0dbeeb | 116 | |
82f97b8d | 117 | tb = (char *)buffer; |
ed0dbeeb IM |
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, | |
e9dc51aa | 124 | bsize, REGISTER_TIMEOUT); |
ed0dbeeb IM |
125 | |
126 | tb += bsize; | |
127 | len -= bsize; | |
128 | off += bsize; | |
129 | } | |
130 | ||
8ff48a8b | 131 | mutex_unlock(&rt2x00dev->csr_mutex); |
ed0dbeeb IM |
132 | |
133 | return status; | |
134 | } | |
96b61baf | 135 | EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff); |
ed0dbeeb | 136 | |
0f829b1d ID |
137 | int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev, |
138 | const unsigned int offset, | |
f255b92b | 139 | const struct rt2x00_field32 field, |
0f829b1d ID |
140 | u32 *reg) |
141 | { | |
142 | unsigned int i; | |
143 | ||
66f84d65 SC |
144 | if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
145 | return -ENODEV; | |
146 | ||
7a5a7352 | 147 | for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) { |
0f829b1d ID |
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 | ||
ec9c4989 JP |
154 | rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n", |
155 | offset, *reg); | |
0f829b1d ID |
156 | *reg = ~0; |
157 | ||
158 | return 0; | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read); | |
161 | ||
0e0d39e5 JS |
162 | |
163 | struct rt2x00_async_read_data { | |
164 | __le32 reg; | |
165 | struct usb_ctrlrequest cr; | |
166 | struct rt2x00_dev *rt2x00dev; | |
a073fdef | 167 | bool (*callback)(struct rt2x00_dev *, int, u32); |
0e0d39e5 JS |
168 | }; |
169 | ||
170 | static void rt2x00usb_register_read_async_cb(struct urb *urb) | |
171 | { | |
172 | struct rt2x00_async_read_data *rd = urb->context; | |
a073fdef ID |
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); | |
0e0d39e5 JS |
178 | } |
179 | ||
180 | void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev, | |
181 | const unsigned int offset, | |
a073fdef | 182 | bool (*callback)(struct rt2x00_dev*, int, u32)) |
0e0d39e5 JS |
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 | ||
95ea3627 ID |
215 | /* |
216 | * TX data handlers. | |
217 | */ | |
7e613e16 | 218 | static void rt2x00usb_work_txdone_entry(struct queue_entry *entry) |
95ea3627 | 219 | { |
95ea3627 | 220 | /* |
7e613e16 | 221 | * If the transfer to hardware succeeded, it does not mean the |
fb55f4d1 | 222 | * frame was send out correctly. It only means the frame |
25985edc | 223 | * was successfully pushed to the hardware, we have no |
fb55f4d1 ID |
224 | * way to determine the transmission status right now. |
225 | * (Only indirectly by looking at the failed TX counters | |
226 | * in the register). | |
95ea3627 | 227 | */ |
7e613e16 | 228 | if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags)) |
3392bece | 229 | rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE); |
7e613e16 | 230 | else |
3392bece | 231 | rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN); |
95ea3627 ID |
232 | } |
233 | ||
7e613e16 ID |
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 | ||
dba5dc1a ID |
245 | if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) || |
246 | !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags)) | |
7e613e16 ID |
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 | ||
df71c9cf | 259 | if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) |
7e613e16 | 260 | return; |
7e613e16 ID |
261 | /* |
262 | * Check if the frame was correctly uploaded | |
263 | */ | |
264 | if (urb->status) | |
a1d1eabc | 265 | set_bit(ENTRY_DATA_IO_FAILED, &entry->flags); |
df71c9cf SG |
266 | /* |
267 | * Report the frame as DMA done | |
268 | */ | |
269 | rt2x00lib_dmadone(entry); | |
7e613e16 | 270 | |
df71c9cf SG |
271 | if (rt2x00dev->ops->lib->tx_dma_done) |
272 | rt2x00dev->ops->lib->tx_dma_done(entry); | |
7e613e16 ID |
273 | /* |
274 | * Schedule the delayed work for reading the TX status | |
275 | * from the device. | |
276 | */ | |
b9d305cc | 277 | if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) || |
f0187a19 JS |
278 | !kfifo_is_empty(&rt2x00dev->txstatus_fifo)) |
279 | queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work); | |
7e613e16 ID |
280 | } |
281 | ||
1dd0dbb3 | 282 | static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data) |
f019d514 | 283 | { |
fe725697 GW |
284 | struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev; |
285 | struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev); | |
f019d514 | 286 | struct queue_entry_priv_usb *entry_priv = entry->priv_data; |
fe725697 | 287 | u32 length; |
d7bb5f84 | 288 | int status; |
fe725697 | 289 | |
dba5dc1a ID |
290 | if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) || |
291 | test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags)) | |
4268d8ed | 292 | return false; |
fe725697 | 293 | |
ee1e755f | 294 | /* |
d823a50e JK |
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. | |
ee1e755f ID |
298 | */ |
299 | length = rt2x00dev->ops->lib->get_tx_data_len(entry); | |
f019d514 | 300 | |
d823a50e JK |
301 | status = skb_padto(entry->skb, length); |
302 | if (unlikely(status)) { | |
303 | /* TODO: report something more appropriate than IO_FAILED. */ | |
ec9c4989 | 304 | rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n"); |
d823a50e JK |
305 | set_bit(ENTRY_DATA_IO_FAILED, &entry->flags); |
306 | rt2x00lib_dmadone(entry); | |
307 | ||
308 | return false; | |
309 | } | |
310 | ||
ee1e755f ID |
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 | ||
d7bb5f84 JS |
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); | |
a13c8f31 ID |
320 | set_bit(ENTRY_DATA_IO_FAILED, &entry->flags); |
321 | rt2x00lib_dmadone(entry); | |
322 | } | |
10e11568 HS |
323 | |
324 | return false; | |
f019d514 ID |
325 | } |
326 | ||
0b7fde54 ID |
327 | /* |
328 | * RX data handlers. | |
329 | */ | |
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 | ||
dba5dc1a ID |
341 | if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) || |
342 | !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags)) | |
0b7fde54 ID |
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 | */ | |
88211021 | 355 | rt2x00lib_rxdone(entry, GFP_KERNEL); |
0b7fde54 ID |
356 | } |
357 | } | |
358 | ||
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; | |
363 | ||
364 | if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) | |
365 | return; | |
366 | ||
367 | /* | |
368 | * Report the frame as DMA done | |
369 | */ | |
370 | rt2x00lib_dmadone(entry); | |
371 | ||
372 | /* | |
373 | * Check if the received data is simply too small | |
374 | * to be actually valid, or if the urb is signaling | |
375 | * a problem. | |
376 | */ | |
377 | if (urb->actual_length < entry->queue->desc_size || urb->status) | |
378 | set_bit(ENTRY_DATA_IO_FAILED, &entry->flags); | |
379 | ||
380 | /* | |
381 | * Schedule the delayed work for reading the RX status | |
382 | * from the device. | |
383 | */ | |
0439f536 | 384 | queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work); |
0b7fde54 ID |
385 | } |
386 | ||
1dd0dbb3 | 387 | static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data) |
0b7fde54 ID |
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 | ||
dba5dc1a ID |
394 | if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) || |
395 | test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags)) | |
4268d8ed | 396 | return false; |
0b7fde54 | 397 | |
64e7d723 ID |
398 | rt2x00lib_dmastart(entry); |
399 | ||
0b7fde54 ID |
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 | } | |
10e11568 HS |
412 | |
413 | return false; | |
0b7fde54 ID |
414 | } |
415 | ||
dbba306f | 416 | void rt2x00usb_kick_queue(struct data_queue *queue) |
f019d514 | 417 | { |
dbba306f | 418 | switch (queue->qid) { |
f615e9a3 ID |
419 | case QID_AC_VO: |
420 | case QID_AC_VI: | |
dbba306f ID |
421 | case QID_AC_BE: |
422 | case QID_AC_BK: | |
dbba306f | 423 | if (!rt2x00queue_empty(queue)) |
1dd0dbb3 HS |
424 | rt2x00queue_for_each_entry(queue, |
425 | Q_INDEX_DONE, | |
426 | Q_INDEX, | |
427 | NULL, | |
dbba306f ID |
428 | rt2x00usb_kick_tx_entry); |
429 | break; | |
0b7fde54 ID |
430 | case QID_RX: |
431 | if (!rt2x00queue_full(queue)) | |
1dd0dbb3 HS |
432 | rt2x00queue_for_each_entry(queue, |
433 | Q_INDEX, | |
434 | Q_INDEX_DONE, | |
435 | NULL, | |
0b7fde54 ID |
436 | rt2x00usb_kick_rx_entry); |
437 | break; | |
dbba306f ID |
438 | default: |
439 | break; | |
440 | } | |
f019d514 | 441 | } |
dbba306f | 442 | EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue); |
f019d514 | 443 | |
1dd0dbb3 | 444 | static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data) |
a2c9b652 | 445 | { |
5eb7efe8 ID |
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; | |
a2c9b652 | 449 | |
5eb7efe8 | 450 | if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) |
4268d8ed | 451 | return false; |
5eb7efe8 ID |
452 | |
453 | usb_kill_urb(entry_priv->urb); | |
a2c9b652 ID |
454 | |
455 | /* | |
5eb7efe8 | 456 | * Kill guardian urb (if required by driver). |
a2c9b652 | 457 | */ |
5eb7efe8 | 458 | if ((entry->queue->qid == QID_BEACON) && |
b9d305cc | 459 | (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))) |
5eb7efe8 | 460 | usb_kill_urb(bcn_priv->guardian_urb); |
10e11568 HS |
461 | |
462 | return false; | |
5eb7efe8 | 463 | } |
a2c9b652 | 464 | |
152a5992 | 465 | void rt2x00usb_flush_queue(struct data_queue *queue, bool drop) |
5eb7efe8 | 466 | { |
5be65609 ID |
467 | struct work_struct *completion; |
468 | unsigned int i; | |
469 | ||
152a5992 | 470 | if (drop) |
1dd0dbb3 | 471 | rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL, |
152a5992 | 472 | rt2x00usb_flush_entry); |
5be65609 ID |
473 | |
474 | /* | |
475 | * Obtain the queue completion handler | |
476 | */ | |
477 | switch (queue->qid) { | |
f615e9a3 ID |
478 | case QID_AC_VO: |
479 | case QID_AC_VI: | |
5be65609 ID |
480 | case QID_AC_BE: |
481 | case QID_AC_BK: | |
5be65609 ID |
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 | ||
152a5992 | 491 | for (i = 0; i < 10; i++) { |
5be65609 ID |
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 | */ | |
0439f536 | 504 | queue_work(queue->rt2x00dev->workqueue, completion); |
5be65609 ID |
505 | |
506 | /* | |
507 | * Wait for a little while to give the driver | |
508 | * the oppurtunity to recover itself. | |
509 | */ | |
510 | msleep(10); | |
511 | } | |
a2c9b652 | 512 | } |
5be65609 | 513 | EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue); |
a2c9b652 | 514 | |
652a9dd2 | 515 | static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue) |
c965c74b | 516 | { |
ec9c4989 JP |
517 | rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n", |
518 | queue->qid); | |
c965c74b | 519 | |
fdbdd25c | 520 | rt2x00queue_stop_queue(queue); |
5be65609 | 521 | rt2x00queue_flush_queue(queue, true); |
fdbdd25c | 522 | rt2x00queue_start_queue(queue); |
c965c74b ID |
523 | } |
524 | ||
75256f03 JS |
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 | ||
c965c74b ID |
533 | void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev) |
534 | { | |
535 | struct data_queue *queue; | |
536 | ||
537 | tx_queue_for_each(rt2x00dev, queue) { | |
1a397696 | 538 | if (!rt2x00queue_empty(queue)) { |
75256f03 | 539 | if (rt2x00usb_dma_timeout(queue)) |
1a397696 | 540 | rt2x00usb_watchdog_tx_dma(queue); |
1a397696 | 541 | } |
c965c74b ID |
542 | } |
543 | } | |
544 | EXPORT_SYMBOL_GPL(rt2x00usb_watchdog); | |
545 | ||
95ea3627 ID |
546 | /* |
547 | * Radio handlers | |
548 | */ | |
95ea3627 ID |
549 | void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev) |
550 | { | |
bd394a74 | 551 | rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0, |
95ea3627 | 552 | REGISTER_TIMEOUT); |
95ea3627 ID |
553 | } |
554 | EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio); | |
555 | ||
556 | /* | |
557 | * Device initialization handlers. | |
558 | */ | |
798b7adb | 559 | void rt2x00usb_clear_entry(struct queue_entry *entry) |
837e7f24 | 560 | { |
7e613e16 ID |
561 | entry->flags = 0; |
562 | ||
0b7fde54 | 563 | if (entry->queue->qid == QID_RX) |
1dd0dbb3 | 564 | rt2x00usb_kick_rx_entry(entry, NULL); |
837e7f24 | 565 | } |
798b7adb | 566 | EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry); |
837e7f24 | 567 | |
f1ca2167 ID |
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); | |
d15cfc3a ID |
608 | } else if (usb_endpoint_is_bulk_out(ep_desc) && |
609 | (queue != queue_end(rt2x00dev))) { | |
f1ca2167 | 610 | rt2x00usb_assign_endpoint(queue, ep_desc); |
d15cfc3a | 611 | queue = queue_next(queue); |
f1ca2167 | 612 | |
f1ca2167 ID |
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) { | |
ec9c4989 | 621 | rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n"); |
f1ca2167 ID |
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 | ||
fa69560f | 638 | static int rt2x00usb_alloc_entries(struct data_queue *queue) |
95ea3627 | 639 | { |
fa69560f | 640 | struct rt2x00_dev *rt2x00dev = queue->rt2x00dev; |
b8be63ff ID |
641 | struct queue_entry_priv_usb *entry_priv; |
642 | struct queue_entry_priv_usb_bcn *bcn_priv; | |
95ea3627 ID |
643 | unsigned int i; |
644 | ||
b8be63ff ID |
645 | for (i = 0; i < queue->limit; i++) { |
646 | entry_priv = queue->entries[i].priv_data; | |
647 | entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL); | |
648 | if (!entry_priv->urb) | |
649 | return -ENOMEM; | |
650 | } | |
651 | ||
95ea3627 | 652 | /* |
b8be63ff ID |
653 | * If this is not the beacon queue or |
654 | * no guardian byte was required for the beacon, | |
655 | * then we are done. | |
95ea3627 | 656 | */ |
fa69560f | 657 | if (queue->qid != QID_BEACON || |
b9d305cc | 658 | !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)) |
b8be63ff ID |
659 | return 0; |
660 | ||
181d6902 | 661 | for (i = 0; i < queue->limit; i++) { |
b8be63ff ID |
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) | |
95ea3627 ID |
665 | return -ENOMEM; |
666 | } | |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
fa69560f | 671 | static void rt2x00usb_free_entries(struct data_queue *queue) |
95ea3627 | 672 | { |
fa69560f | 673 | struct rt2x00_dev *rt2x00dev = queue->rt2x00dev; |
b8be63ff ID |
674 | struct queue_entry_priv_usb *entry_priv; |
675 | struct queue_entry_priv_usb_bcn *bcn_priv; | |
95ea3627 ID |
676 | unsigned int i; |
677 | ||
181d6902 | 678 | if (!queue->entries) |
95ea3627 ID |
679 | return; |
680 | ||
181d6902 | 681 | for (i = 0; i < queue->limit; i++) { |
b8be63ff ID |
682 | entry_priv = queue->entries[i].priv_data; |
683 | usb_kill_urb(entry_priv->urb); | |
684 | usb_free_urb(entry_priv->urb); | |
95ea3627 | 685 | } |
b8be63ff ID |
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 | */ | |
fa69560f | 692 | if (queue->qid != QID_BEACON || |
b9d305cc | 693 | !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)) |
b8be63ff ID |
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 | } | |
95ea3627 ID |
701 | } |
702 | ||
703 | int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev) | |
704 | { | |
181d6902 | 705 | struct data_queue *queue; |
30caa6e3 | 706 | int status; |
95ea3627 | 707 | |
f1ca2167 ID |
708 | /* |
709 | * Find endpoints for each queue | |
710 | */ | |
711 | status = rt2x00usb_find_endpoints(rt2x00dev); | |
712 | if (status) | |
713 | goto exit; | |
714 | ||
95ea3627 ID |
715 | /* |
716 | * Allocate DMA | |
717 | */ | |
181d6902 | 718 | queue_for_each(rt2x00dev, queue) { |
fa69560f | 719 | status = rt2x00usb_alloc_entries(queue); |
95ea3627 ID |
720 | if (status) |
721 | goto exit; | |
722 | } | |
723 | ||
95ea3627 ID |
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 | { | |
181d6902 | 735 | struct data_queue *queue; |
95ea3627 | 736 | |
181d6902 | 737 | queue_for_each(rt2x00dev, queue) |
fa69560f | 738 | rt2x00usb_free_entries(queue); |
95ea3627 ID |
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 | ||
21795094 ID |
753 | kfree(rt2x00dev->csr.cache); |
754 | rt2x00dev->csr.cache = NULL; | |
95ea3627 ID |
755 | } |
756 | ||
757 | static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev) | |
758 | { | |
21795094 ID |
759 | rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL); |
760 | if (!rt2x00dev->csr.cache) | |
95ea3627 ID |
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: | |
ec9c4989 | 774 | rt2x00_probe_err("Failed to allocate registers\n"); |
95ea3627 ID |
775 | |
776 | rt2x00usb_free_reg(rt2x00dev); | |
777 | ||
778 | return -ENOMEM; | |
779 | } | |
780 | ||
781 | int rt2x00usb_probe(struct usb_interface *usb_intf, | |
e01ae27f | 782 | const struct rt2x00_ops *ops) |
95ea3627 ID |
783 | { |
784 | struct usb_device *usb_dev = interface_to_usbdev(usb_intf); | |
95ea3627 ID |
785 | struct ieee80211_hw *hw; |
786 | struct rt2x00_dev *rt2x00dev; | |
787 | int retval; | |
788 | ||
789 | usb_dev = usb_get_dev(usb_dev); | |
bf4c02d5 | 790 | usb_reset_device(usb_dev); |
95ea3627 ID |
791 | |
792 | hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw); | |
793 | if (!hw) { | |
ec9c4989 | 794 | rt2x00_probe_err("Failed to allocate hardware\n"); |
95ea3627 ID |
795 | retval = -ENOMEM; |
796 | goto exit_put_device; | |
797 | } | |
798 | ||
799 | usb_set_intfdata(usb_intf, hw); | |
800 | ||
801 | rt2x00dev = hw->priv; | |
14a3bf89 | 802 | rt2x00dev->dev = &usb_intf->dev; |
95ea3627 ID |
803 | rt2x00dev->ops = ops; |
804 | rt2x00dev->hw = hw; | |
805 | ||
2015d192 GW |
806 | rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB); |
807 | ||
7e613e16 ID |
808 | INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone); |
809 | INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone); | |
f421111b SG |
810 | hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC, |
811 | HRTIMER_MODE_REL); | |
7e613e16 | 812 | |
95ea3627 ID |
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; | |
95ea3627 | 863 | |
543cc38c | 864 | return rt2x00lib_suspend(rt2x00dev, state); |
95ea3627 ID |
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; | |
95ea3627 | 872 | |
499a214c | 873 | return rt2x00lib_resume(rt2x00dev); |
95ea3627 ID |
874 | } |
875 | EXPORT_SYMBOL_GPL(rt2x00usb_resume); | |
876 | #endif /* CONFIG_PM */ | |
877 | ||
878 | /* | |
181d6902 | 879 | * rt2x00usb module information. |
95ea3627 ID |
880 | */ |
881 | MODULE_AUTHOR(DRV_PROJECT); | |
882 | MODULE_VERSION(DRV_VERSION); | |
181d6902 | 883 | MODULE_DESCRIPTION("rt2x00 usb library"); |
95ea3627 | 884 | MODULE_LICENSE("GPL"); |