Daniel Drake | 66bb42f | 2007-11-19 16:20:12 +0000 | [diff] [blame] | 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> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 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 |
Jeff Kirsher | f47cdae | 2013-12-06 03:32:10 -0800 | [diff] [blame^] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifndef _ZD_USB_H |
| 21 | #define _ZD_USB_H |
| 22 | |
| 23 | #include <linux/completion.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/skbuff.h> |
| 27 | #include <linux/usb.h> |
| 28 | |
| 29 | #include "zd_def.h" |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 30 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 31 | #define ZD_USB_TX_HIGH 5 |
| 32 | #define ZD_USB_TX_LOW 2 |
| 33 | |
Jussi Kivilinna | a0fd751 | 2011-01-31 20:49:52 +0200 | [diff] [blame] | 34 | #define ZD_TX_TIMEOUT (HZ * 5) |
| 35 | #define ZD_TX_WATCHDOG_INTERVAL round_jiffies_relative(HZ) |
Jussi Kivilinna | 1f6cccc | 2011-01-31 20:50:12 +0200 | [diff] [blame] | 36 | #define ZD_RX_IDLE_INTERVAL round_jiffies_relative(30 * HZ) |
Jussi Kivilinna | a0fd751 | 2011-01-31 20:49:52 +0200 | [diff] [blame] | 37 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 38 | enum devicetype { |
| 39 | DEVICE_ZD1211 = 0, |
| 40 | DEVICE_ZD1211B = 1, |
Daniel Drake | a1030e9 | 2006-08-13 12:15:29 +0100 | [diff] [blame] | 41 | DEVICE_INSTALLER = 2, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | enum endpoints { |
| 45 | EP_CTRL = 0, |
| 46 | EP_DATA_OUT = 1, |
| 47 | EP_DATA_IN = 2, |
| 48 | EP_INT_IN = 3, |
| 49 | EP_REGS_OUT = 4, |
| 50 | }; |
| 51 | |
| 52 | enum { |
| 53 | USB_MAX_TRANSFER_SIZE = 4096, /* bytes */ |
| 54 | /* FIXME: The original driver uses this value. We have to check, |
| 55 | * whether the MAX_TRANSFER_SIZE is sufficient and this needs only be |
| 56 | * used if one combined frame is split over two USB transactions. |
| 57 | */ |
| 58 | USB_MAX_RX_SIZE = 4800, /* bytes */ |
| 59 | USB_MAX_IOWRITE16_COUNT = 15, |
| 60 | USB_MAX_IOWRITE32_COUNT = USB_MAX_IOWRITE16_COUNT/2, |
| 61 | USB_MAX_IOREAD16_COUNT = 15, |
| 62 | USB_MAX_IOREAD32_COUNT = USB_MAX_IOREAD16_COUNT/2, |
| 63 | USB_MIN_RFWRITE_BIT_COUNT = 16, |
| 64 | USB_MAX_RFWRITE_BIT_COUNT = 28, |
| 65 | USB_MAX_EP_INT_BUFFER = 64, |
| 66 | USB_ZD1211B_BCD_DEVICE = 0x4810, |
| 67 | }; |
| 68 | |
| 69 | enum control_requests { |
| 70 | USB_REQ_WRITE_REGS = 0x21, |
| 71 | USB_REQ_READ_REGS = 0x22, |
| 72 | USB_REQ_WRITE_RF = 0x23, |
| 73 | USB_REQ_PROG_FLASH = 0x24, |
| 74 | USB_REQ_EEPROM_START = 0x0128, /* ? request is a byte */ |
| 75 | USB_REQ_EEPROM_MID = 0x28, |
| 76 | USB_REQ_EEPROM_END = 0x0228, /* ? request is a byte */ |
| 77 | USB_REQ_FIRMWARE_DOWNLOAD = 0x30, |
| 78 | USB_REQ_FIRMWARE_CONFIRM = 0x31, |
| 79 | USB_REQ_FIRMWARE_READ_DATA = 0x32, |
| 80 | }; |
| 81 | |
| 82 | struct usb_req_read_regs { |
| 83 | __le16 id; |
| 84 | __le16 addr[0]; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 85 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 86 | |
| 87 | struct reg_data { |
| 88 | __le16 addr; |
| 89 | __le16 value; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 90 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 91 | |
| 92 | struct usb_req_write_regs { |
| 93 | __le16 id; |
| 94 | struct reg_data reg_writes[0]; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 95 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 96 | |
| 97 | enum { |
| 98 | RF_IF_LE = 0x02, |
| 99 | RF_CLK = 0x04, |
| 100 | RF_DATA = 0x08, |
| 101 | }; |
| 102 | |
| 103 | struct usb_req_rfwrite { |
| 104 | __le16 id; |
| 105 | __le16 value; |
| 106 | /* 1: 3683a */ |
| 107 | /* 2: other (default) */ |
| 108 | __le16 bits; |
| 109 | /* RF2595: 24 */ |
| 110 | __le16 bit_values[0]; |
Jussi Kivilinna | fbd5d17 | 2011-04-02 11:25:54 +0300 | [diff] [blame] | 111 | /* (ZD_CR203 & ~(RF_IF_LE | RF_CLK | RF_DATA)) | (bit ? RF_DATA : 0) */ |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 112 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 113 | |
| 114 | /* USB interrupt */ |
| 115 | |
| 116 | enum usb_int_id { |
| 117 | USB_INT_TYPE = 0x01, |
| 118 | USB_INT_ID_REGS = 0x90, |
| 119 | USB_INT_ID_RETRY_FAILED = 0xa0, |
| 120 | }; |
| 121 | |
| 122 | enum usb_int_flags { |
| 123 | USB_INT_READ_REGS_EN = 0x01, |
| 124 | }; |
| 125 | |
| 126 | struct usb_int_header { |
| 127 | u8 type; /* must always be 1 */ |
| 128 | u8 id; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 129 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 130 | |
| 131 | struct usb_int_regs { |
| 132 | struct usb_int_header hdr; |
| 133 | struct reg_data regs[0]; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 134 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 135 | |
| 136 | struct usb_int_retry_fail { |
| 137 | struct usb_int_header hdr; |
| 138 | u8 new_rate; |
| 139 | u8 _dummy; |
| 140 | u8 addr[ETH_ALEN]; |
| 141 | u8 ibss_wakeup_dest; |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 142 | } __packed; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 143 | |
| 144 | struct read_regs_int { |
| 145 | struct completion completion; |
Jussi Kivilinna | c900eff | 2011-06-20 14:42:44 +0300 | [diff] [blame] | 146 | struct usb_req_read_regs *req; |
| 147 | unsigned int req_count; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 148 | /* Stores the USB int structure and contains the USB address of the |
| 149 | * first requested register before request. |
| 150 | */ |
| 151 | u8 buffer[USB_MAX_EP_INT_BUFFER]; |
| 152 | int length; |
| 153 | __le16 cr_int_addr; |
| 154 | }; |
| 155 | |
| 156 | struct zd_ioreq16 { |
| 157 | zd_addr_t addr; |
| 158 | u16 value; |
| 159 | }; |
| 160 | |
| 161 | struct zd_ioreq32 { |
| 162 | zd_addr_t addr; |
| 163 | u32 value; |
| 164 | }; |
| 165 | |
| 166 | struct zd_usb_interrupt { |
| 167 | struct read_regs_int read_regs; |
| 168 | spinlock_t lock; |
| 169 | struct urb *urb; |
Jussi Kivilinna | 4a3b087 | 2011-01-31 20:49:24 +0200 | [diff] [blame] | 170 | void *buffer; |
| 171 | dma_addr_t buffer_dma; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 172 | int interval; |
Jussi Kivilinna | c900eff | 2011-06-20 14:42:44 +0300 | [diff] [blame] | 173 | atomic_t read_regs_enabled; |
| 174 | u8 read_regs_int_overridden:1; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static inline struct usb_int_regs *get_read_regs(struct zd_usb_interrupt *intr) |
| 178 | { |
| 179 | return (struct usb_int_regs *)intr->read_regs.buffer; |
| 180 | } |
| 181 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 182 | #define RX_URBS_COUNT 5 |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 183 | |
| 184 | struct zd_usb_rx { |
| 185 | spinlock_t lock; |
Jussi Kivilinna | 1f6cccc | 2011-01-31 20:50:12 +0200 | [diff] [blame] | 186 | struct mutex setup_mutex; |
| 187 | struct delayed_work idle_work; |
Jussi Kivilinna | 0235357 | 2011-04-01 19:34:16 +0300 | [diff] [blame] | 188 | struct tasklet_struct reset_timer_tasklet; |
Jussi Kivilinna | 1f6cccc | 2011-01-31 20:50:12 +0200 | [diff] [blame] | 189 | u8 fragment[2 * USB_MAX_RX_SIZE]; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 190 | unsigned int fragment_length; |
| 191 | unsigned int usb_packet_size; |
| 192 | struct urb **urbs; |
| 193 | int urbs_count; |
| 194 | }; |
| 195 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 196 | /** |
| 197 | * struct zd_usb_tx - structure used for transmitting frames |
Jussi Kivilinna | 78fc800 | 2011-01-31 20:47:08 +0200 | [diff] [blame] | 198 | * @enabled: atomic enabled flag, indicates whether tx is enabled |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 199 | * @lock: lock for transmission |
Jussi Kivilinna | 78fc800 | 2011-01-31 20:47:08 +0200 | [diff] [blame] | 200 | * @submitted: anchor for URBs sent to device |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 201 | * @submitted_urbs: atomic integer that counts the URBs having sent to the |
| 202 | * device, which haven't been completed |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 203 | * @stopped: indicates whether higher level tx queues are stopped |
| 204 | */ |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 205 | struct zd_usb_tx { |
Jussi Kivilinna | 78fc800 | 2011-01-31 20:47:08 +0200 | [diff] [blame] | 206 | atomic_t enabled; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 207 | spinlock_t lock; |
Jussi Kivilinna | a0fd751 | 2011-01-31 20:49:52 +0200 | [diff] [blame] | 208 | struct delayed_work watchdog_work; |
| 209 | struct sk_buff_head submitted_skbs; |
Jussi Kivilinna | 78fc800 | 2011-01-31 20:47:08 +0200 | [diff] [blame] | 210 | struct usb_anchor submitted; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 211 | int submitted_urbs; |
Jussi Kivilinna | a0fd751 | 2011-01-31 20:49:52 +0200 | [diff] [blame] | 212 | u8 stopped:1, watchdog_enabled:1; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 213 | }; |
| 214 | |
Daniel Drake | 0ce34bc | 2006-12-12 01:26:11 +0000 | [diff] [blame] | 215 | /* Contains the usb parts. The structure doesn't require a lock because intf |
| 216 | * will not be changed after initialization. |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 217 | */ |
| 218 | struct zd_usb { |
| 219 | struct zd_usb_interrupt intr; |
| 220 | struct zd_usb_rx rx; |
| 221 | struct zd_usb_tx tx; |
| 222 | struct usb_interface *intf; |
Jussi Kivilinna | eefdbec | 2011-02-12 20:43:32 +0200 | [diff] [blame] | 223 | struct usb_anchor submitted_cmds; |
| 224 | struct urb *urb_async_waiting; |
| 225 | int cmd_error; |
Jussi Kivilinna | 9bca0c3 | 2011-01-31 20:49:14 +0200 | [diff] [blame] | 226 | u8 req_buf[64]; /* zd_usb_iowrite16v needs 62 bytes */ |
Jussi Kivilinna | eefdbec | 2011-02-12 20:43:32 +0200 | [diff] [blame] | 227 | u8 is_zd1211b:1, initialized:1, was_running:1, in_async:1; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | #define zd_usb_dev(usb) (&usb->intf->dev) |
| 231 | |
| 232 | static inline struct usb_device *zd_usb_to_usbdev(struct zd_usb *usb) |
| 233 | { |
| 234 | return interface_to_usbdev(usb->intf); |
| 235 | } |
| 236 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 237 | static inline struct ieee80211_hw *zd_intf_to_hw(struct usb_interface *intf) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 238 | { |
| 239 | return usb_get_intfdata(intf); |
| 240 | } |
| 241 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 242 | static inline struct ieee80211_hw *zd_usb_to_hw(struct zd_usb *usb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 243 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 244 | return zd_intf_to_hw(usb->intf); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 247 | void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 248 | struct usb_interface *intf); |
| 249 | int zd_usb_init_hw(struct zd_usb *usb); |
| 250 | void zd_usb_clear(struct zd_usb *usb); |
| 251 | |
| 252 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size); |
| 253 | |
Jussi Kivilinna | a0fd751 | 2011-01-31 20:49:52 +0200 | [diff] [blame] | 254 | void zd_tx_watchdog_enable(struct zd_usb *usb); |
| 255 | void zd_tx_watchdog_disable(struct zd_usb *usb); |
| 256 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 257 | int zd_usb_enable_int(struct zd_usb *usb); |
| 258 | void zd_usb_disable_int(struct zd_usb *usb); |
| 259 | |
| 260 | int zd_usb_enable_rx(struct zd_usb *usb); |
| 261 | void zd_usb_disable_rx(struct zd_usb *usb); |
| 262 | |
Jussi Kivilinna | 1f6cccc | 2011-01-31 20:50:12 +0200 | [diff] [blame] | 263 | void zd_usb_reset_rx_idle_timer(struct zd_usb *usb); |
| 264 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 265 | void zd_usb_enable_tx(struct zd_usb *usb); |
| 266 | void zd_usb_disable_tx(struct zd_usb *usb); |
| 267 | |
| 268 | int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 269 | |
| 270 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, |
| 271 | const zd_addr_t *addresses, unsigned int count); |
| 272 | |
| 273 | static inline int zd_usb_ioread16(struct zd_usb *usb, u16 *value, |
| 274 | const zd_addr_t addr) |
| 275 | { |
Joe Perches | 2c20889 | 2012-06-04 12:44:17 +0000 | [diff] [blame] | 276 | return zd_usb_ioread16v(usb, value, &addr, 1); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Jussi Kivilinna | 8662b25 | 2011-02-12 20:43:42 +0200 | [diff] [blame] | 279 | void zd_usb_iowrite16v_async_start(struct zd_usb *usb); |
| 280 | int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout); |
| 281 | int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
| 282 | unsigned int count); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 283 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
| 284 | unsigned int count); |
| 285 | |
| 286 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits); |
| 287 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 288 | int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len); |
| 289 | |
Ulrich Kunitz | bc5f06a | 2006-09-13 02:42:12 +0100 | [diff] [blame] | 290 | extern struct workqueue_struct *zd_workqueue; |
| 291 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 292 | #endif /* _ZD_USB_H */ |