rt2x00usb.h 14 KB

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