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