Commit | Line | Data |
---|---|---|
1ccea77e | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
66bb42fd DD |
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 | * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net> | |
e85d0918 DD |
7 | */ |
8 | ||
d066c219 | 9 | #include <linux/kernel.h> |
e85d0918 | 10 | #include <linux/init.h> |
e85d0918 DD |
11 | #include <linux/firmware.h> |
12 | #include <linux/device.h> | |
13 | #include <linux/errno.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
e85d0918 DD |
15 | #include <linux/skbuff.h> |
16 | #include <linux/usb.h> | |
bc5f06a8 | 17 | #include <linux/workqueue.h> |
9d9779e7 | 18 | #include <linux/module.h> |
459c51ad | 19 | #include <net/mac80211.h> |
c248725b | 20 | #include <asm/unaligned.h> |
e85d0918 DD |
21 | |
22 | #include "zd_def.h" | |
e85d0918 DD |
23 | #include "zd_mac.h" |
24 | #include "zd_usb.h" | |
e85d0918 | 25 | |
ecf23a78 | 26 | static const struct usb_device_id usb_ids[] = { |
e85d0918 | 27 | /* ZD1211 */ |
3bfbe80e HTL |
28 | { USB_DEVICE(0x0105, 0x145f), .driver_info = DEVICE_ZD1211 }, |
29 | { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 }, | |
30 | { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 }, | |
31 | { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 }, | |
32 | { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 }, | |
33 | { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 }, | |
34 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 35 | { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 }, |
14990c69 | 36 | { USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
37 | { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 }, |
38 | { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 }, | |
39 | { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 40 | { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 }, |
aa1d3a18 | 41 | { USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
42 | { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 }, |
43 | { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 }, | |
44 | { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 }, | |
f2423723 | 45 | { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 }, |
8cecc90e | 46 | { USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 }, |
229782a3 | 47 | { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 }, |
269fca0e | 48 | { USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
49 | { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 }, |
50 | { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 }, | |
9011cd25 | 51 | { USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 }, |
3bfbe80e HTL |
52 | { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 }, |
53 | { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 }, | |
e85d0918 | 54 | /* ZD1211B */ |
3bfbe80e | 55 | { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B }, |
ad580db5 | 56 | { USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
57 | { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B }, |
58 | { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B }, | |
59 | { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B }, | |
60 | { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B }, | |
8b339d05 | 61 | { USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
62 | { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B }, |
63 | { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B }, | |
64 | { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B }, | |
65 | { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B }, | |
66 | { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B }, | |
dd2f5538 | 67 | { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
68 | { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211B }, |
69 | { USB_DEVICE(0x07fa, 0x1196), .driver_info = DEVICE_ZD1211B }, | |
70 | { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B }, | |
8f75e07a | 71 | { USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B }, |
f72151fb | 72 | { USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B }, |
9dfd5500 | 73 | { USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
74 | { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B }, |
75 | { USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B }, | |
aec91028 | 76 | { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B }, |
c445a31c | 77 | { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B }, |
8e97afe5 | 78 | { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B }, |
de262496 | 79 | { USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B }, |
3bfbe80e HTL |
80 | { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B }, |
81 | { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B }, | |
82 | { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B }, | |
83 | { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B }, | |
84 | { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B }, | |
8cecc90e | 85 | { USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B }, |
a1030e92 DD |
86 | /* "Driverless" devices that need ejecting */ |
87 | { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER }, | |
aa1d3a18 | 88 | { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER }, |
e85d0918 DD |
89 | {} |
90 | }; | |
91 | ||
92 | MODULE_LICENSE("GPL"); | |
93 | MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip."); | |
94 | MODULE_AUTHOR("Ulrich Kunitz"); | |
95 | MODULE_AUTHOR("Daniel Drake"); | |
96 | MODULE_VERSION("1.0"); | |
97 | MODULE_DEVICE_TABLE(usb, usb_ids); | |
98 | ||
99 | #define FW_ZD1211_PREFIX "zd1211/zd1211_" | |
100 | #define FW_ZD1211B_PREFIX "zd1211/zd1211b_" | |
101 | ||
c900eff3 JK |
102 | static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req, |
103 | unsigned int count); | |
104 | ||
e85d0918 | 105 | /* USB device initialization */ |
72e77a8a | 106 | static void int_urb_complete(struct urb *urb); |
e85d0918 DD |
107 | |
108 | static int request_fw_file( | |
109 | const struct firmware **fw, const char *name, struct device *device) | |
110 | { | |
111 | int r; | |
112 | ||
113 | dev_dbg_f(device, "fw name %s\n", name); | |
114 | ||
115 | r = request_firmware(fw, name, device); | |
116 | if (r) | |
117 | dev_err(device, | |
118 | "Could not load firmware file %s. Error number %d\n", | |
119 | name, r); | |
120 | return r; | |
121 | } | |
122 | ||
123 | static inline u16 get_bcdDevice(const struct usb_device *udev) | |
124 | { | |
125 | return le16_to_cpu(udev->descriptor.bcdDevice); | |
126 | } | |
127 | ||
128 | enum upload_code_flags { | |
129 | REBOOT = 1, | |
130 | }; | |
131 | ||
132 | /* Ensures that MAX_TRANSFER_SIZE is even. */ | |
133 | #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1) | |
134 | ||
135 | static int upload_code(struct usb_device *udev, | |
136 | const u8 *data, size_t size, u16 code_offset, int flags) | |
137 | { | |
138 | u8 *p; | |
139 | int r; | |
140 | ||
141 | /* USB request blocks need "kmalloced" buffers. | |
142 | */ | |
143 | p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL); | |
144 | if (!p) { | |
e85d0918 DD |
145 | r = -ENOMEM; |
146 | goto error; | |
147 | } | |
148 | ||
149 | size &= ~1; | |
150 | while (size > 0) { | |
151 | size_t transfer_size = size <= MAX_TRANSFER_SIZE ? | |
152 | size : MAX_TRANSFER_SIZE; | |
153 | ||
154 | dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size); | |
155 | ||
156 | memcpy(p, data, transfer_size); | |
157 | r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
158 | USB_REQ_FIRMWARE_DOWNLOAD, | |
159 | USB_DIR_OUT | USB_TYPE_VENDOR, | |
160 | code_offset, 0, p, transfer_size, 1000 /* ms */); | |
161 | if (r < 0) { | |
162 | dev_err(&udev->dev, | |
163 | "USB control request for firmware upload" | |
164 | " failed. Error number %d\n", r); | |
165 | goto error; | |
166 | } | |
167 | transfer_size = r & ~1; | |
168 | ||
169 | size -= transfer_size; | |
170 | data += transfer_size; | |
171 | code_offset += transfer_size/sizeof(u16); | |
172 | } | |
173 | ||
174 | if (flags & REBOOT) { | |
175 | u8 ret; | |
176 | ||
a8c4ea7a | 177 | /* Use "DMA-aware" buffer. */ |
e85d0918 DD |
178 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
179 | USB_REQ_FIRMWARE_CONFIRM, | |
180 | USB_DIR_IN | USB_TYPE_VENDOR, | |
a8c4ea7a | 181 | 0, 0, p, sizeof(ret), 5000 /* ms */); |
e85d0918 DD |
182 | if (r != sizeof(ret)) { |
183 | dev_err(&udev->dev, | |
b46b5993 | 184 | "control request firmware confirmation failed." |
e85d0918 DD |
185 | " Return value %d\n", r); |
186 | if (r >= 0) | |
187 | r = -ENODEV; | |
188 | goto error; | |
189 | } | |
a8c4ea7a | 190 | ret = p[0]; |
e85d0918 DD |
191 | if (ret & 0x80) { |
192 | dev_err(&udev->dev, | |
193 | "Internal error while downloading." | |
194 | " Firmware confirm return value %#04x\n", | |
195 | (unsigned int)ret); | |
196 | r = -ENODEV; | |
197 | goto error; | |
198 | } | |
199 | dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n", | |
200 | (unsigned int)ret); | |
201 | } | |
202 | ||
203 | r = 0; | |
204 | error: | |
205 | kfree(p); | |
206 | return r; | |
207 | } | |
208 | ||
209 | static u16 get_word(const void *data, u16 offset) | |
210 | { | |
211 | const __le16 *p = data; | |
212 | return le16_to_cpu(p[offset]); | |
213 | } | |
214 | ||
74553aed | 215 | static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size, |
e85d0918 DD |
216 | const char* postfix) |
217 | { | |
218 | scnprintf(buffer, size, "%s%s", | |
74553aed | 219 | usb->is_zd1211b ? |
e85d0918 DD |
220 | FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX, |
221 | postfix); | |
222 | return buffer; | |
223 | } | |
224 | ||
74553aed | 225 | static int handle_version_mismatch(struct zd_usb *usb, |
d066c219 DD |
226 | const struct firmware *ub_fw) |
227 | { | |
74553aed | 228 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
d066c219 DD |
229 | const struct firmware *ur_fw = NULL; |
230 | int offset; | |
231 | int r = 0; | |
232 | char fw_name[128]; | |
233 | ||
234 | r = request_fw_file(&ur_fw, | |
74553aed | 235 | get_fw_name(usb, fw_name, sizeof(fw_name), "ur"), |
d066c219 DD |
236 | &udev->dev); |
237 | if (r) | |
238 | goto error; | |
239 | ||
ee302767 | 240 | r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT); |
d066c219 DD |
241 | if (r) |
242 | goto error; | |
243 | ||
ee302767 | 244 | offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16)); |
d066c219 | 245 | r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset, |
ee302767 | 246 | E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT); |
d066c219 DD |
247 | |
248 | /* At this point, the vendor driver downloads the whole firmware | |
249 | * image, hacks around with version IDs, and uploads it again, | |
250 | * completely overwriting the boot code. We do not do this here as | |
251 | * it is not required on any tested devices, and it is suspected to | |
252 | * cause problems. */ | |
253 | error: | |
254 | release_firmware(ur_fw); | |
255 | return r; | |
256 | } | |
257 | ||
74553aed | 258 | static int upload_firmware(struct zd_usb *usb) |
e85d0918 DD |
259 | { |
260 | int r; | |
261 | u16 fw_bcdDevice; | |
262 | u16 bcdDevice; | |
74553aed | 263 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 DD |
264 | const struct firmware *ub_fw = NULL; |
265 | const struct firmware *uph_fw = NULL; | |
266 | char fw_name[128]; | |
267 | ||
268 | bcdDevice = get_bcdDevice(udev); | |
269 | ||
270 | r = request_fw_file(&ub_fw, | |
74553aed | 271 | get_fw_name(usb, fw_name, sizeof(fw_name), "ub"), |
e85d0918 DD |
272 | &udev->dev); |
273 | if (r) | |
274 | goto error; | |
275 | ||
ee302767 | 276 | fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET); |
e85d0918 | 277 | |
e85d0918 DD |
278 | if (fw_bcdDevice != bcdDevice) { |
279 | dev_info(&udev->dev, | |
d066c219 DD |
280 | "firmware version %#06x and device bootcode version " |
281 | "%#06x differ\n", fw_bcdDevice, bcdDevice); | |
282 | if (bcdDevice <= 0x4313) | |
283 | dev_warn(&udev->dev, "device has old bootcode, please " | |
284 | "report success or failure\n"); | |
285 | ||
74553aed | 286 | r = handle_version_mismatch(usb, ub_fw); |
d066c219 DD |
287 | if (r) |
288 | goto error; | |
e85d0918 DD |
289 | } else { |
290 | dev_dbg_f(&udev->dev, | |
291 | "firmware device id %#06x is equal to the " | |
292 | "actual device id\n", fw_bcdDevice); | |
293 | } | |
294 | ||
295 | ||
296 | r = request_fw_file(&uph_fw, | |
74553aed | 297 | get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"), |
e85d0918 DD |
298 | &udev->dev); |
299 | if (r) | |
300 | goto error; | |
301 | ||
ee302767 | 302 | r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT); |
e85d0918 DD |
303 | if (r) { |
304 | dev_err(&udev->dev, | |
305 | "Could not upload firmware code uph. Error number %d\n", | |
306 | r); | |
307 | } | |
308 | ||
309 | /* FALL-THROUGH */ | |
310 | error: | |
311 | release_firmware(ub_fw); | |
312 | release_firmware(uph_fw); | |
313 | return r; | |
314 | } | |
315 | ||
3e8b4d00 BH |
316 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ur"); |
317 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ur"); | |
318 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "ub"); | |
319 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "ub"); | |
320 | MODULE_FIRMWARE(FW_ZD1211B_PREFIX "uphr"); | |
321 | MODULE_FIRMWARE(FW_ZD1211_PREFIX "uphr"); | |
322 | ||
74553aed DD |
323 | /* Read data from device address space using "firmware interface" which does |
324 | * not require firmware to be loaded. */ | |
325 | int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len) | |
326 | { | |
327 | int r; | |
328 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
a8c4ea7a | 329 | u8 *buf; |
74553aed | 330 | |
a8c4ea7a AN |
331 | /* Use "DMA-aware" buffer. */ |
332 | buf = kmalloc(len, GFP_KERNEL); | |
333 | if (!buf) | |
334 | return -ENOMEM; | |
74553aed DD |
335 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
336 | USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0, | |
a8c4ea7a | 337 | buf, len, 5000); |
74553aed DD |
338 | if (r < 0) { |
339 | dev_err(&udev->dev, | |
340 | "read over firmware interface failed: %d\n", r); | |
a8c4ea7a | 341 | goto exit; |
74553aed DD |
342 | } else if (r != len) { |
343 | dev_err(&udev->dev, | |
344 | "incomplete read over firmware interface: %d/%d\n", | |
345 | r, len); | |
a8c4ea7a AN |
346 | r = -EIO; |
347 | goto exit; | |
74553aed | 348 | } |
a8c4ea7a AN |
349 | r = 0; |
350 | memcpy(data, buf, len); | |
351 | exit: | |
352 | kfree(buf); | |
353 | return r; | |
74553aed DD |
354 | } |
355 | ||
e85d0918 DD |
356 | #define urb_dev(urb) (&(urb)->dev->dev) |
357 | ||
c900eff3 JK |
358 | static inline void handle_regs_int_override(struct urb *urb) |
359 | { | |
360 | struct zd_usb *usb = urb->context; | |
361 | struct zd_usb_interrupt *intr = &usb->intr; | |
81454b84 | 362 | unsigned long flags; |
c900eff3 | 363 | |
81454b84 | 364 | spin_lock_irqsave(&intr->lock, flags); |
c900eff3 JK |
365 | if (atomic_read(&intr->read_regs_enabled)) { |
366 | atomic_set(&intr->read_regs_enabled, 0); | |
367 | intr->read_regs_int_overridden = 1; | |
368 | complete(&intr->read_regs.completion); | |
369 | } | |
81454b84 | 370 | spin_unlock_irqrestore(&intr->lock, flags); |
c900eff3 JK |
371 | } |
372 | ||
e85d0918 DD |
373 | static inline void handle_regs_int(struct urb *urb) |
374 | { | |
375 | struct zd_usb *usb = urb->context; | |
376 | struct zd_usb_interrupt *intr = &usb->intr; | |
81454b84 | 377 | unsigned long flags; |
e85d0918 | 378 | int len; |
72e77a8a | 379 | u16 int_num; |
e85d0918 DD |
380 | |
381 | ZD_ASSERT(in_interrupt()); | |
81454b84 | 382 | spin_lock_irqsave(&intr->lock, flags); |
e85d0918 | 383 | |
d63ddcec | 384 | int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2)); |
72e77a8a LCC |
385 | if (int_num == CR_INTERRUPT) { |
386 | struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context)); | |
8b17f75c | 387 | spin_lock(&mac->lock); |
72e77a8a LCC |
388 | memcpy(&mac->intr_buffer, urb->transfer_buffer, |
389 | USB_MAX_EP_INT_BUFFER); | |
8b17f75c | 390 | spin_unlock(&mac->lock); |
72e77a8a | 391 | schedule_work(&mac->process_intr); |
c900eff3 JK |
392 | } else if (atomic_read(&intr->read_regs_enabled)) { |
393 | len = urb->actual_length; | |
394 | intr->read_regs.length = urb->actual_length; | |
e85d0918 DD |
395 | if (len > sizeof(intr->read_regs.buffer)) |
396 | len = sizeof(intr->read_regs.buffer); | |
c900eff3 | 397 | |
e85d0918 | 398 | memcpy(intr->read_regs.buffer, urb->transfer_buffer, len); |
c900eff3 JK |
399 | |
400 | /* Sometimes USB_INT_ID_REGS is not overridden, but comes after | |
401 | * USB_INT_ID_RETRY_FAILED. Read-reg retry then gets this | |
402 | * delayed USB_INT_ID_REGS, but leaves USB_INT_ID_REGS of | |
403 | * retry unhandled. Next read-reg command then might catch | |
404 | * this wrong USB_INT_ID_REGS. Fix by ignoring wrong reads. | |
405 | */ | |
406 | if (!check_read_regs(usb, intr->read_regs.req, | |
407 | intr->read_regs.req_count)) | |
408 | goto out; | |
409 | ||
410 | atomic_set(&intr->read_regs_enabled, 0); | |
411 | intr->read_regs_int_overridden = 0; | |
e85d0918 | 412 | complete(&intr->read_regs.completion); |
c900eff3 | 413 | |
e85d0918 DD |
414 | goto out; |
415 | } | |
416 | ||
e85d0918 | 417 | out: |
81454b84 | 418 | spin_unlock_irqrestore(&intr->lock, flags); |
c900eff3 JK |
419 | |
420 | /* CR_INTERRUPT might override read_reg too. */ | |
421 | if (int_num == CR_INTERRUPT && atomic_read(&intr->read_regs_enabled)) | |
422 | handle_regs_int_override(urb); | |
e85d0918 DD |
423 | } |
424 | ||
7d12e780 | 425 | static void int_urb_complete(struct urb *urb) |
e85d0918 DD |
426 | { |
427 | int r; | |
428 | struct usb_int_header *hdr; | |
c900eff3 JK |
429 | struct zd_usb *usb; |
430 | struct zd_usb_interrupt *intr; | |
e85d0918 DD |
431 | |
432 | switch (urb->status) { | |
433 | case 0: | |
434 | break; | |
435 | case -ESHUTDOWN: | |
436 | case -EINVAL: | |
437 | case -ENODEV: | |
438 | case -ENOENT: | |
439 | case -ECONNRESET: | |
e85d0918 | 440 | case -EPIPE: |
24d24c62 | 441 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
4a3b0874 | 442 | return; |
e85d0918 | 443 | default: |
24d24c62 | 444 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
e85d0918 DD |
445 | goto resubmit; |
446 | } | |
447 | ||
448 | if (urb->actual_length < sizeof(hdr)) { | |
449 | dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb); | |
450 | goto resubmit; | |
451 | } | |
452 | ||
453 | hdr = urb->transfer_buffer; | |
454 | if (hdr->type != USB_INT_TYPE) { | |
455 | dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb); | |
456 | goto resubmit; | |
457 | } | |
458 | ||
c900eff3 JK |
459 | /* USB_INT_ID_RETRY_FAILED triggered by tx-urb submit can override |
460 | * pending USB_INT_ID_REGS causing read command timeout. | |
461 | */ | |
462 | usb = urb->context; | |
463 | intr = &usb->intr; | |
464 | if (hdr->id != USB_INT_ID_REGS && atomic_read(&intr->read_regs_enabled)) | |
465 | handle_regs_int_override(urb); | |
466 | ||
e85d0918 DD |
467 | switch (hdr->id) { |
468 | case USB_INT_ID_REGS: | |
469 | handle_regs_int(urb); | |
470 | break; | |
471 | case USB_INT_ID_RETRY_FAILED: | |
7f4013f0 | 472 | zd_mac_tx_failed(urb); |
e85d0918 DD |
473 | break; |
474 | default: | |
475 | dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb, | |
476 | (unsigned int)hdr->id); | |
477 | goto resubmit; | |
478 | } | |
479 | ||
480 | resubmit: | |
481 | r = usb_submit_urb(urb, GFP_ATOMIC); | |
482 | if (r) { | |
4a3b0874 JK |
483 | dev_dbg_f(urb_dev(urb), "error: resubmit urb %p err code %d\n", |
484 | urb, r); | |
485 | /* TODO: add worker to reset intr->urb */ | |
e85d0918 DD |
486 | } |
487 | return; | |
e85d0918 DD |
488 | } |
489 | ||
490 | static inline int int_urb_interval(struct usb_device *udev) | |
491 | { | |
492 | switch (udev->speed) { | |
493 | case USB_SPEED_HIGH: | |
494 | return 4; | |
495 | case USB_SPEED_LOW: | |
496 | return 10; | |
497 | case USB_SPEED_FULL: | |
498 | default: | |
499 | return 1; | |
500 | } | |
501 | } | |
502 | ||
503 | static inline int usb_int_enabled(struct zd_usb *usb) | |
504 | { | |
505 | unsigned long flags; | |
506 | struct zd_usb_interrupt *intr = &usb->intr; | |
507 | struct urb *urb; | |
508 | ||
509 | spin_lock_irqsave(&intr->lock, flags); | |
510 | urb = intr->urb; | |
511 | spin_unlock_irqrestore(&intr->lock, flags); | |
512 | return urb != NULL; | |
513 | } | |
514 | ||
515 | int zd_usb_enable_int(struct zd_usb *usb) | |
516 | { | |
517 | int r; | |
4a3b0874 | 518 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 | 519 | struct zd_usb_interrupt *intr = &usb->intr; |
e85d0918 DD |
520 | struct urb *urb; |
521 | ||
522 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
523 | ||
35c3404e | 524 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
525 | if (!urb) { |
526 | r = -ENOMEM; | |
527 | goto out; | |
528 | } | |
529 | ||
530 | ZD_ASSERT(!irqs_disabled()); | |
531 | spin_lock_irq(&intr->lock); | |
532 | if (intr->urb) { | |
533 | spin_unlock_irq(&intr->lock); | |
534 | r = 0; | |
535 | goto error_free_urb; | |
536 | } | |
537 | intr->urb = urb; | |
538 | spin_unlock_irq(&intr->lock); | |
539 | ||
e85d0918 | 540 | r = -ENOMEM; |
4a3b0874 JK |
541 | intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER, |
542 | GFP_KERNEL, &intr->buffer_dma); | |
543 | if (!intr->buffer) { | |
e85d0918 DD |
544 | dev_dbg_f(zd_usb_dev(usb), |
545 | "couldn't allocate transfer_buffer\n"); | |
546 | goto error_set_urb_null; | |
547 | } | |
548 | ||
e85d0918 | 549 | usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN), |
4a3b0874 | 550 | intr->buffer, USB_MAX_EP_INT_BUFFER, |
e85d0918 DD |
551 | int_urb_complete, usb, |
552 | intr->interval); | |
4a3b0874 JK |
553 | urb->transfer_dma = intr->buffer_dma; |
554 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
e85d0918 DD |
555 | |
556 | dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb); | |
35c3404e | 557 | r = usb_submit_urb(urb, GFP_KERNEL); |
e85d0918 DD |
558 | if (r) { |
559 | dev_dbg_f(zd_usb_dev(usb), | |
560 | "Couldn't submit urb. Error number %d\n", r); | |
561 | goto error; | |
562 | } | |
563 | ||
564 | return 0; | |
565 | error: | |
4a3b0874 JK |
566 | usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER, |
567 | intr->buffer, intr->buffer_dma); | |
e85d0918 DD |
568 | error_set_urb_null: |
569 | spin_lock_irq(&intr->lock); | |
570 | intr->urb = NULL; | |
571 | spin_unlock_irq(&intr->lock); | |
572 | error_free_urb: | |
573 | usb_free_urb(urb); | |
574 | out: | |
575 | return r; | |
576 | } | |
577 | ||
578 | void zd_usb_disable_int(struct zd_usb *usb) | |
579 | { | |
580 | unsigned long flags; | |
4a3b0874 | 581 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
e85d0918 DD |
582 | struct zd_usb_interrupt *intr = &usb->intr; |
583 | struct urb *urb; | |
4a3b0874 JK |
584 | void *buffer; |
585 | dma_addr_t buffer_dma; | |
e85d0918 DD |
586 | |
587 | spin_lock_irqsave(&intr->lock, flags); | |
588 | urb = intr->urb; | |
589 | if (!urb) { | |
590 | spin_unlock_irqrestore(&intr->lock, flags); | |
591 | return; | |
592 | } | |
593 | intr->urb = NULL; | |
4a3b0874 JK |
594 | buffer = intr->buffer; |
595 | buffer_dma = intr->buffer_dma; | |
596 | intr->buffer = NULL; | |
e85d0918 DD |
597 | spin_unlock_irqrestore(&intr->lock, flags); |
598 | ||
599 | usb_kill_urb(urb); | |
600 | dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb); | |
601 | usb_free_urb(urb); | |
4a3b0874 JK |
602 | |
603 | if (buffer) | |
604 | usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER, | |
605 | buffer, buffer_dma); | |
e85d0918 DD |
606 | } |
607 | ||
608 | static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer, | |
609 | unsigned int length) | |
610 | { | |
611 | int i; | |
e85d0918 DD |
612 | const struct rx_length_info *length_info; |
613 | ||
614 | if (length < sizeof(struct rx_length_info)) { | |
615 | /* It's not a complete packet anyhow. */ | |
b405e1b8 JK |
616 | dev_dbg_f(zd_usb_dev(usb), "invalid, small RX packet : %d\n", |
617 | length); | |
e85d0918 DD |
618 | return; |
619 | } | |
620 | length_info = (struct rx_length_info *) | |
621 | (buffer + length - sizeof(struct rx_length_info)); | |
622 | ||
623 | /* It might be that three frames are merged into a single URB | |
624 | * transaction. We have to check for the length info tag. | |
625 | * | |
626 | * While testing we discovered that length_info might be unaligned, | |
627 | * because if USB transactions are merged, the last packet will not | |
628 | * be padded. Unaligned access might also happen if the length_info | |
629 | * structure is not present. | |
630 | */ | |
533dd1b0 | 631 | if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG) |
b269825b | 632 | { |
e85d0918 DD |
633 | unsigned int l, k, n; |
634 | for (i = 0, l = 0;; i++) { | |
533dd1b0 | 635 | k = get_unaligned_le16(&length_info->length[i]); |
850c211c UK |
636 | if (k == 0) |
637 | return; | |
e85d0918 DD |
638 | n = l+k; |
639 | if (n > length) | |
640 | return; | |
459c51ad | 641 | zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k); |
e85d0918 DD |
642 | if (i >= 2) |
643 | return; | |
644 | l = (n+3) & ~3; | |
645 | } | |
646 | } else { | |
459c51ad | 647 | zd_mac_rx(zd_usb_to_hw(usb), buffer, length); |
e85d0918 DD |
648 | } |
649 | } | |
650 | ||
7d12e780 | 651 | static void rx_urb_complete(struct urb *urb) |
e85d0918 | 652 | { |
24d24c62 | 653 | int r; |
e85d0918 DD |
654 | struct zd_usb *usb; |
655 | struct zd_usb_rx *rx; | |
656 | const u8 *buffer; | |
657 | unsigned int length; | |
81454b84 | 658 | unsigned long flags; |
e85d0918 DD |
659 | |
660 | switch (urb->status) { | |
661 | case 0: | |
662 | break; | |
663 | case -ESHUTDOWN: | |
664 | case -EINVAL: | |
665 | case -ENODEV: | |
666 | case -ENOENT: | |
667 | case -ECONNRESET: | |
e85d0918 | 668 | case -EPIPE: |
24d24c62 | 669 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
b312d799 | 670 | return; |
e85d0918 DD |
671 | default: |
672 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
673 | goto resubmit; | |
674 | } | |
675 | ||
676 | buffer = urb->transfer_buffer; | |
677 | length = urb->actual_length; | |
678 | usb = urb->context; | |
679 | rx = &usb->rx; | |
680 | ||
02353573 | 681 | tasklet_schedule(&rx->reset_timer_tasklet); |
1f6ccccc | 682 | |
e85d0918 DD |
683 | if (length%rx->usb_packet_size > rx->usb_packet_size-4) { |
684 | /* If there is an old first fragment, we don't care. */ | |
685 | dev_dbg_f(urb_dev(urb), "*** first fragment ***\n"); | |
686 | ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment)); | |
81454b84 | 687 | spin_lock_irqsave(&rx->lock, flags); |
e85d0918 DD |
688 | memcpy(rx->fragment, buffer, length); |
689 | rx->fragment_length = length; | |
81454b84 | 690 | spin_unlock_irqrestore(&rx->lock, flags); |
e85d0918 DD |
691 | goto resubmit; |
692 | } | |
693 | ||
81454b84 | 694 | spin_lock_irqsave(&rx->lock, flags); |
e85d0918 DD |
695 | if (rx->fragment_length > 0) { |
696 | /* We are on a second fragment, we believe */ | |
697 | ZD_ASSERT(length + rx->fragment_length <= | |
698 | ARRAY_SIZE(rx->fragment)); | |
699 | dev_dbg_f(urb_dev(urb), "*** second fragment ***\n"); | |
700 | memcpy(rx->fragment+rx->fragment_length, buffer, length); | |
701 | handle_rx_packet(usb, rx->fragment, | |
702 | rx->fragment_length + length); | |
703 | rx->fragment_length = 0; | |
81454b84 | 704 | spin_unlock_irqrestore(&rx->lock, flags); |
e85d0918 | 705 | } else { |
81454b84 | 706 | spin_unlock_irqrestore(&rx->lock, flags); |
e85d0918 DD |
707 | handle_rx_packet(usb, buffer, length); |
708 | } | |
709 | ||
710 | resubmit: | |
24d24c62 JK |
711 | r = usb_submit_urb(urb, GFP_ATOMIC); |
712 | if (r) | |
713 | dev_dbg_f(urb_dev(urb), "urb %p resubmit error %d\n", urb, r); | |
e85d0918 DD |
714 | } |
715 | ||
459c51ad | 716 | static struct urb *alloc_rx_urb(struct zd_usb *usb) |
e85d0918 DD |
717 | { |
718 | struct usb_device *udev = zd_usb_to_usbdev(usb); | |
719 | struct urb *urb; | |
720 | void *buffer; | |
721 | ||
35c3404e | 722 | urb = usb_alloc_urb(0, GFP_KERNEL); |
e85d0918 DD |
723 | if (!urb) |
724 | return NULL; | |
997ea58e DM |
725 | buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL, |
726 | &urb->transfer_dma); | |
e85d0918 DD |
727 | if (!buffer) { |
728 | usb_free_urb(urb); | |
729 | return NULL; | |
730 | } | |
731 | ||
732 | usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN), | |
997ea58e | 733 | buffer, USB_MAX_RX_SIZE, |
e85d0918 DD |
734 | rx_urb_complete, usb); |
735 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
736 | ||
737 | return urb; | |
738 | } | |
739 | ||
459c51ad | 740 | static void free_rx_urb(struct urb *urb) |
e85d0918 DD |
741 | { |
742 | if (!urb) | |
743 | return; | |
997ea58e DM |
744 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
745 | urb->transfer_buffer, urb->transfer_dma); | |
e85d0918 DD |
746 | usb_free_urb(urb); |
747 | } | |
748 | ||
1f6ccccc | 749 | static int __zd_usb_enable_rx(struct zd_usb *usb) |
e85d0918 DD |
750 | { |
751 | int i, r; | |
752 | struct zd_usb_rx *rx = &usb->rx; | |
753 | struct urb **urbs; | |
754 | ||
755 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
756 | ||
757 | r = -ENOMEM; | |
459c51ad | 758 | urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL); |
e85d0918 DD |
759 | if (!urbs) |
760 | goto error; | |
459c51ad DD |
761 | for (i = 0; i < RX_URBS_COUNT; i++) { |
762 | urbs[i] = alloc_rx_urb(usb); | |
e85d0918 DD |
763 | if (!urbs[i]) |
764 | goto error; | |
765 | } | |
766 | ||
767 | ZD_ASSERT(!irqs_disabled()); | |
768 | spin_lock_irq(&rx->lock); | |
769 | if (rx->urbs) { | |
770 | spin_unlock_irq(&rx->lock); | |
771 | r = 0; | |
772 | goto error; | |
773 | } | |
774 | rx->urbs = urbs; | |
459c51ad | 775 | rx->urbs_count = RX_URBS_COUNT; |
e85d0918 DD |
776 | spin_unlock_irq(&rx->lock); |
777 | ||
459c51ad | 778 | for (i = 0; i < RX_URBS_COUNT; i++) { |
35c3404e | 779 | r = usb_submit_urb(urbs[i], GFP_KERNEL); |
e85d0918 DD |
780 | if (r) |
781 | goto error_submit; | |
782 | } | |
783 | ||
784 | return 0; | |
785 | error_submit: | |
459c51ad | 786 | for (i = 0; i < RX_URBS_COUNT; i++) { |
e85d0918 DD |
787 | usb_kill_urb(urbs[i]); |
788 | } | |
789 | spin_lock_irq(&rx->lock); | |
790 | rx->urbs = NULL; | |
791 | rx->urbs_count = 0; | |
792 | spin_unlock_irq(&rx->lock); | |
793 | error: | |
794 | if (urbs) { | |
459c51ad DD |
795 | for (i = 0; i < RX_URBS_COUNT; i++) |
796 | free_rx_urb(urbs[i]); | |
e85d0918 DD |
797 | } |
798 | return r; | |
799 | } | |
800 | ||
1f6ccccc JK |
801 | int zd_usb_enable_rx(struct zd_usb *usb) |
802 | { | |
803 | int r; | |
804 | struct zd_usb_rx *rx = &usb->rx; | |
805 | ||
806 | mutex_lock(&rx->setup_mutex); | |
807 | r = __zd_usb_enable_rx(usb); | |
808 | mutex_unlock(&rx->setup_mutex); | |
809 | ||
810 | zd_usb_reset_rx_idle_timer(usb); | |
811 | ||
812 | return r; | |
813 | } | |
814 | ||
815 | static void __zd_usb_disable_rx(struct zd_usb *usb) | |
e85d0918 DD |
816 | { |
817 | int i; | |
818 | unsigned long flags; | |
819 | struct urb **urbs; | |
820 | unsigned int count; | |
821 | struct zd_usb_rx *rx = &usb->rx; | |
822 | ||
823 | spin_lock_irqsave(&rx->lock, flags); | |
824 | urbs = rx->urbs; | |
825 | count = rx->urbs_count; | |
826 | spin_unlock_irqrestore(&rx->lock, flags); | |
827 | if (!urbs) | |
828 | return; | |
829 | ||
830 | for (i = 0; i < count; i++) { | |
831 | usb_kill_urb(urbs[i]); | |
459c51ad | 832 | free_rx_urb(urbs[i]); |
e85d0918 DD |
833 | } |
834 | kfree(urbs); | |
835 | ||
836 | spin_lock_irqsave(&rx->lock, flags); | |
837 | rx->urbs = NULL; | |
838 | rx->urbs_count = 0; | |
839 | spin_unlock_irqrestore(&rx->lock, flags); | |
840 | } | |
841 | ||
1f6ccccc JK |
842 | void zd_usb_disable_rx(struct zd_usb *usb) |
843 | { | |
844 | struct zd_usb_rx *rx = &usb->rx; | |
845 | ||
846 | mutex_lock(&rx->setup_mutex); | |
847 | __zd_usb_disable_rx(usb); | |
848 | mutex_unlock(&rx->setup_mutex); | |
849 | ||
02353573 | 850 | tasklet_kill(&rx->reset_timer_tasklet); |
1f6ccccc JK |
851 | cancel_delayed_work_sync(&rx->idle_work); |
852 | } | |
853 | ||
854 | static void zd_usb_reset_rx(struct zd_usb *usb) | |
855 | { | |
856 | bool do_reset; | |
857 | struct zd_usb_rx *rx = &usb->rx; | |
858 | unsigned long flags; | |
859 | ||
860 | mutex_lock(&rx->setup_mutex); | |
861 | ||
862 | spin_lock_irqsave(&rx->lock, flags); | |
863 | do_reset = rx->urbs != NULL; | |
864 | spin_unlock_irqrestore(&rx->lock, flags); | |
865 | ||
866 | if (do_reset) { | |
867 | __zd_usb_disable_rx(usb); | |
868 | __zd_usb_enable_rx(usb); | |
869 | } | |
870 | ||
871 | mutex_unlock(&rx->setup_mutex); | |
872 | ||
873 | if (do_reset) | |
874 | zd_usb_reset_rx_idle_timer(usb); | |
875 | } | |
876 | ||
459c51ad DD |
877 | /** |
878 | * zd_usb_disable_tx - disable transmission | |
879 | * @usb: the zd1211rw-private USB structure | |
880 | * | |
881 | * Frees all URBs in the free list and marks the transmission as disabled. | |
882 | */ | |
883 | void zd_usb_disable_tx(struct zd_usb *usb) | |
884 | { | |
885 | struct zd_usb_tx *tx = &usb->tx; | |
886 | unsigned long flags; | |
78fc800f JK |
887 | |
888 | atomic_set(&tx->enabled, 0); | |
889 | ||
890 | /* kill all submitted tx-urbs */ | |
891 | usb_kill_anchored_urbs(&tx->submitted); | |
459c51ad DD |
892 | |
893 | spin_lock_irqsave(&tx->lock, flags); | |
a0fd751f | 894 | WARN_ON(!skb_queue_empty(&tx->submitted_skbs)); |
78fc800f | 895 | WARN_ON(tx->submitted_urbs != 0); |
459c51ad | 896 | tx->submitted_urbs = 0; |
78fc800f JK |
897 | spin_unlock_irqrestore(&tx->lock, flags); |
898 | ||
459c51ad DD |
899 | /* The stopped state is ignored, relying on ieee80211_wake_queues() |
900 | * in a potentionally following zd_usb_enable_tx(). | |
901 | */ | |
459c51ad DD |
902 | } |
903 | ||
904 | /** | |
905 | * zd_usb_enable_tx - enables transmission | |
906 | * @usb: a &struct zd_usb pointer | |
907 | * | |
908 | * This function enables transmission and prepares the &zd_usb_tx data | |
909 | * structure. | |
910 | */ | |
911 | void zd_usb_enable_tx(struct zd_usb *usb) | |
912 | { | |
913 | unsigned long flags; | |
914 | struct zd_usb_tx *tx = &usb->tx; | |
915 | ||
916 | spin_lock_irqsave(&tx->lock, flags); | |
78fc800f | 917 | atomic_set(&tx->enabled, 1); |
459c51ad DD |
918 | tx->submitted_urbs = 0; |
919 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
920 | tx->stopped = 0; | |
921 | spin_unlock_irqrestore(&tx->lock, flags); | |
922 | } | |
923 | ||
459c51ad DD |
924 | static void tx_dec_submitted_urbs(struct zd_usb *usb) |
925 | { | |
926 | struct zd_usb_tx *tx = &usb->tx; | |
927 | unsigned long flags; | |
928 | ||
929 | spin_lock_irqsave(&tx->lock, flags); | |
930 | --tx->submitted_urbs; | |
931 | if (tx->stopped && tx->submitted_urbs <= ZD_USB_TX_LOW) { | |
932 | ieee80211_wake_queues(zd_usb_to_hw(usb)); | |
933 | tx->stopped = 0; | |
934 | } | |
935 | spin_unlock_irqrestore(&tx->lock, flags); | |
936 | } | |
937 | ||
938 | static void tx_inc_submitted_urbs(struct zd_usb *usb) | |
939 | { | |
940 | struct zd_usb_tx *tx = &usb->tx; | |
941 | unsigned long flags; | |
942 | ||
943 | spin_lock_irqsave(&tx->lock, flags); | |
944 | ++tx->submitted_urbs; | |
945 | if (!tx->stopped && tx->submitted_urbs > ZD_USB_TX_HIGH) { | |
946 | ieee80211_stop_queues(zd_usb_to_hw(usb)); | |
947 | tx->stopped = 1; | |
948 | } | |
949 | spin_unlock_irqrestore(&tx->lock, flags); | |
950 | } | |
951 | ||
952 | /** | |
953 | * tx_urb_complete - completes the execution of an URB | |
954 | * @urb: a URB | |
955 | * | |
956 | * This function is called if the URB has been transferred to a device or an | |
957 | * error has happened. | |
958 | */ | |
7d12e780 | 959 | static void tx_urb_complete(struct urb *urb) |
e85d0918 DD |
960 | { |
961 | int r; | |
459c51ad | 962 | struct sk_buff *skb; |
e039fa4a | 963 | struct ieee80211_tx_info *info; |
459c51ad | 964 | struct zd_usb *usb; |
78fc800f JK |
965 | struct zd_usb_tx *tx; |
966 | ||
967 | skb = (struct sk_buff *)urb->context; | |
968 | info = IEEE80211_SKB_CB(skb); | |
969 | /* | |
970 | * grab 'usb' pointer before handing off the skb (since | |
971 | * it might be freed by zd_mac_tx_to_dev or mac80211) | |
972 | */ | |
973 | usb = &zd_hw_mac(info->rate_driver_data[0])->chip.usb; | |
974 | tx = &usb->tx; | |
e85d0918 DD |
975 | |
976 | switch (urb->status) { | |
977 | case 0: | |
978 | break; | |
979 | case -ESHUTDOWN: | |
980 | case -EINVAL: | |
981 | case -ENODEV: | |
982 | case -ENOENT: | |
983 | case -ECONNRESET: | |
b312d799 | 984 | case -EPIPE: |
e85d0918 DD |
985 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); |
986 | break; | |
e85d0918 DD |
987 | default: |
988 | dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status); | |
989 | goto resubmit; | |
990 | } | |
991 | free_urb: | |
a0fd751f | 992 | skb_unlink(skb, &usb->tx.submitted_skbs); |
dbabad0c | 993 | zd_mac_tx_to_dev(skb, urb->status); |
78fc800f | 994 | usb_free_urb(urb); |
459c51ad | 995 | tx_dec_submitted_urbs(usb); |
e85d0918 DD |
996 | return; |
997 | resubmit: | |
78fc800f | 998 | usb_anchor_urb(urb, &tx->submitted); |
e85d0918 DD |
999 | r = usb_submit_urb(urb, GFP_ATOMIC); |
1000 | if (r) { | |
78fc800f | 1001 | usb_unanchor_urb(urb); |
e85d0918 DD |
1002 | dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r); |
1003 | goto free_urb; | |
1004 | } | |
1005 | } | |
1006 | ||
459c51ad DD |
1007 | /** |
1008 | * zd_usb_tx: initiates transfer of a frame of the device | |
1009 | * | |
1010 | * @usb: the zd1211rw-private USB structure | |
1011 | * @skb: a &struct sk_buff pointer | |
1012 | * | |
1013 | * This function tranmits a frame to the device. It doesn't wait for | |
1014 | * completion. The frame must contain the control set and have all the | |
1015 | * control set information available. | |
1016 | * | |
1017 | * The function returns 0 if the transfer has been successfully initiated. | |
e85d0918 | 1018 | */ |
459c51ad | 1019 | int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb) |
e85d0918 DD |
1020 | { |
1021 | int r; | |
a0fd751f | 1022 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
e85d0918 DD |
1023 | struct usb_device *udev = zd_usb_to_usbdev(usb); |
1024 | struct urb *urb; | |
78fc800f | 1025 | struct zd_usb_tx *tx = &usb->tx; |
e85d0918 | 1026 | |
78fc800f JK |
1027 | if (!atomic_read(&tx->enabled)) { |
1028 | r = -ENOENT; | |
1029 | goto out; | |
1030 | } | |
1031 | ||
1032 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
e85d0918 DD |
1033 | if (!urb) { |
1034 | r = -ENOMEM; | |
1035 | goto out; | |
1036 | } | |
1037 | ||
e85d0918 | 1038 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT), |
459c51ad | 1039 | skb->data, skb->len, tx_urb_complete, skb); |
e85d0918 | 1040 | |
a0fd751f JK |
1041 | info->rate_driver_data[1] = (void *)jiffies; |
1042 | skb_queue_tail(&tx->submitted_skbs, skb); | |
78fc800f | 1043 | usb_anchor_urb(urb, &tx->submitted); |
a0fd751f | 1044 | |
e85d0918 | 1045 | r = usb_submit_urb(urb, GFP_ATOMIC); |
78fc800f | 1046 | if (r) { |
24d24c62 | 1047 | dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r); |
78fc800f | 1048 | usb_unanchor_urb(urb); |
a0fd751f | 1049 | skb_unlink(skb, &tx->submitted_skbs); |
e85d0918 | 1050 | goto error; |
78fc800f | 1051 | } |
459c51ad | 1052 | tx_inc_submitted_urbs(usb); |
e85d0918 DD |
1053 | return 0; |
1054 | error: | |
78fc800f | 1055 | usb_free_urb(urb); |
e85d0918 DD |
1056 | out: |
1057 | return r; | |
1058 | } | |
1059 | ||
a0fd751f JK |
1060 | static bool zd_tx_timeout(struct zd_usb *usb) |
1061 | { | |
1062 | struct zd_usb_tx *tx = &usb->tx; | |
1063 | struct sk_buff_head *q = &tx->submitted_skbs; | |
1064 | struct sk_buff *skb, *skbnext; | |
1065 | struct ieee80211_tx_info *info; | |
1066 | unsigned long flags, trans_start; | |
1067 | bool have_timedout = false; | |
1068 | ||
1069 | spin_lock_irqsave(&q->lock, flags); | |
1070 | skb_queue_walk_safe(q, skb, skbnext) { | |
1071 | info = IEEE80211_SKB_CB(skb); | |
1072 | trans_start = (unsigned long)info->rate_driver_data[1]; | |
1073 | ||
1074 | if (time_is_before_jiffies(trans_start + ZD_TX_TIMEOUT)) { | |
1075 | have_timedout = true; | |
1076 | break; | |
1077 | } | |
1078 | } | |
1079 | spin_unlock_irqrestore(&q->lock, flags); | |
1080 | ||
1081 | return have_timedout; | |
1082 | } | |
1083 | ||
1084 | static void zd_tx_watchdog_handler(struct work_struct *work) | |
1085 | { | |
1086 | struct zd_usb *usb = | |
1087 | container_of(work, struct zd_usb, tx.watchdog_work.work); | |
1088 | struct zd_usb_tx *tx = &usb->tx; | |
1089 | ||
1090 | if (!atomic_read(&tx->enabled) || !tx->watchdog_enabled) | |
1091 | goto out; | |
1092 | if (!zd_tx_timeout(usb)) | |
1093 | goto out; | |
1094 | ||
1095 | /* TX halted, try reset */ | |
50db7fa3 | 1096 | dev_warn(zd_usb_dev(usb), "TX-stall detected, resetting device..."); |
a0fd751f JK |
1097 | |
1098 | usb_queue_reset_device(usb->intf); | |
1099 | ||
1100 | /* reset will stop this worker, don't rearm */ | |
1101 | return; | |
1102 | out: | |
1103 | queue_delayed_work(zd_workqueue, &tx->watchdog_work, | |
1104 | ZD_TX_WATCHDOG_INTERVAL); | |
1105 | } | |
1106 | ||
1107 | void zd_tx_watchdog_enable(struct zd_usb *usb) | |
1108 | { | |
1109 | struct zd_usb_tx *tx = &usb->tx; | |
1110 | ||
1111 | if (!tx->watchdog_enabled) { | |
1112 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1113 | queue_delayed_work(zd_workqueue, &tx->watchdog_work, | |
1114 | ZD_TX_WATCHDOG_INTERVAL); | |
1115 | tx->watchdog_enabled = 1; | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | void zd_tx_watchdog_disable(struct zd_usb *usb) | |
1120 | { | |
1121 | struct zd_usb_tx *tx = &usb->tx; | |
1122 | ||
1123 | if (tx->watchdog_enabled) { | |
1124 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1125 | tx->watchdog_enabled = 0; | |
1126 | cancel_delayed_work_sync(&tx->watchdog_work); | |
1127 | } | |
1128 | } | |
1129 | ||
1f6ccccc JK |
1130 | static void zd_rx_idle_timer_handler(struct work_struct *work) |
1131 | { | |
1132 | struct zd_usb *usb = | |
1133 | container_of(work, struct zd_usb, rx.idle_work.work); | |
1134 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1135 | ||
1136 | if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags)) | |
1137 | return; | |
1138 | ||
1139 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1140 | ||
1141 | /* 30 seconds since last rx, reset rx */ | |
1142 | zd_usb_reset_rx(usb); | |
1143 | } | |
1144 | ||
02353573 JK |
1145 | static void zd_usb_reset_rx_idle_timer_tasklet(unsigned long param) |
1146 | { | |
1147 | struct zd_usb *usb = (struct zd_usb *)param; | |
1148 | ||
1149 | zd_usb_reset_rx_idle_timer(usb); | |
1150 | } | |
1151 | ||
1f6ccccc JK |
1152 | void zd_usb_reset_rx_idle_timer(struct zd_usb *usb) |
1153 | { | |
1154 | struct zd_usb_rx *rx = &usb->rx; | |
1155 | ||
41f63c53 | 1156 | mod_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL); |
1f6ccccc JK |
1157 | } |
1158 | ||
e85d0918 DD |
1159 | static inline void init_usb_interrupt(struct zd_usb *usb) |
1160 | { | |
1161 | struct zd_usb_interrupt *intr = &usb->intr; | |
1162 | ||
1163 | spin_lock_init(&intr->lock); | |
1164 | intr->interval = int_urb_interval(zd_usb_to_usbdev(usb)); | |
1165 | init_completion(&intr->read_regs.completion); | |
c900eff3 | 1166 | atomic_set(&intr->read_regs_enabled, 0); |
0ce34bc8 | 1167 | intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT); |
e85d0918 DD |
1168 | } |
1169 | ||
1170 | static inline void init_usb_rx(struct zd_usb *usb) | |
1171 | { | |
1172 | struct zd_usb_rx *rx = &usb->rx; | |
02353573 | 1173 | |
e85d0918 | 1174 | spin_lock_init(&rx->lock); |
1f6ccccc | 1175 | mutex_init(&rx->setup_mutex); |
e85d0918 DD |
1176 | if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) { |
1177 | rx->usb_packet_size = 512; | |
1178 | } else { | |
1179 | rx->usb_packet_size = 64; | |
1180 | } | |
1181 | ZD_ASSERT(rx->fragment_length == 0); | |
1f6ccccc | 1182 | INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler); |
02353573 JK |
1183 | rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet; |
1184 | rx->reset_timer_tasklet.data = (unsigned long)usb; | |
e85d0918 DD |
1185 | } |
1186 | ||
1187 | static inline void init_usb_tx(struct zd_usb *usb) | |
1188 | { | |
459c51ad | 1189 | struct zd_usb_tx *tx = &usb->tx; |
02353573 | 1190 | |
459c51ad | 1191 | spin_lock_init(&tx->lock); |
78fc800f | 1192 | atomic_set(&tx->enabled, 0); |
459c51ad | 1193 | tx->stopped = 0; |
a0fd751f | 1194 | skb_queue_head_init(&tx->submitted_skbs); |
78fc800f | 1195 | init_usb_anchor(&tx->submitted); |
459c51ad | 1196 | tx->submitted_urbs = 0; |
a0fd751f JK |
1197 | tx->watchdog_enabled = 0; |
1198 | INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler); | |
e85d0918 DD |
1199 | } |
1200 | ||
459c51ad | 1201 | void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw, |
e85d0918 DD |
1202 | struct usb_interface *intf) |
1203 | { | |
1204 | memset(usb, 0, sizeof(*usb)); | |
1205 | usb->intf = usb_get_intf(intf); | |
459c51ad | 1206 | usb_set_intfdata(usb->intf, hw); |
eefdbec1 | 1207 | init_usb_anchor(&usb->submitted_cmds); |
e85d0918 DD |
1208 | init_usb_interrupt(usb); |
1209 | init_usb_tx(usb); | |
1210 | init_usb_rx(usb); | |
1211 | } | |
1212 | ||
e85d0918 DD |
1213 | void zd_usb_clear(struct zd_usb *usb) |
1214 | { | |
1215 | usb_set_intfdata(usb->intf, NULL); | |
1216 | usb_put_intf(usb->intf); | |
c48cf125 | 1217 | ZD_MEMCLEAR(usb, sizeof(*usb)); |
e85d0918 DD |
1218 | /* FIXME: usb_interrupt, usb_tx, usb_rx? */ |
1219 | } | |
1220 | ||
1221 | static const char *speed(enum usb_device_speed speed) | |
1222 | { | |
1223 | switch (speed) { | |
1224 | case USB_SPEED_LOW: | |
1225 | return "low"; | |
1226 | case USB_SPEED_FULL: | |
1227 | return "full"; | |
1228 | case USB_SPEED_HIGH: | |
1229 | return "high"; | |
1230 | default: | |
1231 | return "unknown speed"; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | static int scnprint_id(struct usb_device *udev, char *buffer, size_t size) | |
1236 | { | |
1237 | return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s", | |
1238 | le16_to_cpu(udev->descriptor.idVendor), | |
1239 | le16_to_cpu(udev->descriptor.idProduct), | |
1240 | get_bcdDevice(udev), | |
1241 | speed(udev->speed)); | |
1242 | } | |
1243 | ||
1244 | int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size) | |
1245 | { | |
1246 | struct usb_device *udev = interface_to_usbdev(usb->intf); | |
1247 | return scnprint_id(udev, buffer, size); | |
1248 | } | |
1249 | ||
1250 | #ifdef DEBUG | |
1251 | static void print_id(struct usb_device *udev) | |
1252 | { | |
1253 | char buffer[40]; | |
1254 | ||
1255 | scnprint_id(udev, buffer, sizeof(buffer)); | |
1256 | buffer[sizeof(buffer)-1] = 0; | |
1257 | dev_dbg_f(&udev->dev, "%s\n", buffer); | |
1258 | } | |
1259 | #else | |
1260 | #define print_id(udev) do { } while (0) | |
1261 | #endif | |
1262 | ||
a1030e92 DD |
1263 | static int eject_installer(struct usb_interface *intf) |
1264 | { | |
1265 | struct usb_device *udev = interface_to_usbdev(intf); | |
1266 | struct usb_host_interface *iface_desc = &intf->altsetting[0]; | |
1267 | struct usb_endpoint_descriptor *endpoint; | |
1268 | unsigned char *cmd; | |
1269 | u8 bulk_out_ep; | |
1270 | int r; | |
1271 | ||
ca260ece JH |
1272 | if (iface_desc->desc.bNumEndpoints < 2) |
1273 | return -ENODEV; | |
1274 | ||
a1030e92 | 1275 | /* Find bulk out endpoint */ |
11466f13 SS |
1276 | for (r = 1; r >= 0; r--) { |
1277 | endpoint = &iface_desc->endpoint[r].desc; | |
1278 | if (usb_endpoint_dir_out(endpoint) && | |
1279 | usb_endpoint_xfer_bulk(endpoint)) { | |
1280 | bulk_out_ep = endpoint->bEndpointAddress; | |
1281 | break; | |
1282 | } | |
1283 | } | |
1284 | if (r == -1) { | |
a1030e92 DD |
1285 | dev_err(&udev->dev, |
1286 | "zd1211rw: Could not find bulk out endpoint\n"); | |
1287 | return -ENODEV; | |
1288 | } | |
1289 | ||
1290 | cmd = kzalloc(31, GFP_KERNEL); | |
1291 | if (cmd == NULL) | |
1292 | return -ENODEV; | |
1293 | ||
1294 | /* USB bulk command block */ | |
1295 | cmd[0] = 0x55; /* bulk command signature */ | |
1296 | cmd[1] = 0x53; /* bulk command signature */ | |
1297 | cmd[2] = 0x42; /* bulk command signature */ | |
1298 | cmd[3] = 0x43; /* bulk command signature */ | |
1299 | cmd[14] = 6; /* command length */ | |
1300 | ||
1301 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ | |
1302 | cmd[19] = 0x2; /* eject disc */ | |
1303 | ||
1304 | dev_info(&udev->dev, "Ejecting virtual installer media...\n"); | |
1305 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), | |
1306 | cmd, 31, NULL, 2000); | |
1307 | kfree(cmd); | |
1308 | if (r) | |
1309 | return r; | |
1310 | ||
1311 | /* At this point, the device disconnects and reconnects with the real | |
1312 | * ID numbers. */ | |
1313 | ||
1314 | usb_set_intfdata(intf, NULL); | |
1315 | return 0; | |
1316 | } | |
1317 | ||
74553aed DD |
1318 | int zd_usb_init_hw(struct zd_usb *usb) |
1319 | { | |
1320 | int r; | |
1321 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1322 | ||
1323 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1324 | ||
1325 | r = upload_firmware(usb); | |
1326 | if (r) { | |
1327 | dev_err(zd_usb_dev(usb), | |
1328 | "couldn't load firmware. Error number %d\n", r); | |
1329 | return r; | |
1330 | } | |
1331 | ||
1332 | r = usb_reset_configuration(zd_usb_to_usbdev(usb)); | |
1333 | if (r) { | |
1334 | dev_dbg_f(zd_usb_dev(usb), | |
1335 | "couldn't reset configuration. Error number %d\n", r); | |
1336 | return r; | |
1337 | } | |
1338 | ||
459c51ad | 1339 | r = zd_mac_init_hw(mac->hw); |
74553aed DD |
1340 | if (r) { |
1341 | dev_dbg_f(zd_usb_dev(usb), | |
1342 | "couldn't initialize mac. Error number %d\n", r); | |
1343 | return r; | |
1344 | } | |
1345 | ||
1346 | usb->initialized = 1; | |
1347 | return 0; | |
1348 | } | |
1349 | ||
e85d0918 DD |
1350 | static int probe(struct usb_interface *intf, const struct usb_device_id *id) |
1351 | { | |
1352 | int r; | |
1353 | struct usb_device *udev = interface_to_usbdev(intf); | |
459c51ad DD |
1354 | struct zd_usb *usb; |
1355 | struct ieee80211_hw *hw = NULL; | |
e85d0918 DD |
1356 | |
1357 | print_id(udev); | |
1358 | ||
a1030e92 DD |
1359 | if (id->driver_info & DEVICE_INSTALLER) |
1360 | return eject_installer(intf); | |
1361 | ||
e85d0918 DD |
1362 | switch (udev->speed) { |
1363 | case USB_SPEED_LOW: | |
1364 | case USB_SPEED_FULL: | |
1365 | case USB_SPEED_HIGH: | |
1366 | break; | |
1367 | default: | |
1368 | dev_dbg_f(&intf->dev, "Unknown USB speed\n"); | |
1369 | r = -ENODEV; | |
1370 | goto error; | |
1371 | } | |
1372 | ||
459c51ad DD |
1373 | r = usb_reset_device(udev); |
1374 | if (r) { | |
1375 | dev_err(&intf->dev, | |
1376 | "couldn't reset usb device. Error number %d\n", r); | |
1377 | goto error; | |
1378 | } | |
6e3632f6 | 1379 | |
459c51ad DD |
1380 | hw = zd_mac_alloc_hw(intf); |
1381 | if (hw == NULL) { | |
e85d0918 DD |
1382 | r = -ENOMEM; |
1383 | goto error; | |
1384 | } | |
1385 | ||
459c51ad | 1386 | usb = &zd_hw_mac(hw)->chip.usb; |
74553aed | 1387 | usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0; |
e85d0918 | 1388 | |
459c51ad | 1389 | r = zd_mac_preinit_hw(hw); |
e85d0918 DD |
1390 | if (r) { |
1391 | dev_dbg_f(&intf->dev, | |
1392 | "couldn't initialize mac. Error number %d\n", r); | |
1393 | goto error; | |
1394 | } | |
1395 | ||
459c51ad | 1396 | r = ieee80211_register_hw(hw); |
e85d0918 DD |
1397 | if (r) { |
1398 | dev_dbg_f(&intf->dev, | |
459c51ad | 1399 | "couldn't register device. Error number %d\n", r); |
e85d0918 DD |
1400 | goto error; |
1401 | } | |
1402 | ||
1403 | dev_dbg_f(&intf->dev, "successful\n"); | |
459c51ad | 1404 | dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy)); |
e85d0918 DD |
1405 | return 0; |
1406 | error: | |
1407 | usb_reset_device(interface_to_usbdev(intf)); | |
459c51ad DD |
1408 | if (hw) { |
1409 | zd_mac_clear(zd_hw_mac(hw)); | |
1410 | ieee80211_free_hw(hw); | |
1411 | } | |
e85d0918 DD |
1412 | return r; |
1413 | } | |
1414 | ||
1415 | static void disconnect(struct usb_interface *intf) | |
1416 | { | |
459c51ad | 1417 | struct ieee80211_hw *hw = zd_intf_to_hw(intf); |
e0579d57 MP |
1418 | struct zd_mac *mac; |
1419 | struct zd_usb *usb; | |
e85d0918 | 1420 | |
a1030e92 DD |
1421 | /* Either something really bad happened, or we're just dealing with |
1422 | * a DEVICE_INSTALLER. */ | |
459c51ad | 1423 | if (hw == NULL) |
a1030e92 DD |
1424 | return; |
1425 | ||
459c51ad | 1426 | mac = zd_hw_mac(hw); |
e0579d57 MP |
1427 | usb = &mac->chip.usb; |
1428 | ||
e85d0918 DD |
1429 | dev_dbg_f(zd_usb_dev(usb), "\n"); |
1430 | ||
459c51ad | 1431 | ieee80211_unregister_hw(hw); |
e85d0918 DD |
1432 | |
1433 | /* Just in case something has gone wrong! */ | |
78fc800f | 1434 | zd_usb_disable_tx(usb); |
e85d0918 DD |
1435 | zd_usb_disable_rx(usb); |
1436 | zd_usb_disable_int(usb); | |
1437 | ||
1438 | /* If the disconnect has been caused by a removal of the | |
1439 | * driver module, the reset allows reloading of the driver. If the | |
1440 | * reset will not be executed here, the upload of the firmware in the | |
1441 | * probe function caused by the reloading of the driver will fail. | |
1442 | */ | |
1443 | usb_reset_device(interface_to_usbdev(intf)); | |
1444 | ||
459c51ad DD |
1445 | zd_mac_clear(mac); |
1446 | ieee80211_free_hw(hw); | |
e85d0918 DD |
1447 | dev_dbg(&intf->dev, "disconnected\n"); |
1448 | } | |
1449 | ||
a0fd751f JK |
1450 | static void zd_usb_resume(struct zd_usb *usb) |
1451 | { | |
1452 | struct zd_mac *mac = zd_usb_to_mac(usb); | |
1453 | int r; | |
1454 | ||
1455 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1456 | ||
1457 | r = zd_op_start(zd_usb_to_hw(usb)); | |
1458 | if (r < 0) { | |
1459 | dev_warn(zd_usb_dev(usb), "Device resume failed " | |
1460 | "with error code %d. Retrying...\n", r); | |
1461 | if (usb->was_running) | |
1462 | set_bit(ZD_DEVICE_RUNNING, &mac->flags); | |
1463 | usb_queue_reset_device(usb->intf); | |
1464 | return; | |
1465 | } | |
1466 | ||
1467 | if (mac->type != NL80211_IFTYPE_UNSPECIFIED) { | |
1468 | r = zd_restore_settings(mac); | |
1469 | if (r < 0) { | |
1470 | dev_dbg(zd_usb_dev(usb), | |
1471 | "failed to restore settings, %d\n", r); | |
1472 | return; | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | static void zd_usb_stop(struct zd_usb *usb) | |
1478 | { | |
1479 | dev_dbg_f(zd_usb_dev(usb), "\n"); | |
1480 | ||
1481 | zd_op_stop(zd_usb_to_hw(usb)); | |
1482 | ||
1483 | zd_usb_disable_tx(usb); | |
1484 | zd_usb_disable_rx(usb); | |
1485 | zd_usb_disable_int(usb); | |
1486 | ||
1487 | usb->initialized = 0; | |
1488 | } | |
1489 | ||
1490 | static int pre_reset(struct usb_interface *intf) | |
1491 | { | |
1492 | struct ieee80211_hw *hw = usb_get_intfdata(intf); | |
1493 | struct zd_mac *mac; | |
1494 | struct zd_usb *usb; | |
1495 | ||
1496 | if (!hw || intf->condition != USB_INTERFACE_BOUND) | |
1497 | return 0; | |
1498 | ||
1499 | mac = zd_hw_mac(hw); | |
1500 | usb = &mac->chip.usb; | |
1501 | ||
1502 | usb->was_running = test_bit(ZD_DEVICE_RUNNING, &mac->flags); | |
1503 | ||
1504 | zd_usb_stop(usb); | |
1505 | ||
1506 | mutex_lock(&mac->chip.mutex); | |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | static int post_reset(struct usb_interface *intf) | |
1511 | { | |
1512 | struct ieee80211_hw *hw = usb_get_intfdata(intf); | |
1513 | struct zd_mac *mac; | |
1514 | struct zd_usb *usb; | |
1515 | ||
1516 | if (!hw || intf->condition != USB_INTERFACE_BOUND) | |
1517 | return 0; | |
1518 | ||
1519 | mac = zd_hw_mac(hw); | |
1520 | usb = &mac->chip.usb; | |
1521 | ||
1522 | mutex_unlock(&mac->chip.mutex); | |
1523 | ||
1524 | if (usb->was_running) | |
1525 | zd_usb_resume(usb); | |
1526 | return 0; | |
1527 | } | |
1528 | ||
e85d0918 | 1529 | static struct usb_driver driver = { |
459c51ad | 1530 | .name = KBUILD_MODNAME, |
e85d0918 DD |
1531 | .id_table = usb_ids, |
1532 | .probe = probe, | |
1533 | .disconnect = disconnect, | |
a0fd751f JK |
1534 | .pre_reset = pre_reset, |
1535 | .post_reset = post_reset, | |
e1f12eb6 | 1536 | .disable_hub_initiated_lpm = 1, |
e85d0918 DD |
1537 | }; |
1538 | ||
bc5f06a8 UK |
1539 | struct workqueue_struct *zd_workqueue; |
1540 | ||
e85d0918 DD |
1541 | static int __init usb_init(void) |
1542 | { | |
1543 | int r; | |
1544 | ||
741fec53 | 1545 | pr_debug("%s usb_init()\n", driver.name); |
e85d0918 | 1546 | |
bc5f06a8 UK |
1547 | zd_workqueue = create_singlethread_workqueue(driver.name); |
1548 | if (zd_workqueue == NULL) { | |
741fec53 | 1549 | printk(KERN_ERR "%s couldn't create workqueue\n", driver.name); |
bc5f06a8 UK |
1550 | return -ENOMEM; |
1551 | } | |
1552 | ||
e85d0918 DD |
1553 | r = usb_register(&driver); |
1554 | if (r) { | |
192b775c | 1555 | destroy_workqueue(zd_workqueue); |
741fec53 UK |
1556 | printk(KERN_ERR "%s usb_register() failed. Error number %d\n", |
1557 | driver.name, r); | |
e85d0918 DD |
1558 | return r; |
1559 | } | |
1560 | ||
741fec53 | 1561 | pr_debug("%s initialized\n", driver.name); |
e85d0918 DD |
1562 | return 0; |
1563 | } | |
1564 | ||
1565 | static void __exit usb_exit(void) | |
1566 | { | |
741fec53 | 1567 | pr_debug("%s usb_exit()\n", driver.name); |
e85d0918 | 1568 | usb_deregister(&driver); |
bc5f06a8 | 1569 | destroy_workqueue(zd_workqueue); |
e85d0918 DD |
1570 | } |
1571 | ||
1572 | module_init(usb_init); | |
1573 | module_exit(usb_exit); | |
1574 | ||
59342f6a JK |
1575 | static int zd_ep_regs_out_msg(struct usb_device *udev, void *data, int len, |
1576 | int *actual_length, int timeout) | |
1577 | { | |
1578 | /* In USB 2.0 mode EP_REGS_OUT endpoint is interrupt type. However in | |
1579 | * USB 1.1 mode endpoint is bulk. Select correct type URB by endpoint | |
1580 | * descriptor. | |
1581 | */ | |
1582 | struct usb_host_endpoint *ep; | |
1583 | unsigned int pipe; | |
1584 | ||
1585 | pipe = usb_sndintpipe(udev, EP_REGS_OUT); | |
1586 | ep = usb_pipe_endpoint(udev, pipe); | |
1587 | if (!ep) | |
1588 | return -EINVAL; | |
1589 | ||
1590 | if (usb_endpoint_xfer_int(&ep->desc)) { | |
1591 | return usb_interrupt_msg(udev, pipe, data, len, | |
1592 | actual_length, timeout); | |
1593 | } else { | |
1594 | pipe = usb_sndbulkpipe(udev, EP_REGS_OUT); | |
1595 | return usb_bulk_msg(udev, pipe, data, len, actual_length, | |
1596 | timeout); | |
1597 | } | |
1598 | } | |
1599 | ||
e85d0918 DD |
1600 | static int usb_int_regs_length(unsigned int count) |
1601 | { | |
1602 | return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data); | |
1603 | } | |
1604 | ||
c900eff3 JK |
1605 | static void prepare_read_regs_int(struct zd_usb *usb, |
1606 | struct usb_req_read_regs *req, | |
1607 | unsigned int count) | |
e85d0918 DD |
1608 | { |
1609 | struct zd_usb_interrupt *intr = &usb->intr; | |
1610 | ||
a68077de | 1611 | spin_lock_irq(&intr->lock); |
c900eff3 JK |
1612 | atomic_set(&intr->read_regs_enabled, 1); |
1613 | intr->read_regs.req = req; | |
1614 | intr->read_regs.req_count = count; | |
16735d02 | 1615 | reinit_completion(&intr->read_regs.completion); |
a68077de UK |
1616 | spin_unlock_irq(&intr->lock); |
1617 | } | |
1618 | ||
1619 | static void disable_read_regs_int(struct zd_usb *usb) | |
1620 | { | |
1621 | struct zd_usb_interrupt *intr = &usb->intr; | |
1622 | ||
1623 | spin_lock_irq(&intr->lock); | |
c900eff3 | 1624 | atomic_set(&intr->read_regs_enabled, 0); |
a68077de | 1625 | spin_unlock_irq(&intr->lock); |
e85d0918 DD |
1626 | } |
1627 | ||
c900eff3 JK |
1628 | static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req, |
1629 | unsigned int count) | |
e85d0918 | 1630 | { |
e85d0918 DD |
1631 | int i; |
1632 | struct zd_usb_interrupt *intr = &usb->intr; | |
1633 | struct read_regs_int *rr = &intr->read_regs; | |
1634 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; | |
1635 | ||
e85d0918 DD |
1636 | /* The created block size seems to be larger than expected. |
1637 | * However results appear to be correct. | |
1638 | */ | |
1639 | if (rr->length < usb_int_regs_length(count)) { | |
1640 | dev_dbg_f(zd_usb_dev(usb), | |
1641 | "error: actual length %d less than expected %d\n", | |
1642 | rr->length, usb_int_regs_length(count)); | |
c900eff3 | 1643 | return false; |
e85d0918 | 1644 | } |
c900eff3 | 1645 | |
e85d0918 DD |
1646 | if (rr->length > sizeof(rr->buffer)) { |
1647 | dev_dbg_f(zd_usb_dev(usb), | |
1648 | "error: actual length %d exceeds buffer size %zu\n", | |
1649 | rr->length, sizeof(rr->buffer)); | |
c900eff3 | 1650 | return false; |
e85d0918 DD |
1651 | } |
1652 | ||
1653 | for (i = 0; i < count; i++) { | |
1654 | struct reg_data *rd = ®s->regs[i]; | |
1655 | if (rd->addr != req->addr[i]) { | |
1656 | dev_dbg_f(zd_usb_dev(usb), | |
1657 | "rd[%d] addr %#06hx expected %#06hx\n", i, | |
1658 | le16_to_cpu(rd->addr), | |
1659 | le16_to_cpu(req->addr[i])); | |
c900eff3 | 1660 | return false; |
e85d0918 | 1661 | } |
c900eff3 JK |
1662 | } |
1663 | ||
1664 | return true; | |
1665 | } | |
1666 | ||
1667 | static int get_results(struct zd_usb *usb, u16 *values, | |
1668 | struct usb_req_read_regs *req, unsigned int count, | |
1669 | bool *retry) | |
1670 | { | |
1671 | int r; | |
1672 | int i; | |
1673 | struct zd_usb_interrupt *intr = &usb->intr; | |
1674 | struct read_regs_int *rr = &intr->read_regs; | |
1675 | struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer; | |
1676 | ||
1677 | spin_lock_irq(&intr->lock); | |
1678 | ||
1679 | r = -EIO; | |
1680 | ||
1681 | /* Read failed because firmware bug? */ | |
1682 | *retry = !!intr->read_regs_int_overridden; | |
1683 | if (*retry) | |
1684 | goto error_unlock; | |
1685 | ||
1686 | if (!check_read_regs(usb, req, count)) { | |
1687 | dev_dbg_f(zd_usb_dev(usb), "error: invalid read regs\n"); | |
1688 | goto error_unlock; | |
1689 | } | |
1690 | ||
1691 | for (i = 0; i < count; i++) { | |
1692 | struct reg_data *rd = ®s->regs[i]; | |
e85d0918 DD |
1693 | values[i] = le16_to_cpu(rd->value); |
1694 | } | |
1695 | ||
1696 | r = 0; | |
1697 | error_unlock: | |
a68077de | 1698 | spin_unlock_irq(&intr->lock); |
e85d0918 DD |
1699 | return r; |
1700 | } | |
1701 | ||
1702 | int zd_usb_ioread16v(struct zd_usb *usb, u16 *values, | |
1703 | const zd_addr_t *addresses, unsigned int count) | |
1704 | { | |
c900eff3 | 1705 | int r, i, req_len, actual_req_len, try_count = 0; |
e85d0918 DD |
1706 | struct usb_device *udev; |
1707 | struct usb_req_read_regs *req = NULL; | |
1708 | unsigned long timeout; | |
c900eff3 | 1709 | bool retry = false; |
e85d0918 DD |
1710 | |
1711 | if (count < 1) { | |
1712 | dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n"); | |
1713 | return -EINVAL; | |
1714 | } | |
1715 | if (count > USB_MAX_IOREAD16_COUNT) { | |
1716 | dev_dbg_f(zd_usb_dev(usb), | |
1717 | "error: count %u exceeds possible max %u\n", | |
1718 | count, USB_MAX_IOREAD16_COUNT); | |
1719 | return -EINVAL; | |
1720 | } | |
1721 | if (in_atomic()) { | |
1722 | dev_dbg_f(zd_usb_dev(usb), | |
1723 | "error: io in atomic context not supported\n"); | |
1724 | return -EWOULDBLOCK; | |
1725 | } | |
1726 | if (!usb_int_enabled(usb)) { | |
9bca0c3b | 1727 | dev_dbg_f(zd_usb_dev(usb), |
e85d0918 DD |
1728 | "error: usb interrupt not enabled\n"); |
1729 | return -EWOULDBLOCK; | |
1730 | } | |
1731 | ||
9bca0c3b JK |
1732 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); |
1733 | BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT * | |
1734 | sizeof(__le16) > sizeof(usb->req_buf)); | |
1735 | BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) > | |
1736 | sizeof(usb->req_buf)); | |
1737 | ||
e85d0918 | 1738 | req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16); |
9bca0c3b JK |
1739 | req = (void *)usb->req_buf; |
1740 | ||
e85d0918 DD |
1741 | req->id = cpu_to_le16(USB_REQ_READ_REGS); |
1742 | for (i = 0; i < count; i++) | |
0ce34bc8 | 1743 | req->addr[i] = cpu_to_le16((u16)addresses[i]); |
e85d0918 | 1744 | |
c900eff3 JK |
1745 | retry_read: |
1746 | try_count++; | |
e85d0918 | 1747 | udev = zd_usb_to_usbdev(usb); |
c900eff3 | 1748 | prepare_read_regs_int(usb, req, count); |
59342f6a | 1749 | r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/); |
e85d0918 DD |
1750 | if (r) { |
1751 | dev_dbg_f(zd_usb_dev(usb), | |
59342f6a | 1752 | "error in zd_ep_regs_out_msg(). Error number %d\n", r); |
e85d0918 DD |
1753 | goto error; |
1754 | } | |
1755 | if (req_len != actual_req_len) { | |
59342f6a | 1756 | dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()\n" |
e85d0918 DD |
1757 | " req_len %d != actual_req_len %d\n", |
1758 | req_len, actual_req_len); | |
1759 | r = -EIO; | |
1760 | goto error; | |
1761 | } | |
1762 | ||
1763 | timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion, | |
8f2d8f86 | 1764 | msecs_to_jiffies(50)); |
e85d0918 DD |
1765 | if (!timeout) { |
1766 | disable_read_regs_int(usb); | |
1767 | dev_dbg_f(zd_usb_dev(usb), "read timed out\n"); | |
1768 | r = -ETIMEDOUT; | |
1769 | goto error; | |
1770 | } | |
1771 | ||
c900eff3 JK |
1772 | r = get_results(usb, values, req, count, &retry); |
1773 | if (retry && try_count < 20) { | |
1774 | dev_dbg_f(zd_usb_dev(usb), "read retry, tries so far: %d\n", | |
1775 | try_count); | |
1776 | goto retry_read; | |
1777 | } | |
e85d0918 | 1778 | error: |
e85d0918 DD |
1779 | return r; |
1780 | } | |
1781 | ||
eefdbec1 JK |
1782 | static void iowrite16v_urb_complete(struct urb *urb) |
1783 | { | |
1784 | struct zd_usb *usb = urb->context; | |
1785 | ||
1786 | if (urb->status && !usb->cmd_error) | |
1787 | usb->cmd_error = urb->status; | |
2fc713b2 JK |
1788 | |
1789 | if (!usb->cmd_error && | |
1790 | urb->actual_length != urb->transfer_buffer_length) | |
1791 | usb->cmd_error = -EIO; | |
eefdbec1 JK |
1792 | } |
1793 | ||
1794 | static int zd_submit_waiting_urb(struct zd_usb *usb, bool last) | |
1795 | { | |
8662b251 | 1796 | int r = 0; |
eefdbec1 JK |
1797 | struct urb *urb = usb->urb_async_waiting; |
1798 | ||
1799 | if (!urb) | |
1800 | return 0; | |
1801 | ||
1802 | usb->urb_async_waiting = NULL; | |
1803 | ||
1804 | if (!last) | |
1805 | urb->transfer_flags |= URB_NO_INTERRUPT; | |
1806 | ||
1807 | usb_anchor_urb(urb, &usb->submitted_cmds); | |
1808 | r = usb_submit_urb(urb, GFP_KERNEL); | |
1809 | if (r) { | |
1810 | usb_unanchor_urb(urb); | |
1811 | dev_dbg_f(zd_usb_dev(usb), | |
1812 | "error in usb_submit_urb(). Error number %d\n", r); | |
1813 | goto error; | |
1814 | } | |
1815 | ||
1816 | /* fall-through with r == 0 */ | |
1817 | error: | |
1818 | usb_free_urb(urb); | |
1819 | return r; | |
1820 | } | |
1821 | ||
8662b251 | 1822 | void zd_usb_iowrite16v_async_start(struct zd_usb *usb) |
eefdbec1 JK |
1823 | { |
1824 | ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds)); | |
1825 | ZD_ASSERT(usb->urb_async_waiting == NULL); | |
1826 | ZD_ASSERT(!usb->in_async); | |
1827 | ||
1828 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1829 | ||
1830 | usb->in_async = 1; | |
1831 | usb->cmd_error = 0; | |
1832 | usb->urb_async_waiting = NULL; | |
1833 | } | |
1834 | ||
8662b251 | 1835 | int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout) |
eefdbec1 JK |
1836 | { |
1837 | int r; | |
1838 | ||
1839 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1840 | ZD_ASSERT(usb->in_async); | |
1841 | ||
1842 | /* Submit last iowrite16v URB */ | |
1843 | r = zd_submit_waiting_urb(usb, true); | |
1844 | if (r) { | |
1845 | dev_dbg_f(zd_usb_dev(usb), | |
1846 | "error in zd_submit_waiting_usb(). " | |
1847 | "Error number %d\n", r); | |
1848 | ||
1849 | usb_kill_anchored_urbs(&usb->submitted_cmds); | |
1850 | goto error; | |
1851 | } | |
1852 | ||
1853 | if (timeout) | |
1854 | timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds, | |
1855 | timeout); | |
1856 | if (!timeout) { | |
1857 | usb_kill_anchored_urbs(&usb->submitted_cmds); | |
1858 | if (usb->cmd_error == -ENOENT) { | |
1859 | dev_dbg_f(zd_usb_dev(usb), "timed out"); | |
1860 | r = -ETIMEDOUT; | |
1861 | goto error; | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | r = usb->cmd_error; | |
1866 | error: | |
1867 | usb->in_async = 0; | |
1868 | return r; | |
1869 | } | |
1870 | ||
8662b251 JK |
1871 | int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
1872 | unsigned int count) | |
e85d0918 DD |
1873 | { |
1874 | int r; | |
1875 | struct usb_device *udev; | |
1876 | struct usb_req_write_regs *req = NULL; | |
eefdbec1 JK |
1877 | int i, req_len; |
1878 | struct urb *urb; | |
1879 | struct usb_host_endpoint *ep; | |
1880 | ||
1881 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); | |
1882 | ZD_ASSERT(usb->in_async); | |
e85d0918 DD |
1883 | |
1884 | if (count == 0) | |
1885 | return 0; | |
1886 | if (count > USB_MAX_IOWRITE16_COUNT) { | |
1887 | dev_dbg_f(zd_usb_dev(usb), | |
1888 | "error: count %u exceeds possible max %u\n", | |
1889 | count, USB_MAX_IOWRITE16_COUNT); | |
1890 | return -EINVAL; | |
1891 | } | |
1892 | if (in_atomic()) { | |
1893 | dev_dbg_f(zd_usb_dev(usb), | |
1894 | "error: io in atomic context not supported\n"); | |
1895 | return -EWOULDBLOCK; | |
1896 | } | |
1897 | ||
eefdbec1 JK |
1898 | udev = zd_usb_to_usbdev(usb); |
1899 | ||
1900 | ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT)); | |
1901 | if (!ep) | |
1902 | return -ENOENT; | |
1903 | ||
1904 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1905 | if (!urb) | |
1906 | return -ENOMEM; | |
9bca0c3b | 1907 | |
0c7beb2d | 1908 | req_len = struct_size(req, reg_writes, count); |
eefdbec1 JK |
1909 | req = kmalloc(req_len, GFP_KERNEL); |
1910 | if (!req) { | |
1911 | r = -ENOMEM; | |
1912 | goto error; | |
1913 | } | |
e85d0918 DD |
1914 | |
1915 | req->id = cpu_to_le16(USB_REQ_WRITE_REGS); | |
1916 | for (i = 0; i < count; i++) { | |
1917 | struct reg_data *rw = &req->reg_writes[i]; | |
0ce34bc8 | 1918 | rw->addr = cpu_to_le16((u16)ioreqs[i].addr); |
e85d0918 DD |
1919 | rw->value = cpu_to_le16(ioreqs[i].value); |
1920 | } | |
1921 | ||
59342f6a JK |
1922 | /* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode |
1923 | * endpoint is bulk. Select correct type URB by endpoint descriptor. | |
1924 | */ | |
1925 | if (usb_endpoint_xfer_int(&ep->desc)) | |
1926 | usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT), | |
1927 | req, req_len, iowrite16v_urb_complete, usb, | |
1928 | ep->desc.bInterval); | |
1929 | else | |
1930 | usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_REGS_OUT), | |
1931 | req, req_len, iowrite16v_urb_complete, usb); | |
1932 | ||
2fc713b2 | 1933 | urb->transfer_flags |= URB_FREE_BUFFER; |
eefdbec1 JK |
1934 | |
1935 | /* Submit previous URB */ | |
1936 | r = zd_submit_waiting_urb(usb, false); | |
e85d0918 DD |
1937 | if (r) { |
1938 | dev_dbg_f(zd_usb_dev(usb), | |
eefdbec1 JK |
1939 | "error in zd_submit_waiting_usb(). " |
1940 | "Error number %d\n", r); | |
e85d0918 DD |
1941 | goto error; |
1942 | } | |
1943 | ||
eefdbec1 JK |
1944 | /* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs |
1945 | * of currect batch except for very last. | |
1946 | */ | |
1947 | usb->urb_async_waiting = urb; | |
1948 | return 0; | |
e85d0918 | 1949 | error: |
eefdbec1 | 1950 | usb_free_urb(urb); |
e85d0918 DD |
1951 | return r; |
1952 | } | |
1953 | ||
eefdbec1 JK |
1954 | int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs, |
1955 | unsigned int count) | |
1956 | { | |
1957 | int r; | |
1958 | ||
1959 | zd_usb_iowrite16v_async_start(usb); | |
1960 | r = zd_usb_iowrite16v_async(usb, ioreqs, count); | |
1961 | if (r) { | |
1962 | zd_usb_iowrite16v_async_end(usb, 0); | |
1963 | return r; | |
1964 | } | |
1965 | return zd_usb_iowrite16v_async_end(usb, 50 /* ms */); | |
1966 | } | |
1967 | ||
e85d0918 DD |
1968 | int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits) |
1969 | { | |
1970 | int r; | |
1971 | struct usb_device *udev; | |
1972 | struct usb_req_rfwrite *req = NULL; | |
1973 | int i, req_len, actual_req_len; | |
1974 | u16 bit_value_template; | |
1975 | ||
1976 | if (in_atomic()) { | |
1977 | dev_dbg_f(zd_usb_dev(usb), | |
1978 | "error: io in atomic context not supported\n"); | |
1979 | return -EWOULDBLOCK; | |
1980 | } | |
1981 | if (bits < USB_MIN_RFWRITE_BIT_COUNT) { | |
1982 | dev_dbg_f(zd_usb_dev(usb), | |
1983 | "error: bits %d are smaller than" | |
1984 | " USB_MIN_RFWRITE_BIT_COUNT %d\n", | |
1985 | bits, USB_MIN_RFWRITE_BIT_COUNT); | |
1986 | return -EINVAL; | |
1987 | } | |
1988 | if (bits > USB_MAX_RFWRITE_BIT_COUNT) { | |
1989 | dev_dbg_f(zd_usb_dev(usb), | |
1990 | "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n", | |
1991 | bits, USB_MAX_RFWRITE_BIT_COUNT); | |
1992 | return -EINVAL; | |
1993 | } | |
1994 | #ifdef DEBUG | |
1995 | if (value & (~0UL << bits)) { | |
1996 | dev_dbg_f(zd_usb_dev(usb), | |
1997 | "error: value %#09x has bits >= %d set\n", | |
1998 | value, bits); | |
1999 | return -EINVAL; | |
2000 | } | |
2001 | #endif /* DEBUG */ | |
2002 | ||
2003 | dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits); | |
2004 | ||
fbd5d17b | 2005 | r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203); |
e85d0918 DD |
2006 | if (r) { |
2007 | dev_dbg_f(zd_usb_dev(usb), | |
fbd5d17b | 2008 | "error %d: Couldn't read ZD_CR203\n", r); |
9bca0c3b | 2009 | return r; |
e85d0918 DD |
2010 | } |
2011 | bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA); | |
2012 | ||
9bca0c3b JK |
2013 | ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex)); |
2014 | BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) + | |
2015 | USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) > | |
2016 | sizeof(usb->req_buf)); | |
2017 | BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) > | |
2018 | sizeof(usb->req_buf)); | |
2019 | ||
e85d0918 | 2020 | req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16); |
9bca0c3b | 2021 | req = (void *)usb->req_buf; |
e85d0918 DD |
2022 | |
2023 | req->id = cpu_to_le16(USB_REQ_WRITE_RF); | |
2024 | /* 1: 3683a, but not used in ZYDAS driver */ | |
2025 | req->value = cpu_to_le16(2); | |
2026 | req->bits = cpu_to_le16(bits); | |
2027 | ||
2028 | for (i = 0; i < bits; i++) { | |
2029 | u16 bv = bit_value_template; | |
2030 | if (value & (1 << (bits-1-i))) | |
2031 | bv |= RF_DATA; | |
2032 | req->bit_values[i] = cpu_to_le16(bv); | |
2033 | } | |
2034 | ||
2035 | udev = zd_usb_to_usbdev(usb); | |
59342f6a | 2036 | r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/); |
e85d0918 DD |
2037 | if (r) { |
2038 | dev_dbg_f(zd_usb_dev(usb), | |
59342f6a | 2039 | "error in zd_ep_regs_out_msg(). Error number %d\n", r); |
e85d0918 DD |
2040 | goto out; |
2041 | } | |
2042 | if (req_len != actual_req_len) { | |
59342f6a | 2043 | dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()" |
e85d0918 DD |
2044 | " req_len %d != actual_req_len %d\n", |
2045 | req_len, actual_req_len); | |
2046 | r = -EIO; | |
2047 | goto out; | |
2048 | } | |
2049 | ||
2050 | /* FALL-THROUGH with r == 0 */ | |
2051 | out: | |
e85d0918 DD |
2052 | return r; |
2053 | } |