blob: 9943e428bc2152ba1cfd6092ea1ff007ea175e73 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn4e54c712009-01-17 20:42:32 +01002 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19 */
20
21/*
22 Module: rt2x00usb
23 Abstract: Data structures for the rt2x00usb module.
24 */
25
26#ifndef RT2X00USB_H
27#define RT2X00USB_H
28
Ivo van Doornc1d35df2008-06-16 19:57:11 +020029#define to_usb_device_intf(d) \
30({ \
31 struct usb_interface *intf = to_usb_interface(d); \
32 interface_to_usbdev(intf); \
33})
34
Ivo van Doorn95ea3622007-09-25 17:57:13 -070035/*
36 * This variable should be used with the
37 * usb_driver structure initialization.
38 */
39#define USB_DEVICE_DATA(__ops) .driver_info = (kernel_ulong_t)(__ops)
40
41/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070042 * For USB vendor requests we need to pass a timeout
43 * time in ms, for this we use the REGISTER_TIMEOUT,
44 * however when loading firmware a higher value is
45 * required. In that case we use the REGISTER_TIMEOUT_FIRMWARE.
46 */
Ivo van Doorne9136552007-09-25 20:54:20 +020047#define REGISTER_TIMEOUT 500
Ivo van Doorn95ea3622007-09-25 17:57:13 -070048#define REGISTER_TIMEOUT_FIRMWARE 1000
49
Ivo van Doornbd394a72008-04-21 19:01:58 +020050/**
51 * REGISTER_TIMEOUT16 - Determine the timeout for 16bit register access
52 * @__datalen: Data length
53 */
54#define REGISTER_TIMEOUT16(__datalen) \
55 ( REGISTER_TIMEOUT * ((__datalen) / sizeof(u16)) )
56
57/**
58 * REGISTER_TIMEOUT32 - Determine the timeout for 32bit register access
59 * @__datalen: Data length
60 */
61#define REGISTER_TIMEOUT32(__datalen) \
62 ( REGISTER_TIMEOUT * ((__datalen) / sizeof(u32)) )
63
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064/*
65 * Cache size
66 */
Iwo Merglered0dbee2008-07-19 16:16:54 +020067#define CSR_CACHE_SIZE 64
Ivo van Doorn95ea3622007-09-25 17:57:13 -070068
69/*
70 * USB request types.
71 */
72#define USB_VENDOR_REQUEST ( USB_TYPE_VENDOR | USB_RECIP_DEVICE )
73#define USB_VENDOR_REQUEST_IN ( USB_DIR_IN | USB_VENDOR_REQUEST )
74#define USB_VENDOR_REQUEST_OUT ( USB_DIR_OUT | USB_VENDOR_REQUEST )
75
Ivo van Doorn3b640f22008-02-03 15:54:11 +010076/**
77 * enum rt2x00usb_vendor_request: USB vendor commands.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070078 */
Ivo van Doorn3b640f22008-02-03 15:54:11 +010079enum rt2x00usb_vendor_request {
80 USB_DEVICE_MODE = 1,
81 USB_SINGLE_WRITE = 2,
82 USB_SINGLE_READ = 3,
83 USB_MULTI_WRITE = 6,
84 USB_MULTI_READ = 7,
85 USB_EEPROM_WRITE = 8,
86 USB_EEPROM_READ = 9,
87 USB_LED_CONTROL = 10, /* RT73USB */
88 USB_RX_CONTROL = 12,
89};
Ivo van Doorn95ea3622007-09-25 17:57:13 -070090
Ivo van Doorn3b640f22008-02-03 15:54:11 +010091/**
92 * enum rt2x00usb_mode_offset: Device modes offset.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070093 */
Ivo van Doorn3b640f22008-02-03 15:54:11 +010094enum rt2x00usb_mode_offset {
95 USB_MODE_RESET = 1,
96 USB_MODE_UNPLUG = 2,
97 USB_MODE_FUNCTION = 3,
98 USB_MODE_TEST = 4,
99 USB_MODE_SLEEP = 7, /* RT73USB */
100 USB_MODE_FIRMWARE = 8, /* RT73USB */
101 USB_MODE_WAKEUP = 9, /* RT73USB */
102};
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100104/**
105 * rt2x00usb_vendor_request - Send register command to device
106 * @rt2x00dev: Pointer to &struct rt2x00_dev
107 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
108 * @requesttype: Request type &USB_VENDOR_REQUEST_*
109 * @offset: Register offset to perform action on
110 * @value: Value to write to device
111 * @buffer: Buffer where information will be read/written to by device
112 * @buffer_length: Size of &buffer
113 * @timeout: Operation timeout
114 *
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700115 * This is the main function to communicate with the device,
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100116 * the &buffer argument _must_ either be NULL or point to
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700117 * a buffer allocated by kmalloc. Failure to do so can lead
118 * to unexpected behavior depending on the architecture.
119 */
Adam Baker0e14f6d2007-10-27 13:41:25 +0200120int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700121 const u8 request, const u8 requesttype,
122 const u16 offset, const u16 value,
123 void *buffer, const u16 buffer_length,
Ivo van Doorne9136552007-09-25 20:54:20 +0200124 const int timeout);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700125
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100126/**
127 * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
128 * @rt2x00dev: Pointer to &struct rt2x00_dev
129 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
130 * @requesttype: Request type &USB_VENDOR_REQUEST_*
131 * @offset: Register offset to perform action on
132 * @buffer: Buffer where information will be read/written to by device
133 * @buffer_length: Size of &buffer
134 * @timeout: Operation timeout
135 *
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700136 * This function will use a previously with kmalloc allocated cache
137 * to communicate with the device. The contents of the buffer pointer
138 * will be copied to this cache when writing, or read from the cache
139 * when reading.
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100140 * Buffers send to &rt2x00usb_vendor_request _must_ be allocated with
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700141 * kmalloc. Hence the reason for using a previously allocated cache
142 * which has been allocated properly.
143 */
Adam Baker0e14f6d2007-10-27 13:41:25 +0200144int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700145 const u8 request, const u8 requesttype,
146 const u16 offset, void *buffer,
Ivo van Doorne9136552007-09-25 20:54:20 +0200147 const u16 buffer_length, const int timeout);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700148
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100149/**
150 * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
151 * @rt2x00dev: Pointer to &struct rt2x00_dev
152 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
153 * @requesttype: Request type &USB_VENDOR_REQUEST_*
154 * @offset: Register offset to perform action on
155 * @buffer: Buffer where information will be read/written to by device
156 * @buffer_length: Size of &buffer
157 * @timeout: Operation timeout
158 *
159 * A version of &rt2x00usb_vendor_request_buff which must be called
160 * if the usb_cache_mutex is already held.
161 */
Adam Baker3d823462007-10-27 13:43:29 +0200162int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
163 const u8 request, const u8 requesttype,
164 const u16 offset, void *buffer,
165 const u16 buffer_length, const int timeout);
166
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100167/**
Iwo Merglered0dbee2008-07-19 16:16:54 +0200168 * rt2x00usb_vendor_request_large_buff - Send register command to device (buffered)
169 * @rt2x00dev: Pointer to &struct rt2x00_dev
170 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
171 * @requesttype: Request type &USB_VENDOR_REQUEST_*
172 * @offset: Register start offset to perform action on
173 * @buffer: Buffer where information will be read/written to by device
174 * @buffer_length: Size of &buffer
175 * @timeout: Operation timeout
176 *
177 * This function is used to transfer register data in blocks larger
178 * then CSR_CACHE_SIZE. Use for firmware upload, keys and beacons.
179 */
180int rt2x00usb_vendor_request_large_buff(struct rt2x00_dev *rt2x00dev,
181 const u8 request, const u8 requesttype,
Ivo van Doorn82f97b82008-08-02 01:31:09 -0700182 const u16 offset, const void *buffer,
Iwo Merglered0dbee2008-07-19 16:16:54 +0200183 const u16 buffer_length,
184 const int timeout);
185
186/**
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100187 * rt2x00usb_vendor_request_sw - Send single register command to device
188 * @rt2x00dev: Pointer to &struct rt2x00_dev
189 * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
190 * @offset: Register offset to perform action on
191 * @value: Value to write to device
192 * @timeout: Operation timeout
193 *
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700194 * Simple wrapper around rt2x00usb_vendor_request to write a single
195 * command to the device. Since we don't use the buffer argument we
196 * don't have to worry about kmalloc here.
197 */
Adam Baker0e14f6d2007-10-27 13:41:25 +0200198static inline int rt2x00usb_vendor_request_sw(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700199 const u8 request,
200 const u16 offset,
201 const u16 value,
Ivo van Doorne9136552007-09-25 20:54:20 +0200202 const int timeout)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700203{
204 return rt2x00usb_vendor_request(rt2x00dev, request,
205 USB_VENDOR_REQUEST_OUT, offset,
206 value, NULL, 0, timeout);
207}
208
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100209/**
210 * rt2x00usb_eeprom_read - Read eeprom from device
211 * @rt2x00dev: Pointer to &struct rt2x00_dev
212 * @eeprom: Pointer to eeprom array to store the information in
213 * @length: Number of bytes to read from the eeprom
214 *
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700215 * Simple wrapper around rt2x00usb_vendor_request to read the eeprom
216 * from the device. Note that the eeprom argument _must_ be allocated using
217 * kmalloc for correct handling inside the kernel USB layer.
218 */
Adam Baker0e14f6d2007-10-27 13:41:25 +0200219static inline int rt2x00usb_eeprom_read(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn9a46d442008-04-21 19:02:17 +0200220 __le16 *eeprom, const u16 length)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700221{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700222 return rt2x00usb_vendor_request(rt2x00dev, USB_EEPROM_READ,
Ivo van Doorn3b640f22008-02-03 15:54:11 +0100223 USB_VENDOR_REQUEST_IN, 0, 0,
Ivo van Doorn9a46d442008-04-21 19:02:17 +0200224 eeprom, length,
225 REGISTER_TIMEOUT16(length));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700226}
227
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100228/**
Bartlomiej Zolnierkiewicz02a39c22009-11-04 18:32:50 +0100229 * rt2x00usb_register_read - Read 32bit register word
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100230 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
231 * @offset: Register offset
232 * @value: Pointer to where register contents should be stored
233 *
234 * This function is a simple wrapper for 32bit register access
235 * through rt2x00usb_vendor_request_buff().
236 */
237static inline void rt2x00usb_register_read(struct rt2x00_dev *rt2x00dev,
238 const unsigned int offset,
239 u32 *value)
240{
241 __le32 reg;
242 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
243 USB_VENDOR_REQUEST_IN, offset,
244 &reg, sizeof(reg), REGISTER_TIMEOUT);
245 *value = le32_to_cpu(reg);
246}
247
248/**
249 * rt2x00usb_register_read_lock - Read 32bit register word
250 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
251 * @offset: Register offset
252 * @value: Pointer to where register contents should be stored
253 *
254 * This function is a simple wrapper for 32bit register access
255 * through rt2x00usb_vendor_req_buff_lock().
256 */
257static inline void rt2x00usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
258 const unsigned int offset,
259 u32 *value)
260{
261 __le32 reg;
262 rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
263 USB_VENDOR_REQUEST_IN, offset,
264 &reg, sizeof(reg), REGISTER_TIMEOUT);
265 *value = le32_to_cpu(reg);
266}
267
268/**
269 * rt2x00usb_register_multiread - Read 32bit register words
270 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
271 * @offset: Register offset
272 * @value: Pointer to where register contents should be stored
273 * @length: Length of the data
274 *
275 * This function is a simple wrapper for 32bit register access
276 * through rt2x00usb_vendor_request_buff().
277 */
278static inline void rt2x00usb_register_multiread(struct rt2x00_dev *rt2x00dev,
279 const unsigned int offset,
280 void *value, const u32 length)
281{
282 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
283 USB_VENDOR_REQUEST_IN, offset,
284 value, length,
285 REGISTER_TIMEOUT32(length));
286}
287
288/**
289 * rt2x00usb_register_write - Write 32bit register word
290 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
291 * @offset: Register offset
292 * @value: Data which should be written
293 *
294 * This function is a simple wrapper for 32bit register access
295 * through rt2x00usb_vendor_request_buff().
296 */
297static inline void rt2x00usb_register_write(struct rt2x00_dev *rt2x00dev,
298 const unsigned int offset,
299 u32 value)
300{
301 __le32 reg = cpu_to_le32(value);
302 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
303 USB_VENDOR_REQUEST_OUT, offset,
304 &reg, sizeof(reg), REGISTER_TIMEOUT);
305}
306
307/**
308 * rt2x00usb_register_write_lock - Write 32bit register word
309 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
310 * @offset: Register offset
311 * @value: Data which should be written
312 *
313 * This function is a simple wrapper for 32bit register access
314 * through rt2x00usb_vendor_req_buff_lock().
315 */
316static inline void rt2x00usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
317 const unsigned int offset,
318 u32 value)
319{
320 __le32 reg = cpu_to_le32(value);
321 rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
322 USB_VENDOR_REQUEST_OUT, offset,
323 &reg, sizeof(reg), REGISTER_TIMEOUT);
324}
325
326/**
327 * rt2x00usb_register_multiwrite - Write 32bit register words
328 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
329 * @offset: Register offset
330 * @value: Data which should be written
331 * @length: Length of the data
332 *
333 * This function is a simple wrapper for 32bit register access
334 * through rt2x00usb_vendor_request_buff().
335 */
336static inline void rt2x00usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
Bartlomiej Zolnierkiewicz5b10b092009-11-04 18:35:10 +0100337 const unsigned int offset,
338 const void *value,
339 const u32 length)
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100340{
341 rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
342 USB_VENDOR_REQUEST_OUT, offset,
Bartlomiej Zolnierkiewicz5b10b092009-11-04 18:35:10 +0100343 (void *)value, length,
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100344 REGISTER_TIMEOUT32(length));
345}
346
347/**
348 * rt2x00usb_regbusy_read - Read from register with busy check
349 * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
350 * @offset: Register offset
351 * @field: Field to check if register is busy
352 * @reg: Pointer to where register contents should be stored
353 *
354 * This function will read the given register, and checks if the
355 * register is busy. If it is, it will sleep for a couple of
356 * microseconds before reading the register again. If the register
357 * is not read after a certain timeout, this function will return
358 * FALSE.
359 */
360int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
361 const unsigned int offset,
Bartlomiej Zolnierkiewiczf255b922009-11-04 18:35:18 +0100362 const struct rt2x00_field32 field,
Ivo van Doorn0f829b12008-11-10 19:42:18 +0100363 u32 *reg);
364
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700365/*
366 * Radio handlers
367 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700368void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev);
369
Ivo van Doorn6db37862008-06-06 22:50:28 +0200370/**
371 * rt2x00usb_write_tx_data - Initialize URB for TX operation
372 * @entry: The entry where the frame is located
373 *
374 * This function will initialize the URB and skb descriptor
375 * to prepare the entry for the actual TX operation.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700376 */
Ivo van Doorn6db37862008-06-06 22:50:28 +0200377int rt2x00usb_write_tx_data(struct queue_entry *entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700378
Ivo van Doorn181d6902008-02-05 16:42:23 -0500379/**
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200380 * struct queue_entry_priv_usb: Per entry USB specific information
Ivo van Doorn181d6902008-02-05 16:42:23 -0500381 *
382 * @urb: Urb structure used for device communication.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500383 */
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200384struct queue_entry_priv_usb {
Ivo van Doorn181d6902008-02-05 16:42:23 -0500385 struct urb *urb;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500386};
387
388/**
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200389 * struct queue_entry_priv_usb_bcn: Per TX entry USB specific information
Ivo van Doorn181d6902008-02-05 16:42:23 -0500390 *
Ivo van Doornb8be63f2008-05-10 13:46:03 +0200391 * The first section should match &struct queue_entry_priv_usb exactly.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500392 * rt2500usb can use this structure to send a guardian byte when working
393 * with beacons.
394 *
395 * @urb: Urb structure used for device communication.
Ivo van Doorn181d6902008-02-05 16:42:23 -0500396 * @guardian_data: Set to 0, used for sending the guardian data.
397 * @guardian_urb: Urb structure used to send the guardian data.
398 */
399struct queue_entry_priv_usb_bcn {
400 struct urb *urb;
401
Ivo van Doorn181d6902008-02-05 16:42:23 -0500402 unsigned int guardian_data;
403 struct urb *guardian_urb;
404};
405
Ivo van Doornf019d512008-06-06 22:47:39 +0200406/**
407 * rt2x00usb_kick_tx_queue - Kick data queue
408 * @rt2x00dev: Pointer to &struct rt2x00_dev
409 * @qid: Data queue to kick
410 *
411 * This will walk through all entries of the queue and push all pending
412 * frames to the hardware as a single burst.
413 */
414void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
415 const enum data_queue_qid qid);
416
Ivo van Doorna2c9b652009-01-28 00:32:33 +0100417/**
418 * rt2x00usb_kill_tx_queue - Kill data queue
419 * @rt2x00dev: Pointer to &struct rt2x00_dev
420 * @qid: Data queue to kill
421 *
422 * This will walk through all entries of the queue and kill all
423 * previously kicked frames before they can be send.
424 */
425void rt2x00usb_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
426 const enum data_queue_qid qid);
427
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700428/*
429 * Device initialization handlers.
430 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100431void rt2x00usb_clear_entry(struct queue_entry *entry);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700432int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev);
433void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev);
434
435/*
436 * USB driver handlers.
437 */
438int rt2x00usb_probe(struct usb_interface *usb_intf,
439 const struct usb_device_id *id);
440void rt2x00usb_disconnect(struct usb_interface *usb_intf);
441#ifdef CONFIG_PM
442int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state);
443int rt2x00usb_resume(struct usb_interface *usb_intf);
444#else
445#define rt2x00usb_suspend NULL
446#define rt2x00usb_resume NULL
447#endif /* CONFIG_PM */
448
449#endif /* RT2X00USB_H */