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.
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.
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
18 #include <asm/unaligned.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/firmware.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/skbuff.h>
26 #include <linux/usb.h>
27 #include <net/ieee80211.h>
30 #include "zd_netdev.h"
35 static struct usb_device_id usb_ids[] = {
37 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
38 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
51 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
52 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
53 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
54 /* "Driverless" devices that need ejecting */
55 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
59 MODULE_LICENSE("GPL");
60 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
61 MODULE_AUTHOR("Ulrich Kunitz");
62 MODULE_AUTHOR("Daniel Drake");
63 MODULE_VERSION("1.0");
64 MODULE_DEVICE_TABLE(usb, usb_ids);
66 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
67 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
69 /* register address handling */
72 static int check_addr(struct zd_usb *usb, zd_addr_t addr)
74 u32 base = ZD_ADDR_BASE(addr);
75 u32 offset = ZD_OFFSET(addr);
77 if ((u32)addr & ADDR_ZERO_MASK)
83 if (offset > CR_MAX_OFFSET) {
84 dev_dbg(zd_usb_dev(usb),
85 "CR offset %#010x larger than"
86 " CR_MAX_OFFSET %#10x\n",
87 offset, CR_MAX_OFFSET);
91 dev_dbg(zd_usb_dev(usb),
92 "CR offset %#010x is not a multiple of 2\n",
98 if (offset > E2P_MAX_OFFSET) {
99 dev_dbg(zd_usb_dev(usb),
100 "E2P offset %#010x larger than"
101 " E2P_MAX_OFFSET %#010x\n",
102 offset, E2P_MAX_OFFSET);
103 goto invalid_address;
107 if (!usb->fw_base_offset) {
108 dev_dbg(zd_usb_dev(usb),
109 "ERROR: fw base offset has not been set\n");
112 if (offset > FW_MAX_OFFSET) {
113 dev_dbg(zd_usb_dev(usb),
114 "FW offset %#10x is larger than"
115 " FW_MAX_OFFSET %#010x\n",
116 offset, FW_MAX_OFFSET);
117 goto invalid_address;
121 dev_dbg(zd_usb_dev(usb),
122 "address has unsupported base %#010x\n", addr);
123 goto invalid_address;
128 dev_dbg(zd_usb_dev(usb),
129 "ERROR: invalid address: %#010x\n", addr);
134 static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr)
139 base = ZD_ADDR_BASE(addr);
140 offset = ZD_OFFSET(addr);
142 ZD_ASSERT(check_addr(usb, addr) == 0);
146 offset += CR_BASE_OFFSET;
149 offset += E2P_BASE_OFFSET;
152 offset += usb->fw_base_offset;
159 /* USB device initialization */
161 static int request_fw_file(
162 const struct firmware **fw, const char *name, struct device *device)
166 dev_dbg_f(device, "fw name %s\n", name);
168 r = request_firmware(fw, name, device);
171 "Could not load firmware file %s. Error number %d\n",
176 static inline u16 get_bcdDevice(const struct usb_device *udev)
178 return le16_to_cpu(udev->descriptor.bcdDevice);
181 enum upload_code_flags {
185 /* Ensures that MAX_TRANSFER_SIZE is even. */
186 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
188 static int upload_code(struct usb_device *udev,
189 const u8 *data, size_t size, u16 code_offset, int flags)
194 /* USB request blocks need "kmalloced" buffers.
196 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
198 dev_err(&udev->dev, "out of memory\n");
205 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
206 size : MAX_TRANSFER_SIZE;
208 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
210 memcpy(p, data, transfer_size);
211 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
212 USB_REQ_FIRMWARE_DOWNLOAD,
213 USB_DIR_OUT | USB_TYPE_VENDOR,
214 code_offset, 0, p, transfer_size, 1000 /* ms */);
217 "USB control request for firmware upload"
218 " failed. Error number %d\n", r);
221 transfer_size = r & ~1;
223 size -= transfer_size;
224 data += transfer_size;
225 code_offset += transfer_size/sizeof(u16);
228 if (flags & REBOOT) {
231 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
232 USB_REQ_FIRMWARE_CONFIRM,
233 USB_DIR_IN | USB_TYPE_VENDOR,
234 0, 0, &ret, sizeof(ret), 5000 /* ms */);
235 if (r != sizeof(ret)) {
237 "control request firmeware confirmation failed."
238 " Return value %d\n", r);
245 "Internal error while downloading."
246 " Firmware confirm return value %#04x\n",
251 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
261 static u16 get_word(const void *data, u16 offset)
263 const __le16 *p = data;
264 return le16_to_cpu(p[offset]);
267 static char *get_fw_name(char *buffer, size_t size, u8 device_type,
270 scnprintf(buffer, size, "%s%s",
271 device_type == DEVICE_ZD1211B ?
272 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
277 static int handle_version_mismatch(struct usb_device *udev, u8 device_type,
278 const struct firmware *ub_fw)
280 const struct firmware *ur_fw = NULL;
285 r = request_fw_file(&ur_fw,
286 get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"),
291 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START_OFFSET,
296 offset = ((EEPROM_REGS_OFFSET + EEPROM_REGS_SIZE) * sizeof(u16));
297 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
298 E2P_BASE_OFFSET + EEPROM_REGS_SIZE, REBOOT);
300 /* At this point, the vendor driver downloads the whole firmware
301 * image, hacks around with version IDs, and uploads it again,
302 * completely overwriting the boot code. We do not do this here as
303 * it is not required on any tested devices, and it is suspected to
306 release_firmware(ur_fw);
310 static int upload_firmware(struct usb_device *udev, u8 device_type)
315 const struct firmware *ub_fw = NULL;
316 const struct firmware *uph_fw = NULL;
319 bcdDevice = get_bcdDevice(udev);
321 r = request_fw_file(&ub_fw,
322 get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"),
327 fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET);
329 if (fw_bcdDevice != bcdDevice) {
331 "firmware version %#06x and device bootcode version "
332 "%#06x differ\n", fw_bcdDevice, bcdDevice);
333 if (bcdDevice <= 0x4313)
334 dev_warn(&udev->dev, "device has old bootcode, please "
335 "report success or failure\n");
337 r = handle_version_mismatch(udev, device_type, ub_fw);
341 dev_dbg_f(&udev->dev,
342 "firmware device id %#06x is equal to the "
343 "actual device id\n", fw_bcdDevice);
347 r = request_fw_file(&uph_fw,
348 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
353 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET,
357 "Could not upload firmware code uph. Error number %d\n",
363 release_firmware(ub_fw);
364 release_firmware(uph_fw);
368 static void disable_read_regs_int(struct zd_usb *usb)
370 struct zd_usb_interrupt *intr = &usb->intr;
372 spin_lock(&intr->lock);
373 intr->read_regs_enabled = 0;
374 spin_unlock(&intr->lock);
377 #define urb_dev(urb) (&(urb)->dev->dev)
379 static inline void handle_regs_int(struct urb *urb)
381 struct zd_usb *usb = urb->context;
382 struct zd_usb_interrupt *intr = &usb->intr;
385 ZD_ASSERT(in_interrupt());
386 spin_lock(&intr->lock);
388 if (intr->read_regs_enabled) {
389 intr->read_regs.length = len = urb->actual_length;
391 if (len > sizeof(intr->read_regs.buffer))
392 len = sizeof(intr->read_regs.buffer);
393 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
394 intr->read_regs_enabled = 0;
395 complete(&intr->read_regs.completion);
399 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
401 spin_unlock(&intr->lock);
404 static inline void handle_retry_failed_int(struct urb *urb)
406 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
410 static void int_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
413 struct usb_int_header *hdr;
415 switch (urb->status) {
429 if (urb->actual_length < sizeof(hdr)) {
430 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
434 hdr = urb->transfer_buffer;
435 if (hdr->type != USB_INT_TYPE) {
436 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
441 case USB_INT_ID_REGS:
442 handle_regs_int(urb);
444 case USB_INT_ID_RETRY_FAILED:
445 handle_retry_failed_int(urb);
448 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
449 (unsigned int)hdr->id);
454 r = usb_submit_urb(urb, GFP_ATOMIC);
456 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
461 kfree(urb->transfer_buffer);
464 static inline int int_urb_interval(struct usb_device *udev)
466 switch (udev->speed) {
477 static inline int usb_int_enabled(struct zd_usb *usb)
480 struct zd_usb_interrupt *intr = &usb->intr;
483 spin_lock_irqsave(&intr->lock, flags);
485 spin_unlock_irqrestore(&intr->lock, flags);
489 int zd_usb_enable_int(struct zd_usb *usb)
492 struct usb_device *udev;
493 struct zd_usb_interrupt *intr = &usb->intr;
494 void *transfer_buffer = NULL;
497 dev_dbg_f(zd_usb_dev(usb), "\n");
499 urb = usb_alloc_urb(0, GFP_NOFS);
505 ZD_ASSERT(!irqs_disabled());
506 spin_lock_irq(&intr->lock);
508 spin_unlock_irq(&intr->lock);
513 spin_unlock_irq(&intr->lock);
515 /* TODO: make it a DMA buffer */
517 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
518 if (!transfer_buffer) {
519 dev_dbg_f(zd_usb_dev(usb),
520 "couldn't allocate transfer_buffer\n");
521 goto error_set_urb_null;
524 udev = zd_usb_to_usbdev(usb);
525 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
526 transfer_buffer, USB_MAX_EP_INT_BUFFER,
527 int_urb_complete, usb,
530 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
531 r = usb_submit_urb(urb, GFP_NOFS);
533 dev_dbg_f(zd_usb_dev(usb),
534 "Couldn't submit urb. Error number %d\n", r);
540 kfree(transfer_buffer);
542 spin_lock_irq(&intr->lock);
544 spin_unlock_irq(&intr->lock);
551 void zd_usb_disable_int(struct zd_usb *usb)
554 struct zd_usb_interrupt *intr = &usb->intr;
557 spin_lock_irqsave(&intr->lock, flags);
560 spin_unlock_irqrestore(&intr->lock, flags);
564 spin_unlock_irqrestore(&intr->lock, flags);
567 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
571 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
575 struct zd_mac *mac = zd_usb_to_mac(usb);
576 const struct rx_length_info *length_info;
578 if (length < sizeof(struct rx_length_info)) {
579 /* It's not a complete packet anyhow. */
582 length_info = (struct rx_length_info *)
583 (buffer + length - sizeof(struct rx_length_info));
585 /* It might be that three frames are merged into a single URB
586 * transaction. We have to check for the length info tag.
588 * While testing we discovered that length_info might be unaligned,
589 * because if USB transactions are merged, the last packet will not
590 * be padded. Unaligned access might also happen if the length_info
591 * structure is not present.
593 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
595 unsigned int l, k, n;
596 for (i = 0, l = 0;; i++) {
597 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
601 zd_mac_rx(mac, buffer+l, k);
607 zd_mac_rx(mac, buffer, length);
611 static void rx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
614 struct zd_usb_rx *rx;
618 switch (urb->status) {
629 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
633 buffer = urb->transfer_buffer;
634 length = urb->actual_length;
638 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
639 /* If there is an old first fragment, we don't care. */
640 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
641 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
642 spin_lock(&rx->lock);
643 memcpy(rx->fragment, buffer, length);
644 rx->fragment_length = length;
645 spin_unlock(&rx->lock);
649 spin_lock(&rx->lock);
650 if (rx->fragment_length > 0) {
651 /* We are on a second fragment, we believe */
652 ZD_ASSERT(length + rx->fragment_length <=
653 ARRAY_SIZE(rx->fragment));
654 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
655 memcpy(rx->fragment+rx->fragment_length, buffer, length);
656 handle_rx_packet(usb, rx->fragment,
657 rx->fragment_length + length);
658 rx->fragment_length = 0;
659 spin_unlock(&rx->lock);
661 spin_unlock(&rx->lock);
662 handle_rx_packet(usb, buffer, length);
666 usb_submit_urb(urb, GFP_ATOMIC);
669 static struct urb *alloc_urb(struct zd_usb *usb)
671 struct usb_device *udev = zd_usb_to_usbdev(usb);
675 urb = usb_alloc_urb(0, GFP_NOFS);
678 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS,
685 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
686 buffer, USB_MAX_RX_SIZE,
687 rx_urb_complete, usb);
688 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
693 static void free_urb(struct urb *urb)
697 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
698 urb->transfer_buffer, urb->transfer_dma);
702 int zd_usb_enable_rx(struct zd_usb *usb)
705 struct zd_usb_rx *rx = &usb->rx;
708 dev_dbg_f(zd_usb_dev(usb), "\n");
711 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS);
714 for (i = 0; i < URBS_COUNT; i++) {
715 urbs[i] = alloc_urb(usb);
720 ZD_ASSERT(!irqs_disabled());
721 spin_lock_irq(&rx->lock);
723 spin_unlock_irq(&rx->lock);
728 rx->urbs_count = URBS_COUNT;
729 spin_unlock_irq(&rx->lock);
731 for (i = 0; i < URBS_COUNT; i++) {
732 r = usb_submit_urb(urbs[i], GFP_NOFS);
739 for (i = 0; i < URBS_COUNT; i++) {
740 usb_kill_urb(urbs[i]);
742 spin_lock_irq(&rx->lock);
745 spin_unlock_irq(&rx->lock);
748 for (i = 0; i < URBS_COUNT; i++)
754 void zd_usb_disable_rx(struct zd_usb *usb)
760 struct zd_usb_rx *rx = &usb->rx;
762 spin_lock_irqsave(&rx->lock, flags);
764 count = rx->urbs_count;
765 spin_unlock_irqrestore(&rx->lock, flags);
769 for (i = 0; i < count; i++) {
770 usb_kill_urb(urbs[i]);
775 spin_lock_irqsave(&rx->lock, flags);
778 spin_unlock_irqrestore(&rx->lock, flags);
781 static void tx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
785 switch (urb->status) {
794 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
797 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
801 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
802 urb->transfer_buffer, urb->transfer_dma);
806 r = usb_submit_urb(urb, GFP_ATOMIC);
808 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
813 /* Puts the frame on the USB endpoint. It doesn't wait for
814 * completion. The frame must contain the control set.
816 int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
819 struct usb_device *udev = zd_usb_to_usbdev(usb);
823 urb = usb_alloc_urb(0, GFP_ATOMIC);
829 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
835 memcpy(buffer, frame, length);
837 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
838 buffer, length, tx_urb_complete, NULL);
839 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
841 r = usb_submit_urb(urb, GFP_ATOMIC);
846 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
854 static inline void init_usb_interrupt(struct zd_usb *usb)
856 struct zd_usb_interrupt *intr = &usb->intr;
858 spin_lock_init(&intr->lock);
859 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
860 init_completion(&intr->read_regs.completion);
861 intr->read_regs.cr_int_addr = cpu_to_le16(usb_addr(usb, CR_INTERRUPT));
864 static inline void init_usb_rx(struct zd_usb *usb)
866 struct zd_usb_rx *rx = &usb->rx;
867 spin_lock_init(&rx->lock);
868 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
869 rx->usb_packet_size = 512;
871 rx->usb_packet_size = 64;
873 ZD_ASSERT(rx->fragment_length == 0);
876 static inline void init_usb_tx(struct zd_usb *usb)
878 /* FIXME: at this point we will allocate a fixed number of urb's for
879 * use in a cyclic scheme */
882 void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
883 struct usb_interface *intf)
885 memset(usb, 0, sizeof(*usb));
886 usb->intf = usb_get_intf(intf);
887 usb_set_intfdata(usb->intf, netdev);
888 init_usb_interrupt(usb);
893 int zd_usb_init_hw(struct zd_usb *usb)
896 struct zd_chip *chip = zd_usb_to_chip(usb);
898 ZD_ASSERT(mutex_is_locked(&chip->mutex));
899 r = zd_ioread16_locked(chip, &usb->fw_base_offset,
900 USB_REG((u16)FW_BASE_ADDR_OFFSET));
903 dev_dbg_f(zd_usb_dev(usb), "fw_base_offset: %#06hx\n",
904 usb->fw_base_offset);
909 void zd_usb_clear(struct zd_usb *usb)
911 usb_set_intfdata(usb->intf, NULL);
912 usb_put_intf(usb->intf);
913 ZD_MEMCLEAR(usb, sizeof(*usb));
914 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
917 static const char *speed(enum usb_device_speed speed)
927 return "unknown speed";
931 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
933 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
934 le16_to_cpu(udev->descriptor.idVendor),
935 le16_to_cpu(udev->descriptor.idProduct),
940 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
942 struct usb_device *udev = interface_to_usbdev(usb->intf);
943 return scnprint_id(udev, buffer, size);
947 static void print_id(struct usb_device *udev)
951 scnprint_id(udev, buffer, sizeof(buffer));
952 buffer[sizeof(buffer)-1] = 0;
953 dev_dbg_f(&udev->dev, "%s\n", buffer);
956 #define print_id(udev) do { } while (0)
959 static int eject_installer(struct usb_interface *intf)
961 struct usb_device *udev = interface_to_usbdev(intf);
962 struct usb_host_interface *iface_desc = &intf->altsetting[0];
963 struct usb_endpoint_descriptor *endpoint;
968 /* Find bulk out endpoint */
969 endpoint = &iface_desc->endpoint[1].desc;
970 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
971 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
972 USB_ENDPOINT_XFER_BULK) {
973 bulk_out_ep = endpoint->bEndpointAddress;
976 "zd1211rw: Could not find bulk out endpoint\n");
980 cmd = kzalloc(31, GFP_KERNEL);
984 /* USB bulk command block */
985 cmd[0] = 0x55; /* bulk command signature */
986 cmd[1] = 0x53; /* bulk command signature */
987 cmd[2] = 0x42; /* bulk command signature */
988 cmd[3] = 0x43; /* bulk command signature */
989 cmd[14] = 6; /* command length */
991 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
992 cmd[19] = 0x2; /* eject disc */
994 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
995 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
996 cmd, 31, NULL, 2000);
1001 /* At this point, the device disconnects and reconnects with the real
1004 usb_set_intfdata(intf, NULL);
1008 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
1011 struct usb_device *udev = interface_to_usbdev(intf);
1012 struct net_device *netdev = NULL;
1016 if (id->driver_info & DEVICE_INSTALLER)
1017 return eject_installer(intf);
1019 switch (udev->speed) {
1021 case USB_SPEED_FULL:
1022 case USB_SPEED_HIGH:
1025 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1030 netdev = zd_netdev_alloc(intf);
1031 if (netdev == NULL) {
1036 r = upload_firmware(udev, id->driver_info);
1039 "couldn't load firmware. Error number %d\n", r);
1043 r = usb_reset_configuration(udev);
1045 dev_dbg_f(&intf->dev,
1046 "couldn't reset configuration. Error number %d\n", r);
1050 /* At this point the interrupt endpoint is not generally enabled. We
1051 * save the USB bandwidth until the network device is opened. But
1052 * notify that the initialization of the MAC will require the
1053 * interrupts to be temporary enabled.
1055 r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info);
1057 dev_dbg_f(&intf->dev,
1058 "couldn't initialize mac. Error number %d\n", r);
1062 r = register_netdev(netdev);
1064 dev_dbg_f(&intf->dev,
1065 "couldn't register netdev. Error number %d\n", r);
1069 dev_dbg_f(&intf->dev, "successful\n");
1070 dev_info(&intf->dev,"%s\n", netdev->name);
1073 usb_reset_device(interface_to_usbdev(intf));
1074 zd_netdev_free(netdev);
1078 static void disconnect(struct usb_interface *intf)
1080 struct net_device *netdev = zd_intf_to_netdev(intf);
1081 struct zd_mac *mac = zd_netdev_mac(netdev);
1082 struct zd_usb *usb = &mac->chip.usb;
1084 /* Either something really bad happened, or we're just dealing with
1085 * a DEVICE_INSTALLER. */
1089 dev_dbg_f(zd_usb_dev(usb), "\n");
1091 zd_netdev_disconnect(netdev);
1093 /* Just in case something has gone wrong! */
1094 zd_usb_disable_rx(usb);
1095 zd_usb_disable_int(usb);
1097 /* If the disconnect has been caused by a removal of the
1098 * driver module, the reset allows reloading of the driver. If the
1099 * reset will not be executed here, the upload of the firmware in the
1100 * probe function caused by the reloading of the driver will fail.
1102 usb_reset_device(interface_to_usbdev(intf));
1104 zd_netdev_free(netdev);
1105 dev_dbg(&intf->dev, "disconnected\n");
1108 static struct usb_driver driver = {
1110 .id_table = usb_ids,
1112 .disconnect = disconnect,
1115 static int __init usb_init(void)
1119 pr_debug("usb_init()\n");
1121 r = usb_register(&driver);
1123 printk(KERN_ERR "usb_register() failed. Error number %d\n", r);
1127 pr_debug("zd1211rw initialized\n");
1131 static void __exit usb_exit(void)
1133 pr_debug("usb_exit()\n");
1134 usb_deregister(&driver);
1137 module_init(usb_init);
1138 module_exit(usb_exit);
1140 static int usb_int_regs_length(unsigned int count)
1142 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1145 static void prepare_read_regs_int(struct zd_usb *usb)
1147 struct zd_usb_interrupt *intr = &usb->intr;
1149 spin_lock(&intr->lock);
1150 intr->read_regs_enabled = 1;
1151 INIT_COMPLETION(intr->read_regs.completion);
1152 spin_unlock(&intr->lock);
1155 static int get_results(struct zd_usb *usb, u16 *values,
1156 struct usb_req_read_regs *req, unsigned int count)
1160 struct zd_usb_interrupt *intr = &usb->intr;
1161 struct read_regs_int *rr = &intr->read_regs;
1162 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1164 spin_lock(&intr->lock);
1167 /* The created block size seems to be larger than expected.
1168 * However results appear to be correct.
1170 if (rr->length < usb_int_regs_length(count)) {
1171 dev_dbg_f(zd_usb_dev(usb),
1172 "error: actual length %d less than expected %d\n",
1173 rr->length, usb_int_regs_length(count));
1176 if (rr->length > sizeof(rr->buffer)) {
1177 dev_dbg_f(zd_usb_dev(usb),
1178 "error: actual length %d exceeds buffer size %zu\n",
1179 rr->length, sizeof(rr->buffer));
1183 for (i = 0; i < count; i++) {
1184 struct reg_data *rd = ®s->regs[i];
1185 if (rd->addr != req->addr[i]) {
1186 dev_dbg_f(zd_usb_dev(usb),
1187 "rd[%d] addr %#06hx expected %#06hx\n", i,
1188 le16_to_cpu(rd->addr),
1189 le16_to_cpu(req->addr[i]));
1192 values[i] = le16_to_cpu(rd->value);
1197 spin_unlock(&intr->lock);
1201 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1202 const zd_addr_t *addresses, unsigned int count)
1205 int i, req_len, actual_req_len;
1206 struct usb_device *udev;
1207 struct usb_req_read_regs *req = NULL;
1208 unsigned long timeout;
1211 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1214 if (count > USB_MAX_IOREAD16_COUNT) {
1215 dev_dbg_f(zd_usb_dev(usb),
1216 "error: count %u exceeds possible max %u\n",
1217 count, USB_MAX_IOREAD16_COUNT);
1221 dev_dbg_f(zd_usb_dev(usb),
1222 "error: io in atomic context not supported\n");
1223 return -EWOULDBLOCK;
1225 if (!usb_int_enabled(usb)) {
1226 dev_dbg_f(zd_usb_dev(usb),
1227 "error: usb interrupt not enabled\n");
1228 return -EWOULDBLOCK;
1231 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1232 req = kmalloc(req_len, GFP_NOFS);
1235 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1236 for (i = 0; i < count; i++)
1237 req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i]));
1239 udev = zd_usb_to_usbdev(usb);
1240 prepare_read_regs_int(usb);
1241 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1242 req, req_len, &actual_req_len, 1000 /* ms */);
1244 dev_dbg_f(zd_usb_dev(usb),
1245 "error in usb_bulk_msg(). Error number %d\n", r);
1248 if (req_len != actual_req_len) {
1249 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1250 " req_len %d != actual_req_len %d\n",
1251 req_len, actual_req_len);
1256 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1257 msecs_to_jiffies(1000));
1259 disable_read_regs_int(usb);
1260 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1265 r = get_results(usb, values, req, count);
1271 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1275 struct usb_device *udev;
1276 struct usb_req_write_regs *req = NULL;
1277 int i, req_len, actual_req_len;
1281 if (count > USB_MAX_IOWRITE16_COUNT) {
1282 dev_dbg_f(zd_usb_dev(usb),
1283 "error: count %u exceeds possible max %u\n",
1284 count, USB_MAX_IOWRITE16_COUNT);
1288 dev_dbg_f(zd_usb_dev(usb),
1289 "error: io in atomic context not supported\n");
1290 return -EWOULDBLOCK;
1293 req_len = sizeof(struct usb_req_write_regs) +
1294 count * sizeof(struct reg_data);
1295 req = kmalloc(req_len, GFP_NOFS);
1299 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1300 for (i = 0; i < count; i++) {
1301 struct reg_data *rw = &req->reg_writes[i];
1302 rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr));
1303 rw->value = cpu_to_le16(ioreqs[i].value);
1306 udev = zd_usb_to_usbdev(usb);
1307 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1308 req, req_len, &actual_req_len, 1000 /* ms */);
1310 dev_dbg_f(zd_usb_dev(usb),
1311 "error in usb_bulk_msg(). Error number %d\n", r);
1314 if (req_len != actual_req_len) {
1315 dev_dbg_f(zd_usb_dev(usb),
1316 "error in usb_bulk_msg()"
1317 " req_len %d != actual_req_len %d\n",
1318 req_len, actual_req_len);
1323 /* FALL-THROUGH with r == 0 */
1329 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1332 struct usb_device *udev;
1333 struct usb_req_rfwrite *req = NULL;
1334 int i, req_len, actual_req_len;
1335 u16 bit_value_template;
1338 dev_dbg_f(zd_usb_dev(usb),
1339 "error: io in atomic context not supported\n");
1340 return -EWOULDBLOCK;
1342 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1343 dev_dbg_f(zd_usb_dev(usb),
1344 "error: bits %d are smaller than"
1345 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1346 bits, USB_MIN_RFWRITE_BIT_COUNT);
1349 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1350 dev_dbg_f(zd_usb_dev(usb),
1351 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1352 bits, USB_MAX_RFWRITE_BIT_COUNT);
1356 if (value & (~0UL << bits)) {
1357 dev_dbg_f(zd_usb_dev(usb),
1358 "error: value %#09x has bits >= %d set\n",
1364 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1366 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1368 dev_dbg_f(zd_usb_dev(usb),
1369 "error %d: Couldn't read CR203\n", r);
1372 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1374 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1375 req = kmalloc(req_len, GFP_NOFS);
1379 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1380 /* 1: 3683a, but not used in ZYDAS driver */
1381 req->value = cpu_to_le16(2);
1382 req->bits = cpu_to_le16(bits);
1384 for (i = 0; i < bits; i++) {
1385 u16 bv = bit_value_template;
1386 if (value & (1 << (bits-1-i)))
1388 req->bit_values[i] = cpu_to_le16(bv);
1391 udev = zd_usb_to_usbdev(usb);
1392 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1393 req, req_len, &actual_req_len, 1000 /* ms */);
1395 dev_dbg_f(zd_usb_dev(usb),
1396 "error in usb_bulk_msg(). Error number %d\n", r);
1399 if (req_len != actual_req_len) {
1400 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1401 " req_len %d != actual_req_len %d\n",
1402 req_len, actual_req_len);
1407 /* FALL-THROUGH with r == 0 */