rt2x00usb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  4. <http://rt2x00.serialmonkey.com>
  5. */
  6. /*
  7. Module: rt2x00usb
  8. Abstract: Data structures for the rt2x00usb module.
  9. */
  10. #ifndef RT2X00USB_H
  11. #define RT2X00USB_H
  12. #include <linux/usb.h>
  13. #define to_usb_device_intf(d) \
  14. ({ \
  15. struct usb_interface *intf = to_usb_interface(d); \
  16. interface_to_usbdev(intf); \
  17. })
  18. /*
  19. * For USB vendor requests we need to pass a timeout time in ms, for this we
  20. * use the REGISTER_TIMEOUT, however when loading firmware or read EEPROM
  21. * a higher value is required. In that case we use the REGISTER_TIMEOUT_FIRMWARE
  22. * and EEPROM_TIMEOUT.
  23. */
  24. #define REGISTER_TIMEOUT 100
  25. #define REGISTER_TIMEOUT_FIRMWARE 1000
  26. #define EEPROM_TIMEOUT 2000
  27. /*
  28. * Cache size
  29. */
  30. #define CSR_CACHE_SIZE 64
  31. /*
  32. * USB request types.
  33. */
  34. #define USB_VENDOR_REQUEST ( USB_TYPE_VENDOR | USB_RECIP_DEVICE )
  35. #define USB_VENDOR_REQUEST_IN ( USB_DIR_IN | USB_VENDOR_REQUEST )
  36. #define USB_VENDOR_REQUEST_OUT ( USB_DIR_OUT | USB_VENDOR_REQUEST )
  37. /**
  38. * enum rt2x00usb_vendor_request: USB vendor commands.
  39. */
  40. enum rt2x00usb_vendor_request {
  41. USB_DEVICE_MODE = 1,
  42. USB_SINGLE_WRITE = 2,
  43. USB_SINGLE_READ = 3,
  44. USB_MULTI_WRITE = 6,
  45. USB_MULTI_READ = 7,
  46. USB_EEPROM_WRITE = 8,
  47. USB_EEPROM_READ = 9,
  48. USB_LED_CONTROL = 10, /* RT73USB */
  49. USB_RX_CONTROL = 12,
  50. };
  51. /**
  52. * enum rt2x00usb_mode_offset: Device modes offset.
  53. */
  54. enum rt2x00usb_mode_offset {
  55. USB_MODE_RESET = 1,
  56. USB_MODE_UNPLUG = 2,
  57. USB_MODE_FUNCTION = 3,
  58. USB_MODE_TEST = 4,
  59. USB_MODE_SLEEP = 7, /* RT73USB */
  60. USB_MODE_FIRMWARE = 8, /* RT73USB */
  61. USB_MODE_WAKEUP = 9, /* RT73USB */
  62. USB_MODE_AUTORUN = 17, /* RT2800USB */
  63. };
  64. /**
  65. * rt2x00usb_vendor_request - Send register command to device
  66. * @rt2x00dev: Pointer to &struct rt2x00_dev
  67. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  68. * @requesttype: Request type &USB_VENDOR_REQUEST_*
  69. * @offset: Register offset to perform action on
  70. * @value: Value to write to device
  71. * @buffer: Buffer where information will be read/written to by device
  72. * @buffer_length: Size of &buffer
  73. * @timeout: Operation timeout
  74. *
  75. * This is the main function to communicate with the device,
  76. * the &buffer argument _must_ either be NULL or point to
  77. * a buffer allocated by kmalloc. Failure to do so can lead
  78. * to unexpected behavior depending on the architecture.
  79. */
  80. int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  81. const u8 request, const u8 requesttype,
  82. const u16 offset, const u16 value,
  83. void *buffer, const u16 buffer_length,
  84. const int timeout);
  85. /**
  86. * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
  87. * @rt2x00dev: Pointer to &struct rt2x00_dev
  88. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  89. * @requesttype: Request type &USB_VENDOR_REQUEST_*
  90. * @offset: Register offset to perform action on
  91. * @buffer: Buffer where information will be read/written to by device
  92. * @buffer_length: Size of &buffer
  93. *
  94. * This function will use a previously with kmalloc allocated cache
  95. * to communicate with the device. The contents of the buffer pointer
  96. * will be copied to this cache when writing, or read from the cache
  97. * when reading.
  98. * Buffers send to &rt2x00usb_vendor_request _must_ be allocated with
  99. * kmalloc. Hence the reason for using a previously allocated cache
  100. * which has been allocated properly.
  101. */
  102. int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
  103. const u8 request, const u8 requesttype,
  104. const u16 offset, void *buffer,
  105. const u16 buffer_length);
  106. /**
  107. * rt2x00usb_vendor_request_buff - Send register command to device (buffered)
  108. * @rt2x00dev: Pointer to &struct rt2x00_dev
  109. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  110. * @requesttype: Request type &USB_VENDOR_REQUEST_*
  111. * @offset: Register offset to perform action on
  112. * @buffer: Buffer where information will be read/written to by device
  113. * @buffer_length: Size of &buffer
  114. * @timeout: Operation timeout
  115. *
  116. * A version of &rt2x00usb_vendor_request_buff which must be called
  117. * if the usb_cache_mutex is already held.
  118. */
  119. int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
  120. const u8 request, const u8 requesttype,
  121. const u16 offset, void *buffer,
  122. const u16 buffer_length, const int timeout);
  123. /**
  124. * rt2x00usb_vendor_request_sw - Send single register command to device
  125. * @rt2x00dev: Pointer to &struct rt2x00_dev
  126. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  127. * @offset: Register offset to perform action on
  128. * @value: Value to write to device
  129. * @timeout: Operation timeout
  130. *
  131. * Simple wrapper around rt2x00usb_vendor_request to write a single
  132. * command to the device. Since we don't use the buffer argument we
  133. * don't have to worry about kmalloc here.
  134. */
  135. static inline int rt2x00usb_vendor_request_sw(struct rt2x00_dev *rt2x00dev,
  136. const u8 request,
  137. const u16 offset,
  138. const u16 value,
  139. const int timeout)
  140. {
  141. return rt2x00usb_vendor_request(rt2x00dev, request,
  142. USB_VENDOR_REQUEST_OUT, offset,
  143. value, NULL, 0, timeout);
  144. }
  145. /**
  146. * rt2x00usb_eeprom_read - Read eeprom from device
  147. * @rt2x00dev: Pointer to &struct rt2x00_dev
  148. * @eeprom: Pointer to eeprom array to store the information in
  149. * @length: Number of bytes to read from the eeprom
  150. *
  151. * Simple wrapper around rt2x00usb_vendor_request to read the eeprom
  152. * from the device. Note that the eeprom argument _must_ be allocated using
  153. * kmalloc for correct handling inside the kernel USB layer.
  154. */
  155. static inline int rt2x00usb_eeprom_read(struct rt2x00_dev *rt2x00dev,
  156. __le16 *eeprom, const u16 length)
  157. {
  158. return rt2x00usb_vendor_request(rt2x00dev, USB_EEPROM_READ,
  159. USB_VENDOR_REQUEST_IN, 0, 0,
  160. eeprom, length, EEPROM_TIMEOUT);
  161. }
  162. /**
  163. * rt2x00usb_register_read - Read 32bit register word
  164. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  165. * @offset: Register offset
  166. *
  167. * This function is a simple wrapper for 32bit register access
  168. * through rt2x00usb_vendor_request_buff().
  169. */
  170. static inline u32 rt2x00usb_register_read(struct rt2x00_dev *rt2x00dev,
  171. const unsigned int offset)
  172. {
  173. __le32 reg = 0;
  174. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  175. USB_VENDOR_REQUEST_IN, offset,
  176. &reg, sizeof(reg));
  177. return le32_to_cpu(reg);
  178. }
  179. /**
  180. * rt2x00usb_register_read_lock - Read 32bit register word
  181. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  182. * @offset: Register offset
  183. *
  184. * This function is a simple wrapper for 32bit register access
  185. * through rt2x00usb_vendor_req_buff_lock().
  186. */
  187. static inline u32 rt2x00usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
  188. const unsigned int offset)
  189. {
  190. __le32 reg = 0;
  191. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
  192. USB_VENDOR_REQUEST_IN, offset,
  193. &reg, sizeof(reg), REGISTER_TIMEOUT);
  194. return le32_to_cpu(reg);
  195. }
  196. /**
  197. * rt2x00usb_register_multiread - Read 32bit register words
  198. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  199. * @offset: Register offset
  200. * @value: Pointer to where register contents should be stored
  201. * @length: Length of the data
  202. *
  203. * This function is a simple wrapper for 32bit register access
  204. * through rt2x00usb_vendor_request_buff().
  205. */
  206. static inline void rt2x00usb_register_multiread(struct rt2x00_dev *rt2x00dev,
  207. const unsigned int offset,
  208. void *value, const u32 length)
  209. {
  210. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  211. USB_VENDOR_REQUEST_IN, offset,
  212. value, length);
  213. }
  214. /**
  215. * rt2x00usb_register_write - Write 32bit register word
  216. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  217. * @offset: Register offset
  218. * @value: Data which should be written
  219. *
  220. * This function is a simple wrapper for 32bit register access
  221. * through rt2x00usb_vendor_request_buff().
  222. */
  223. static inline void rt2x00usb_register_write(struct rt2x00_dev *rt2x00dev,
  224. const unsigned int offset,
  225. u32 value)
  226. {
  227. __le32 reg = cpu_to_le32(value);
  228. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  229. USB_VENDOR_REQUEST_OUT, offset,
  230. &reg, sizeof(reg));
  231. }
  232. /**
  233. * rt2x00usb_register_write_lock - Write 32bit register word
  234. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  235. * @offset: Register offset
  236. * @value: Data which should be written
  237. *
  238. * This function is a simple wrapper for 32bit register access
  239. * through rt2x00usb_vendor_req_buff_lock().
  240. */
  241. static inline void rt2x00usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
  242. const unsigned int offset,
  243. u32 value)
  244. {
  245. __le32 reg = cpu_to_le32(value);
  246. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
  247. USB_VENDOR_REQUEST_OUT, offset,
  248. &reg, sizeof(reg), REGISTER_TIMEOUT);
  249. }
  250. /**
  251. * rt2x00usb_register_multiwrite - Write 32bit register words
  252. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  253. * @offset: Register offset
  254. * @value: Data which should be written
  255. * @length: Length of the data
  256. *
  257. * This function is a simple wrapper for 32bit register access
  258. * through rt2x00usb_vendor_request_buff().
  259. */
  260. static inline void rt2x00usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
  261. const unsigned int offset,
  262. const void *value,
  263. const u32 length)
  264. {
  265. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  266. USB_VENDOR_REQUEST_OUT, offset,
  267. (void *)value, length);
  268. }
  269. /**
  270. * rt2x00usb_regbusy_read - Read from register with busy check
  271. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  272. * @offset: Register offset
  273. * @field: Field to check if register is busy
  274. * @reg: Pointer to where register contents should be stored
  275. *
  276. * This function will read the given register, and checks if the
  277. * register is busy. If it is, it will sleep for a couple of
  278. * microseconds before reading the register again. If the register
  279. * is not read after a certain timeout, this function will return
  280. * FALSE.
  281. */
  282. int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
  283. const unsigned int offset,
  284. const struct rt2x00_field32 field,
  285. u32 *reg);
  286. /**
  287. * rt2x00usb_register_read_async - Asynchronously read 32bit register word
  288. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  289. * @offset: Register offset
  290. * @callback: Functon to call when read completes.
  291. *
  292. * Submit a control URB to read a 32bit register. This safe to
  293. * be called from atomic context. The callback will be called
  294. * when the URB completes. Otherwise the function is similar
  295. * to rt2x00usb_register_read().
  296. * When the callback function returns false, the memory will be cleaned up,
  297. * when it returns true, the urb will be fired again.
  298. */
  299. void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
  300. const unsigned int offset,
  301. bool (*callback)(struct rt2x00_dev*, int, u32));
  302. /*
  303. * Radio handlers
  304. */
  305. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev);
  306. /**
  307. * struct queue_entry_priv_usb: Per entry USB specific information
  308. *
  309. * @urb: Urb structure used for device communication.
  310. */
  311. struct queue_entry_priv_usb {
  312. struct urb *urb;
  313. };
  314. /**
  315. * struct queue_entry_priv_usb_bcn: Per TX entry USB specific information
  316. *
  317. * The first section should match &struct queue_entry_priv_usb exactly.
  318. * rt2500usb can use this structure to send a guardian byte when working
  319. * with beacons.
  320. *
  321. * @urb: Urb structure used for device communication.
  322. * @guardian_data: Set to 0, used for sending the guardian data.
  323. * @guardian_urb: Urb structure used to send the guardian data.
  324. */
  325. struct queue_entry_priv_usb_bcn {
  326. struct urb *urb;
  327. unsigned int guardian_data;
  328. struct urb *guardian_urb;
  329. };
  330. /**
  331. * rt2x00usb_kick_queue - Kick data queue
  332. * @queue: Data queue to kick
  333. *
  334. * This will walk through all entries of the queue and push all pending
  335. * frames to the hardware as a single burst.
  336. */
  337. void rt2x00usb_kick_queue(struct data_queue *queue);
  338. /**
  339. * rt2x00usb_flush_queue - Flush data queue
  340. * @queue: Data queue to stop
  341. * @drop: True to drop all pending frames.
  342. *
  343. * This will walk through all entries of the queue and will optionally
  344. * kill all URB's which were send to the device, or at least wait until
  345. * they have been returned from the device..
  346. */
  347. void rt2x00usb_flush_queue(struct data_queue *queue, bool drop);
  348. /**
  349. * rt2x00usb_watchdog - Watchdog for USB communication
  350. * @rt2x00dev: Pointer to &struct rt2x00_dev
  351. *
  352. * Check the health of the USB communication and determine
  353. * if timeouts have occurred. If this is the case, this function
  354. * will reset all communication to restore functionality again.
  355. */
  356. void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev);
  357. /*
  358. * Device initialization handlers.
  359. */
  360. void rt2x00usb_clear_entry(struct queue_entry *entry);
  361. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev);
  362. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev);
  363. /*
  364. * USB driver handlers.
  365. */
  366. int rt2x00usb_probe(struct usb_interface *usb_intf,
  367. const struct rt2x00_ops *ops);
  368. void rt2x00usb_disconnect(struct usb_interface *usb_intf);
  369. #ifdef CONFIG_PM
  370. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state);
  371. int rt2x00usb_resume(struct usb_interface *usb_intf);
  372. #else
  373. #define rt2x00usb_suspend NULL
  374. #define rt2x00usb_resume NULL
  375. #endif /* CONFIG_PM */
  376. #endif /* RT2X00USB_H */