blob: bf942843b733f7623bd24176a1a4472e0298665d [file] [log] [blame]
Daniel Drake66bb42f2007-11-19 16:20:12 +00001/* 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 Drakee85d0912006-06-02 17:11:32 +01005 *
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 Drakee85d0912006-06-02 17:11:32 +010031
Daniel Drake459c51a2007-11-19 15:00:29 +000032#define ZD_USB_TX_HIGH 5
33#define ZD_USB_TX_LOW 2
34
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +020035#define ZD_TX_TIMEOUT (HZ * 5)
36#define ZD_TX_WATCHDOG_INTERVAL round_jiffies_relative(HZ)
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +020037#define ZD_RX_IDLE_INTERVAL round_jiffies_relative(30 * HZ)
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +020038
Daniel Drakee85d0912006-06-02 17:11:32 +010039enum devicetype {
40 DEVICE_ZD1211 = 0,
41 DEVICE_ZD1211B = 1,
Daniel Drakea1030e922006-08-13 12:15:29 +010042 DEVICE_INSTALLER = 2,
Daniel Drakee85d0912006-06-02 17:11:32 +010043};
44
45enum endpoints {
46 EP_CTRL = 0,
47 EP_DATA_OUT = 1,
48 EP_DATA_IN = 2,
49 EP_INT_IN = 3,
50 EP_REGS_OUT = 4,
51};
52
53enum {
54 USB_MAX_TRANSFER_SIZE = 4096, /* bytes */
55 /* FIXME: The original driver uses this value. We have to check,
56 * whether the MAX_TRANSFER_SIZE is sufficient and this needs only be
57 * used if one combined frame is split over two USB transactions.
58 */
59 USB_MAX_RX_SIZE = 4800, /* bytes */
60 USB_MAX_IOWRITE16_COUNT = 15,
61 USB_MAX_IOWRITE32_COUNT = USB_MAX_IOWRITE16_COUNT/2,
62 USB_MAX_IOREAD16_COUNT = 15,
63 USB_MAX_IOREAD32_COUNT = USB_MAX_IOREAD16_COUNT/2,
64 USB_MIN_RFWRITE_BIT_COUNT = 16,
65 USB_MAX_RFWRITE_BIT_COUNT = 28,
66 USB_MAX_EP_INT_BUFFER = 64,
67 USB_ZD1211B_BCD_DEVICE = 0x4810,
68};
69
70enum control_requests {
71 USB_REQ_WRITE_REGS = 0x21,
72 USB_REQ_READ_REGS = 0x22,
73 USB_REQ_WRITE_RF = 0x23,
74 USB_REQ_PROG_FLASH = 0x24,
75 USB_REQ_EEPROM_START = 0x0128, /* ? request is a byte */
76 USB_REQ_EEPROM_MID = 0x28,
77 USB_REQ_EEPROM_END = 0x0228, /* ? request is a byte */
78 USB_REQ_FIRMWARE_DOWNLOAD = 0x30,
79 USB_REQ_FIRMWARE_CONFIRM = 0x31,
80 USB_REQ_FIRMWARE_READ_DATA = 0x32,
81};
82
83struct usb_req_read_regs {
84 __le16 id;
85 __le16 addr[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +000086} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +010087
88struct reg_data {
89 __le16 addr;
90 __le16 value;
Eric Dumazetba2d3582010-06-02 18:10:09 +000091} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +010092
93struct usb_req_write_regs {
94 __le16 id;
95 struct reg_data reg_writes[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +000096} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +010097
98enum {
99 RF_IF_LE = 0x02,
100 RF_CLK = 0x04,
101 RF_DATA = 0x08,
102};
103
104struct usb_req_rfwrite {
105 __le16 id;
106 __le16 value;
107 /* 1: 3683a */
108 /* 2: other (default) */
109 __le16 bits;
110 /* RF2595: 24 */
111 __le16 bit_values[0];
Jussi Kivilinnafbd5d172011-04-02 11:25:54 +0300112 /* (ZD_CR203 & ~(RF_IF_LE | RF_CLK | RF_DATA)) | (bit ? RF_DATA : 0) */
Eric Dumazetba2d3582010-06-02 18:10:09 +0000113} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +0100114
115/* USB interrupt */
116
117enum usb_int_id {
118 USB_INT_TYPE = 0x01,
119 USB_INT_ID_REGS = 0x90,
120 USB_INT_ID_RETRY_FAILED = 0xa0,
121};
122
123enum usb_int_flags {
124 USB_INT_READ_REGS_EN = 0x01,
125};
126
127struct usb_int_header {
128 u8 type; /* must always be 1 */
129 u8 id;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000130} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +0100131
132struct usb_int_regs {
133 struct usb_int_header hdr;
134 struct reg_data regs[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000135} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +0100136
137struct usb_int_retry_fail {
138 struct usb_int_header hdr;
139 u8 new_rate;
140 u8 _dummy;
141 u8 addr[ETH_ALEN];
142 u8 ibss_wakeup_dest;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000143} __packed;
Daniel Drakee85d0912006-06-02 17:11:32 +0100144
145struct read_regs_int {
146 struct completion completion;
147 /* Stores the USB int structure and contains the USB address of the
148 * first requested register before request.
149 */
150 u8 buffer[USB_MAX_EP_INT_BUFFER];
151 int length;
152 __le16 cr_int_addr;
153};
154
155struct zd_ioreq16 {
156 zd_addr_t addr;
157 u16 value;
158};
159
160struct zd_ioreq32 {
161 zd_addr_t addr;
162 u32 value;
163};
164
165struct zd_usb_interrupt {
166 struct read_regs_int read_regs;
167 spinlock_t lock;
168 struct urb *urb;
Jussi Kivilinna4a3b0872011-01-31 20:49:24 +0200169 void *buffer;
170 dma_addr_t buffer_dma;
Daniel Drakee85d0912006-06-02 17:11:32 +0100171 int interval;
172 u8 read_regs_enabled:1;
173};
174
175static inline struct usb_int_regs *get_read_regs(struct zd_usb_interrupt *intr)
176{
177 return (struct usb_int_regs *)intr->read_regs.buffer;
178}
179
Daniel Drake459c51a2007-11-19 15:00:29 +0000180#define RX_URBS_COUNT 5
Daniel Drakee85d0912006-06-02 17:11:32 +0100181
182struct zd_usb_rx {
183 spinlock_t lock;
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200184 struct mutex setup_mutex;
185 struct delayed_work idle_work;
Jussi Kivilinna02353572011-04-01 19:34:16 +0300186 struct tasklet_struct reset_timer_tasklet;
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200187 u8 fragment[2 * USB_MAX_RX_SIZE];
Daniel Drakee85d0912006-06-02 17:11:32 +0100188 unsigned int fragment_length;
189 unsigned int usb_packet_size;
190 struct urb **urbs;
191 int urbs_count;
192};
193
Daniel Drake459c51a2007-11-19 15:00:29 +0000194/**
195 * struct zd_usb_tx - structure used for transmitting frames
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200196 * @enabled: atomic enabled flag, indicates whether tx is enabled
Daniel Drake459c51a2007-11-19 15:00:29 +0000197 * @lock: lock for transmission
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200198 * @submitted: anchor for URBs sent to device
Daniel Drake459c51a2007-11-19 15:00:29 +0000199 * @submitted_urbs: atomic integer that counts the URBs having sent to the
200 * device, which haven't been completed
Daniel Drake459c51a2007-11-19 15:00:29 +0000201 * @stopped: indicates whether higher level tx queues are stopped
202 */
Daniel Drakee85d0912006-06-02 17:11:32 +0100203struct zd_usb_tx {
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200204 atomic_t enabled;
Daniel Drakee85d0912006-06-02 17:11:32 +0100205 spinlock_t lock;
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200206 struct delayed_work watchdog_work;
207 struct sk_buff_head submitted_skbs;
Jussi Kivilinna78fc8002011-01-31 20:47:08 +0200208 struct usb_anchor submitted;
Daniel Drake459c51a2007-11-19 15:00:29 +0000209 int submitted_urbs;
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200210 u8 stopped:1, watchdog_enabled:1;
Daniel Drakee85d0912006-06-02 17:11:32 +0100211};
212
Daniel Drake0ce34bc2006-12-12 01:26:11 +0000213/* Contains the usb parts. The structure doesn't require a lock because intf
214 * will not be changed after initialization.
Daniel Drakee85d0912006-06-02 17:11:32 +0100215 */
216struct zd_usb {
217 struct zd_usb_interrupt intr;
218 struct zd_usb_rx rx;
219 struct zd_usb_tx tx;
220 struct usb_interface *intf;
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +0200221 struct usb_anchor submitted_cmds;
222 struct urb *urb_async_waiting;
223 int cmd_error;
Jussi Kivilinna9bca0c32011-01-31 20:49:14 +0200224 u8 req_buf[64]; /* zd_usb_iowrite16v needs 62 bytes */
Jussi Kivilinnaeefdbec2011-02-12 20:43:32 +0200225 u8 is_zd1211b:1, initialized:1, was_running:1, in_async:1;
Daniel Drakee85d0912006-06-02 17:11:32 +0100226};
227
228#define zd_usb_dev(usb) (&usb->intf->dev)
229
230static inline struct usb_device *zd_usb_to_usbdev(struct zd_usb *usb)
231{
232 return interface_to_usbdev(usb->intf);
233}
234
Daniel Drake459c51a2007-11-19 15:00:29 +0000235static inline struct ieee80211_hw *zd_intf_to_hw(struct usb_interface *intf)
Daniel Drakee85d0912006-06-02 17:11:32 +0100236{
237 return usb_get_intfdata(intf);
238}
239
Daniel Drake459c51a2007-11-19 15:00:29 +0000240static inline struct ieee80211_hw *zd_usb_to_hw(struct zd_usb *usb)
Daniel Drakee85d0912006-06-02 17:11:32 +0100241{
Daniel Drake459c51a2007-11-19 15:00:29 +0000242 return zd_intf_to_hw(usb->intf);
Daniel Drakee85d0912006-06-02 17:11:32 +0100243}
244
Daniel Drake459c51a2007-11-19 15:00:29 +0000245void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
Daniel Drakee85d0912006-06-02 17:11:32 +0100246 struct usb_interface *intf);
247int zd_usb_init_hw(struct zd_usb *usb);
248void zd_usb_clear(struct zd_usb *usb);
249
250int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size);
251
Jussi Kivilinnaa0fd7512011-01-31 20:49:52 +0200252void zd_tx_watchdog_enable(struct zd_usb *usb);
253void zd_tx_watchdog_disable(struct zd_usb *usb);
254
Daniel Drakee85d0912006-06-02 17:11:32 +0100255int zd_usb_enable_int(struct zd_usb *usb);
256void zd_usb_disable_int(struct zd_usb *usb);
257
258int zd_usb_enable_rx(struct zd_usb *usb);
259void zd_usb_disable_rx(struct zd_usb *usb);
260
Jussi Kivilinna1f6cccc2011-01-31 20:50:12 +0200261void zd_usb_reset_rx_idle_timer(struct zd_usb *usb);
262
Daniel Drake459c51a2007-11-19 15:00:29 +0000263void zd_usb_enable_tx(struct zd_usb *usb);
264void zd_usb_disable_tx(struct zd_usb *usb);
265
266int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb);
Daniel Drakee85d0912006-06-02 17:11:32 +0100267
268int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
269 const zd_addr_t *addresses, unsigned int count);
270
271static inline int zd_usb_ioread16(struct zd_usb *usb, u16 *value,
272 const zd_addr_t addr)
273{
274 return zd_usb_ioread16v(usb, value, (const zd_addr_t *)&addr, 1);
275}
276
Jussi Kivilinna8662b252011-02-12 20:43:42 +0200277void zd_usb_iowrite16v_async_start(struct zd_usb *usb);
278int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout);
279int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
280 unsigned int count);
Daniel Drakee85d0912006-06-02 17:11:32 +0100281int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
282 unsigned int count);
283
284int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits);
285
Daniel Drake74553ae2007-07-01 18:22:32 +0100286int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len);
287
Ulrich Kunitzbc5f06a2006-09-13 02:42:12 +0100288extern struct workqueue_struct *zd_workqueue;
289
Daniel Drakee85d0912006-06-02 17:11:32 +0100290#endif /* _ZD_USB_H */