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