zd_usb.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _ZD_USB_H
  20. #define _ZD_USB_H
  21. #include <linux/completion.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/usb.h>
  26. #include "zd_def.h"
  27. #define ZD_USB_TX_HIGH 5
  28. #define ZD_USB_TX_LOW 2
  29. #define ZD_TX_TIMEOUT (HZ * 5)
  30. #define ZD_TX_WATCHDOG_INTERVAL round_jiffies_relative(HZ)
  31. #define ZD_RX_IDLE_INTERVAL round_jiffies_relative(30 * HZ)
  32. enum devicetype {
  33. DEVICE_ZD1211 = 0,
  34. DEVICE_ZD1211B = 1,
  35. DEVICE_INSTALLER = 2,
  36. };
  37. enum endpoints {
  38. EP_CTRL = 0,
  39. EP_DATA_OUT = 1,
  40. EP_DATA_IN = 2,
  41. EP_INT_IN = 3,
  42. EP_REGS_OUT = 4,
  43. };
  44. enum {
  45. USB_MAX_TRANSFER_SIZE = 4096, /* bytes */
  46. /* FIXME: The original driver uses this value. We have to check,
  47. * whether the MAX_TRANSFER_SIZE is sufficient and this needs only be
  48. * used if one combined frame is split over two USB transactions.
  49. */
  50. USB_MAX_RX_SIZE = 4800, /* bytes */
  51. USB_MAX_IOWRITE16_COUNT = 15,
  52. USB_MAX_IOWRITE32_COUNT = USB_MAX_IOWRITE16_COUNT/2,
  53. USB_MAX_IOREAD16_COUNT = 15,
  54. USB_MAX_IOREAD32_COUNT = USB_MAX_IOREAD16_COUNT/2,
  55. USB_MIN_RFWRITE_BIT_COUNT = 16,
  56. USB_MAX_RFWRITE_BIT_COUNT = 28,
  57. USB_MAX_EP_INT_BUFFER = 64,
  58. USB_ZD1211B_BCD_DEVICE = 0x4810,
  59. };
  60. enum control_requests {
  61. USB_REQ_WRITE_REGS = 0x21,
  62. USB_REQ_READ_REGS = 0x22,
  63. USB_REQ_WRITE_RF = 0x23,
  64. USB_REQ_PROG_FLASH = 0x24,
  65. USB_REQ_EEPROM_START = 0x0128, /* ? request is a byte */
  66. USB_REQ_EEPROM_MID = 0x28,
  67. USB_REQ_EEPROM_END = 0x0228, /* ? request is a byte */
  68. USB_REQ_FIRMWARE_DOWNLOAD = 0x30,
  69. USB_REQ_FIRMWARE_CONFIRM = 0x31,
  70. USB_REQ_FIRMWARE_READ_DATA = 0x32,
  71. };
  72. struct usb_req_read_regs {
  73. __le16 id;
  74. __le16 addr[0];
  75. } __packed;
  76. struct reg_data {
  77. __le16 addr;
  78. __le16 value;
  79. } __packed;
  80. struct usb_req_write_regs {
  81. __le16 id;
  82. struct reg_data reg_writes[0];
  83. } __packed;
  84. enum {
  85. RF_IF_LE = 0x02,
  86. RF_CLK = 0x04,
  87. RF_DATA = 0x08,
  88. };
  89. struct usb_req_rfwrite {
  90. __le16 id;
  91. __le16 value;
  92. /* 1: 3683a */
  93. /* 2: other (default) */
  94. __le16 bits;
  95. /* RF2595: 24 */
  96. __le16 bit_values[0];
  97. /* (ZD_CR203 & ~(RF_IF_LE | RF_CLK | RF_DATA)) | (bit ? RF_DATA : 0) */
  98. } __packed;
  99. /* USB interrupt */
  100. enum usb_int_id {
  101. USB_INT_TYPE = 0x01,
  102. USB_INT_ID_REGS = 0x90,
  103. USB_INT_ID_RETRY_FAILED = 0xa0,
  104. };
  105. enum usb_int_flags {
  106. USB_INT_READ_REGS_EN = 0x01,
  107. };
  108. struct usb_int_header {
  109. u8 type; /* must always be 1 */
  110. u8 id;
  111. } __packed;
  112. struct usb_int_regs {
  113. struct usb_int_header hdr;
  114. struct reg_data regs[0];
  115. } __packed;
  116. struct usb_int_retry_fail {
  117. struct usb_int_header hdr;
  118. u8 new_rate;
  119. u8 _dummy;
  120. u8 addr[ETH_ALEN];
  121. u8 ibss_wakeup_dest;
  122. } __packed;
  123. struct read_regs_int {
  124. struct completion completion;
  125. struct usb_req_read_regs *req;
  126. unsigned int req_count;
  127. /* Stores the USB int structure and contains the USB address of the
  128. * first requested register before request.
  129. */
  130. u8 buffer[USB_MAX_EP_INT_BUFFER];
  131. int length;
  132. __le16 cr_int_addr;
  133. };
  134. struct zd_ioreq16 {
  135. zd_addr_t addr;
  136. u16 value;
  137. };
  138. struct zd_ioreq32 {
  139. zd_addr_t addr;
  140. u32 value;
  141. };
  142. struct zd_usb_interrupt {
  143. struct read_regs_int read_regs;
  144. spinlock_t lock;
  145. struct urb *urb;
  146. void *buffer;
  147. dma_addr_t buffer_dma;
  148. int interval;
  149. atomic_t read_regs_enabled;
  150. u8 read_regs_int_overridden:1;
  151. };
  152. static inline struct usb_int_regs *get_read_regs(struct zd_usb_interrupt *intr)
  153. {
  154. return (struct usb_int_regs *)intr->read_regs.buffer;
  155. }
  156. #define RX_URBS_COUNT 5
  157. struct zd_usb_rx {
  158. spinlock_t lock;
  159. struct mutex setup_mutex;
  160. struct delayed_work idle_work;
  161. struct tasklet_struct reset_timer_tasklet;
  162. u8 fragment[2 * USB_MAX_RX_SIZE];
  163. unsigned int fragment_length;
  164. unsigned int usb_packet_size;
  165. struct urb **urbs;
  166. int urbs_count;
  167. };
  168. /**
  169. * struct zd_usb_tx - structure used for transmitting frames
  170. * @enabled: atomic enabled flag, indicates whether tx is enabled
  171. * @lock: lock for transmission
  172. * @submitted: anchor for URBs sent to device
  173. * @submitted_urbs: atomic integer that counts the URBs having sent to the
  174. * device, which haven't been completed
  175. * @stopped: indicates whether higher level tx queues are stopped
  176. */
  177. struct zd_usb_tx {
  178. atomic_t enabled;
  179. spinlock_t lock;
  180. struct delayed_work watchdog_work;
  181. struct sk_buff_head submitted_skbs;
  182. struct usb_anchor submitted;
  183. int submitted_urbs;
  184. u8 stopped:1, watchdog_enabled:1;
  185. };
  186. /* Contains the usb parts. The structure doesn't require a lock because intf
  187. * will not be changed after initialization.
  188. */
  189. struct zd_usb {
  190. struct zd_usb_interrupt intr;
  191. struct zd_usb_rx rx;
  192. struct zd_usb_tx tx;
  193. struct usb_interface *intf;
  194. struct usb_anchor submitted_cmds;
  195. struct urb *urb_async_waiting;
  196. int cmd_error;
  197. u8 req_buf[64]; /* zd_usb_iowrite16v needs 62 bytes */
  198. u8 is_zd1211b:1, initialized:1, was_running:1, in_async:1;
  199. };
  200. #define zd_usb_dev(usb) (&usb->intf->dev)
  201. static inline struct usb_device *zd_usb_to_usbdev(struct zd_usb *usb)
  202. {
  203. return interface_to_usbdev(usb->intf);
  204. }
  205. static inline struct ieee80211_hw *zd_intf_to_hw(struct usb_interface *intf)
  206. {
  207. return usb_get_intfdata(intf);
  208. }
  209. static inline struct ieee80211_hw *zd_usb_to_hw(struct zd_usb *usb)
  210. {
  211. return zd_intf_to_hw(usb->intf);
  212. }
  213. void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
  214. struct usb_interface *intf);
  215. int zd_usb_init_hw(struct zd_usb *usb);
  216. void zd_usb_clear(struct zd_usb *usb);
  217. int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size);
  218. void zd_tx_watchdog_enable(struct zd_usb *usb);
  219. void zd_tx_watchdog_disable(struct zd_usb *usb);
  220. int zd_usb_enable_int(struct zd_usb *usb);
  221. void zd_usb_disable_int(struct zd_usb *usb);
  222. int zd_usb_enable_rx(struct zd_usb *usb);
  223. void zd_usb_disable_rx(struct zd_usb *usb);
  224. void zd_usb_reset_rx_idle_timer(struct zd_usb *usb);
  225. void zd_usb_enable_tx(struct zd_usb *usb);
  226. void zd_usb_disable_tx(struct zd_usb *usb);
  227. int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb);
  228. int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
  229. const zd_addr_t *addresses, unsigned int count);
  230. static inline int zd_usb_ioread16(struct zd_usb *usb, u16 *value,
  231. const zd_addr_t addr)
  232. {
  233. return zd_usb_ioread16v(usb, value, &addr, 1);
  234. }
  235. void zd_usb_iowrite16v_async_start(struct zd_usb *usb);
  236. int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout);
  237. int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
  238. unsigned int count);
  239. int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
  240. unsigned int count);
  241. int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits);
  242. int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len);
  243. extern struct workqueue_struct *zd_workqueue;
  244. #endif /* _ZD_USB_H */