rt2x00usb.h 15 KB

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