Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003     Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
0004     <http://rt2x00.serialmonkey.com>
0005 
0006  */
0007 
0008 /*
0009     Module: rt2x00usb
0010     Abstract: Data structures for the rt2x00usb module.
0011  */
0012 
0013 #ifndef RT2X00USB_H
0014 #define RT2X00USB_H
0015 
0016 #include <linux/usb.h>
0017 
0018 #define to_usb_device_intf(d) \
0019 ({ \
0020     struct usb_interface *intf = to_usb_interface(d); \
0021     interface_to_usbdev(intf); \
0022 })
0023 
0024 /*
0025  * For USB vendor requests we need to pass a timeout time in ms, for this we
0026  * use the REGISTER_TIMEOUT, however when loading firmware or read EEPROM
0027  * a higher value is required. In that case we use the REGISTER_TIMEOUT_FIRMWARE
0028  * and EEPROM_TIMEOUT.
0029  */
0030 #define REGISTER_TIMEOUT        100
0031 #define REGISTER_TIMEOUT_FIRMWARE   1000
0032 #define EEPROM_TIMEOUT          2000
0033 
0034 /*
0035  * Cache size
0036  */
0037 #define CSR_CACHE_SIZE          64
0038 
0039 /*
0040  * USB request types.
0041  */
0042 #define USB_VENDOR_REQUEST  ( USB_TYPE_VENDOR | USB_RECIP_DEVICE )
0043 #define USB_VENDOR_REQUEST_IN   ( USB_DIR_IN | USB_VENDOR_REQUEST )
0044 #define USB_VENDOR_REQUEST_OUT  ( USB_DIR_OUT | USB_VENDOR_REQUEST )
0045 
0046 /**
0047  * enum rt2x00usb_vendor_request: USB vendor commands.
0048  */
0049 enum rt2x00usb_vendor_request {
0050     USB_DEVICE_MODE = 1,
0051     USB_SINGLE_WRITE = 2,
0052     USB_SINGLE_READ = 3,
0053     USB_MULTI_WRITE = 6,
0054     USB_MULTI_READ = 7,
0055     USB_EEPROM_WRITE = 8,
0056     USB_EEPROM_READ = 9,
0057     USB_LED_CONTROL = 10, /* RT73USB */
0058     USB_RX_CONTROL = 12,
0059 };
0060 
0061 /**
0062  * enum rt2x00usb_mode_offset: Device modes offset.
0063  */
0064 enum rt2x00usb_mode_offset {
0065     USB_MODE_RESET = 1,
0066     USB_MODE_UNPLUG = 2,
0067     USB_MODE_FUNCTION = 3,
0068     USB_MODE_TEST = 4,
0069     USB_MODE_SLEEP = 7, /* RT73USB */
0070     USB_MODE_FIRMWARE = 8,  /* RT73USB */
0071     USB_MODE_WAKEUP = 9,    /* RT73USB */
0072     USB_MODE_AUTORUN = 17, /* RT2800USB */
0073 };
0074 
0075 /**
0076  * rt2x00usb_vendor_request - Send register command to device
0077  * @rt2x00dev: Pointer to &struct rt2x00_dev
0078  * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
0079  * @requesttype: Request type &USB_VENDOR_REQUEST_*
0080  * @offset: Register offset to perform action on
0081  * @value: Value to write to device
0082  * @buffer: Buffer where information will be read/written to by device
0083  * @buffer_length: Size of &buffer
0084  * @timeout: Operation timeout
0085  *
0086  * This is the main function to communicate with the device,
0087  * the &buffer argument _must_ either be NULL or point to
0088  * a buffer allocated by kmalloc. Failure to do so can lead
0089  * to unexpected behavior depending on the architecture.
0090  */
0091 int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
0092                  const u8 request, const u8 requesttype,
0093                  const u16 offset, const u16 value,
0094                  void *buffer, const u16 buffer_length,
0095                  const int timeout);
0096 
0097 /**
0098  * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
0099  * @rt2x00dev: Pointer to &struct rt2x00_dev
0100  * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
0101  * @requesttype: Request type &USB_VENDOR_REQUEST_*
0102  * @offset: Register offset to perform action on
0103  * @buffer: Buffer where information will be read/written to by device
0104  * @buffer_length: Size of &buffer
0105  *
0106  * This function will use a previously with kmalloc allocated cache
0107  * to communicate with the device. The contents of the buffer pointer
0108  * will be copied to this cache when writing, or read from the cache
0109  * when reading.
0110  * Buffers send to &rt2x00usb_vendor_request _must_ be allocated with
0111  * kmalloc. Hence the reason for using a previously allocated cache
0112  * which has been allocated properly.
0113  */
0114 int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
0115                   const u8 request, const u8 requesttype,
0116                   const u16 offset, void *buffer,
0117                   const u16 buffer_length);
0118 
0119 /**
0120  * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
0121  * @rt2x00dev: Pointer to &struct rt2x00_dev
0122  * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
0123  * @requesttype: Request type &USB_VENDOR_REQUEST_*
0124  * @offset: Register offset to perform action on
0125  * @buffer: Buffer where information will be read/written to by device
0126  * @buffer_length: Size of &buffer
0127  * @timeout: Operation timeout
0128  *
0129  * A version of &rt2x00usb_vendor_request_buff which must be called
0130  * if the usb_cache_mutex is already held.
0131  */
0132 int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
0133                    const u8 request, const u8 requesttype,
0134                    const u16 offset, void *buffer,
0135                    const u16 buffer_length, const int timeout);
0136 
0137 /**
0138  * rt2x00usb_vendor_request_sw - Send single register command to device
0139  * @rt2x00dev: Pointer to &struct rt2x00_dev
0140  * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
0141  * @offset: Register offset to perform action on
0142  * @value: Value to write to device
0143  * @timeout: Operation timeout
0144  *
0145  * Simple wrapper around rt2x00usb_vendor_request to write a single
0146  * command to the device. Since we don't use the buffer argument we
0147  * don't have to worry about kmalloc here.
0148  */
0149 static inline int rt2x00usb_vendor_request_sw(struct rt2x00_dev *rt2x00dev,
0150                           const u8 request,
0151                           const u16 offset,
0152                           const u16 value,
0153                           const int timeout)
0154 {
0155     return rt2x00usb_vendor_request(rt2x00dev, request,
0156                     USB_VENDOR_REQUEST_OUT, offset,
0157                     value, NULL, 0, timeout);
0158 }
0159 
0160 /**
0161  * rt2x00usb_eeprom_read - Read eeprom from device
0162  * @rt2x00dev: Pointer to &struct rt2x00_dev
0163  * @eeprom: Pointer to eeprom array to store the information in
0164  * @length: Number of bytes to read from the eeprom
0165  *
0166  * Simple wrapper around rt2x00usb_vendor_request to read the eeprom
0167  * from the device. Note that the eeprom argument _must_ be allocated using
0168  * kmalloc for correct handling inside the kernel USB layer.
0169  */
0170 static inline int rt2x00usb_eeprom_read(struct rt2x00_dev *rt2x00dev,
0171                     __le16 *eeprom, const u16 length)
0172 {
0173     return rt2x00usb_vendor_request(rt2x00dev, USB_EEPROM_READ,
0174                     USB_VENDOR_REQUEST_IN, 0, 0,
0175                     eeprom, length, EEPROM_TIMEOUT);
0176 }
0177 
0178 /**
0179  * rt2x00usb_register_read - Read 32bit register word
0180  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0181  * @offset: Register offset
0182  *
0183  * This function is a simple wrapper for 32bit register access
0184  * through rt2x00usb_vendor_request_buff().
0185  */
0186 static inline u32 rt2x00usb_register_read(struct rt2x00_dev *rt2x00dev,
0187                       const unsigned int offset)
0188 {
0189     __le32 reg = 0;
0190     rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
0191                       USB_VENDOR_REQUEST_IN, offset,
0192                       &reg, sizeof(reg));
0193     return le32_to_cpu(reg);
0194 }
0195 
0196 /**
0197  * rt2x00usb_register_read_lock - Read 32bit register word
0198  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0199  * @offset: Register offset
0200  *
0201  * This function is a simple wrapper for 32bit register access
0202  * through rt2x00usb_vendor_req_buff_lock().
0203  */
0204 static inline u32 rt2x00usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
0205                            const unsigned int offset)
0206 {
0207     __le32 reg = 0;
0208     rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
0209                        USB_VENDOR_REQUEST_IN, offset,
0210                        &reg, sizeof(reg), REGISTER_TIMEOUT);
0211     return le32_to_cpu(reg);
0212 }
0213 
0214 /**
0215  * rt2x00usb_register_multiread - Read 32bit register words
0216  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0217  * @offset: Register offset
0218  * @value: Pointer to where register contents should be stored
0219  * @length: Length of the data
0220  *
0221  * This function is a simple wrapper for 32bit register access
0222  * through rt2x00usb_vendor_request_buff().
0223  */
0224 static inline void rt2x00usb_register_multiread(struct rt2x00_dev *rt2x00dev,
0225                         const unsigned int offset,
0226                         void *value, const u32 length)
0227 {
0228     rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
0229                       USB_VENDOR_REQUEST_IN, offset,
0230                       value, length);
0231 }
0232 
0233 /**
0234  * rt2x00usb_register_write - Write 32bit register word
0235  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0236  * @offset: Register offset
0237  * @value: Data which should be written
0238  *
0239  * This function is a simple wrapper for 32bit register access
0240  * through rt2x00usb_vendor_request_buff().
0241  */
0242 static inline void rt2x00usb_register_write(struct rt2x00_dev *rt2x00dev,
0243                         const unsigned int offset,
0244                         u32 value)
0245 {
0246     __le32 reg = cpu_to_le32(value);
0247     rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
0248                       USB_VENDOR_REQUEST_OUT, offset,
0249                       &reg, sizeof(reg));
0250 }
0251 
0252 /**
0253  * rt2x00usb_register_write_lock - Write 32bit register word
0254  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0255  * @offset: Register offset
0256  * @value: Data which should be written
0257  *
0258  * This function is a simple wrapper for 32bit register access
0259  * through rt2x00usb_vendor_req_buff_lock().
0260  */
0261 static inline void rt2x00usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
0262                          const unsigned int offset,
0263                          u32 value)
0264 {
0265     __le32 reg = cpu_to_le32(value);
0266     rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
0267                        USB_VENDOR_REQUEST_OUT, offset,
0268                        &reg, sizeof(reg), REGISTER_TIMEOUT);
0269 }
0270 
0271 /**
0272  * rt2x00usb_register_multiwrite - Write 32bit register words
0273  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0274  * @offset: Register offset
0275  * @value: Data which should be written
0276  * @length: Length of the data
0277  *
0278  * This function is a simple wrapper for 32bit register access
0279  * through rt2x00usb_vendor_request_buff().
0280  */
0281 static inline void rt2x00usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
0282                          const unsigned int offset,
0283                          const void *value,
0284                          const u32 length)
0285 {
0286     rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
0287                       USB_VENDOR_REQUEST_OUT, offset,
0288                       (void *)value, length);
0289 }
0290 
0291 /**
0292  * rt2x00usb_regbusy_read - Read from register with busy check
0293  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0294  * @offset: Register offset
0295  * @field: Field to check if register is busy
0296  * @reg: Pointer to where register contents should be stored
0297  *
0298  * This function will read the given register, and checks if the
0299  * register is busy. If it is, it will sleep for a couple of
0300  * microseconds before reading the register again. If the register
0301  * is not read after a certain timeout, this function will return
0302  * FALSE.
0303  */
0304 int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
0305                const unsigned int offset,
0306                const struct rt2x00_field32 field,
0307                u32 *reg);
0308 
0309 /**
0310  * rt2x00usb_register_read_async - Asynchronously read 32bit register word
0311  * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
0312  * @offset: Register offset
0313  * @callback: Functon to call when read completes.
0314  *
0315  * Submit a control URB to read a 32bit register. This safe to
0316  * be called from atomic context.  The callback will be called
0317  * when the URB completes. Otherwise the function is similar
0318  * to rt2x00usb_register_read().
0319  * When the callback function returns false, the memory will be cleaned up,
0320  * when it returns true, the urb will be fired again.
0321  */
0322 void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
0323                    const unsigned int offset,
0324                    bool (*callback)(struct rt2x00_dev*, int, u32));
0325 
0326 /*
0327  * Radio handlers
0328  */
0329 void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev);
0330 
0331 /**
0332  * struct queue_entry_priv_usb: Per entry USB specific information
0333  *
0334  * @urb: Urb structure used for device communication.
0335  */
0336 struct queue_entry_priv_usb {
0337     struct urb *urb;
0338 };
0339 
0340 /**
0341  * struct queue_entry_priv_usb_bcn: Per TX entry USB specific information
0342  *
0343  * The first section should match &struct queue_entry_priv_usb exactly.
0344  * rt2500usb can use this structure to send a guardian byte when working
0345  * with beacons.
0346  *
0347  * @urb: Urb structure used for device communication.
0348  * @guardian_data: Set to 0, used for sending the guardian data.
0349  * @guardian_urb: Urb structure used to send the guardian data.
0350  */
0351 struct queue_entry_priv_usb_bcn {
0352     struct urb *urb;
0353 
0354     unsigned int guardian_data;
0355     struct urb *guardian_urb;
0356 };
0357 
0358 /**
0359  * rt2x00usb_kick_queue - Kick data queue
0360  * @queue: Data queue to kick
0361  *
0362  * This will walk through all entries of the queue and push all pending
0363  * frames to the hardware as a single burst.
0364  */
0365 void rt2x00usb_kick_queue(struct data_queue *queue);
0366 
0367 /**
0368  * rt2x00usb_flush_queue - Flush data queue
0369  * @queue: Data queue to stop
0370  * @drop: True to drop all pending frames.
0371  *
0372  * This will walk through all entries of the queue and will optionally
0373  * kill all URB's which were send to the device, or at least wait until
0374  * they have been returned from the device..
0375  */
0376 void rt2x00usb_flush_queue(struct data_queue *queue, bool drop);
0377 
0378 /**
0379  * rt2x00usb_watchdog - Watchdog for USB communication
0380  * @rt2x00dev: Pointer to &struct rt2x00_dev
0381  *
0382  * Check the health of the USB communication and determine
0383  * if timeouts have occurred. If this is the case, this function
0384  * will reset all communication to restore functionality again.
0385  */
0386 void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev);
0387 
0388 /*
0389  * Device initialization handlers.
0390  */
0391 void rt2x00usb_clear_entry(struct queue_entry *entry);
0392 int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev);
0393 void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev);
0394 
0395 /*
0396  * USB driver handlers.
0397  */
0398 int rt2x00usb_probe(struct usb_interface *usb_intf,
0399             const struct rt2x00_ops *ops);
0400 void rt2x00usb_disconnect(struct usb_interface *usb_intf);
0401 #ifdef CONFIG_PM
0402 int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state);
0403 int rt2x00usb_resume(struct usb_interface *usb_intf);
0404 #else
0405 #define rt2x00usb_suspend   NULL
0406 #define rt2x00usb_resume    NULL
0407 #endif /* CONFIG_PM */
0408 
0409 #endif /* RT2X00USB_H */