treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / ralink / rt2x00 / rt2x00usb.c
blob92e9e023c3499c8adaf7b7bf7223316896c3f9dc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
4 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
5 <http://rt2x00.serialmonkey.com>
7 */
9 /*
10 Module: rt2x00usb
11 Abstract: rt2x00 generic usb device routines.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18 #include <linux/bug.h>
20 #include "rt2x00.h"
21 #include "rt2x00usb.h"
23 static bool rt2x00usb_check_usb_error(struct rt2x00_dev *rt2x00dev, int status)
25 if (status == -ENODEV || status == -ENOENT)
26 return true;
28 if (status == -EPROTO || status == -ETIMEDOUT)
29 rt2x00dev->num_proto_errs++;
30 else
31 rt2x00dev->num_proto_errs = 0;
33 if (rt2x00dev->num_proto_errs > 3)
34 return true;
36 return false;
40 * Interfacing with the HW.
42 int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
43 const u8 request, const u8 requesttype,
44 const u16 offset, const u16 value,
45 void *buffer, const u16 buffer_length,
46 const int timeout)
48 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
49 int status;
50 unsigned int pipe =
51 (requesttype == USB_VENDOR_REQUEST_IN) ?
52 usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
53 unsigned long expire = jiffies + msecs_to_jiffies(timeout);
55 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
56 return -ENODEV;
58 do {
59 status = usb_control_msg(usb_dev, pipe, request, requesttype,
60 value, offset, buffer, buffer_length,
61 timeout / 2);
62 if (status >= 0)
63 return 0;
65 if (rt2x00usb_check_usb_error(rt2x00dev, status)) {
66 /* Device has disappeared. */
67 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
68 break;
70 } while (time_before(jiffies, expire));
72 rt2x00_err(rt2x00dev,
73 "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
74 request, offset, status);
76 return status;
78 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
80 int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
81 const u8 request, const u8 requesttype,
82 const u16 offset, void *buffer,
83 const u16 buffer_length, const int timeout)
85 int status;
87 BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
90 * Check for Cache availability.
92 if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
93 rt2x00_err(rt2x00dev, "CSR cache not available\n");
94 return -ENOMEM;
97 if (requesttype == USB_VENDOR_REQUEST_OUT)
98 memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
100 status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
101 offset, 0, rt2x00dev->csr.cache,
102 buffer_length, timeout);
104 if (!status && requesttype == USB_VENDOR_REQUEST_IN)
105 memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
107 return status;
109 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
111 int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
112 const u8 request, const u8 requesttype,
113 const u16 offset, void *buffer,
114 const u16 buffer_length)
116 int status = 0;
117 unsigned char *tb;
118 u16 off, len, bsize;
120 mutex_lock(&rt2x00dev->csr_mutex);
122 tb = (char *)buffer;
123 off = offset;
124 len = buffer_length;
125 while (len && !status) {
126 bsize = min_t(u16, CSR_CACHE_SIZE, len);
127 status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
128 requesttype, off, tb,
129 bsize, REGISTER_TIMEOUT);
131 tb += bsize;
132 len -= bsize;
133 off += bsize;
136 mutex_unlock(&rt2x00dev->csr_mutex);
138 return status;
140 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
142 int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
143 const unsigned int offset,
144 const struct rt2x00_field32 field,
145 u32 *reg)
147 unsigned int i;
149 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
150 return -ENODEV;
152 for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
153 *reg = rt2x00usb_register_read_lock(rt2x00dev, offset);
154 if (!rt2x00_get_field32(*reg, field))
155 return 1;
156 udelay(REGISTER_BUSY_DELAY);
159 rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
160 offset, *reg);
161 *reg = ~0;
163 return 0;
165 EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
168 struct rt2x00_async_read_data {
169 __le32 reg;
170 struct usb_ctrlrequest cr;
171 struct rt2x00_dev *rt2x00dev;
172 bool (*callback)(struct rt2x00_dev *, int, u32);
175 static void rt2x00usb_register_read_async_cb(struct urb *urb)
177 struct rt2x00_async_read_data *rd = urb->context;
178 if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
179 usb_anchor_urb(urb, rd->rt2x00dev->anchor);
180 if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
181 usb_unanchor_urb(urb);
182 kfree(rd);
184 } else
185 kfree(rd);
188 void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
189 const unsigned int offset,
190 bool (*callback)(struct rt2x00_dev*, int, u32))
192 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
193 struct urb *urb;
194 struct rt2x00_async_read_data *rd;
196 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
197 if (!rd)
198 return;
200 urb = usb_alloc_urb(0, GFP_ATOMIC);
201 if (!urb) {
202 kfree(rd);
203 return;
206 rd->rt2x00dev = rt2x00dev;
207 rd->callback = callback;
208 rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
209 rd->cr.bRequest = USB_MULTI_READ;
210 rd->cr.wValue = 0;
211 rd->cr.wIndex = cpu_to_le16(offset);
212 rd->cr.wLength = cpu_to_le16(sizeof(u32));
214 usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
215 (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
216 rt2x00usb_register_read_async_cb, rd);
217 usb_anchor_urb(urb, rt2x00dev->anchor);
218 if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
219 usb_unanchor_urb(urb);
220 kfree(rd);
222 usb_free_urb(urb);
224 EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
227 * TX data handlers.
229 static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
232 * If the transfer to hardware succeeded, it does not mean the
233 * frame was send out correctly. It only means the frame
234 * was successfully pushed to the hardware, we have no
235 * way to determine the transmission status right now.
236 * (Only indirectly by looking at the failed TX counters
237 * in the register).
239 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
240 rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
241 else
242 rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
245 static void rt2x00usb_work_txdone(struct work_struct *work)
247 struct rt2x00_dev *rt2x00dev =
248 container_of(work, struct rt2x00_dev, txdone_work);
249 struct data_queue *queue;
250 struct queue_entry *entry;
252 tx_queue_for_each(rt2x00dev, queue) {
253 while (!rt2x00queue_empty(queue)) {
254 entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
256 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
257 !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
258 break;
260 rt2x00usb_work_txdone_entry(entry);
265 static void rt2x00usb_interrupt_txdone(struct urb *urb)
267 struct queue_entry *entry = (struct queue_entry *)urb->context;
268 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
270 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
271 return;
273 * Check if the frame was correctly uploaded
275 if (urb->status)
276 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
278 * Report the frame as DMA done
280 rt2x00lib_dmadone(entry);
282 if (rt2x00dev->ops->lib->tx_dma_done)
283 rt2x00dev->ops->lib->tx_dma_done(entry);
285 * Schedule the delayed work for reading the TX status
286 * from the device.
288 if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
289 !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
290 queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
293 static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
295 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
296 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
297 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
298 u32 length;
299 int status;
301 if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
302 test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
303 return false;
306 * USB devices require certain padding at the end of each frame
307 * and urb. Those paddings are not included in skbs. Pass entry
308 * to the driver to determine what the overall length should be.
310 length = rt2x00dev->ops->lib->get_tx_data_len(entry);
312 status = skb_padto(entry->skb, length);
313 if (unlikely(status)) {
314 /* TODO: report something more appropriate than IO_FAILED. */
315 rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
316 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
317 rt2x00lib_dmadone(entry);
319 return false;
322 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
323 usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
324 entry->skb->data, length,
325 rt2x00usb_interrupt_txdone, entry);
327 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
328 if (status) {
329 if (rt2x00usb_check_usb_error(rt2x00dev, status))
330 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
331 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
332 rt2x00lib_dmadone(entry);
335 return false;
339 * RX data handlers.
341 static void rt2x00usb_work_rxdone(struct work_struct *work)
343 struct rt2x00_dev *rt2x00dev =
344 container_of(work, struct rt2x00_dev, rxdone_work);
345 struct queue_entry *entry;
346 struct skb_frame_desc *skbdesc;
347 u8 rxd[32];
349 while (!rt2x00queue_empty(rt2x00dev->rx)) {
350 entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
352 if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
353 break;
356 * Fill in desc fields of the skb descriptor
358 skbdesc = get_skb_frame_desc(entry->skb);
359 skbdesc->desc = rxd;
360 skbdesc->desc_len = entry->queue->desc_size;
363 * Send the frame to rt2x00lib for further processing.
365 rt2x00lib_rxdone(entry, GFP_KERNEL);
369 static void rt2x00usb_interrupt_rxdone(struct urb *urb)
371 struct queue_entry *entry = (struct queue_entry *)urb->context;
372 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
374 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
375 return;
378 * Check if the received data is simply too small
379 * to be actually valid, or if the urb is signaling
380 * a problem.
382 if (urb->actual_length < entry->queue->desc_size || urb->status)
383 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
386 * Report the frame as DMA done
388 rt2x00lib_dmadone(entry);
391 * Schedule the delayed work for processing RX data
393 queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
396 static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
398 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
399 struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
400 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
401 int status;
403 if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
404 return false;
406 rt2x00lib_dmastart(entry);
408 usb_fill_bulk_urb(entry_priv->urb, usb_dev,
409 usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
410 entry->skb->data, entry->skb->len,
411 rt2x00usb_interrupt_rxdone, entry);
413 status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
414 if (status) {
415 if (rt2x00usb_check_usb_error(rt2x00dev, status))
416 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
417 set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
418 rt2x00lib_dmadone(entry);
421 return false;
424 void rt2x00usb_kick_queue(struct data_queue *queue)
426 switch (queue->qid) {
427 case QID_AC_VO:
428 case QID_AC_VI:
429 case QID_AC_BE:
430 case QID_AC_BK:
431 if (!rt2x00queue_empty(queue))
432 rt2x00queue_for_each_entry(queue,
433 Q_INDEX_DONE,
434 Q_INDEX,
435 NULL,
436 rt2x00usb_kick_tx_entry);
437 break;
438 case QID_RX:
439 if (!rt2x00queue_full(queue))
440 rt2x00queue_for_each_entry(queue,
441 Q_INDEX,
442 Q_INDEX_DONE,
443 NULL,
444 rt2x00usb_kick_rx_entry);
445 break;
446 default:
447 break;
450 EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
452 static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
454 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
455 struct queue_entry_priv_usb *entry_priv = entry->priv_data;
456 struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
458 if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
459 return false;
461 usb_kill_urb(entry_priv->urb);
464 * Kill guardian urb (if required by driver).
466 if ((entry->queue->qid == QID_BEACON) &&
467 (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
468 usb_kill_urb(bcn_priv->guardian_urb);
470 return false;
473 void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
475 struct work_struct *completion;
476 unsigned int i;
478 if (drop)
479 rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
480 rt2x00usb_flush_entry);
483 * Obtain the queue completion handler
485 switch (queue->qid) {
486 case QID_AC_VO:
487 case QID_AC_VI:
488 case QID_AC_BE:
489 case QID_AC_BK:
490 completion = &queue->rt2x00dev->txdone_work;
491 break;
492 case QID_RX:
493 completion = &queue->rt2x00dev->rxdone_work;
494 break;
495 default:
496 return;
499 for (i = 0; i < 10; i++) {
501 * Check if the driver is already done, otherwise we
502 * have to sleep a little while to give the driver/hw
503 * the oppurtunity to complete interrupt process itself.
505 if (rt2x00queue_empty(queue))
506 break;
509 * Schedule the completion handler manually, when this
510 * worker function runs, it should cleanup the queue.
512 queue_work(queue->rt2x00dev->workqueue, completion);
515 * Wait for a little while to give the driver
516 * the oppurtunity to recover itself.
518 msleep(50);
521 EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
523 static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
525 rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced reset\n",
526 queue->qid);
528 rt2x00queue_stop_queue(queue);
529 rt2x00queue_flush_queue(queue, true);
530 rt2x00queue_start_queue(queue);
533 static int rt2x00usb_dma_timeout(struct data_queue *queue)
535 struct queue_entry *entry;
537 entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
538 return rt2x00queue_dma_timeout(entry);
541 void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
543 struct data_queue *queue;
545 tx_queue_for_each(rt2x00dev, queue) {
546 if (!rt2x00queue_empty(queue)) {
547 if (rt2x00usb_dma_timeout(queue))
548 rt2x00usb_watchdog_tx_dma(queue);
552 EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
555 * Radio handlers
557 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
559 rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
560 REGISTER_TIMEOUT);
562 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
565 * Device initialization handlers.
567 void rt2x00usb_clear_entry(struct queue_entry *entry)
569 entry->flags = 0;
571 if (entry->queue->qid == QID_RX)
572 rt2x00usb_kick_rx_entry(entry, NULL);
574 EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
576 static void rt2x00usb_assign_endpoint(struct data_queue *queue,
577 struct usb_endpoint_descriptor *ep_desc)
579 struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
580 int pipe;
582 queue->usb_endpoint = usb_endpoint_num(ep_desc);
584 if (queue->qid == QID_RX) {
585 pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
586 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
587 } else {
588 pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
589 queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
592 if (!queue->usb_maxpacket)
593 queue->usb_maxpacket = 1;
596 static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
598 struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
599 struct usb_host_interface *intf_desc = intf->cur_altsetting;
600 struct usb_endpoint_descriptor *ep_desc;
601 struct data_queue *queue = rt2x00dev->tx;
602 struct usb_endpoint_descriptor *tx_ep_desc = NULL;
603 unsigned int i;
606 * Walk through all available endpoints to search for "bulk in"
607 * and "bulk out" endpoints. When we find such endpoints collect
608 * the information we need from the descriptor and assign it
609 * to the queue.
611 for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
612 ep_desc = &intf_desc->endpoint[i].desc;
614 if (usb_endpoint_is_bulk_in(ep_desc)) {
615 rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
616 } else if (usb_endpoint_is_bulk_out(ep_desc) &&
617 (queue != queue_end(rt2x00dev))) {
618 rt2x00usb_assign_endpoint(queue, ep_desc);
619 queue = queue_next(queue);
621 tx_ep_desc = ep_desc;
626 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
628 if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
629 rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
630 return -EPIPE;
634 * It might be possible not all queues have a dedicated endpoint.
635 * Loop through all TX queues and copy the endpoint information
636 * which we have gathered from already assigned endpoints.
638 txall_queue_for_each(rt2x00dev, queue) {
639 if (!queue->usb_endpoint)
640 rt2x00usb_assign_endpoint(queue, tx_ep_desc);
643 return 0;
646 static int rt2x00usb_alloc_entries(struct data_queue *queue)
648 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
649 struct queue_entry_priv_usb *entry_priv;
650 struct queue_entry_priv_usb_bcn *bcn_priv;
651 unsigned int i;
653 for (i = 0; i < queue->limit; i++) {
654 entry_priv = queue->entries[i].priv_data;
655 entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
656 if (!entry_priv->urb)
657 return -ENOMEM;
661 * If this is not the beacon queue or
662 * no guardian byte was required for the beacon,
663 * then we are done.
665 if (queue->qid != QID_BEACON ||
666 !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
667 return 0;
669 for (i = 0; i < queue->limit; i++) {
670 bcn_priv = queue->entries[i].priv_data;
671 bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
672 if (!bcn_priv->guardian_urb)
673 return -ENOMEM;
676 return 0;
679 static void rt2x00usb_free_entries(struct data_queue *queue)
681 struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
682 struct queue_entry_priv_usb *entry_priv;
683 struct queue_entry_priv_usb_bcn *bcn_priv;
684 unsigned int i;
686 if (!queue->entries)
687 return;
689 for (i = 0; i < queue->limit; i++) {
690 entry_priv = queue->entries[i].priv_data;
691 usb_kill_urb(entry_priv->urb);
692 usb_free_urb(entry_priv->urb);
696 * If this is not the beacon queue or
697 * no guardian byte was required for the beacon,
698 * then we are done.
700 if (queue->qid != QID_BEACON ||
701 !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
702 return;
704 for (i = 0; i < queue->limit; i++) {
705 bcn_priv = queue->entries[i].priv_data;
706 usb_kill_urb(bcn_priv->guardian_urb);
707 usb_free_urb(bcn_priv->guardian_urb);
711 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
713 struct data_queue *queue;
714 int status;
717 * Find endpoints for each queue
719 status = rt2x00usb_find_endpoints(rt2x00dev);
720 if (status)
721 goto exit;
724 * Allocate DMA
726 queue_for_each(rt2x00dev, queue) {
727 status = rt2x00usb_alloc_entries(queue);
728 if (status)
729 goto exit;
732 return 0;
734 exit:
735 rt2x00usb_uninitialize(rt2x00dev);
737 return status;
739 EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
741 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
743 struct data_queue *queue;
745 usb_kill_anchored_urbs(rt2x00dev->anchor);
746 hrtimer_cancel(&rt2x00dev->txstatus_timer);
747 cancel_work_sync(&rt2x00dev->rxdone_work);
748 cancel_work_sync(&rt2x00dev->txdone_work);
750 queue_for_each(rt2x00dev, queue)
751 rt2x00usb_free_entries(queue);
753 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
756 * USB driver handlers.
758 static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
760 kfree(rt2x00dev->rf);
761 rt2x00dev->rf = NULL;
763 kfree(rt2x00dev->eeprom);
764 rt2x00dev->eeprom = NULL;
766 kfree(rt2x00dev->csr.cache);
767 rt2x00dev->csr.cache = NULL;
770 static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
772 rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
773 if (!rt2x00dev->csr.cache)
774 goto exit;
776 rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
777 if (!rt2x00dev->eeprom)
778 goto exit;
780 rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
781 if (!rt2x00dev->rf)
782 goto exit;
784 return 0;
786 exit:
787 rt2x00_probe_err("Failed to allocate registers\n");
789 rt2x00usb_free_reg(rt2x00dev);
791 return -ENOMEM;
794 int rt2x00usb_probe(struct usb_interface *usb_intf,
795 const struct rt2x00_ops *ops)
797 struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
798 struct ieee80211_hw *hw;
799 struct rt2x00_dev *rt2x00dev;
800 int retval;
802 usb_dev = usb_get_dev(usb_dev);
803 usb_reset_device(usb_dev);
805 hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
806 if (!hw) {
807 rt2x00_probe_err("Failed to allocate hardware\n");
808 retval = -ENOMEM;
809 goto exit_put_device;
812 usb_set_intfdata(usb_intf, hw);
814 rt2x00dev = hw->priv;
815 rt2x00dev->dev = &usb_intf->dev;
816 rt2x00dev->ops = ops;
817 rt2x00dev->hw = hw;
819 rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
821 INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
822 INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
823 hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
824 HRTIMER_MODE_REL);
826 retval = rt2x00usb_alloc_reg(rt2x00dev);
827 if (retval)
828 goto exit_free_device;
830 rt2x00dev->anchor = devm_kmalloc(&usb_dev->dev,
831 sizeof(struct usb_anchor),
832 GFP_KERNEL);
833 if (!rt2x00dev->anchor) {
834 retval = -ENOMEM;
835 goto exit_free_reg;
837 init_usb_anchor(rt2x00dev->anchor);
839 retval = rt2x00lib_probe_dev(rt2x00dev);
840 if (retval)
841 goto exit_free_anchor;
843 return 0;
845 exit_free_anchor:
846 usb_kill_anchored_urbs(rt2x00dev->anchor);
848 exit_free_reg:
849 rt2x00usb_free_reg(rt2x00dev);
851 exit_free_device:
852 ieee80211_free_hw(hw);
854 exit_put_device:
855 usb_put_dev(usb_dev);
857 usb_set_intfdata(usb_intf, NULL);
859 return retval;
861 EXPORT_SYMBOL_GPL(rt2x00usb_probe);
863 void rt2x00usb_disconnect(struct usb_interface *usb_intf)
865 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
866 struct rt2x00_dev *rt2x00dev = hw->priv;
869 * Free all allocated data.
871 rt2x00lib_remove_dev(rt2x00dev);
872 rt2x00usb_free_reg(rt2x00dev);
873 ieee80211_free_hw(hw);
876 * Free the USB device data.
878 usb_set_intfdata(usb_intf, NULL);
879 usb_put_dev(interface_to_usbdev(usb_intf));
881 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
883 #ifdef CONFIG_PM
884 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
886 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
887 struct rt2x00_dev *rt2x00dev = hw->priv;
889 return rt2x00lib_suspend(rt2x00dev, state);
891 EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
893 int rt2x00usb_resume(struct usb_interface *usb_intf)
895 struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
896 struct rt2x00_dev *rt2x00dev = hw->priv;
898 return rt2x00lib_resume(rt2x00dev);
900 EXPORT_SYMBOL_GPL(rt2x00usb_resume);
901 #endif /* CONFIG_PM */
904 * rt2x00usb module information.
906 MODULE_AUTHOR(DRV_PROJECT);
907 MODULE_VERSION(DRV_VERSION);
908 MODULE_DESCRIPTION("rt2x00 usb library");
909 MODULE_LICENSE("GPL");