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  * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/firmware.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/usb.h>
17 #include <linux/workqueue.h>
18 #include <linux/module.h>
19 #include <net/mac80211.h>
20 #include <asm/unaligned.h>
21 
22 #include "zd_def.h"
23 #include "zd_mac.h"
24 #include "zd_usb.h"
25 
26 static const struct usb_device_id usb_ids[] = {
27 	/* ZD1211 */
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 },
35 	{ USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
36 	{ USB_DEVICE(0x0ace, 0xa211), .driver_info = DEVICE_ZD1211 },
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 },
40 	{ USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
41 	{ USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },
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 },
45 	{ USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
46 	{ USB_DEVICE(0x14ea, 0xab10), .driver_info = DEVICE_ZD1211 },
47 	{ USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
48 	{ USB_DEVICE(0x157e, 0x300a), .driver_info = DEVICE_ZD1211 },
49 	{ USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
50 	{ USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
51 	{ USB_DEVICE(0x157e, 0x3207), .driver_info = DEVICE_ZD1211 },
52 	{ USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
53 	{ USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
54 	/* ZD1211B */
55 	{ USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
56 	{ USB_DEVICE(0x0409, 0x0248), .driver_info = DEVICE_ZD1211B },
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 },
61 	{ USB_DEVICE(0x054c, 0x0257), .driver_info = DEVICE_ZD1211B },
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 },
67 	{ USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
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 },
71 	{ USB_DEVICE(0x083a, 0xe501), .driver_info = DEVICE_ZD1211B },
72 	{ USB_DEVICE(0x083a, 0xe503), .driver_info = DEVICE_ZD1211B },
73 	{ USB_DEVICE(0x083a, 0xe506), .driver_info = DEVICE_ZD1211B },
74 	{ USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
75 	{ USB_DEVICE(0x0ace, 0xb215), .driver_info = DEVICE_ZD1211B },
76 	{ USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
77 	{ USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
78 	{ USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
79 	{ USB_DEVICE(0x0df6, 0x0036), .driver_info = DEVICE_ZD1211B },
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 },
85 	{ USB_DEVICE(0x2019, 0xed01), .driver_info = DEVICE_ZD1211B },
86 	/* "Driverless" devices that need ejecting */
87 	{ USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
88 	{ USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
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 
102 static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
103 			    unsigned int count);
104 
105 /* USB device initialization */
106 static void int_urb_complete(struct urb *urb);
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) {
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 
177 		/* Use "DMA-aware" buffer. */
178 		r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
179 			USB_REQ_FIRMWARE_CONFIRM,
180 			USB_DIR_IN | USB_TYPE_VENDOR,
181 			0, 0, p, sizeof(ret), 5000 /* ms */);
182 		if (r != sizeof(ret)) {
183 			dev_err(&udev->dev,
184 				"control request firmware confirmation failed."
185 				" Return value %d\n", r);
186 			if (r >= 0)
187 				r = -ENODEV;
188 			goto error;
189 		}
190 		ret = p[0];
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 
215 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
216 	               const char* postfix)
217 {
218 	scnprintf(buffer, size, "%s%s",
219 		usb->is_zd1211b ?
220 			FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
221 		postfix);
222 	return buffer;
223 }
224 
225 static int handle_version_mismatch(struct zd_usb *usb,
226 	const struct firmware *ub_fw)
227 {
228 	struct usb_device *udev = zd_usb_to_usbdev(usb);
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,
235 		get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
236 		&udev->dev);
237 	if (r)
238 		goto error;
239 
240 	r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
241 	if (r)
242 		goto error;
243 
244 	offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
245 	r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
246 		E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
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 
258 static int upload_firmware(struct zd_usb *usb)
259 {
260 	int r;
261 	u16 fw_bcdDevice;
262 	u16 bcdDevice;
263 	struct usb_device *udev = zd_usb_to_usbdev(usb);
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,
271 		get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
272 		&udev->dev);
273 	if (r)
274 		goto error;
275 
276 	fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
277 
278 	if (fw_bcdDevice != bcdDevice) {
279 		dev_info(&udev->dev,
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 
286 		r = handle_version_mismatch(usb, ub_fw);
287 		if (r)
288 			goto error;
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,
297 		get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
298 		&udev->dev);
299 	if (r)
300 		goto error;
301 
302 	r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
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 
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 
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);
329 	u8 *buf;
330 
331 	/* Use "DMA-aware" buffer. */
332 	buf = kmalloc(len, GFP_KERNEL);
333 	if (!buf)
334 		return -ENOMEM;
335 	r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
336 		USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
337 		buf, len, 5000);
338 	if (r < 0) {
339 		dev_err(&udev->dev,
340 			"read over firmware interface failed: %d\n", r);
341 		goto exit;
342 	} else if (r != len) {
343 		dev_err(&udev->dev,
344 			"incomplete read over firmware interface: %d/%d\n",
345 			r, len);
346 		r = -EIO;
347 		goto exit;
348 	}
349 	r = 0;
350 	memcpy(data, buf, len);
351 exit:
352 	kfree(buf);
353 	return r;
354 }
355 
356 #define urb_dev(urb) (&(urb)->dev->dev)
357 
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;
362 	unsigned long flags;
363 
364 	spin_lock_irqsave(&intr->lock, flags);
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 	}
370 	spin_unlock_irqrestore(&intr->lock, flags);
371 }
372 
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;
377 	unsigned long flags;
378 	int len;
379 	u16 int_num;
380 
381 	ZD_ASSERT(in_interrupt());
382 	spin_lock_irqsave(&intr->lock, flags);
383 
384 	int_num = le16_to_cpu(*(__le16 *)(urb->transfer_buffer+2));
385 	if (int_num == CR_INTERRUPT) {
386 		struct zd_mac *mac = zd_hw_mac(zd_usb_to_hw(urb->context));
387 		spin_lock(&mac->lock);
388 		memcpy(&mac->intr_buffer, urb->transfer_buffer,
389 				USB_MAX_EP_INT_BUFFER);
390 		spin_unlock(&mac->lock);
391 		schedule_work(&mac->process_intr);
392 	} else if (atomic_read(&intr->read_regs_enabled)) {
393 		len = urb->actual_length;
394 		intr->read_regs.length = urb->actual_length;
395 		if (len > sizeof(intr->read_regs.buffer))
396 			len = sizeof(intr->read_regs.buffer);
397 
398 		memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
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;
412 		complete(&intr->read_regs.completion);
413 
414 		goto out;
415 	}
416 
417 out:
418 	spin_unlock_irqrestore(&intr->lock, flags);
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);
423 }
424 
425 static void int_urb_complete(struct urb *urb)
426 {
427 	int r;
428 	struct usb_int_header *hdr;
429 	struct zd_usb *usb;
430 	struct zd_usb_interrupt *intr;
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:
440 	case -EPIPE:
441 		dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
442 		return;
443 	default:
444 		dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
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 
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 
467 	switch (hdr->id) {
468 	case USB_INT_ID_REGS:
469 		handle_regs_int(urb);
470 		break;
471 	case USB_INT_ID_RETRY_FAILED:
472 		zd_mac_tx_failed(urb);
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) {
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 */
486 	}
487 	return;
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;
518 	struct usb_device *udev = zd_usb_to_usbdev(usb);
519 	struct zd_usb_interrupt *intr = &usb->intr;
520 	struct urb *urb;
521 
522 	dev_dbg_f(zd_usb_dev(usb), "\n");
523 
524 	urb = usb_alloc_urb(0, GFP_KERNEL);
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 
540 	r = -ENOMEM;
541 	intr->buffer = usb_alloc_coherent(udev, USB_MAX_EP_INT_BUFFER,
542 					  GFP_KERNEL, &intr->buffer_dma);
543 	if (!intr->buffer) {
544 		dev_dbg_f(zd_usb_dev(usb),
545 			"couldn't allocate transfer_buffer\n");
546 		goto error_set_urb_null;
547 	}
548 
549 	usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
550 			 intr->buffer, USB_MAX_EP_INT_BUFFER,
551 			 int_urb_complete, usb,
552 			 intr->interval);
553 	urb->transfer_dma = intr->buffer_dma;
554 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
555 
556 	dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
557 	r = usb_submit_urb(urb, GFP_KERNEL);
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:
566 	usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
567 			  intr->buffer, intr->buffer_dma);
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;
581 	struct usb_device *udev = zd_usb_to_usbdev(usb);
582 	struct zd_usb_interrupt *intr = &usb->intr;
583 	struct urb *urb;
584 	void *buffer;
585 	dma_addr_t buffer_dma;
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;
594 	buffer = intr->buffer;
595 	buffer_dma = intr->buffer_dma;
596 	intr->buffer = NULL;
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);
602 
603 	if (buffer)
604 		usb_free_coherent(udev, USB_MAX_EP_INT_BUFFER,
605 				  buffer, buffer_dma);
606 }
607 
608 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
609 			     unsigned int length)
610 {
611 	int i;
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. */
616 		dev_dbg_f(zd_usb_dev(usb), "invalid, small RX packet : %d\n",
617 					   length);
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 	 */
631 	if (get_unaligned_le16(&length_info->tag) == RX_LENGTH_INFO_TAG)
632 	{
633 		unsigned int l, k, n;
634 		for (i = 0, l = 0;; i++) {
635 			k = get_unaligned_le16(&length_info->length[i]);
636 			if (k == 0)
637 				return;
638 			n = l+k;
639 			if (n > length)
640 				return;
641 			zd_mac_rx(zd_usb_to_hw(usb), buffer+l, k);
642 			if (i >= 2)
643 				return;
644 			l = (n+3) & ~3;
645 		}
646 	} else {
647 		zd_mac_rx(zd_usb_to_hw(usb), buffer, length);
648 	}
649 }
650 
651 static void rx_urb_complete(struct urb *urb)
652 {
653 	int r;
654 	struct zd_usb *usb;
655 	struct zd_usb_rx *rx;
656 	const u8 *buffer;
657 	unsigned int length;
658 	unsigned long flags;
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:
668 	case -EPIPE:
669 		dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
670 		return;
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 
681 	tasklet_schedule(&rx->reset_timer_tasklet);
682 
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));
687 		spin_lock_irqsave(&rx->lock, flags);
688 		memcpy(rx->fragment, buffer, length);
689 		rx->fragment_length = length;
690 		spin_unlock_irqrestore(&rx->lock, flags);
691 		goto resubmit;
692 	}
693 
694 	spin_lock_irqsave(&rx->lock, flags);
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;
704 		spin_unlock_irqrestore(&rx->lock, flags);
705 	} else {
706 		spin_unlock_irqrestore(&rx->lock, flags);
707 		handle_rx_packet(usb, buffer, length);
708 	}
709 
710 resubmit:
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);
714 }
715 
716 static struct urb *alloc_rx_urb(struct zd_usb *usb)
717 {
718 	struct usb_device *udev = zd_usb_to_usbdev(usb);
719 	struct urb *urb;
720 	void *buffer;
721 
722 	urb = usb_alloc_urb(0, GFP_KERNEL);
723 	if (!urb)
724 		return NULL;
725 	buffer = usb_alloc_coherent(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
726 				    &urb->transfer_dma);
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),
733 			  buffer, USB_MAX_RX_SIZE,
734 			  rx_urb_complete, usb);
735 	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
736 
737 	return urb;
738 }
739 
740 static void free_rx_urb(struct urb *urb)
741 {
742 	if (!urb)
743 		return;
744 	usb_free_coherent(urb->dev, urb->transfer_buffer_length,
745 			  urb->transfer_buffer, urb->transfer_dma);
746 	usb_free_urb(urb);
747 }
748 
749 static int __zd_usb_enable_rx(struct zd_usb *usb)
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;
758 	urbs = kcalloc(RX_URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
759 	if (!urbs)
760 		goto error;
761 	for (i = 0; i < RX_URBS_COUNT; i++) {
762 		urbs[i] = alloc_rx_urb(usb);
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;
775 	rx->urbs_count = RX_URBS_COUNT;
776 	spin_unlock_irq(&rx->lock);
777 
778 	for (i = 0; i < RX_URBS_COUNT; i++) {
779 		r = usb_submit_urb(urbs[i], GFP_KERNEL);
780 		if (r)
781 			goto error_submit;
782 	}
783 
784 	return 0;
785 error_submit:
786 	for (i = 0; i < RX_URBS_COUNT; i++) {
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) {
795 		for (i = 0; i < RX_URBS_COUNT; i++)
796 			free_rx_urb(urbs[i]);
797 	}
798 	return r;
799 }
800 
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)
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]);
832 		free_rx_urb(urbs[i]);
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 
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 
850 	tasklet_kill(&rx->reset_timer_tasklet);
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 
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;
887 
888 	atomic_set(&tx->enabled, 0);
889 
890 	/* kill all submitted tx-urbs */
891 	usb_kill_anchored_urbs(&tx->submitted);
892 
893 	spin_lock_irqsave(&tx->lock, flags);
894 	WARN_ON(!skb_queue_empty(&tx->submitted_skbs));
895 	WARN_ON(tx->submitted_urbs != 0);
896 	tx->submitted_urbs = 0;
897 	spin_unlock_irqrestore(&tx->lock, flags);
898 
899 	/* The stopped state is ignored, relying on ieee80211_wake_queues()
900 	 * in a potentionally following zd_usb_enable_tx().
901 	 */
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);
917 	atomic_set(&tx->enabled, 1);
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 
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  */
959 static void tx_urb_complete(struct urb *urb)
960 {
961 	int r;
962 	struct sk_buff *skb;
963 	struct ieee80211_tx_info *info;
964 	struct zd_usb *usb;
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;
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:
984 	case -EPIPE:
985 		dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
986 		break;
987 	default:
988 		dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
989 		goto resubmit;
990 	}
991 free_urb:
992 	skb_unlink(skb, &usb->tx.submitted_skbs);
993 	zd_mac_tx_to_dev(skb, urb->status);
994 	usb_free_urb(urb);
995 	tx_dec_submitted_urbs(usb);
996 	return;
997 resubmit:
998 	usb_anchor_urb(urb, &tx->submitted);
999 	r = usb_submit_urb(urb, GFP_ATOMIC);
1000 	if (r) {
1001 		usb_unanchor_urb(urb);
1002 		dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
1003 		goto free_urb;
1004 	}
1005 }
1006 
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.
1018  */
1019 int zd_usb_tx(struct zd_usb *usb, struct sk_buff *skb)
1020 {
1021 	int r;
1022 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1023 	struct usb_device *udev = zd_usb_to_usbdev(usb);
1024 	struct urb *urb;
1025 	struct zd_usb_tx *tx = &usb->tx;
1026 
1027 	if (!atomic_read(&tx->enabled)) {
1028 		r = -ENOENT;
1029 		goto out;
1030 	}
1031 
1032 	urb = usb_alloc_urb(0, GFP_ATOMIC);
1033 	if (!urb) {
1034 		r = -ENOMEM;
1035 		goto out;
1036 	}
1037 
1038 	usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
1039 		          skb->data, skb->len, tx_urb_complete, skb);
1040 
1041 	info->rate_driver_data[1] = (void *)jiffies;
1042 	skb_queue_tail(&tx->submitted_skbs, skb);
1043 	usb_anchor_urb(urb, &tx->submitted);
1044 
1045 	r = usb_submit_urb(urb, GFP_ATOMIC);
1046 	if (r) {
1047 		dev_dbg_f(zd_usb_dev(usb), "error submit urb %p %d\n", urb, r);
1048 		usb_unanchor_urb(urb);
1049 		skb_unlink(skb, &tx->submitted_skbs);
1050 		goto error;
1051 	}
1052 	tx_inc_submitted_urbs(usb);
1053 	return 0;
1054 error:
1055 	usb_free_urb(urb);
1056 out:
1057 	return r;
1058 }
1059 
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 */
1096 	dev_warn(zd_usb_dev(usb), "TX-stall detected, resetting device...");
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 
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 
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 
1152 void zd_usb_reset_rx_idle_timer(struct zd_usb *usb)
1153 {
1154 	struct zd_usb_rx *rx = &usb->rx;
1155 
1156 	mod_delayed_work(zd_workqueue, &rx->idle_work, ZD_RX_IDLE_INTERVAL);
1157 }
1158 
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);
1166 	atomic_set(&intr->read_regs_enabled, 0);
1167 	intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
1168 }
1169 
1170 static inline void init_usb_rx(struct zd_usb *usb)
1171 {
1172 	struct zd_usb_rx *rx = &usb->rx;
1173 
1174 	spin_lock_init(&rx->lock);
1175 	mutex_init(&rx->setup_mutex);
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);
1182 	INIT_DELAYED_WORK(&rx->idle_work, zd_rx_idle_timer_handler);
1183 	rx->reset_timer_tasklet.func = zd_usb_reset_rx_idle_timer_tasklet;
1184 	rx->reset_timer_tasklet.data = (unsigned long)usb;
1185 }
1186 
1187 static inline void init_usb_tx(struct zd_usb *usb)
1188 {
1189 	struct zd_usb_tx *tx = &usb->tx;
1190 
1191 	spin_lock_init(&tx->lock);
1192 	atomic_set(&tx->enabled, 0);
1193 	tx->stopped = 0;
1194 	skb_queue_head_init(&tx->submitted_skbs);
1195 	init_usb_anchor(&tx->submitted);
1196 	tx->submitted_urbs = 0;
1197 	tx->watchdog_enabled = 0;
1198 	INIT_DELAYED_WORK(&tx->watchdog_work, zd_tx_watchdog_handler);
1199 }
1200 
1201 void zd_usb_init(struct zd_usb *usb, struct ieee80211_hw *hw,
1202 	         struct usb_interface *intf)
1203 {
1204 	memset(usb, 0, sizeof(*usb));
1205 	usb->intf = usb_get_intf(intf);
1206 	usb_set_intfdata(usb->intf, hw);
1207 	init_usb_anchor(&usb->submitted_cmds);
1208 	init_usb_interrupt(usb);
1209 	init_usb_tx(usb);
1210 	init_usb_rx(usb);
1211 }
1212 
1213 void zd_usb_clear(struct zd_usb *usb)
1214 {
1215 	usb_set_intfdata(usb->intf, NULL);
1216 	usb_put_intf(usb->intf);
1217 	ZD_MEMCLEAR(usb, sizeof(*usb));
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 
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 
1272 	if (iface_desc->desc.bNumEndpoints < 2)
1273 		return -ENODEV;
1274 
1275 	/* Find bulk out endpoint */
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) {
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 
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 
1339 	r = zd_mac_init_hw(mac->hw);
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 
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);
1354 	struct zd_usb *usb;
1355 	struct ieee80211_hw *hw = NULL;
1356 
1357 	print_id(udev);
1358 
1359 	if (id->driver_info & DEVICE_INSTALLER)
1360 		return eject_installer(intf);
1361 
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 
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 	}
1379 
1380 	hw = zd_mac_alloc_hw(intf);
1381 	if (hw == NULL) {
1382 		r = -ENOMEM;
1383 		goto error;
1384 	}
1385 
1386 	usb = &zd_hw_mac(hw)->chip.usb;
1387 	usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1388 
1389 	r = zd_mac_preinit_hw(hw);
1390 	if (r) {
1391 		dev_dbg_f(&intf->dev,
1392 		         "couldn't initialize mac. Error number %d\n", r);
1393 		goto error;
1394 	}
1395 
1396 	r = ieee80211_register_hw(hw);
1397 	if (r) {
1398 		dev_dbg_f(&intf->dev,
1399 			 "couldn't register device. Error number %d\n", r);
1400 		goto error;
1401 	}
1402 
1403 	dev_dbg_f(&intf->dev, "successful\n");
1404 	dev_info(&intf->dev, "%s\n", wiphy_name(hw->wiphy));
1405 	return 0;
1406 error:
1407 	usb_reset_device(interface_to_usbdev(intf));
1408 	if (hw) {
1409 		zd_mac_clear(zd_hw_mac(hw));
1410 		ieee80211_free_hw(hw);
1411 	}
1412 	return r;
1413 }
1414 
1415 static void disconnect(struct usb_interface *intf)
1416 {
1417 	struct ieee80211_hw *hw = zd_intf_to_hw(intf);
1418 	struct zd_mac *mac;
1419 	struct zd_usb *usb;
1420 
1421 	/* Either something really bad happened, or we're just dealing with
1422 	 * a DEVICE_INSTALLER. */
1423 	if (hw == NULL)
1424 		return;
1425 
1426 	mac = zd_hw_mac(hw);
1427 	usb = &mac->chip.usb;
1428 
1429 	dev_dbg_f(zd_usb_dev(usb), "\n");
1430 
1431 	ieee80211_unregister_hw(hw);
1432 
1433 	/* Just in case something has gone wrong! */
1434 	zd_usb_disable_tx(usb);
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 
1445 	zd_mac_clear(mac);
1446 	ieee80211_free_hw(hw);
1447 	dev_dbg(&intf->dev, "disconnected\n");
1448 }
1449 
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 
1529 static struct usb_driver driver = {
1530 	.name		= KBUILD_MODNAME,
1531 	.id_table	= usb_ids,
1532 	.probe		= probe,
1533 	.disconnect	= disconnect,
1534 	.pre_reset	= pre_reset,
1535 	.post_reset	= post_reset,
1536 	.disable_hub_initiated_lpm = 1,
1537 };
1538 
1539 struct workqueue_struct *zd_workqueue;
1540 
1541 static int __init usb_init(void)
1542 {
1543 	int r;
1544 
1545 	pr_debug("%s usb_init()\n", driver.name);
1546 
1547 	zd_workqueue = create_singlethread_workqueue(driver.name);
1548 	if (zd_workqueue == NULL) {
1549 		printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1550 		return -ENOMEM;
1551 	}
1552 
1553 	r = usb_register(&driver);
1554 	if (r) {
1555 		destroy_workqueue(zd_workqueue);
1556 		printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1557 		       driver.name, r);
1558 		return r;
1559 	}
1560 
1561 	pr_debug("%s initialized\n", driver.name);
1562 	return 0;
1563 }
1564 
1565 static void __exit usb_exit(void)
1566 {
1567 	pr_debug("%s usb_exit()\n", driver.name);
1568 	usb_deregister(&driver);
1569 	destroy_workqueue(zd_workqueue);
1570 }
1571 
1572 module_init(usb_init);
1573 module_exit(usb_exit);
1574 
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 
1600 static void prepare_read_regs_int(struct zd_usb *usb,
1601 				  struct usb_req_read_regs *req,
1602 				  unsigned int count)
1603 {
1604 	struct zd_usb_interrupt *intr = &usb->intr;
1605 
1606 	spin_lock_irq(&intr->lock);
1607 	atomic_set(&intr->read_regs_enabled, 1);
1608 	intr->read_regs.req = req;
1609 	intr->read_regs.req_count = count;
1610 	reinit_completion(&intr->read_regs.completion);
1611 	spin_unlock_irq(&intr->lock);
1612 }
1613 
1614 static void disable_read_regs_int(struct zd_usb *usb)
1615 {
1616 	struct zd_usb_interrupt *intr = &usb->intr;
1617 
1618 	spin_lock_irq(&intr->lock);
1619 	atomic_set(&intr->read_regs_enabled, 0);
1620 	spin_unlock_irq(&intr->lock);
1621 }
1622 
1623 static bool check_read_regs(struct zd_usb *usb, struct usb_req_read_regs *req,
1624 			    unsigned int count)
1625 {
1626 	int i;
1627 	struct zd_usb_interrupt *intr = &usb->intr;
1628 	struct read_regs_int *rr = &intr->read_regs;
1629 	struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1630 
1631 	/* The created block size seems to be larger than expected.
1632 	 * However results appear to be correct.
1633 	 */
1634 	if (rr->length < struct_size(regs, regs, count)) {
1635 		dev_dbg_f(zd_usb_dev(usb),
1636 			 "error: actual length %d less than expected %zu\n",
1637 			 rr->length, struct_size(regs, regs, count));
1638 		return false;
1639 	}
1640 
1641 	if (rr->length > sizeof(rr->buffer)) {
1642 		dev_dbg_f(zd_usb_dev(usb),
1643 			 "error: actual length %d exceeds buffer size %zu\n",
1644 			 rr->length, sizeof(rr->buffer));
1645 		return false;
1646 	}
1647 
1648 	for (i = 0; i < count; i++) {
1649 		struct reg_data *rd = &regs->regs[i];
1650 		if (rd->addr != req->addr[i]) {
1651 			dev_dbg_f(zd_usb_dev(usb),
1652 				 "rd[%d] addr %#06hx expected %#06hx\n", i,
1653 				 le16_to_cpu(rd->addr),
1654 				 le16_to_cpu(req->addr[i]));
1655 			return false;
1656 		}
1657 	}
1658 
1659 	return true;
1660 }
1661 
1662 static int get_results(struct zd_usb *usb, u16 *values,
1663 		       struct usb_req_read_regs *req, unsigned int count,
1664 		       bool *retry)
1665 {
1666 	int r;
1667 	int i;
1668 	struct zd_usb_interrupt *intr = &usb->intr;
1669 	struct read_regs_int *rr = &intr->read_regs;
1670 	struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1671 
1672 	spin_lock_irq(&intr->lock);
1673 
1674 	r = -EIO;
1675 
1676 	/* Read failed because firmware bug? */
1677 	*retry = !!intr->read_regs_int_overridden;
1678 	if (*retry)
1679 		goto error_unlock;
1680 
1681 	if (!check_read_regs(usb, req, count)) {
1682 		dev_dbg_f(zd_usb_dev(usb), "error: invalid read regs\n");
1683 		goto error_unlock;
1684 	}
1685 
1686 	for (i = 0; i < count; i++) {
1687 		struct reg_data *rd = &regs->regs[i];
1688 		values[i] = le16_to_cpu(rd->value);
1689 	}
1690 
1691 	r = 0;
1692 error_unlock:
1693 	spin_unlock_irq(&intr->lock);
1694 	return r;
1695 }
1696 
1697 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1698 	             const zd_addr_t *addresses, unsigned int count)
1699 {
1700 	int r, i, req_len, actual_req_len, try_count = 0;
1701 	struct usb_device *udev;
1702 	struct usb_req_read_regs *req = NULL;
1703 	unsigned long timeout;
1704 	bool retry = false;
1705 
1706 	if (count < 1) {
1707 		dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1708 		return -EINVAL;
1709 	}
1710 	if (count > USB_MAX_IOREAD16_COUNT) {
1711 		dev_dbg_f(zd_usb_dev(usb),
1712 			 "error: count %u exceeds possible max %u\n",
1713 			 count, USB_MAX_IOREAD16_COUNT);
1714 		return -EINVAL;
1715 	}
1716 	if (in_atomic()) {
1717 		dev_dbg_f(zd_usb_dev(usb),
1718 			 "error: io in atomic context not supported\n");
1719 		return -EWOULDBLOCK;
1720 	}
1721 	if (!usb_int_enabled(usb)) {
1722 		dev_dbg_f(zd_usb_dev(usb),
1723 			  "error: usb interrupt not enabled\n");
1724 		return -EWOULDBLOCK;
1725 	}
1726 
1727 	ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1728 	BUILD_BUG_ON(sizeof(struct usb_req_read_regs) + USB_MAX_IOREAD16_COUNT *
1729 		     sizeof(__le16) > sizeof(usb->req_buf));
1730 	BUG_ON(sizeof(struct usb_req_read_regs) + count * sizeof(__le16) >
1731 	       sizeof(usb->req_buf));
1732 
1733 	req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1734 	req = (void *)usb->req_buf;
1735 
1736 	req->id = cpu_to_le16(USB_REQ_READ_REGS);
1737 	for (i = 0; i < count; i++)
1738 		req->addr[i] = cpu_to_le16((u16)addresses[i]);
1739 
1740 retry_read:
1741 	try_count++;
1742 	udev = zd_usb_to_usbdev(usb);
1743 	prepare_read_regs_int(usb, req, count);
1744 	r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
1745 	if (r) {
1746 		dev_dbg_f(zd_usb_dev(usb),
1747 			"error in zd_ep_regs_out_msg(). Error number %d\n", r);
1748 		goto error;
1749 	}
1750 	if (req_len != actual_req_len) {
1751 		dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()\n"
1752 			" req_len %d != actual_req_len %d\n",
1753 			req_len, actual_req_len);
1754 		r = -EIO;
1755 		goto error;
1756 	}
1757 
1758 	timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1759 					      msecs_to_jiffies(50));
1760 	if (!timeout) {
1761 		disable_read_regs_int(usb);
1762 		dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1763 		r = -ETIMEDOUT;
1764 		goto error;
1765 	}
1766 
1767 	r = get_results(usb, values, req, count, &retry);
1768 	if (retry && try_count < 20) {
1769 		dev_dbg_f(zd_usb_dev(usb), "read retry, tries so far: %d\n",
1770 				try_count);
1771 		goto retry_read;
1772 	}
1773 error:
1774 	return r;
1775 }
1776 
1777 static void iowrite16v_urb_complete(struct urb *urb)
1778 {
1779 	struct zd_usb *usb = urb->context;
1780 
1781 	if (urb->status && !usb->cmd_error)
1782 		usb->cmd_error = urb->status;
1783 
1784 	if (!usb->cmd_error &&
1785 			urb->actual_length != urb->transfer_buffer_length)
1786 		usb->cmd_error = -EIO;
1787 }
1788 
1789 static int zd_submit_waiting_urb(struct zd_usb *usb, bool last)
1790 {
1791 	int r = 0;
1792 	struct urb *urb = usb->urb_async_waiting;
1793 
1794 	if (!urb)
1795 		return 0;
1796 
1797 	usb->urb_async_waiting = NULL;
1798 
1799 	if (!last)
1800 		urb->transfer_flags |= URB_NO_INTERRUPT;
1801 
1802 	usb_anchor_urb(urb, &usb->submitted_cmds);
1803 	r = usb_submit_urb(urb, GFP_KERNEL);
1804 	if (r) {
1805 		usb_unanchor_urb(urb);
1806 		dev_dbg_f(zd_usb_dev(usb),
1807 			"error in usb_submit_urb(). Error number %d\n", r);
1808 		goto error;
1809 	}
1810 
1811 	/* fall-through with r == 0 */
1812 error:
1813 	usb_free_urb(urb);
1814 	return r;
1815 }
1816 
1817 void zd_usb_iowrite16v_async_start(struct zd_usb *usb)
1818 {
1819 	ZD_ASSERT(usb_anchor_empty(&usb->submitted_cmds));
1820 	ZD_ASSERT(usb->urb_async_waiting == NULL);
1821 	ZD_ASSERT(!usb->in_async);
1822 
1823 	ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1824 
1825 	usb->in_async = 1;
1826 	usb->cmd_error = 0;
1827 	usb->urb_async_waiting = NULL;
1828 }
1829 
1830 int zd_usb_iowrite16v_async_end(struct zd_usb *usb, unsigned int timeout)
1831 {
1832 	int r;
1833 
1834 	ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1835 	ZD_ASSERT(usb->in_async);
1836 
1837 	/* Submit last iowrite16v URB */
1838 	r = zd_submit_waiting_urb(usb, true);
1839 	if (r) {
1840 		dev_dbg_f(zd_usb_dev(usb),
1841 			"error in zd_submit_waiting_usb(). "
1842 			"Error number %d\n", r);
1843 
1844 		usb_kill_anchored_urbs(&usb->submitted_cmds);
1845 		goto error;
1846 	}
1847 
1848 	if (timeout)
1849 		timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds,
1850 							timeout);
1851 	if (!timeout) {
1852 		usb_kill_anchored_urbs(&usb->submitted_cmds);
1853 		if (usb->cmd_error == -ENOENT) {
1854 			dev_dbg_f(zd_usb_dev(usb), "timed out");
1855 			r = -ETIMEDOUT;
1856 			goto error;
1857 		}
1858 	}
1859 
1860 	r = usb->cmd_error;
1861 error:
1862 	usb->in_async = 0;
1863 	return r;
1864 }
1865 
1866 int zd_usb_iowrite16v_async(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1867 			    unsigned int count)
1868 {
1869 	int r;
1870 	struct usb_device *udev;
1871 	struct usb_req_write_regs *req = NULL;
1872 	int i, req_len;
1873 	struct urb *urb;
1874 	struct usb_host_endpoint *ep;
1875 
1876 	ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
1877 	ZD_ASSERT(usb->in_async);
1878 
1879 	if (count == 0)
1880 		return 0;
1881 	if (count > USB_MAX_IOWRITE16_COUNT) {
1882 		dev_dbg_f(zd_usb_dev(usb),
1883 			"error: count %u exceeds possible max %u\n",
1884 			count, USB_MAX_IOWRITE16_COUNT);
1885 		return -EINVAL;
1886 	}
1887 	if (in_atomic()) {
1888 		dev_dbg_f(zd_usb_dev(usb),
1889 			"error: io in atomic context not supported\n");
1890 		return -EWOULDBLOCK;
1891 	}
1892 
1893 	udev = zd_usb_to_usbdev(usb);
1894 
1895 	ep = usb_pipe_endpoint(udev, usb_sndintpipe(udev, EP_REGS_OUT));
1896 	if (!ep)
1897 		return -ENOENT;
1898 
1899 	urb = usb_alloc_urb(0, GFP_KERNEL);
1900 	if (!urb)
1901 		return -ENOMEM;
1902 
1903 	req_len = struct_size(req, reg_writes, count);
1904 	req = kmalloc(req_len, GFP_KERNEL);
1905 	if (!req) {
1906 		r = -ENOMEM;
1907 		goto error;
1908 	}
1909 
1910 	req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1911 	for (i = 0; i < count; i++) {
1912 		struct reg_data *rw  = &req->reg_writes[i];
1913 		rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1914 		rw->value = cpu_to_le16(ioreqs[i].value);
1915 	}
1916 
1917 	/* In USB 2.0 mode endpoint is interrupt type. However in USB 1.1 mode
1918 	 * endpoint is bulk. Select correct type URB by endpoint descriptor.
1919 	 */
1920 	if (usb_endpoint_xfer_int(&ep->desc))
1921 		usb_fill_int_urb(urb, udev, usb_sndintpipe(udev, EP_REGS_OUT),
1922 				 req, req_len, iowrite16v_urb_complete, usb,
1923 				 ep->desc.bInterval);
1924 	else
1925 		usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1926 				  req, req_len, iowrite16v_urb_complete, usb);
1927 
1928 	urb->transfer_flags |= URB_FREE_BUFFER;
1929 
1930 	/* Submit previous URB */
1931 	r = zd_submit_waiting_urb(usb, false);
1932 	if (r) {
1933 		dev_dbg_f(zd_usb_dev(usb),
1934 			"error in zd_submit_waiting_usb(). "
1935 			"Error number %d\n", r);
1936 		goto error;
1937 	}
1938 
1939 	/* Delay submit so that URB_NO_INTERRUPT flag can be set for all URBs
1940 	 * of currect batch except for very last.
1941 	 */
1942 	usb->urb_async_waiting = urb;
1943 	return 0;
1944 error:
1945 	usb_free_urb(urb);
1946 	return r;
1947 }
1948 
1949 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1950 			unsigned int count)
1951 {
1952 	int r;
1953 
1954 	zd_usb_iowrite16v_async_start(usb);
1955 	r = zd_usb_iowrite16v_async(usb, ioreqs, count);
1956 	if (r) {
1957 		zd_usb_iowrite16v_async_end(usb, 0);
1958 		return r;
1959 	}
1960 	return zd_usb_iowrite16v_async_end(usb, 50 /* ms */);
1961 }
1962 
1963 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1964 {
1965 	int r;
1966 	struct usb_device *udev;
1967 	struct usb_req_rfwrite *req = NULL;
1968 	int i, req_len, actual_req_len;
1969 	u16 bit_value_template;
1970 
1971 	if (in_atomic()) {
1972 		dev_dbg_f(zd_usb_dev(usb),
1973 			"error: io in atomic context not supported\n");
1974 		return -EWOULDBLOCK;
1975 	}
1976 	if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1977 		dev_dbg_f(zd_usb_dev(usb),
1978 			"error: bits %d are smaller than"
1979 			" USB_MIN_RFWRITE_BIT_COUNT %d\n",
1980 			bits, USB_MIN_RFWRITE_BIT_COUNT);
1981 		return -EINVAL;
1982 	}
1983 	if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1984 		dev_dbg_f(zd_usb_dev(usb),
1985 			"error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1986 			bits, USB_MAX_RFWRITE_BIT_COUNT);
1987 		return -EINVAL;
1988 	}
1989 #ifdef DEBUG
1990 	if (value & (~0UL << bits)) {
1991 		dev_dbg_f(zd_usb_dev(usb),
1992 			"error: value %#09x has bits >= %d set\n",
1993 			value, bits);
1994 		return -EINVAL;
1995 	}
1996 #endif /* DEBUG */
1997 
1998 	dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1999 
2000 	r = zd_usb_ioread16(usb, &bit_value_template, ZD_CR203);
2001 	if (r) {
2002 		dev_dbg_f(zd_usb_dev(usb),
2003 			"error %d: Couldn't read ZD_CR203\n", r);
2004 		return r;
2005 	}
2006 	bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
2007 
2008 	ZD_ASSERT(mutex_is_locked(&zd_usb_to_chip(usb)->mutex));
2009 	BUILD_BUG_ON(sizeof(struct usb_req_rfwrite) +
2010 		     USB_MAX_RFWRITE_BIT_COUNT * sizeof(__le16) >
2011 		     sizeof(usb->req_buf));
2012 	BUG_ON(sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16) >
2013 	       sizeof(usb->req_buf));
2014 
2015 	req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
2016 	req = (void *)usb->req_buf;
2017 
2018 	req->id = cpu_to_le16(USB_REQ_WRITE_RF);
2019 	/* 1: 3683a, but not used in ZYDAS driver */
2020 	req->value = cpu_to_le16(2);
2021 	req->bits = cpu_to_le16(bits);
2022 
2023 	for (i = 0; i < bits; i++) {
2024 		u16 bv = bit_value_template;
2025 		if (value & (1 << (bits-1-i)))
2026 			bv |= RF_DATA;
2027 		req->bit_values[i] = cpu_to_le16(bv);
2028 	}
2029 
2030 	udev = zd_usb_to_usbdev(usb);
2031 	r = zd_ep_regs_out_msg(udev, req, req_len, &actual_req_len, 50 /*ms*/);
2032 	if (r) {
2033 		dev_dbg_f(zd_usb_dev(usb),
2034 			"error in zd_ep_regs_out_msg(). Error number %d\n", r);
2035 		goto out;
2036 	}
2037 	if (req_len != actual_req_len) {
2038 		dev_dbg_f(zd_usb_dev(usb), "error in zd_ep_regs_out_msg()"
2039 			" req_len %d != actual_req_len %d\n",
2040 			req_len, actual_req_len);
2041 		r = -EIO;
2042 		goto out;
2043 	}
2044 
2045 	/* FALL-THROUGH with r == 0 */
2046 out:
2047 	return r;
2048 }
2049