zd_usb.h 7.3 KB
Newer Older
D
Daniel Drake 已提交
1 2 3 4
/* ZD1211 USB-WLAN driver for Linux
 *
 * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
 * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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 20 21 22 23 24 25 26 27 28 29 30
 */

#ifndef _ZD_USB_H
#define _ZD_USB_H

#include <linux/completion.h>
#include <linux/netdevice.h>
#include <linux/spinlock.h>
#include <linux/skbuff.h>
#include <linux/usb.h>

#include "zd_def.h"

D
Daniel Drake 已提交
31 32 33
#define ZD_USB_TX_HIGH  5
#define ZD_USB_TX_LOW   2

34 35
#define ZD_TX_TIMEOUT		(HZ * 5)
#define ZD_TX_WATCHDOG_INTERVAL	round_jiffies_relative(HZ)
36
#define ZD_RX_IDLE_INTERVAL	round_jiffies_relative(30 * HZ)
37

38 39 40
enum devicetype {
	DEVICE_ZD1211  = 0,
	DEVICE_ZD1211B = 1,
41
	DEVICE_INSTALLER = 2,
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
};

enum endpoints {
	EP_CTRL	    = 0,
	EP_DATA_OUT = 1,
	EP_DATA_IN  = 2,
	EP_INT_IN   = 3,
	EP_REGS_OUT = 4,
};

enum {
	USB_MAX_TRANSFER_SIZE		= 4096, /* bytes */
	/* FIXME: The original driver uses this value. We have to check,
	 * whether the MAX_TRANSFER_SIZE is sufficient and this needs only be
	 * used if one combined frame is split over two USB transactions.
	 */
	USB_MAX_RX_SIZE			= 4800, /* bytes */
	USB_MAX_IOWRITE16_COUNT		= 15,
	USB_MAX_IOWRITE32_COUNT		= USB_MAX_IOWRITE16_COUNT/2,
	USB_MAX_IOREAD16_COUNT		= 15,
	USB_MAX_IOREAD32_COUNT		= USB_MAX_IOREAD16_COUNT/2,
	USB_MIN_RFWRITE_BIT_COUNT	= 16,
	USB_MAX_RFWRITE_BIT_COUNT	= 28,
	USB_MAX_EP_INT_BUFFER		= 64,
	USB_ZD1211B_BCD_DEVICE		= 0x4810,
};

enum control_requests {
	USB_REQ_WRITE_REGS		= 0x21,
	USB_REQ_READ_REGS		= 0x22,
	USB_REQ_WRITE_RF		= 0x23,
	USB_REQ_PROG_FLASH		= 0x24,
	USB_REQ_EEPROM_START		= 0x0128, /* ? request is a byte */
	USB_REQ_EEPROM_MID		= 0x28,
	USB_REQ_EEPROM_END		= 0x0228, /* ? request is a byte */
	USB_REQ_FIRMWARE_DOWNLOAD	= 0x30,
	USB_REQ_FIRMWARE_CONFIRM	= 0x31,
	USB_REQ_FIRMWARE_READ_DATA	= 0x32,
};

struct usb_req_read_regs {
	__le16 id;
	__le16 addr[0];
85
} __packed;
86 87 88 89

struct reg_data {
	__le16 addr;
	__le16 value;
90
} __packed;
91 92 93 94

struct usb_req_write_regs {
	__le16 id;
	struct reg_data reg_writes[0];
95
} __packed;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

enum {
	RF_IF_LE = 0x02,
	RF_CLK   = 0x04,
	RF_DATA	 = 0x08,
};

struct usb_req_rfwrite {
	__le16 id;
	__le16 value;
	/* 1: 3683a */
	/* 2: other (default) */
	__le16 bits;
	/* RF2595: 24 */
	__le16 bit_values[0];
111
	/* (ZD_CR203 & ~(RF_IF_LE | RF_CLK | RF_DATA)) | (bit ? RF_DATA : 0) */
112
} __packed;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

/* USB interrupt */

enum usb_int_id {
	USB_INT_TYPE			= 0x01,
	USB_INT_ID_REGS			= 0x90,
	USB_INT_ID_RETRY_FAILED		= 0xa0,
};

enum usb_int_flags {
	USB_INT_READ_REGS_EN		= 0x01,
};

struct usb_int_header {
	u8 type;	/* must always be 1 */
	u8 id;
129
} __packed;
130 131 132 133

struct usb_int_regs {
	struct usb_int_header hdr;
	struct reg_data regs[0];
134
} __packed;
135 136 137 138 139 140 141

struct usb_int_retry_fail {
	struct usb_int_header hdr;
	u8 new_rate;
	u8 _dummy;
	u8 addr[ETH_ALEN];
	u8 ibss_wakeup_dest;
142
} __packed;
143 144 145

struct read_regs_int {
	struct completion completion;
146 147
	struct usb_req_read_regs *req;
	unsigned int req_count;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	/* Stores the USB int structure and contains the USB address of the
	 * first requested register before request.
	 */
	u8 buffer[USB_MAX_EP_INT_BUFFER];
	int length;
	__le16 cr_int_addr;
};

struct zd_ioreq16 {
	zd_addr_t addr;
	u16 value;
};

struct zd_ioreq32 {
	zd_addr_t addr;
	u32 value;
};

struct zd_usb_interrupt {
	struct read_regs_int read_regs;
	spinlock_t lock;
	struct urb *urb;
170 171
	void *buffer;
	dma_addr_t buffer_dma;
172
	int interval;
173 174
	atomic_t read_regs_enabled;
	u8 read_regs_int_overridden:1;
175 176 177 178 179 180 181
};

static inline struct usb_int_regs *get_read_regs(struct zd_usb_interrupt *intr)
{
	return (struct usb_int_regs *)intr->read_regs.buffer;
}

D
Daniel Drake 已提交
182
#define RX_URBS_COUNT 5
183 184 185

struct zd_usb_rx {
	spinlock_t lock;
186 187
	struct mutex setup_mutex;
	struct delayed_work idle_work;
188
	struct tasklet_struct reset_timer_tasklet;
189
	u8 fragment[2 * USB_MAX_RX_SIZE];
190 191 192 193 194 195
	unsigned int fragment_length;
	unsigned int usb_packet_size;
	struct urb **urbs;
	int urbs_count;
};

D
Daniel Drake 已提交
196 197
/**
 * struct zd_usb_tx - structure used for transmitting frames
198
 * @enabled: atomic enabled flag, indicates whether tx is enabled
D
Daniel Drake 已提交
199
 * @lock: lock for transmission
200
 * @submitted: anchor for URBs sent to device
D
Daniel Drake 已提交
201 202 203 204
 * @submitted_urbs: atomic integer that counts the URBs having sent to the
 *	device, which haven't been completed
 * @stopped: indicates whether higher level tx queues are stopped
 */
205
struct zd_usb_tx {
206
	atomic_t enabled;
207
	spinlock_t lock;
208 209
	struct delayed_work watchdog_work;
	struct sk_buff_head submitted_skbs;
210
	struct usb_anchor submitted;
D
Daniel Drake 已提交
211
	int submitted_urbs;
212
	u8 stopped:1, watchdog_enabled:1;
213 214
};

215 216
/* Contains the usb parts. The structure doesn't require a lock because intf
 * will not be changed after initialization.
217 218 219 220 221 222
 */
struct zd_usb {
	struct zd_usb_interrupt intr;
	struct zd_usb_rx rx;
	struct zd_usb_tx tx;
	struct usb_interface *intf;
223 224 225
	struct usb_anchor submitted_cmds;
	struct urb *urb_async_waiting;
	int cmd_error;
226
	u8 req_buf[64]; /* zd_usb_iowrite16v needs 62 bytes */
227
	u8 is_zd1211b:1, initialized:1, was_running:1, in_async:1;
228 229 230 231 232 233 234 235 236
};

#define zd_usb_dev(usb) (&usb->intf->dev)

static inline struct usb_device *zd_usb_to_usbdev(struct zd_usb *usb)
{
	return interface_to_usbdev(usb->intf);
}

D
Daniel Drake 已提交
237
static inline struct ieee80211_hw *zd_intf_to_hw(struct usb_interface *intf)
238 239 240 241
{
	return usb_get_intfdata(intf);
}

D
Daniel Drake 已提交
242
static inline struct ieee80211_hw *zd_usb_to_hw(struct zd_usb *usb)
243
{
D
Daniel Drake 已提交
244
	return zd_intf_to_hw(usb->intf);
245 246
}

D
Daniel Drake 已提交
247
void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
248 249 250 251 252 253
	         struct usb_interface *intf);
int zd_usb_init_hw(struct zd_usb *usb);
void zd_usb_clear(struct zd_usb *usb);

int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size);

254 255 256
void zd_tx_watchdog_enable(struct zd_usb *usb);
void zd_tx_watchdog_disable(struct zd_usb *usb);

257 258 259 260 261 262
int zd_usb_enable_int(struct zd_usb *usb);
void zd_usb_disable_int(struct zd_usb *usb);

int zd_usb_enable_rx(struct zd_usb *usb);
void zd_usb_disable_rx(struct zd_usb *usb);

263 264
void zd_usb_reset_rx_idle_timer(struct zd_usb *usb);

D
Daniel Drake 已提交
265 266 267 268
void zd_usb_enable_tx(struct zd_usb *usb);
void zd_usb_disable_tx(struct zd_usb *usb);

int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb);
269 270 271 272 273 274 275

int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
	         const zd_addr_t *addresses, unsigned int count);

static inline int zd_usb_ioread16(struct zd_usb *usb, u16 *value,
	                      const zd_addr_t addr)
{
276
	return zd_usb_ioread16v(usb, value, &addr, 1);
277 278
}

279 280 281 282
void zd_usb_iowrite16v_async_start(struct zd_usb *usb);
int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout);
int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
			    unsigned int count);
283 284 285 286 287
int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
	              unsigned int count);

int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits);

288 289
int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len);

290 291
extern struct workqueue_struct *zd_workqueue;

292
#endif /* _ZD_USB_H */