2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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, see <http://www.gnu.org/licenses/>.
21 Abstract: Data structures for the rt2x00usb module.
27 #include <linux/usb.h>
29 #define to_usb_device_intf(d) \
31 struct usb_interface *intf = to_usb_interface(d); \
32 interface_to_usbdev(intf); \
36 * For USB vendor requests we need to pass a timeout
37 * time in ms, for this we use the REGISTER_TIMEOUT,
38 * however when loading firmware a higher value is
39 * required. In that case we use the REGISTER_TIMEOUT_FIRMWARE.
41 #define REGISTER_TIMEOUT 500
42 #define REGISTER_TIMEOUT_FIRMWARE 1000
45 * REGISTER_TIMEOUT16 - Determine the timeout for 16bit register access
46 * @__datalen: Data length
48 #define REGISTER_TIMEOUT16(__datalen) \
49 ( REGISTER_TIMEOUT * ((__datalen) / sizeof(u16)) )
52 * REGISTER_TIMEOUT32 - Determine the timeout for 32bit register access
53 * @__datalen: Data length
55 #define REGISTER_TIMEOUT32(__datalen) \
56 ( REGISTER_TIMEOUT * ((__datalen) / sizeof(u32)) )
61 #define CSR_CACHE_SIZE 64
66 #define USB_VENDOR_REQUEST ( USB_TYPE_VENDOR | USB_RECIP_DEVICE )
67 #define USB_VENDOR_REQUEST_IN ( USB_DIR_IN | USB_VENDOR_REQUEST )
68 #define USB_VENDOR_REQUEST_OUT ( USB_DIR_OUT | USB_VENDOR_REQUEST )
71 * enum rt2x00usb_vendor_request: USB vendor commands.
73 enum rt2x00usb_vendor_request {
81 USB_LED_CONTROL = 10, /* RT73USB */
86 * enum rt2x00usb_mode_offset: Device modes offset.
88 enum rt2x00usb_mode_offset {
91 USB_MODE_FUNCTION = 3,
93 USB_MODE_SLEEP = 7, /* RT73USB */
94 USB_MODE_FIRMWARE = 8, /* RT73USB */
95 USB_MODE_WAKEUP = 9, /* RT73USB */
96 USB_MODE_AUTORUN = 17, /* RT2800USB */
100 * rt2x00usb_vendor_request - Send register command to device
101 * @rt2x00dev: Pointer to &struct rt2x00_dev
102 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
103 * @requesttype: Request type &USB_VENDOR_REQUEST_*
104 * @offset: Register offset to perform action on
105 * @value: Value to write to device
106 * @buffer: Buffer where information will be read/written to by device
107 * @buffer_length: Size of &buffer
108 * @timeout: Operation timeout
110 * This is the main function to communicate with the device,
111 * the &buffer argument _must_ either be NULL or point to
112 * a buffer allocated by kmalloc. Failure to do so can lead
113 * to unexpected behavior depending on the architecture.
115 int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
116 const u8 request, const u8 requesttype,
117 const u16 offset, const u16 value,
118 void *buffer, const u16 buffer_length,
122 * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
123 * @rt2x00dev: Pointer to &struct rt2x00_dev
124 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
125 * @requesttype: Request type &USB_VENDOR_REQUEST_*
126 * @offset: Register offset to perform action on
127 * @buffer: Buffer where information will be read/written to by device
128 * @buffer_length: Size of &buffer
129 * @timeout: Operation timeout
131 * This function will use a previously with kmalloc allocated cache
132 * to communicate with the device. The contents of the buffer pointer
133 * will be copied to this cache when writing, or read from the cache
135 * Buffers send to &rt2x00usb_vendor_request _must_ be allocated with
136 * kmalloc. Hence the reason for using a previously allocated cache
137 * which has been allocated properly.
139 int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
140 const u8 request, const u8 requesttype,
141 const u16 offset, void *buffer,
142 const u16 buffer_length, const int timeout);
145 * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
146 * @rt2x00dev: Pointer to &struct rt2x00_dev
147 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
148 * @requesttype: Request type &USB_VENDOR_REQUEST_*
149 * @offset: Register offset to perform action on
150 * @buffer: Buffer where information will be read/written to by device
151 * @buffer_length: Size of &buffer
152 * @timeout: Operation timeout
154 * A version of &rt2x00usb_vendor_request_buff which must be called
155 * if the usb_cache_mutex is already held.
157 int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
158 const u8 request, const u8 requesttype,
159 const u16 offset, void *buffer,
160 const u16 buffer_length, const int timeout);
163 * rt2x00usb_vendor_request_sw - Send single register command to device
164 * @rt2x00dev: Pointer to &struct rt2x00_dev
165 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
166 * @offset: Register offset to perform action on
167 * @value: Value to write to device
168 * @timeout: Operation timeout
170 * Simple wrapper around rt2x00usb_vendor_request to write a single
171 * command to the device. Since we don't use the buffer argument we
172 * don't have to worry about kmalloc here.
174 static inline int rt2x00usb_vendor_request_sw(struct rt2x00_dev *rt2x00dev,
180 return rt2x00usb_vendor_request(rt2x00dev, request,
181 USB_VENDOR_REQUEST_OUT, offset,
182 value, NULL, 0, timeout);
186 * rt2x00usb_eeprom_read - Read eeprom from device
187 * @rt2x00dev: Pointer to &struct rt2x00_dev
188 * @eeprom: Pointer to eeprom array to store the information in
189 * @length: Number of bytes to read from the eeprom
191 * Simple wrapper around rt2x00usb_vendor_request to read the eeprom
192 * from the device. Note that the eeprom argument _must_ be allocated using
193 * kmalloc for correct handling inside the kernel USB layer.
195 static inline int rt2x00usb_eeprom_read(struct rt2x00_dev *rt2x00dev,
196 __le16 *eeprom, const u16 length)
198 return rt2x00usb_vendor_request(rt2x00dev, USB_EEPROM_READ,
199 USB_VENDOR_REQUEST_IN, 0, 0,
201 REGISTER_TIMEOUT16(length));
205 * rt2x00usb_register_read - Read 32bit register word
206 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
207 * @offset: Register offset
208 * @value: Pointer to where register contents should be stored
210 * This function is a simple wrapper for 32bit register access
211 * through rt2x00usb_vendor_request_buff().
213 static inline void rt2x00usb_register_read(struct rt2x00_dev *rt2x00dev,
214 const unsigned int offset,
218 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
219 USB_VENDOR_REQUEST_IN, offset,
220 ®, sizeof(reg), REGISTER_TIMEOUT);
221 *value = le32_to_cpu(reg);
225 * rt2x00usb_register_read_lock - Read 32bit register word
226 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
227 * @offset: Register offset
228 * @value: Pointer to where register contents should be stored
230 * This function is a simple wrapper for 32bit register access
231 * through rt2x00usb_vendor_req_buff_lock().
233 static inline void rt2x00usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
234 const unsigned int offset,
238 rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
239 USB_VENDOR_REQUEST_IN, offset,
240 ®, sizeof(reg), REGISTER_TIMEOUT);
241 *value = le32_to_cpu(reg);
245 * rt2x00usb_register_multiread - Read 32bit register words
246 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
247 * @offset: Register offset
248 * @value: Pointer to where register contents should be stored
249 * @length: Length of the data
251 * This function is a simple wrapper for 32bit register access
252 * through rt2x00usb_vendor_request_buff().
254 static inline void rt2x00usb_register_multiread(struct rt2x00_dev *rt2x00dev,
255 const unsigned int offset,
256 void *value, const u32 length)
258 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
259 USB_VENDOR_REQUEST_IN, offset,
261 REGISTER_TIMEOUT32(length));
265 * rt2x00usb_register_write - Write 32bit register word
266 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
267 * @offset: Register offset
268 * @value: Data which should be written
270 * This function is a simple wrapper for 32bit register access
271 * through rt2x00usb_vendor_request_buff().
273 static inline void rt2x00usb_register_write(struct rt2x00_dev *rt2x00dev,
274 const unsigned int offset,
277 __le32 reg = cpu_to_le32(value);
278 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
279 USB_VENDOR_REQUEST_OUT, offset,
280 ®, sizeof(reg), REGISTER_TIMEOUT);
284 * rt2x00usb_register_write_lock - Write 32bit register word
285 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
286 * @offset: Register offset
287 * @value: Data which should be written
289 * This function is a simple wrapper for 32bit register access
290 * through rt2x00usb_vendor_req_buff_lock().
292 static inline void rt2x00usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
293 const unsigned int offset,
296 __le32 reg = cpu_to_le32(value);
297 rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
298 USB_VENDOR_REQUEST_OUT, offset,
299 ®, sizeof(reg), REGISTER_TIMEOUT);
303 * rt2x00usb_register_multiwrite - Write 32bit register words
304 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
305 * @offset: Register offset
306 * @value: Data which should be written
307 * @length: Length of the data
309 * This function is a simple wrapper for 32bit register access
310 * through rt2x00usb_vendor_request_buff().
312 static inline void rt2x00usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
313 const unsigned int offset,
317 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
318 USB_VENDOR_REQUEST_OUT, offset,
319 (void *)value, length,
320 REGISTER_TIMEOUT32(length));
324 * rt2x00usb_regbusy_read - Read from register with busy check
325 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
326 * @offset: Register offset
327 * @field: Field to check if register is busy
328 * @reg: Pointer to where register contents should be stored
330 * This function will read the given register, and checks if the
331 * register is busy. If it is, it will sleep for a couple of
332 * microseconds before reading the register again. If the register
333 * is not read after a certain timeout, this function will return
336 int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
337 const unsigned int offset,
338 const struct rt2x00_field32 field,
342 * rt2x00usb_register_read_async - Asynchronously read 32bit register word
343 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
344 * @offset: Register offset
345 * @callback: Functon to call when read completes.
347 * Submit a control URB to read a 32bit register. This safe to
348 * be called from atomic context. The callback will be called
349 * when the URB completes. Otherwise the function is similar
350 * to rt2x00usb_register_read().
351 * When the callback function returns false, the memory will be cleaned up,
352 * when it returns true, the urb will be fired again.
354 void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
355 const unsigned int offset,
356 bool (*callback)(struct rt2x00_dev*, int, u32));
361 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev);
364 * struct queue_entry_priv_usb: Per entry USB specific information
366 * @urb: Urb structure used for device communication.
368 struct queue_entry_priv_usb {
373 * struct queue_entry_priv_usb_bcn: Per TX entry USB specific information
375 * The first section should match &struct queue_entry_priv_usb exactly.
376 * rt2500usb can use this structure to send a guardian byte when working
379 * @urb: Urb structure used for device communication.
380 * @guardian_data: Set to 0, used for sending the guardian data.
381 * @guardian_urb: Urb structure used to send the guardian data.
383 struct queue_entry_priv_usb_bcn {
386 unsigned int guardian_data;
387 struct urb *guardian_urb;
391 * rt2x00usb_kick_queue - Kick data queue
392 * @queue: Data queue to kick
394 * This will walk through all entries of the queue and push all pending
395 * frames to the hardware as a single burst.
397 void rt2x00usb_kick_queue(struct data_queue *queue);
400 * rt2x00usb_flush_queue - Flush data queue
401 * @queue: Data queue to stop
402 * @drop: True to drop all pending frames.
404 * This will walk through all entries of the queue and will optionally
405 * kill all URB's which were send to the device, or at least wait until
406 * they have been returned from the device..
408 void rt2x00usb_flush_queue(struct data_queue *queue, bool drop);
411 * rt2x00usb_watchdog - Watchdog for USB communication
412 * @rt2x00dev: Pointer to &struct rt2x00_dev
414 * Check the health of the USB communication and determine
415 * if timeouts have occurred. If this is the case, this function
416 * will reset all communication to restore functionality again.
418 void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev);
421 * Device initialization handlers.
423 void rt2x00usb_clear_entry(struct queue_entry *entry);
424 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev);
425 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev);
428 * USB driver handlers.
430 int rt2x00usb_probe(struct usb_interface *usb_intf,
431 const struct rt2x00_ops *ops);
432 void rt2x00usb_disconnect(struct usb_interface *usb_intf);
434 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state);
435 int rt2x00usb_resume(struct usb_interface *usb_intf);
437 #define rt2x00usb_suspend NULL
438 #define rt2x00usb_resume NULL
439 #endif /* CONFIG_PM */
441 #endif /* RT2X00USB_H */