rt2x00usb.h 14 KB

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