rt2x00usb.h 14 KB

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