rt2x00usb.h 14 KB

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