rt2x00usb.h 14 KB

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