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