1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
21 #include "rt2x00usb.h"
23 static bool rt2x00usb_check_usb_error(struct rt2x00_dev
*rt2x00dev
, int status
)
25 if (status
== -ENODEV
|| status
== -ENOENT
)
28 if (status
== -EPROTO
|| status
== -ETIMEDOUT
)
29 rt2x00dev
->num_proto_errs
++;
31 rt2x00dev
->num_proto_errs
= 0;
33 if (rt2x00dev
->num_proto_errs
> 3)
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
,
48 struct usb_device
*usb_dev
= to_usb_device_intf(rt2x00dev
->dev
);
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
))
59 status
= usb_control_msg(usb_dev
, pipe
, request
, requesttype
,
60 value
, offset
, buffer
, buffer_length
,
65 if (rt2x00usb_check_usb_error(rt2x00dev
, status
)) {
66 /* Device has disappeared. */
67 clear_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
70 } while (time_before(jiffies
, expire
));
73 "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
74 request
, offset
, 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
)
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");
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
);
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
)
120 mutex_lock(&rt2x00dev
->csr_mutex
);
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
);
136 mutex_unlock(&rt2x00dev
->csr_mutex
);
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
,
149 if (!test_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
))
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
))
156 udelay(REGISTER_BUSY_DELAY
);
159 rt2x00_err(rt2x00dev
, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
165 EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read
);
168 struct rt2x00_async_read_data
{
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
);
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
);
194 struct rt2x00_async_read_data
*rd
;
196 rd
= kmalloc(sizeof(*rd
), GFP_ATOMIC
);
200 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
206 rd
->rt2x00dev
= rt2x00dev
;
207 rd
->callback
= callback
;
208 rd
->cr
.bRequestType
= USB_VENDOR_REQUEST_IN
;
209 rd
->cr
.bRequest
= USB_MULTI_READ
;
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
);
224 EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async
);
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
239 if (test_bit(ENTRY_DATA_IO_FAILED
, &entry
->flags
))
240 rt2x00lib_txdone_noinfo(entry
, TXDONE_FAILURE
);
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
))
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
))
273 * Check if the frame was correctly uploaded
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
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
;
301 if (!test_and_clear_bit(ENTRY_DATA_PENDING
, &entry
->flags
) ||
302 test_bit(ENTRY_DATA_STATUS_PENDING
, &entry
->flags
))
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
);
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
);
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
);
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
;
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
))
356 * Fill in desc fields of the skb descriptor
358 skbdesc
= get_skb_frame_desc(entry
->skb
);
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
))
378 * Check if the received data is simply too small
379 * to be actually valid, or if the urb is signaling
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
;
403 if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA
, &entry
->flags
))
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
);
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
);
424 void rt2x00usb_kick_queue(struct data_queue
*queue
)
426 switch (queue
->qid
) {
431 if (!rt2x00queue_empty(queue
))
432 rt2x00queue_for_each_entry(queue
,
436 rt2x00usb_kick_tx_entry
);
439 if (!rt2x00queue_full(queue
))
440 rt2x00queue_for_each_entry(queue
,
444 rt2x00usb_kick_rx_entry
);
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
))
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
);
473 void rt2x00usb_flush_queue(struct data_queue
*queue
, bool drop
)
475 struct work_struct
*completion
;
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
) {
490 completion
= &queue
->rt2x00dev
->txdone_work
;
493 completion
= &queue
->rt2x00dev
->rxdone_work
;
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
))
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.
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",
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
);
557 void rt2x00usb_disable_radio(struct rt2x00_dev
*rt2x00dev
)
559 rt2x00usb_vendor_request_sw(rt2x00dev
, USB_RX_CONTROL
, 0, 0,
562 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio
);
565 * Device initialization handlers.
567 void rt2x00usb_clear_entry(struct queue_entry
*entry
)
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
);
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);
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
;
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
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");
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
);
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
;
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
)
661 * If this is not the beacon queue or
662 * no guardian byte was required for the beacon,
665 if (queue
->qid
!= QID_BEACON
||
666 !rt2x00_has_cap_flag(rt2x00dev
, REQUIRE_BEACON_GUARD
))
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
)
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
;
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,
700 if (queue
->qid
!= QID_BEACON
||
701 !rt2x00_has_cap_flag(rt2x00dev
, REQUIRE_BEACON_GUARD
))
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
;
717 * Find endpoints for each queue
719 status
= rt2x00usb_find_endpoints(rt2x00dev
);
726 queue_for_each(rt2x00dev
, queue
) {
727 status
= rt2x00usb_alloc_entries(queue
);
735 rt2x00usb_uninitialize(rt2x00dev
);
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
)
776 rt2x00dev
->eeprom
= kzalloc(rt2x00dev
->ops
->eeprom_size
, GFP_KERNEL
);
777 if (!rt2x00dev
->eeprom
)
780 rt2x00dev
->rf
= kzalloc(rt2x00dev
->ops
->rf_size
, GFP_KERNEL
);
787 rt2x00_probe_err("Failed to allocate registers\n");
789 rt2x00usb_free_reg(rt2x00dev
);
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
;
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
);
807 rt2x00_probe_err("Failed to allocate hardware\n");
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
;
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
,
826 retval
= rt2x00usb_alloc_reg(rt2x00dev
);
828 goto exit_free_device
;
830 rt2x00dev
->anchor
= devm_kmalloc(&usb_dev
->dev
,
831 sizeof(struct usb_anchor
),
833 if (!rt2x00dev
->anchor
) {
837 init_usb_anchor(rt2x00dev
->anchor
);
839 retval
= rt2x00lib_probe_dev(rt2x00dev
);
841 goto exit_free_anchor
;
846 usb_kill_anchored_urbs(rt2x00dev
->anchor
);
849 rt2x00usb_free_reg(rt2x00dev
);
852 ieee80211_free_hw(hw
);
855 usb_put_dev(usb_dev
);
857 usb_set_intfdata(usb_intf
, NULL
);
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
);
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");