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