2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Abstract: rt2x00 generic usb device routines.
24 Supported chipsets: rt2570, rt2571W & rt2671.
28 * Set enviroment defines for rt2x00.h
30 #define DRV_NAME "rt2x00usb"
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
39 #include "rt2x00lib.h"
40 #include "rt2x00usb.h"
43 * Interfacing with the HW.
45 int rt2x00usb_vendor_request(const struct rt2x00_dev
*rt2x00dev
,
46 const u8 request
, const u8 type
, const u16 offset
,
47 u32 value
, void *buffer
, const u16 buffer_length
, const u16 timeout
)
49 struct usb_device
*usb_dev
= interface_to_usbdev(
50 rt2x00dev_usb(rt2x00dev
));
54 for (i
= 0; i
< REGISTER_BUSY_COUNT
; i
++) {
55 status
= usb_control_msg(
57 (type
== USB_VENDOR_REQUEST_IN
) ?
58 usb_rcvctrlpipe(usb_dev
, 0) :
59 usb_sndctrlpipe(usb_dev
, 0),
60 request
, type
, value
, offset
, buffer
, buffer_length
,
66 ERROR(rt2x00dev
, "Vendor Request 0x%02x failed for offset 0x%04x"
67 " with error %d.\n", request
, offset
, status
);
71 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request
);
76 int rt2x00usb_beacon_update(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
77 struct ieee80211_tx_control
*control
)
79 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
80 struct usb_device
*usb_dev
=
81 interface_to_usbdev(rt2x00dev_usb(rt2x00dev
));
82 struct data_ring
*ring
=
83 rt2x00_get_ring(rt2x00dev
, IEEE80211_TX_QUEUE_BEACON
);
84 struct data_entry
*beacon
;
85 struct data_entry
*guardian
;
89 * Just in case the ieee80211 doesn't set this,
90 * but we need this queue set for the descriptor
93 control
->queue
= IEEE80211_TX_QUEUE_BEACON
;
96 * Obtain 2 entries, one for the guardian byte,
97 * the second for the actual beacon.
99 guardian
= rt2x00_get_data_entry(ring
);
100 rt2x00_ring_index_inc(ring
);
101 beacon
= rt2x00_get_data_entry(ring
);
104 * First we create the beacon.
106 skb_push(skb
, ring
->desc_size
);
107 rt2x00lib_write_tx_desc(rt2x00dev
, beacon
,
108 (struct data_desc
*)skb
->data
,
109 (struct ieee80211_hdr
*)(skb
->data
+ ring
->desc_size
),
110 skb
->len
- ring
->desc_size
,
114 * Length passed to usb_fill_urb cannot be an odd number,
115 * so add 1 byte to make it even.
124 usb_sndbulkpipe(usb_dev
, 1),
127 rt2x00usb_beacondone
,
133 * Second we need to create the guardian byte.
134 * We only need a single byte, so lets recycle
135 * the 'flags' field we are not using for beacons.
141 usb_sndbulkpipe(usb_dev
, 1),
144 rt2x00usb_beacondone
,
148 * Send out the guardian byte.
150 usb_submit_urb(guardian
->priv
, GFP_ATOMIC
);
153 * Enable beacon generation.
155 rt2x00dev
->ops
->lib
->kick_tx_queue(rt2x00dev
, control
->queue
);
159 EXPORT_SYMBOL_GPL(rt2x00usb_beacon_update
);
161 void rt2x00usb_beacondone(struct urb
*urb
)
163 struct data_entry
*entry
= (struct data_entry
*)urb
->context
;
164 struct data_ring
*ring
= entry
->ring
;
166 if (!test_bit(DEVICE_ENABLED_RADIO
, &ring
->rt2x00dev
->flags
))
170 * Check if this was the guardian beacon,
171 * if that was the case we need to send the real beacon now.
172 * Otherwise we should free the sk_buffer, the device
173 * should be doing the rest of the work now.
175 if (ring
->index
== 1) {
176 rt2x00_ring_index_done_inc(ring
);
177 entry
= rt2x00_get_data_entry(ring
);
178 usb_submit_urb(entry
->priv
, GFP_ATOMIC
);
179 rt2x00_ring_index_inc(ring
);
180 } else if (ring
->index_done
== 1) {
181 entry
= rt2x00_get_data_entry_done(ring
);
183 dev_kfree_skb(entry
->skb
);
186 rt2x00_ring_index_done_inc(ring
);
189 EXPORT_SYMBOL_GPL(rt2x00usb_beacondone
);
194 static void rt2x00usb_interrupt_txdone(struct urb
*urb
)
196 struct data_entry
*entry
= (struct data_entry
*)urb
->context
;
197 struct data_ring
*ring
= entry
->ring
;
198 struct rt2x00_dev
*rt2x00dev
= ring
->rt2x00dev
;
199 struct data_desc
*txd
= (struct data_desc
*)entry
->skb
->data
;
203 if (!test_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
) ||
204 !__test_and_clear_bit(ENTRY_OWNER_NIC
, &entry
->flags
))
207 rt2x00_desc_read(txd
, 0, &word
);
210 * Remove the descriptor data from the buffer.
212 skb_pull(entry
->skb
, ring
->desc_size
);
215 * Obtain the status about this packet.
217 tx_status
= !urb
->status
? TX_SUCCESS
: TX_FAIL_RETRY
;
219 rt2x00lib_txdone(entry
, tx_status
, 0);
222 * Make this entry available for reuse.
225 rt2x00_ring_index_done_inc(entry
->ring
);
228 * If the data ring was full before the txdone handler
229 * we must make sure the packet queue in the mac80211 stack
230 * is reenabled when the txdone handler has finished.
232 if (!rt2x00_ring_full(ring
))
233 ieee80211_wake_queue(rt2x00dev
->hw
,
234 entry
->tx_status
.control
.queue
);
237 int rt2x00usb_write_tx_data(struct rt2x00_dev
*rt2x00dev
,
238 struct data_ring
*ring
, struct sk_buff
*skb
,
239 struct ieee80211_tx_control
*control
)
241 struct usb_device
*usb_dev
=
242 interface_to_usbdev(rt2x00dev_usb(rt2x00dev
));
243 struct ieee80211_hdr
*ieee80211hdr
= (struct ieee80211_hdr
*)skb
->data
;
244 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
245 struct data_desc
*txd
;
246 u32 length
= skb
->len
;
248 if (rt2x00_ring_full(ring
)) {
249 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
253 if (test_bit(ENTRY_OWNER_NIC
, &entry
->flags
)) {
255 "Arrived at non-free entry in the non-full queue %d.\n"
256 "Please file bug report to %s.\n",
257 control
->queue
, DRV_PROJECT
);
258 ieee80211_stop_queue( rt2x00dev
->hw
, control
->queue
);
262 skb_push(skb
, rt2x00dev
->hw
->extra_tx_headroom
);
263 txd
= (struct data_desc
*)skb
->data
;
264 rt2x00lib_write_tx_desc(rt2x00dev
, entry
, txd
, ieee80211hdr
,
266 memcpy(&entry
->tx_status
.control
, control
, sizeof(*control
));
270 * Length passed to usb_fill_urb cannot be an odd number,
271 * so add 1 byte to make it even.
273 length
+= rt2x00dev
->hw
->extra_tx_headroom
;
277 __set_bit(ENTRY_OWNER_NIC
, &entry
->flags
);
281 usb_sndbulkpipe(usb_dev
, 1),
284 rt2x00usb_interrupt_txdone
,
286 usb_submit_urb(entry
->priv
, GFP_ATOMIC
);
288 rt2x00_ring_index_inc(ring
);
290 if (rt2x00_ring_full(ring
))
291 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
295 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data
);
300 static void rt2x00usb_interrupt_rxdone(struct urb
*urb
)
302 struct data_entry
*entry
= (struct data_entry
*)urb
->context
;
303 struct data_ring
*ring
= entry
->ring
;
304 struct rt2x00_dev
*rt2x00dev
= ring
->rt2x00dev
;
310 if (!test_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
) ||
311 !test_and_clear_bit(ENTRY_OWNER_NIC
, &entry
->flags
))
315 * Check if the received data is simply too small
316 * to be actually valid, or if the urb is signaling
319 if (urb
->actual_length
< entry
->ring
->desc_size
|| urb
->status
)
322 size
= rt2x00dev
->ops
->lib
->fill_rxdone(entry
, &signal
, &rssi
, &ofdm
);
327 * Trim the skb_buffer to only contain the valid
328 * frame data (so ignore the device's descriptor).
330 skb_trim(entry
->skb
, size
);
333 * Send the packet to upper layer, and update urb.
335 rt2x00lib_rxdone(entry
, NULL
, ring
->data_size
+ ring
->desc_size
,
337 urb
->transfer_buffer
= entry
->skb
->data
;
338 urb
->transfer_buffer_length
= entry
->skb
->len
;
341 if (test_bit(DEVICE_ENABLED_RADIO
, &ring
->rt2x00dev
->flags
)) {
342 __set_bit(ENTRY_OWNER_NIC
, &entry
->flags
);
343 usb_submit_urb(urb
, GFP_ATOMIC
);
346 rt2x00_ring_index_inc(ring
);
352 void rt2x00usb_enable_radio(struct rt2x00_dev
*rt2x00dev
)
354 struct usb_device
*usb_dev
=
355 interface_to_usbdev(rt2x00dev_usb(rt2x00dev
));
356 struct data_ring
*ring
;
357 struct data_entry
*entry
;
361 * Initialize the TX rings
363 txringall_for_each(rt2x00dev
, ring
) {
364 for (i
= 0; i
< ring
->stats
.limit
; i
++)
365 ring
->entry
[i
].flags
= 0;
367 rt2x00_ring_index_clear(ring
);
371 * Initialize and start the RX ring.
373 rt2x00_ring_index_clear(rt2x00dev
->rx
);
375 for (i
= 0; i
< rt2x00dev
->rx
->stats
.limit
; i
++) {
376 entry
= &rt2x00dev
->rx
->entry
[i
];
381 usb_rcvbulkpipe(usb_dev
, 1),
384 rt2x00usb_interrupt_rxdone
,
387 __set_bit(ENTRY_OWNER_NIC
, &entry
->flags
);
388 usb_submit_urb(entry
->priv
, GFP_ATOMIC
);
391 EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio
);
393 void rt2x00usb_disable_radio(struct rt2x00_dev
*rt2x00dev
)
395 struct data_ring
*ring
;
398 rt2x00usb_vendor_request(rt2x00dev
, USB_RX_CONTROL
,
399 USB_VENDOR_REQUEST_OUT
, 0x00, 0x00, NULL
, 0, REGISTER_TIMEOUT
);
404 ring_for_each(rt2x00dev
, ring
) {
405 for (i
= 0; i
< ring
->stats
.limit
; i
++)
406 usb_kill_urb(ring
->entry
[i
].priv
);
409 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio
);
412 * Device initialization handlers.
414 static int rt2x00usb_alloc_ring(struct rt2x00_dev
*rt2x00dev
,
415 struct data_ring
*ring
)
422 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
423 ring
->entry
[i
].priv
= usb_alloc_urb(0, GFP_KERNEL
);
424 if (!ring
->entry
[i
].priv
)
431 int rt2x00usb_initialize(struct rt2x00_dev
*rt2x00dev
)
433 struct data_ring
*ring
;
435 unsigned int entry_size
;
442 ring_for_each(rt2x00dev
, ring
) {
443 status
= rt2x00usb_alloc_ring(rt2x00dev
, ring
);
449 * For the RX ring, skb's should be allocated.
451 entry_size
= ring
->data_size
+ ring
->desc_size
;
452 for (i
= 0; i
< rt2x00dev
->rx
->stats
.limit
; i
++) {
453 skb
= dev_alloc_skb(NET_IP_ALIGN
+ entry_size
);
457 skb_reserve(skb
, NET_IP_ALIGN
);
458 skb_put(skb
, entry_size
);
460 rt2x00dev
->rx
->entry
[i
].skb
= skb
;
466 rt2x00usb_uninitialize(rt2x00dev
);
470 EXPORT_SYMBOL_GPL(rt2x00usb_initialize
);
472 void rt2x00usb_uninitialize(struct rt2x00_dev
*rt2x00dev
)
474 struct data_ring
*ring
;
477 ring_for_each(rt2x00dev
, ring
) {
481 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
482 usb_kill_urb(ring
->entry
[i
].priv
);
483 usb_free_urb(ring
->entry
[i
].priv
);
484 if (ring
->entry
[i
].skb
)
485 kfree_skb(ring
->entry
[i
].skb
);
489 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize
);
492 * USB driver handlers.
494 int rt2x00usb_probe(struct usb_interface
*usb_intf
,
495 const struct usb_device_id
*id
)
497 struct usb_device
*usb_dev
= interface_to_usbdev(usb_intf
);
498 struct rt2x00_ops
*ops
= (struct rt2x00_ops
*)id
->driver_info
;
499 struct ieee80211_hw
*hw
;
500 struct rt2x00_dev
*rt2x00dev
;
503 usb_dev
= usb_get_dev(usb_dev
);
505 hw
= ieee80211_alloc_hw(sizeof(struct rt2x00_dev
), ops
->hw
);
507 ERROR_PROBE("Failed to allocate hardware.\n");
509 goto exit_put_device
;
512 usb_set_intfdata(usb_intf
, hw
);
514 rt2x00dev
= hw
->priv
;
515 rt2x00dev
->dev
= usb_intf
;
516 rt2x00dev
->ops
= ops
;
519 retval
= rt2x00lib_probe_dev(rt2x00dev
);
521 goto exit_free_device
;
526 ieee80211_free_hw(hw
);
529 usb_put_dev(usb_dev
);
531 usb_set_intfdata(usb_intf
, NULL
);
535 EXPORT_SYMBOL_GPL(rt2x00usb_probe
);
537 void rt2x00usb_disconnect(struct usb_interface
*usb_intf
)
539 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
540 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
543 * Free all allocated data.
545 rt2x00lib_remove_dev(rt2x00dev
);
546 ieee80211_free_hw(hw
);
549 * Free the USB device data.
551 usb_set_intfdata(usb_intf
, NULL
);
552 usb_put_dev(interface_to_usbdev(usb_intf
));
554 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect
);
557 int rt2x00usb_suspend(struct usb_interface
*usb_intf
, pm_message_t state
)
559 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
560 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
563 retval
= rt2x00lib_suspend(rt2x00dev
, state
);
568 * Decrease usbdev refcount.
570 usb_put_dev(interface_to_usbdev(usb_intf
));
574 EXPORT_SYMBOL_GPL(rt2x00usb_suspend
);
576 int rt2x00usb_resume(struct usb_interface
*usb_intf
)
578 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
579 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
581 usb_get_dev(interface_to_usbdev(usb_intf
));
583 return rt2x00lib_resume(rt2x00dev
);
585 EXPORT_SYMBOL_GPL(rt2x00usb_resume
);
586 #endif /* CONFIG_PM */
589 * rt2x00pci module information.
591 MODULE_AUTHOR(DRV_PROJECT
);
592 MODULE_VERSION(DRV_VERSION
);
593 MODULE_DESCRIPTION("rt2x00 library");
594 MODULE_LICENSE("GPL");