rt2x00usb.h 14 KB

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