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 "rt2x00usb.h"
42 * Interfacing with the HW.
44 int rt2x00usb_vendor_request(const struct rt2x00_dev
*rt2x00dev
,
45 const u8 request
, const u8 type
, const u16 offset
,
46 u32 value
, void *buffer
, const u16 buffer_length
, const u16 timeout
)
48 struct usb_device
*usb_dev
= interface_to_usbdev(
49 rt2x00dev_usb(rt2x00dev
));
53 for (i
= 0; i
< REGISTER_BUSY_COUNT
; i
++) {
54 status
= usb_control_msg(
56 (type
== USB_VENDOR_REQUEST_IN
) ?
57 usb_rcvctrlpipe(usb_dev
, 0) :
58 usb_sndctrlpipe(usb_dev
, 0),
59 request
, type
, value
, offset
, buffer
, buffer_length
,
65 ERROR(rt2x00dev
, "vendor request error. Request 0x%02x failed "
66 "for offset 0x%04x with error %d.\n", request
, offset
, status
);
70 EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request
);
75 void rt2x00usb_enable_radio(struct rt2x00_dev
*rt2x00dev
)
82 for (i
= 0; i
< rt2x00dev
->rx
->stats
.limit
; i
++) {
83 __set_bit(ENTRY_OWNER_NIC
, &rt2x00dev
->rx
->entry
[i
].flags
);
84 usb_submit_urb(rt2x00dev
->rx
->entry
[i
].priv
, GFP_ATOMIC
);
87 EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio
);
89 void rt2x00usb_disable_radio(struct rt2x00_dev
*rt2x00dev
)
91 struct data_ring
*ring
;
94 rt2x00usb_vendor_request(rt2x00dev
, USB_RX_CONTROL
,
95 USB_VENDOR_REQUEST_OUT
, 0x00, 0x00, NULL
, 0, REGISTER_TIMEOUT
);
100 ring_for_each(rt2x00dev
, ring
) {
101 for (i
= 0; i
< ring
->stats
.limit
; i
++)
102 usb_kill_urb(ring
->entry
[i
].priv
);
105 EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio
);
110 int rt2x00usb_beacon_update(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
111 struct ieee80211_tx_control
*control
)
113 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
114 struct usb_device
*usb_dev
=
115 interface_to_usbdev(rt2x00dev_usb(rt2x00dev
));
116 struct data_ring
*ring
=
117 rt2x00_get_ring(rt2x00dev
, IEEE80211_TX_QUEUE_BEACON
);
118 struct data_entry
*beacon
;
119 struct data_entry
*guardian
;
123 * Just in case the ieee80211 doesn't set this,
124 * but we need this queue set for the descriptor
127 control
->queue
= IEEE80211_TX_QUEUE_BEACON
;
130 * Obtain 2 entries, one for the guardian byte,
131 * the second for the actual beacon.
133 guardian
= rt2x00_get_data_entry(ring
);
134 rt2x00_ring_index_inc(ring
);
135 beacon
= rt2x00_get_data_entry(ring
);
138 * First we create the beacon.
140 skb_push(skb
, ring
->desc_size
);
141 rt2x00lib_write_tx_desc(rt2x00dev
, beacon
,
142 (struct data_desc
*)skb
->data
,
143 (struct ieee80211_hdr
*)(skb
->data
+ ring
->desc_size
),
144 skb
->len
- ring
->desc_size
,
148 * Length passed to usb_fill_urb cannot be an odd number,
149 * so add 1 byte to make it even.
158 usb_sndbulkpipe(usb_dev
, 1),
161 rt2x00usb_beacondone
,
167 * Second we need to create the guardian byte.
168 * We only need a single byte, so lets recycle
169 * the 'flags' field we are not using for beacons.
175 usb_sndbulkpipe(usb_dev
, 1),
178 rt2x00usb_beacondone
,
182 * Send out the guardian byte.
184 usb_submit_urb(guardian
->priv
, GFP_ATOMIC
);
187 * Enable beacon generation.
189 rt2x00dev
->ops
->lib
->kick_tx_queue(rt2x00dev
, control
->queue
);
193 EXPORT_SYMBOL_GPL(rt2x00usb_beacon_update
);
195 void rt2x00usb_beacondone(struct urb
*urb
)
197 struct data_entry
*entry
= (struct data_entry
*)urb
->context
;
198 struct data_ring
*ring
= entry
->ring
;
200 if (!test_bit(DEVICE_ENABLED_RADIO
, &ring
->rt2x00dev
->flags
))
204 * Check if this was the guardian beacon,
205 * if that was the case we need to send the real beacon now.
206 * Otherwise we should free the sk_buffer, the device
207 * should be doing the rest of the work now.
209 if (ring
->index
== 1) {
210 rt2x00_ring_index_done_inc(ring
);
211 entry
= rt2x00_get_data_entry(ring
);
212 usb_submit_urb(entry
->priv
, GFP_ATOMIC
);
213 rt2x00_ring_index_inc(ring
);
214 } else if (ring
->index_done
== 1) {
215 entry
= rt2x00_get_data_entry_done(ring
);
217 dev_kfree_skb(entry
->skb
);
220 rt2x00_ring_index_done_inc(ring
);
223 EXPORT_SYMBOL_GPL(rt2x00usb_beacondone
);
228 static void rt2x00usb_interrupt_txdone(struct urb
*urb
)
230 struct data_entry
*entry
= (struct data_entry
*)urb
->context
;
231 struct data_ring
*ring
= entry
->ring
;
232 struct rt2x00_dev
*rt2x00dev
= ring
->rt2x00dev
;
233 struct data_desc
*txd
= (struct data_desc
*)entry
->skb
->data
;
237 if (!test_bit(DEVICE_ENABLED_RADIO
, &rt2x00dev
->flags
) ||
238 !__test_and_clear_bit(ENTRY_OWNER_NIC
, &entry
->flags
))
241 rt2x00_desc_read(txd
, 0, &word
);
244 * Remove the descriptor data from the buffer.
246 skb_pull(entry
->skb
, ring
->desc_size
);
249 * Obtain the status about this packet.
251 tx_status
= !urb
->status
? TX_SUCCESS
: TX_FAIL_RETRY
;
253 rt2x00lib_txdone(entry
, tx_status
, 0);
256 * Make this entry available for reuse.
259 rt2x00_ring_index_done_inc(entry
->ring
);
262 * If the data ring was full before the txdone handler
263 * we must make sure the packet queue in the mac80211 stack
264 * is reenabled when the txdone handler has finished.
266 if (!rt2x00_ring_full(ring
))
267 ieee80211_wake_queue(rt2x00dev
->hw
,
268 entry
->tx_status
.control
.queue
);
271 int rt2x00usb_write_tx_data(struct rt2x00_dev
*rt2x00dev
,
272 struct data_ring
*ring
, struct sk_buff
*skb
,
273 struct ieee80211_tx_control
*control
)
275 struct usb_device
*usb_dev
=
276 interface_to_usbdev(rt2x00dev_usb(rt2x00dev
));
277 struct ieee80211_hdr
*ieee80211hdr
= (struct ieee80211_hdr
*)skb
->data
;
278 struct data_entry
*entry
= rt2x00_get_data_entry(ring
);
279 struct data_desc
*txd
;
280 u32 length
= skb
->len
;
282 if (rt2x00_ring_full(ring
)) {
283 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
287 if (test_bit(ENTRY_OWNER_NIC
, &entry
->flags
)) {
289 "Arrived at non-free entry in the non-full queue %d.\n"
290 "Please file bug report to %s.\n",
291 control
->queue
, DRV_PROJECT
);
292 ieee80211_stop_queue( rt2x00dev
->hw
, control
->queue
);
296 skb_push(skb
, rt2x00dev
->hw
->extra_tx_headroom
);
297 txd
= (struct data_desc
*)skb
->data
;
298 rt2x00lib_write_tx_desc(rt2x00dev
, entry
, txd
, ieee80211hdr
,
300 memcpy(&entry
->tx_status
.control
, control
, sizeof(*control
));
304 * Length passed to usb_fill_urb cannot be an odd number,
305 * so add 1 byte to make it even.
307 length
+= rt2x00dev
->hw
->extra_tx_headroom
;
311 __set_bit(ENTRY_OWNER_NIC
, &entry
->flags
);
315 usb_sndbulkpipe(usb_dev
, 1),
318 rt2x00usb_interrupt_txdone
,
320 usb_submit_urb(entry
->priv
, GFP_ATOMIC
);
322 rt2x00_ring_index_inc(ring
);
324 if (rt2x00_ring_full(ring
))
325 ieee80211_stop_queue(rt2x00dev
->hw
, control
->queue
);
329 EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data
);
332 * Device initialization handlers.
334 static int rt2x00usb_alloc_ring(struct rt2x00_dev
*rt2x00dev
,
335 struct data_ring
*ring
)
342 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
343 ring
->entry
[i
].priv
= usb_alloc_urb(0, GFP_KERNEL
);
344 if (!ring
->entry
[i
].priv
)
351 int rt2x00usb_initialize(struct rt2x00_dev
*rt2x00dev
)
353 struct data_ring
*ring
;
355 unsigned int entry_size
;
362 ring_for_each(rt2x00dev
, ring
) {
363 status
= rt2x00usb_alloc_ring(rt2x00dev
, ring
);
369 * For the RX ring, skb's should be allocated.
371 entry_size
= ring
->data_size
+ ring
->desc_size
;
372 for (i
= 0; i
< rt2x00dev
->rx
->stats
.limit
; i
++) {
373 skb
= dev_alloc_skb(NET_IP_ALIGN
+ entry_size
);
377 skb_reserve(skb
, NET_IP_ALIGN
);
378 skb_put(skb
, entry_size
);
380 rt2x00dev
->rx
->entry
[i
].skb
= skb
;
386 rt2x00usb_uninitialize(rt2x00dev
);
390 EXPORT_SYMBOL_GPL(rt2x00usb_initialize
);
392 void rt2x00usb_uninitialize(struct rt2x00_dev
*rt2x00dev
)
394 struct data_ring
*ring
;
397 ring_for_each(rt2x00dev
, ring
) {
401 for (i
= 0; i
< ring
->stats
.limit
; i
++) {
402 usb_kill_urb(ring
->entry
[i
].priv
);
403 usb_free_urb(ring
->entry
[i
].priv
);
404 if (ring
->entry
[i
].skb
)
405 kfree_skb(ring
->entry
[i
].skb
);
409 EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize
);
412 * USB driver handlers.
414 int rt2x00usb_probe(struct usb_interface
*usb_intf
,
415 const struct usb_device_id
*id
)
417 struct usb_device
*usb_dev
= interface_to_usbdev(usb_intf
);
418 struct rt2x00_ops
*ops
= (struct rt2x00_ops
*)id
->driver_info
;
419 struct ieee80211_hw
*hw
;
420 struct rt2x00_dev
*rt2x00dev
;
423 usb_dev
= usb_get_dev(usb_dev
);
425 hw
= ieee80211_alloc_hw(sizeof(struct rt2x00_dev
), ops
->hw
);
427 ERROR_PROBE("Failed to allocate hardware.\n");
429 goto exit_put_device
;
432 usb_set_intfdata(usb_intf
, hw
);
434 rt2x00dev
= hw
->priv
;
435 rt2x00dev
->dev
= usb_intf
;
436 rt2x00dev
->device
= &usb_intf
->dev
;
437 rt2x00dev
->ops
= ops
;
440 retval
= rt2x00lib_probe_dev(rt2x00dev
);
442 goto exit_free_device
;
447 ieee80211_free_hw(hw
);
450 usb_put_dev(usb_dev
);
452 usb_set_intfdata(usb_intf
, NULL
);
456 EXPORT_SYMBOL_GPL(rt2x00usb_probe
);
458 void rt2x00usb_disconnect(struct usb_interface
*usb_intf
)
460 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
461 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
464 * Free all allocated data.
466 rt2x00lib_remove_dev(rt2x00dev
);
467 ieee80211_free_hw(hw
);
470 * Free the USB device data.
472 usb_set_intfdata(usb_intf
, NULL
);
473 usb_put_dev(interface_to_usbdev(usb_intf
));
475 EXPORT_SYMBOL_GPL(rt2x00usb_disconnect
);
478 int rt2x00usb_suspend(struct usb_interface
*usb_intf
, pm_message_t state
)
480 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
481 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
484 retval
= rt2x00lib_suspend(rt2x00dev
, state
);
489 * Decrease usbdev refcount.
491 usb_put_dev(interface_to_usbdev(usb_intf
));
495 EXPORT_SYMBOL_GPL(rt2x00usb_suspend
);
497 int rt2x00usb_resume(struct usb_interface
*usb_intf
)
499 struct ieee80211_hw
*hw
= usb_get_intfdata(usb_intf
);
500 struct rt2x00_dev
*rt2x00dev
= hw
->priv
;
502 usb_get_dev(interface_to_usbdev(usb_intf
));
504 return rt2x00lib_resume(rt2x00dev
);
506 EXPORT_SYMBOL_GPL(rt2x00usb_resume
);
507 #endif /* CONFIG_PM */
510 * rt2x00pci module information.
512 MODULE_AUTHOR(DRV_PROJECT
);
513 MODULE_VERSION(DRV_VERSION
);
514 MODULE_DESCRIPTION("rt2x00 library");
515 MODULE_LICENSE("GPL");