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