aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/zd1211rw/zd_usb.c
diff options
context:
space:
mode:
authorDaniel Drake <dsd@gentoo.org>2006-06-02 12:11:32 -0400
committerJeff Garzik <jeff@garzik.org>2006-07-05 13:42:58 -0400
commite85d0918b54fbd9b38003752f7d665416b06edd8 (patch)
tree6f53e6bb10562eec331defc6811fb6d434eb21e5 /drivers/net/wireless/zd1211rw/zd_usb.c
parent4a232e725b5cc1bc7fc5b177424a9ff8313b23ad (diff)
[PATCH] ZyDAS ZD1211 USB-WLAN driver
There are 60+ USB wifi adapters available on the market based on the ZyDAS ZD1211 chip. Unlike the predecessor (ZD1201), ZD1211 does not have a hardware MAC, so most data operations are coordinated by the device driver. The ZD1211 chip sits alongside an RF transceiver which is also controlled by the driver. Our driver currently supports 2 RF types, we know of one other available in a few marketed products which we will be supporting soon. Our driver also supports the newer revision of ZD1211, called ZD1211B. The initialization and RF operations are slightly different for the new revision, but the main difference is 802.11e support. Our driver does not support the QoS features yet, but we think we know how to use them. This driver is based on ZyDAS's own GPL driver available from www.zydas.com.tw. ZyDAS engineers have been responsive and supportive of our efforts, so thumbs up to them. Additionally, the firmware is redistributable and they have provided device specs. This driver has been written primarily by Ulrich Kunitz and myself. Graham Gower, Greg KH, Remco and Bryan Rittmeyer have also contributed. The developers of ieee80211 and softmac have made our lives so much easier- thanks! We maintain a small info-page: http://zd1211.ath.cx/wiki/DriverRewrite If there is enough time for review, we would like to aim for inclusion in 2.6.18. The driver works nicely as a STA, and can connect to both open and encrypted networks (we are using software-based encryption for now). We will work towards supporting more advanced features in the future (ad-hoc, master mode, 802.11a, ...). Signed-off-by: Daniel Drake <dsd@gentoo.org> Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'drivers/net/wireless/zd1211rw/zd_usb.c')
-rw-r--r--drivers/net/wireless/zd1211rw/zd_usb.c1316
1 files changed, 1316 insertions, 0 deletions
diff --git a/drivers/net/wireless/zd1211rw/zd_usb.c b/drivers/net/wireless/zd1211rw/zd_usb.c
new file mode 100644
index 000000000000..ce1cb2c6aa8d
--- /dev/null
+++ b/drivers/net/wireless/zd1211rw/zd_usb.c
@@ -0,0 +1,1316 @@
1/* zd_usb.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18#include <asm/unaligned.h>
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/firmware.h>
22#include <linux/device.h>
23#include <linux/errno.h>
24#include <linux/skbuff.h>
25#include <linux/usb.h>
26#include <net/ieee80211.h>
27
28#include "zd_def.h"
29#include "zd_netdev.h"
30#include "zd_mac.h"
31#include "zd_usb.h"
32#include "zd_util.h"
33
34static struct usb_device_id usb_ids[] = {
35 /* ZD1211 */
36 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
37 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
38 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
42 /* ZD1211B */
43 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
44 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
45 {}
46};
47
48MODULE_LICENSE("GPL");
49MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
50MODULE_AUTHOR("Ulrich Kunitz");
51MODULE_AUTHOR("Daniel Drake");
52MODULE_VERSION("1.0");
53MODULE_DEVICE_TABLE(usb, usb_ids);
54
55#define FW_ZD1211_PREFIX "zd1211/zd1211_"
56#define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
57
58/* register address handling */
59
60#ifdef DEBUG
61static int check_addr(struct zd_usb *usb, zd_addr_t addr)
62{
63 u32 base = ZD_ADDR_BASE(addr);
64 u32 offset = ZD_OFFSET(addr);
65
66 if ((u32)addr & ADDR_ZERO_MASK)
67 goto invalid_address;
68 switch (base) {
69 case USB_BASE:
70 break;
71 case CR_BASE:
72 if (offset > CR_MAX_OFFSET) {
73 dev_dbg(zd_usb_dev(usb),
74 "CR offset %#010x larger than"
75 " CR_MAX_OFFSET %#10x\n",
76 offset, CR_MAX_OFFSET);
77 goto invalid_address;
78 }
79 if (offset & 1) {
80 dev_dbg(zd_usb_dev(usb),
81 "CR offset %#010x is not a multiple of 2\n",
82 offset);
83 goto invalid_address;
84 }
85 break;
86 case E2P_BASE:
87 if (offset > E2P_MAX_OFFSET) {
88 dev_dbg(zd_usb_dev(usb),
89 "E2P offset %#010x larger than"
90 " E2P_MAX_OFFSET %#010x\n",
91 offset, E2P_MAX_OFFSET);
92 goto invalid_address;
93 }
94 break;
95 case FW_BASE:
96 if (!usb->fw_base_offset) {
97 dev_dbg(zd_usb_dev(usb),
98 "ERROR: fw base offset has not been set\n");
99 return -EAGAIN;
100 }
101 if (offset > FW_MAX_OFFSET) {
102 dev_dbg(zd_usb_dev(usb),
103 "FW offset %#10x is larger than"
104 " FW_MAX_OFFSET %#010x\n",
105 offset, FW_MAX_OFFSET);
106 goto invalid_address;
107 }
108 break;
109 default:
110 dev_dbg(zd_usb_dev(usb),
111 "address has unsupported base %#010x\n", addr);
112 goto invalid_address;
113 }
114
115 return 0;
116invalid_address:
117 dev_dbg(zd_usb_dev(usb),
118 "ERROR: invalid address: %#010x\n", addr);
119 return -EINVAL;
120}
121#endif /* DEBUG */
122
123static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr)
124{
125 u32 base;
126 u16 offset;
127
128 base = ZD_ADDR_BASE(addr);
129 offset = ZD_OFFSET(addr);
130
131 ZD_ASSERT(check_addr(usb, addr) == 0);
132
133 switch (base) {
134 case CR_BASE:
135 offset += CR_BASE_OFFSET;
136 break;
137 case E2P_BASE:
138 offset += E2P_BASE_OFFSET;
139 break;
140 case FW_BASE:
141 offset += usb->fw_base_offset;
142 break;
143 }
144
145 return offset;
146}
147
148/* USB device initialization */
149
150static int request_fw_file(
151 const struct firmware **fw, const char *name, struct device *device)
152{
153 int r;
154
155 dev_dbg_f(device, "fw name %s\n", name);
156
157 r = request_firmware(fw, name, device);
158 if (r)
159 dev_err(device,
160 "Could not load firmware file %s. Error number %d\n",
161 name, r);
162 return r;
163}
164
165static inline u16 get_bcdDevice(const struct usb_device *udev)
166{
167 return le16_to_cpu(udev->descriptor.bcdDevice);
168}
169
170enum upload_code_flags {
171 REBOOT = 1,
172};
173
174/* Ensures that MAX_TRANSFER_SIZE is even. */
175#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
176
177static int upload_code(struct usb_device *udev,
178 const u8 *data, size_t size, u16 code_offset, int flags)
179{
180 u8 *p;
181 int r;
182
183 /* USB request blocks need "kmalloced" buffers.
184 */
185 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
186 if (!p) {
187 dev_err(&udev->dev, "out of memory\n");
188 r = -ENOMEM;
189 goto error;
190 }
191
192 size &= ~1;
193 while (size > 0) {
194 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
195 size : MAX_TRANSFER_SIZE;
196
197 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
198
199 memcpy(p, data, transfer_size);
200 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
201 USB_REQ_FIRMWARE_DOWNLOAD,
202 USB_DIR_OUT | USB_TYPE_VENDOR,
203 code_offset, 0, p, transfer_size, 1000 /* ms */);
204 if (r < 0) {
205 dev_err(&udev->dev,
206 "USB control request for firmware upload"
207 " failed. Error number %d\n", r);
208 goto error;
209 }
210 transfer_size = r & ~1;
211
212 size -= transfer_size;
213 data += transfer_size;
214 code_offset += transfer_size/sizeof(u16);
215 }
216
217 if (flags & REBOOT) {
218 u8 ret;
219
220 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
221 USB_REQ_FIRMWARE_CONFIRM,
222 USB_DIR_IN | USB_TYPE_VENDOR,
223 0, 0, &ret, sizeof(ret), 5000 /* ms */);
224 if (r != sizeof(ret)) {
225 dev_err(&udev->dev,
226 "control request firmeware confirmation failed."
227 " Return value %d\n", r);
228 if (r >= 0)
229 r = -ENODEV;
230 goto error;
231 }
232 if (ret & 0x80) {
233 dev_err(&udev->dev,
234 "Internal error while downloading."
235 " Firmware confirm return value %#04x\n",
236 (unsigned int)ret);
237 r = -ENODEV;
238 goto error;
239 }
240 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
241 (unsigned int)ret);
242 }
243
244 r = 0;
245error:
246 kfree(p);
247 return r;
248}
249
250static u16 get_word(const void *data, u16 offset)
251{
252 const __le16 *p = data;
253 return le16_to_cpu(p[offset]);
254}
255
256static char *get_fw_name(char *buffer, size_t size, u8 device_type,
257 const char* postfix)
258{
259 scnprintf(buffer, size, "%s%s",
260 device_type == DEVICE_ZD1211B ?
261 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
262 postfix);
263 return buffer;
264}
265
266static int upload_firmware(struct usb_device *udev, u8 device_type)
267{
268 int r;
269 u16 fw_bcdDevice;
270 u16 bcdDevice;
271 const struct firmware *ub_fw = NULL;
272 const struct firmware *uph_fw = NULL;
273 char fw_name[128];
274
275 bcdDevice = get_bcdDevice(udev);
276
277 r = request_fw_file(&ub_fw,
278 get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"),
279 &udev->dev);
280 if (r)
281 goto error;
282
283 fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET);
284
285 /* FIXME: do we have any reason to perform the kludge that the vendor
286 * driver does when there is a version mismatch? (their driver uploads
287 * different firmwares and stuff)
288 */
289 if (fw_bcdDevice != bcdDevice) {
290 dev_info(&udev->dev,
291 "firmware device id %#06x and actual device id "
292 "%#06x differ, continuing anyway\n",
293 fw_bcdDevice, bcdDevice);
294 } else {
295 dev_dbg_f(&udev->dev,
296 "firmware device id %#06x is equal to the "
297 "actual device id\n", fw_bcdDevice);
298 }
299
300
301 r = request_fw_file(&uph_fw,
302 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
303 &udev->dev);
304 if (r)
305 goto error;
306
307 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET,
308 REBOOT);
309 if (r) {
310 dev_err(&udev->dev,
311 "Could not upload firmware code uph. Error number %d\n",
312 r);
313 }
314
315 /* FALL-THROUGH */
316error:
317 release_firmware(ub_fw);
318 release_firmware(uph_fw);
319 return r;
320}
321
322static void disable_read_regs_int(struct zd_usb *usb)
323{
324 struct zd_usb_interrupt *intr = &usb->intr;
325
326 ZD_ASSERT(in_interrupt());
327 spin_lock(&intr->lock);
328 intr->read_regs_enabled = 0;
329 spin_unlock(&intr->lock);
330}
331
332#define urb_dev(urb) (&(urb)->dev->dev)
333
334static inline void handle_regs_int(struct urb *urb)
335{
336 struct zd_usb *usb = urb->context;
337 struct zd_usb_interrupt *intr = &usb->intr;
338 int len;
339
340 ZD_ASSERT(in_interrupt());
341 spin_lock(&intr->lock);
342
343 if (intr->read_regs_enabled) {
344 intr->read_regs.length = len = urb->actual_length;
345
346 if (len > sizeof(intr->read_regs.buffer))
347 len = sizeof(intr->read_regs.buffer);
348 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
349 intr->read_regs_enabled = 0;
350 complete(&intr->read_regs.completion);
351 goto out;
352 }
353
354 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
355out:
356 spin_unlock(&intr->lock);
357}
358
359static inline void handle_retry_failed_int(struct urb *urb)
360{
361 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
362}
363
364
365static void int_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
366{
367 int r;
368 struct usb_int_header *hdr;
369
370 switch (urb->status) {
371 case 0:
372 break;
373 case -ESHUTDOWN:
374 case -EINVAL:
375 case -ENODEV:
376 case -ENOENT:
377 case -ECONNRESET:
378 goto kfree;
379 case -EPIPE:
380 usb_clear_halt(urb->dev, EP_INT_IN);
381 /* FALL-THROUGH */
382 default:
383 goto resubmit;
384 }
385
386 if (urb->actual_length < sizeof(hdr)) {
387 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
388 goto resubmit;
389 }
390
391 hdr = urb->transfer_buffer;
392 if (hdr->type != USB_INT_TYPE) {
393 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
394 goto resubmit;
395 }
396
397 switch (hdr->id) {
398 case USB_INT_ID_REGS:
399 handle_regs_int(urb);
400 break;
401 case USB_INT_ID_RETRY_FAILED:
402 handle_retry_failed_int(urb);
403 break;
404 default:
405 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
406 (unsigned int)hdr->id);
407 goto resubmit;
408 }
409
410resubmit:
411 r = usb_submit_urb(urb, GFP_ATOMIC);
412 if (r) {
413 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
414 goto kfree;
415 }
416 return;
417kfree:
418 kfree(urb->transfer_buffer);
419}
420
421static inline int int_urb_interval(struct usb_device *udev)
422{
423 switch (udev->speed) {
424 case USB_SPEED_HIGH:
425 return 4;
426 case USB_SPEED_LOW:
427 return 10;
428 case USB_SPEED_FULL:
429 default:
430 return 1;
431 }
432}
433
434static inline int usb_int_enabled(struct zd_usb *usb)
435{
436 unsigned long flags;
437 struct zd_usb_interrupt *intr = &usb->intr;
438 struct urb *urb;
439
440 spin_lock_irqsave(&intr->lock, flags);
441 urb = intr->urb;
442 spin_unlock_irqrestore(&intr->lock, flags);
443 return urb != NULL;
444}
445
446int zd_usb_enable_int(struct zd_usb *usb)
447{
448 int r;
449 struct usb_device *udev;
450 struct zd_usb_interrupt *intr = &usb->intr;
451 void *transfer_buffer = NULL;
452 struct urb *urb;
453
454 dev_dbg_f(zd_usb_dev(usb), "\n");
455
456 urb = usb_alloc_urb(0, GFP_NOFS);
457 if (!urb) {
458 r = -ENOMEM;
459 goto out;
460 }
461
462 ZD_ASSERT(!irqs_disabled());
463 spin_lock_irq(&intr->lock);
464 if (intr->urb) {
465 spin_unlock_irq(&intr->lock);
466 r = 0;
467 goto error_free_urb;
468 }
469 intr->urb = urb;
470 spin_unlock_irq(&intr->lock);
471
472 /* TODO: make it a DMA buffer */
473 r = -ENOMEM;
474 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
475 if (!transfer_buffer) {
476 dev_dbg_f(zd_usb_dev(usb),
477 "couldn't allocate transfer_buffer\n");
478 goto error_set_urb_null;
479 }
480
481 udev = zd_usb_to_usbdev(usb);
482 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
483 transfer_buffer, USB_MAX_EP_INT_BUFFER,
484 int_urb_complete, usb,
485 intr->interval);
486
487 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
488 r = usb_submit_urb(urb, GFP_NOFS);
489 if (r) {
490 dev_dbg_f(zd_usb_dev(usb),
491 "Couldn't submit urb. Error number %d\n", r);
492 goto error;
493 }
494
495 return 0;
496error:
497 kfree(transfer_buffer);
498error_set_urb_null:
499 spin_lock_irq(&intr->lock);
500 intr->urb = NULL;
501 spin_unlock_irq(&intr->lock);
502error_free_urb:
503 usb_free_urb(urb);
504out:
505 return r;
506}
507
508void zd_usb_disable_int(struct zd_usb *usb)
509{
510 unsigned long flags;
511 struct zd_usb_interrupt *intr = &usb->intr;
512 struct urb *urb;
513
514 spin_lock_irqsave(&intr->lock, flags);
515 urb = intr->urb;
516 if (!urb) {
517 spin_unlock_irqrestore(&intr->lock, flags);
518 return;
519 }
520 intr->urb = NULL;
521 spin_unlock_irqrestore(&intr->lock, flags);
522
523 usb_kill_urb(urb);
524 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
525 usb_free_urb(urb);
526}
527
528static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
529 unsigned int length)
530{
531 int i;
532 struct zd_mac *mac = zd_usb_to_mac(usb);
533 const struct rx_length_info *length_info;
534
535 if (length < sizeof(struct rx_length_info)) {
536 /* It's not a complete packet anyhow. */
537 return;
538 }
539 length_info = (struct rx_length_info *)
540 (buffer + length - sizeof(struct rx_length_info));
541
542 /* It might be that three frames are merged into a single URB
543 * transaction. We have to check for the length info tag.
544 *
545 * While testing we discovered that length_info might be unaligned,
546 * because if USB transactions are merged, the last packet will not
547 * be padded. Unaligned access might also happen if the length_info
548 * structure is not present.
549 */
550 if (get_unaligned(&length_info->tag) == RX_LENGTH_INFO_TAG) {
551 unsigned int l, k, n;
552 for (i = 0, l = 0;; i++) {
553 k = le16_to_cpu(get_unaligned(
554 &length_info->length[i]));
555 n = l+k;
556 if (n > length)
557 return;
558 zd_mac_rx(mac, buffer+l, k);
559 if (i >= 2)
560 return;
561 l = (n+3) & ~3;
562 }
563 } else {
564 zd_mac_rx(mac, buffer, length);
565 }
566}
567
568static void rx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
569{
570 struct zd_usb *usb;
571 struct zd_usb_rx *rx;
572 const u8 *buffer;
573 unsigned int length;
574
575 switch (urb->status) {
576 case 0:
577 break;
578 case -ESHUTDOWN:
579 case -EINVAL:
580 case -ENODEV:
581 case -ENOENT:
582 case -ECONNRESET:
583 return;
584 case -EPIPE:
585 usb_clear_halt(urb->dev, EP_DATA_IN);
586 /* FALL-THROUGH */
587 default:
588 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
589 goto resubmit;
590 }
591
592 buffer = urb->transfer_buffer;
593 length = urb->actual_length;
594 usb = urb->context;
595 rx = &usb->rx;
596
597 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
598 /* If there is an old first fragment, we don't care. */
599 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
600 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
601 spin_lock(&rx->lock);
602 memcpy(rx->fragment, buffer, length);
603 rx->fragment_length = length;
604 spin_unlock(&rx->lock);
605 goto resubmit;
606 }
607
608 spin_lock(&rx->lock);
609 if (rx->fragment_length > 0) {
610 /* We are on a second fragment, we believe */
611 ZD_ASSERT(length + rx->fragment_length <=
612 ARRAY_SIZE(rx->fragment));
613 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
614 memcpy(rx->fragment+rx->fragment_length, buffer, length);
615 handle_rx_packet(usb, rx->fragment,
616 rx->fragment_length + length);
617 rx->fragment_length = 0;
618 spin_unlock(&rx->lock);
619 } else {
620 spin_unlock(&rx->lock);
621 handle_rx_packet(usb, buffer, length);
622 }
623
624resubmit:
625 usb_submit_urb(urb, GFP_ATOMIC);
626}
627
628struct urb *alloc_urb(struct zd_usb *usb)
629{
630 struct usb_device *udev = zd_usb_to_usbdev(usb);
631 struct urb *urb;
632 void *buffer;
633
634 urb = usb_alloc_urb(0, GFP_NOFS);
635 if (!urb)
636 return NULL;
637 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS,
638 &urb->transfer_dma);
639 if (!buffer) {
640 usb_free_urb(urb);
641 return NULL;
642 }
643
644 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
645 buffer, USB_MAX_RX_SIZE,
646 rx_urb_complete, usb);
647 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
648
649 return urb;
650}
651
652void free_urb(struct urb *urb)
653{
654 if (!urb)
655 return;
656 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
657 urb->transfer_buffer, urb->transfer_dma);
658 usb_free_urb(urb);
659}
660
661int zd_usb_enable_rx(struct zd_usb *usb)
662{
663 int i, r;
664 struct zd_usb_rx *rx = &usb->rx;
665 struct urb **urbs;
666
667 dev_dbg_f(zd_usb_dev(usb), "\n");
668
669 r = -ENOMEM;
670 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS);
671 if (!urbs)
672 goto error;
673 for (i = 0; i < URBS_COUNT; i++) {
674 urbs[i] = alloc_urb(usb);
675 if (!urbs[i])
676 goto error;
677 }
678
679 ZD_ASSERT(!irqs_disabled());
680 spin_lock_irq(&rx->lock);
681 if (rx->urbs) {
682 spin_unlock_irq(&rx->lock);
683 r = 0;
684 goto error;
685 }
686 rx->urbs = urbs;
687 rx->urbs_count = URBS_COUNT;
688 spin_unlock_irq(&rx->lock);
689
690 for (i = 0; i < URBS_COUNT; i++) {
691 r = usb_submit_urb(urbs[i], GFP_NOFS);
692 if (r)
693 goto error_submit;
694 }
695
696 return 0;
697error_submit:
698 for (i = 0; i < URBS_COUNT; i++) {
699 usb_kill_urb(urbs[i]);
700 }
701 spin_lock_irq(&rx->lock);
702 rx->urbs = NULL;
703 rx->urbs_count = 0;
704 spin_unlock_irq(&rx->lock);
705error:
706 if (urbs) {
707 for (i = 0; i < URBS_COUNT; i++)
708 free_urb(urbs[i]);
709 }
710 return r;
711}
712
713void zd_usb_disable_rx(struct zd_usb *usb)
714{
715 int i;
716 unsigned long flags;
717 struct urb **urbs;
718 unsigned int count;
719 struct zd_usb_rx *rx = &usb->rx;
720
721 spin_lock_irqsave(&rx->lock, flags);
722 urbs = rx->urbs;
723 count = rx->urbs_count;
724 spin_unlock_irqrestore(&rx->lock, flags);
725 if (!urbs)
726 return;
727
728 for (i = 0; i < count; i++) {
729 usb_kill_urb(urbs[i]);
730 free_urb(urbs[i]);
731 }
732 kfree(urbs);
733
734 spin_lock_irqsave(&rx->lock, flags);
735 rx->urbs = NULL;
736 rx->urbs_count = 0;
737 spin_unlock_irqrestore(&rx->lock, flags);
738}
739
740static void tx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
741{
742 int r;
743
744 switch (urb->status) {
745 case 0:
746 break;
747 case -ESHUTDOWN:
748 case -EINVAL:
749 case -ENODEV:
750 case -ENOENT:
751 case -ECONNRESET:
752 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
753 break;
754 case -EPIPE:
755 usb_clear_halt(urb->dev, EP_DATA_OUT);
756 /* FALL-THROUGH */
757 default:
758 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
759 goto resubmit;
760 }
761free_urb:
762 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
763 urb->transfer_buffer, urb->transfer_dma);
764 usb_free_urb(urb);
765 return;
766resubmit:
767 r = usb_submit_urb(urb, GFP_ATOMIC);
768 if (r) {
769 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
770 goto free_urb;
771 }
772}
773
774/* Puts the frame on the USB endpoint. It doesn't wait for
775 * completion. The frame must contain the control set.
776 */
777int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
778{
779 int r;
780 struct usb_device *udev = zd_usb_to_usbdev(usb);
781 struct urb *urb;
782 void *buffer;
783
784 urb = usb_alloc_urb(0, GFP_ATOMIC);
785 if (!urb) {
786 r = -ENOMEM;
787 goto out;
788 }
789
790 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
791 &urb->transfer_dma);
792 if (!buffer) {
793 r = -ENOMEM;
794 goto error_free_urb;
795 }
796 memcpy(buffer, frame, length);
797
798 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
799 buffer, length, tx_urb_complete, NULL);
800 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
801
802 r = usb_submit_urb(urb, GFP_ATOMIC);
803 if (r)
804 goto error;
805 return 0;
806error:
807 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
808 urb->transfer_dma);
809error_free_urb:
810 usb_free_urb(urb);
811out:
812 return r;
813}
814
815static inline void init_usb_interrupt(struct zd_usb *usb)
816{
817 struct zd_usb_interrupt *intr = &usb->intr;
818
819 spin_lock_init(&intr->lock);
820 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
821 init_completion(&intr->read_regs.completion);
822 intr->read_regs.cr_int_addr = cpu_to_le16(usb_addr(usb, CR_INTERRUPT));
823}
824
825static inline void init_usb_rx(struct zd_usb *usb)
826{
827 struct zd_usb_rx *rx = &usb->rx;
828 spin_lock_init(&rx->lock);
829 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
830 rx->usb_packet_size = 512;
831 } else {
832 rx->usb_packet_size = 64;
833 }
834 ZD_ASSERT(rx->fragment_length == 0);
835}
836
837static inline void init_usb_tx(struct zd_usb *usb)
838{
839 /* FIXME: at this point we will allocate a fixed number of urb's for
840 * use in a cyclic scheme */
841}
842
843void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
844 struct usb_interface *intf)
845{
846 memset(usb, 0, sizeof(*usb));
847 usb->intf = usb_get_intf(intf);
848 usb_set_intfdata(usb->intf, netdev);
849 init_usb_interrupt(usb);
850 init_usb_tx(usb);
851 init_usb_rx(usb);
852}
853
854int zd_usb_init_hw(struct zd_usb *usb)
855{
856 int r;
857 struct zd_chip *chip = zd_usb_to_chip(usb);
858
859 ZD_ASSERT(mutex_is_locked(&chip->mutex));
860 r = zd_ioread16_locked(chip, &usb->fw_base_offset,
861 USB_REG((u16)FW_BASE_ADDR_OFFSET));
862 if (r)
863 return r;
864 dev_dbg_f(zd_usb_dev(usb), "fw_base_offset: %#06hx\n",
865 usb->fw_base_offset);
866
867 return 0;
868}
869
870void zd_usb_clear(struct zd_usb *usb)
871{
872 usb_set_intfdata(usb->intf, NULL);
873 usb_put_intf(usb->intf);
874 memset(usb, 0, sizeof(*usb));
875 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
876}
877
878static const char *speed(enum usb_device_speed speed)
879{
880 switch (speed) {
881 case USB_SPEED_LOW:
882 return "low";
883 case USB_SPEED_FULL:
884 return "full";
885 case USB_SPEED_HIGH:
886 return "high";
887 default:
888 return "unknown speed";
889 }
890}
891
892static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
893{
894 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
895 le16_to_cpu(udev->descriptor.idVendor),
896 le16_to_cpu(udev->descriptor.idProduct),
897 get_bcdDevice(udev),
898 speed(udev->speed));
899}
900
901int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
902{
903 struct usb_device *udev = interface_to_usbdev(usb->intf);
904 return scnprint_id(udev, buffer, size);
905}
906
907#ifdef DEBUG
908static void print_id(struct usb_device *udev)
909{
910 char buffer[40];
911
912 scnprint_id(udev, buffer, sizeof(buffer));
913 buffer[sizeof(buffer)-1] = 0;
914 dev_dbg_f(&udev->dev, "%s\n", buffer);
915}
916#else
917#define print_id(udev) do { } while (0)
918#endif
919
920static int probe(struct usb_interface *intf, const struct usb_device_id *id)
921{
922 int r;
923 struct usb_device *udev = interface_to_usbdev(intf);
924 struct net_device *netdev = NULL;
925
926 print_id(udev);
927
928 switch (udev->speed) {
929 case USB_SPEED_LOW:
930 case USB_SPEED_FULL:
931 case USB_SPEED_HIGH:
932 break;
933 default:
934 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
935 r = -ENODEV;
936 goto error;
937 }
938
939 netdev = zd_netdev_alloc(intf);
940 if (netdev == NULL) {
941 r = -ENOMEM;
942 goto error;
943 }
944
945 r = upload_firmware(udev, id->driver_info);
946 if (r) {
947 dev_err(&intf->dev,
948 "couldn't load firmware. Error number %d\n", r);
949 goto error;
950 }
951
952 r = usb_reset_configuration(udev);
953 if (r) {
954 dev_dbg_f(&intf->dev,
955 "couldn't reset configuration. Error number %d\n", r);
956 goto error;
957 }
958
959 /* At this point the interrupt endpoint is not generally enabled. We
960 * save the USB bandwidth until the network device is opened. But
961 * notify that the initialization of the MAC will require the
962 * interrupts to be temporary enabled.
963 */
964 r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info);
965 if (r) {
966 dev_dbg_f(&intf->dev,
967 "couldn't initialize mac. Error number %d\n", r);
968 goto error;
969 }
970
971 r = register_netdev(netdev);
972 if (r) {
973 dev_dbg_f(&intf->dev,
974 "couldn't register netdev. Error number %d\n", r);
975 goto error;
976 }
977
978 dev_dbg_f(&intf->dev, "successful\n");
979 dev_info(&intf->dev,"%s\n", netdev->name);
980 return 0;
981error:
982 usb_reset_device(interface_to_usbdev(intf));
983 zd_netdev_free(netdev);
984 return r;
985}
986
987static void disconnect(struct usb_interface *intf)
988{
989 struct net_device *netdev = zd_intf_to_netdev(intf);
990 struct zd_mac *mac = zd_netdev_mac(netdev);
991 struct zd_usb *usb = &mac->chip.usb;
992
993 dev_dbg_f(zd_usb_dev(usb), "\n");
994
995 zd_netdev_disconnect(netdev);
996
997 /* Just in case something has gone wrong! */
998 zd_usb_disable_rx(usb);
999 zd_usb_disable_int(usb);
1000
1001 /* If the disconnect has been caused by a removal of the
1002 * driver module, the reset allows reloading of the driver. If the
1003 * reset will not be executed here, the upload of the firmware in the
1004 * probe function caused by the reloading of the driver will fail.
1005 */
1006 usb_reset_device(interface_to_usbdev(intf));
1007
1008 /* If somebody still waits on this lock now, this is an error. */
1009 zd_netdev_free(netdev);
1010 dev_dbg(&intf->dev, "disconnected\n");
1011}
1012
1013static struct usb_driver driver = {
1014 .name = "zd1211rw",
1015 .id_table = usb_ids,
1016 .probe = probe,
1017 .disconnect = disconnect,
1018};
1019
1020static int __init usb_init(void)
1021{
1022 int r;
1023
1024 pr_debug("usb_init()\n");
1025
1026 r = usb_register(&driver);
1027 if (r) {
1028 printk(KERN_ERR "usb_register() failed. Error number %d\n", r);
1029 return r;
1030 }
1031
1032 pr_debug("zd1211rw initialized\n");
1033 return 0;
1034}
1035
1036static void __exit usb_exit(void)
1037{
1038 pr_debug("usb_exit()\n");
1039 usb_deregister(&driver);
1040}
1041
1042module_init(usb_init);
1043module_exit(usb_exit);
1044
1045static int usb_int_regs_length(unsigned int count)
1046{
1047 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1048}
1049
1050static void prepare_read_regs_int(struct zd_usb *usb)
1051{
1052 struct zd_usb_interrupt *intr = &usb->intr;
1053
1054 spin_lock(&intr->lock);
1055 intr->read_regs_enabled = 1;
1056 INIT_COMPLETION(intr->read_regs.completion);
1057 spin_unlock(&intr->lock);
1058}
1059
1060static int get_results(struct zd_usb *usb, u16 *values,
1061 struct usb_req_read_regs *req, unsigned int count)
1062{
1063 int r;
1064 int i;
1065 struct zd_usb_interrupt *intr = &usb->intr;
1066 struct read_regs_int *rr = &intr->read_regs;
1067 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1068
1069 spin_lock(&intr->lock);
1070
1071 r = -EIO;
1072 /* The created block size seems to be larger than expected.
1073 * However results appear to be correct.
1074 */
1075 if (rr->length < usb_int_regs_length(count)) {
1076 dev_dbg_f(zd_usb_dev(usb),
1077 "error: actual length %d less than expected %d\n",
1078 rr->length, usb_int_regs_length(count));
1079 goto error_unlock;
1080 }
1081 if (rr->length > sizeof(rr->buffer)) {
1082 dev_dbg_f(zd_usb_dev(usb),
1083 "error: actual length %d exceeds buffer size %zu\n",
1084 rr->length, sizeof(rr->buffer));
1085 goto error_unlock;
1086 }
1087
1088 for (i = 0; i < count; i++) {
1089 struct reg_data *rd = &regs->regs[i];
1090 if (rd->addr != req->addr[i]) {
1091 dev_dbg_f(zd_usb_dev(usb),
1092 "rd[%d] addr %#06hx expected %#06hx\n", i,
1093 le16_to_cpu(rd->addr),
1094 le16_to_cpu(req->addr[i]));
1095 goto error_unlock;
1096 }
1097 values[i] = le16_to_cpu(rd->value);
1098 }
1099
1100 r = 0;
1101error_unlock:
1102 spin_unlock(&intr->lock);
1103 return r;
1104}
1105
1106int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1107 const zd_addr_t *addresses, unsigned int count)
1108{
1109 int r;
1110 int i, req_len, actual_req_len;
1111 struct usb_device *udev;
1112 struct usb_req_read_regs *req = NULL;
1113 unsigned long timeout;
1114
1115 if (count < 1) {
1116 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1117 return -EINVAL;
1118 }
1119 if (count > USB_MAX_IOREAD16_COUNT) {
1120 dev_dbg_f(zd_usb_dev(usb),
1121 "error: count %u exceeds possible max %u\n",
1122 count, USB_MAX_IOREAD16_COUNT);
1123 return -EINVAL;
1124 }
1125 if (in_atomic()) {
1126 dev_dbg_f(zd_usb_dev(usb),
1127 "error: io in atomic context not supported\n");
1128 return -EWOULDBLOCK;
1129 }
1130 if (!usb_int_enabled(usb)) {
1131 dev_dbg_f(zd_usb_dev(usb),
1132 "error: usb interrupt not enabled\n");
1133 return -EWOULDBLOCK;
1134 }
1135
1136 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1137 req = kmalloc(req_len, GFP_NOFS);
1138 if (!req)
1139 return -ENOMEM;
1140 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1141 for (i = 0; i < count; i++)
1142 req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i]));
1143
1144 udev = zd_usb_to_usbdev(usb);
1145 prepare_read_regs_int(usb);
1146 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1147 req, req_len, &actual_req_len, 1000 /* ms */);
1148 if (r) {
1149 dev_dbg_f(zd_usb_dev(usb),
1150 "error in usb_bulk_msg(). Error number %d\n", r);
1151 goto error;
1152 }
1153 if (req_len != actual_req_len) {
1154 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1155 " req_len %d != actual_req_len %d\n",
1156 req_len, actual_req_len);
1157 r = -EIO;
1158 goto error;
1159 }
1160
1161 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1162 msecs_to_jiffies(1000));
1163 if (!timeout) {
1164 disable_read_regs_int(usb);
1165 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1166 r = -ETIMEDOUT;
1167 goto error;
1168 }
1169
1170 r = get_results(usb, values, req, count);
1171error:
1172 kfree(req);
1173 return r;
1174}
1175
1176int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1177 unsigned int count)
1178{
1179 int r;
1180 struct usb_device *udev;
1181 struct usb_req_write_regs *req = NULL;
1182 int i, req_len, actual_req_len;
1183
1184 if (count == 0)
1185 return 0;
1186 if (count > USB_MAX_IOWRITE16_COUNT) {
1187 dev_dbg_f(zd_usb_dev(usb),
1188 "error: count %u exceeds possible max %u\n",
1189 count, USB_MAX_IOWRITE16_COUNT);
1190 return -EINVAL;
1191 }
1192 if (in_atomic()) {
1193 dev_dbg_f(zd_usb_dev(usb),
1194 "error: io in atomic context not supported\n");
1195 return -EWOULDBLOCK;
1196 }
1197
1198 req_len = sizeof(struct usb_req_write_regs) +
1199 count * sizeof(struct reg_data);
1200 req = kmalloc(req_len, GFP_NOFS);
1201 if (!req)
1202 return -ENOMEM;
1203
1204 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1205 for (i = 0; i < count; i++) {
1206 struct reg_data *rw = &req->reg_writes[i];
1207 rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr));
1208 rw->value = cpu_to_le16(ioreqs[i].value);
1209 }
1210
1211 udev = zd_usb_to_usbdev(usb);
1212 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1213 req, req_len, &actual_req_len, 1000 /* ms */);
1214 if (r) {
1215 dev_dbg_f(zd_usb_dev(usb),
1216 "error in usb_bulk_msg(). Error number %d\n", r);
1217 goto error;
1218 }
1219 if (req_len != actual_req_len) {
1220 dev_dbg_f(zd_usb_dev(usb),
1221 "error in usb_bulk_msg()"
1222 " req_len %d != actual_req_len %d\n",
1223 req_len, actual_req_len);
1224 r = -EIO;
1225 goto error;
1226 }
1227
1228 /* FALL-THROUGH with r == 0 */
1229error:
1230 kfree(req);
1231 return r;
1232}
1233
1234int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1235{
1236 int r;
1237 struct usb_device *udev;
1238 struct usb_req_rfwrite *req = NULL;
1239 int i, req_len, actual_req_len;
1240 u16 bit_value_template;
1241
1242 if (in_atomic()) {
1243 dev_dbg_f(zd_usb_dev(usb),
1244 "error: io in atomic context not supported\n");
1245 return -EWOULDBLOCK;
1246 }
1247 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1248 dev_dbg_f(zd_usb_dev(usb),
1249 "error: bits %d are smaller than"
1250 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1251 bits, USB_MIN_RFWRITE_BIT_COUNT);
1252 return -EINVAL;
1253 }
1254 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1255 dev_dbg_f(zd_usb_dev(usb),
1256 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1257 bits, USB_MAX_RFWRITE_BIT_COUNT);
1258 return -EINVAL;
1259 }
1260#ifdef DEBUG
1261 if (value & (~0UL << bits)) {
1262 dev_dbg_f(zd_usb_dev(usb),
1263 "error: value %#09x has bits >= %d set\n",
1264 value, bits);
1265 return -EINVAL;
1266 }
1267#endif /* DEBUG */
1268
1269 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1270
1271 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1272 if (r) {
1273 dev_dbg_f(zd_usb_dev(usb),
1274 "error %d: Couldn't read CR203\n", r);
1275 goto out;
1276 }
1277 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1278
1279 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1280 req = kmalloc(req_len, GFP_NOFS);
1281 if (!req)
1282 return -ENOMEM;
1283
1284 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1285 /* 1: 3683a, but not used in ZYDAS driver */
1286 req->value = cpu_to_le16(2);
1287 req->bits = cpu_to_le16(bits);
1288
1289 for (i = 0; i < bits; i++) {
1290 u16 bv = bit_value_template;
1291 if (value & (1 << (bits-1-i)))
1292 bv |= RF_DATA;
1293 req->bit_values[i] = cpu_to_le16(bv);
1294 }
1295
1296 udev = zd_usb_to_usbdev(usb);
1297 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1298 req, req_len, &actual_req_len, 1000 /* ms */);
1299 if (r) {
1300 dev_dbg_f(zd_usb_dev(usb),
1301 "error in usb_bulk_msg(). Error number %d\n", r);
1302 goto out;
1303 }
1304 if (req_len != actual_req_len) {
1305 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1306 " req_len %d != actual_req_len %d\n",
1307 req_len, actual_req_len);
1308 r = -EIO;
1309 goto out;
1310 }
1311
1312 /* FALL-THROUGH with r == 0 */
1313out:
1314 kfree(req);
1315 return r;
1316}