rt2x00usb.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_large_buff - Send register command to device (buffered)
  152. * @rt2x00dev: Pointer to &struct rt2x00_dev
  153. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  154. * @requesttype: Request type &USB_VENDOR_REQUEST_*
  155. * @offset: Register start offset to perform action on
  156. * @buffer: Buffer where information will be read/written to by device
  157. * @buffer_length: Size of &buffer
  158. * @timeout: Operation timeout
  159. *
  160. * This function is used to transfer register data in blocks larger
  161. * then CSR_CACHE_SIZE. Use for firmware upload, keys and beacons.
  162. */
  163. int rt2x00usb_vendor_request_large_buff(struct rt2x00_dev *rt2x00dev,
  164. const u8 request, const u8 requesttype,
  165. const u16 offset, const void *buffer,
  166. const u16 buffer_length,
  167. const int timeout);
  168. /**
  169. * rt2x00usb_vendor_request_sw - Send single register command to device
  170. * @rt2x00dev: Pointer to &struct rt2x00_dev
  171. * @request: USB vendor command (See &enum rt2x00usb_vendor_request)
  172. * @offset: Register offset to perform action on
  173. * @value: Value to write to device
  174. * @timeout: Operation timeout
  175. *
  176. * Simple wrapper around rt2x00usb_vendor_request to write a single
  177. * command to the device. Since we don't use the buffer argument we
  178. * don't have to worry about kmalloc here.
  179. */
  180. static inline int rt2x00usb_vendor_request_sw(struct rt2x00_dev *rt2x00dev,
  181. const u8 request,
  182. const u16 offset,
  183. const u16 value,
  184. const int timeout)
  185. {
  186. return rt2x00usb_vendor_request(rt2x00dev, request,
  187. USB_VENDOR_REQUEST_OUT, offset,
  188. value, NULL, 0, timeout);
  189. }
  190. /**
  191. * rt2x00usb_eeprom_read - Read eeprom from device
  192. * @rt2x00dev: Pointer to &struct rt2x00_dev
  193. * @eeprom: Pointer to eeprom array to store the information in
  194. * @length: Number of bytes to read from the eeprom
  195. *
  196. * Simple wrapper around rt2x00usb_vendor_request to read the eeprom
  197. * from the device. Note that the eeprom argument _must_ be allocated using
  198. * kmalloc for correct handling inside the kernel USB layer.
  199. */
  200. static inline int rt2x00usb_eeprom_read(struct rt2x00_dev *rt2x00dev,
  201. __le16 *eeprom, const u16 length)
  202. {
  203. return rt2x00usb_vendor_request(rt2x00dev, USB_EEPROM_READ,
  204. USB_VENDOR_REQUEST_IN, 0, 0,
  205. eeprom, length,
  206. REGISTER_TIMEOUT16(length));
  207. }
  208. /**
  209. * rt2x00usb_register_read - Read 32bit register word
  210. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  211. * @offset: Register offset
  212. * @value: Pointer to where register contents should be stored
  213. *
  214. * This function is a simple wrapper for 32bit register access
  215. * through rt2x00usb_vendor_request_buff().
  216. */
  217. static inline void rt2x00usb_register_read(struct rt2x00_dev *rt2x00dev,
  218. const unsigned int offset,
  219. u32 *value)
  220. {
  221. __le32 reg;
  222. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  223. USB_VENDOR_REQUEST_IN, offset,
  224. &reg, sizeof(reg), REGISTER_TIMEOUT);
  225. *value = le32_to_cpu(reg);
  226. }
  227. /**
  228. * rt2x00usb_register_read_lock - Read 32bit register word
  229. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  230. * @offset: Register offset
  231. * @value: Pointer to where register contents should be stored
  232. *
  233. * This function is a simple wrapper for 32bit register access
  234. * through rt2x00usb_vendor_req_buff_lock().
  235. */
  236. static inline void rt2x00usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
  237. const unsigned int offset,
  238. u32 *value)
  239. {
  240. __le32 reg;
  241. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
  242. USB_VENDOR_REQUEST_IN, offset,
  243. &reg, sizeof(reg), REGISTER_TIMEOUT);
  244. *value = le32_to_cpu(reg);
  245. }
  246. /**
  247. * rt2x00usb_register_multiread - Read 32bit register words
  248. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  249. * @offset: Register offset
  250. * @value: Pointer to where register contents should be stored
  251. * @length: Length of the data
  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_multiread(struct rt2x00_dev *rt2x00dev,
  257. const unsigned int offset,
  258. void *value, const u32 length)
  259. {
  260. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
  261. USB_VENDOR_REQUEST_IN, offset,
  262. value, length,
  263. REGISTER_TIMEOUT32(length));
  264. }
  265. /**
  266. * rt2x00usb_register_write - 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_request_buff().
  273. */
  274. static inline void rt2x00usb_register_write(struct rt2x00_dev *rt2x00dev,
  275. const unsigned int offset,
  276. u32 value)
  277. {
  278. __le32 reg = cpu_to_le32(value);
  279. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  280. USB_VENDOR_REQUEST_OUT, offset,
  281. &reg, sizeof(reg), REGISTER_TIMEOUT);
  282. }
  283. /**
  284. * rt2x00usb_register_write_lock - Write 32bit register word
  285. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  286. * @offset: Register offset
  287. * @value: Data which should be written
  288. *
  289. * This function is a simple wrapper for 32bit register access
  290. * through rt2x00usb_vendor_req_buff_lock().
  291. */
  292. static inline void rt2x00usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
  293. const unsigned int offset,
  294. u32 value)
  295. {
  296. __le32 reg = cpu_to_le32(value);
  297. rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
  298. USB_VENDOR_REQUEST_OUT, offset,
  299. &reg, sizeof(reg), REGISTER_TIMEOUT);
  300. }
  301. /**
  302. * rt2x00usb_register_multiwrite - Write 32bit register words
  303. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  304. * @offset: Register offset
  305. * @value: Data which should be written
  306. * @length: Length of the data
  307. *
  308. * This function is a simple wrapper for 32bit register access
  309. * through rt2x00usb_vendor_request_buff().
  310. */
  311. static inline void rt2x00usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
  312. const unsigned int offset,
  313. const void *value,
  314. const u32 length)
  315. {
  316. rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
  317. USB_VENDOR_REQUEST_OUT, offset,
  318. (void *)value, length,
  319. REGISTER_TIMEOUT32(length));
  320. }
  321. /**
  322. * rt2x00usb_regbusy_read - Read from register with busy check
  323. * @rt2x00dev: Device pointer, see &struct rt2x00_dev.
  324. * @offset: Register offset
  325. * @field: Field to check if register is busy
  326. * @reg: Pointer to where register contents should be stored
  327. *
  328. * This function will read the given register, and checks if the
  329. * register is busy. If it is, it will sleep for a couple of
  330. * microseconds before reading the register again. If the register
  331. * is not read after a certain timeout, this function will return
  332. * FALSE.
  333. */
  334. int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
  335. const unsigned int offset,
  336. const struct rt2x00_field32 field,
  337. u32 *reg);
  338. /*
  339. * Radio handlers
  340. */
  341. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev);
  342. /**
  343. * rt2x00usb_write_tx_data - Initialize URB for TX operation
  344. * @entry: The entry where the frame is located
  345. *
  346. * This function will initialize the URB and skb descriptor
  347. * to prepare the entry for the actual TX operation.
  348. */
  349. int rt2x00usb_write_tx_data(struct queue_entry *entry);
  350. /**
  351. * struct queue_entry_priv_usb: Per entry USB specific information
  352. *
  353. * @urb: Urb structure used for device communication.
  354. */
  355. struct queue_entry_priv_usb {
  356. struct urb *urb;
  357. };
  358. /**
  359. * struct queue_entry_priv_usb_bcn: Per TX entry USB specific information
  360. *
  361. * The first section should match &struct queue_entry_priv_usb exactly.
  362. * rt2500usb can use this structure to send a guardian byte when working
  363. * with beacons.
  364. *
  365. * @urb: Urb structure used for device communication.
  366. * @guardian_data: Set to 0, used for sending the guardian data.
  367. * @guardian_urb: Urb structure used to send the guardian data.
  368. */
  369. struct queue_entry_priv_usb_bcn {
  370. struct urb *urb;
  371. unsigned int guardian_data;
  372. struct urb *guardian_urb;
  373. };
  374. /**
  375. * rt2x00usb_kick_tx_queue - Kick data queue
  376. * @rt2x00dev: Pointer to &struct rt2x00_dev
  377. * @qid: Data queue to kick
  378. *
  379. * This will walk through all entries of the queue and push all pending
  380. * frames to the hardware as a single burst.
  381. */
  382. void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  383. const enum data_queue_qid qid);
  384. /**
  385. * rt2x00usb_kill_tx_queue - Kill data queue
  386. * @rt2x00dev: Pointer to &struct rt2x00_dev
  387. * @qid: Data queue to kill
  388. *
  389. * This will walk through all entries of the queue and kill all
  390. * previously kicked frames before they can be send.
  391. */
  392. void rt2x00usb_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
  393. const enum data_queue_qid qid);
  394. /*
  395. * Device initialization handlers.
  396. */
  397. void rt2x00usb_clear_entry(struct queue_entry *entry);
  398. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev);
  399. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev);
  400. /*
  401. * USB driver handlers.
  402. */
  403. int rt2x00usb_probe(struct usb_interface *usb_intf,
  404. const struct usb_device_id *id);
  405. void rt2x00usb_disconnect(struct usb_interface *usb_intf);
  406. #ifdef CONFIG_PM
  407. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state);
  408. int rt2x00usb_resume(struct usb_interface *usb_intf);
  409. #else
  410. #define rt2x00usb_suspend NULL
  411. #define rt2x00usb_resume NULL
  412. #endif /* CONFIG_PM */
  413. #endif /* RT2X00USB_H */