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 <linux/workqueue.h>
28 #include <net/ieee80211.h>
31 #include "zd_netdev.h"
36 static struct usb_device_id usb_ids[] = {
38 { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
39 { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
40 { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
41 { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
42 { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
43 { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
44 { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
45 { USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },
46 { USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },
47 { USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },
48 { USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },
49 { USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },
50 { USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },
51 { USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },
52 { USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },
53 { USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },
54 { USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
57 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
58 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
59 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
60 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
61 /* "Driverless" devices that need ejecting */
62 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
66 MODULE_LICENSE("GPL");
67 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
68 MODULE_AUTHOR("Ulrich Kunitz");
69 MODULE_AUTHOR("Daniel Drake");
70 MODULE_VERSION("1.0");
71 MODULE_DEVICE_TABLE(usb, usb_ids);
73 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
74 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
76 /* register address handling */
79 static int check_addr(struct zd_usb *usb, zd_addr_t addr)
81 u32 base = ZD_ADDR_BASE(addr);
82 u32 offset = ZD_OFFSET(addr);
84 if ((u32)addr & ADDR_ZERO_MASK)
90 if (offset > CR_MAX_OFFSET) {
91 dev_dbg(zd_usb_dev(usb),
92 "CR offset %#010x larger than"
93 " CR_MAX_OFFSET %#10x\n",
94 offset, CR_MAX_OFFSET);
98 dev_dbg(zd_usb_dev(usb),
99 "CR offset %#010x is not a multiple of 2\n",
101 goto invalid_address;
105 if (offset > E2P_MAX_OFFSET) {
106 dev_dbg(zd_usb_dev(usb),
107 "E2P offset %#010x larger than"
108 " E2P_MAX_OFFSET %#010x\n",
109 offset, E2P_MAX_OFFSET);
110 goto invalid_address;
114 if (!usb->fw_base_offset) {
115 dev_dbg(zd_usb_dev(usb),
116 "ERROR: fw base offset has not been set\n");
119 if (offset > FW_MAX_OFFSET) {
120 dev_dbg(zd_usb_dev(usb),
121 "FW offset %#10x is larger than"
122 " FW_MAX_OFFSET %#010x\n",
123 offset, FW_MAX_OFFSET);
124 goto invalid_address;
128 dev_dbg(zd_usb_dev(usb),
129 "address has unsupported base %#010x\n", addr);
130 goto invalid_address;
135 dev_dbg(zd_usb_dev(usb),
136 "ERROR: invalid address: %#010x\n", addr);
141 static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr)
146 base = ZD_ADDR_BASE(addr);
147 offset = ZD_OFFSET(addr);
149 ZD_ASSERT(check_addr(usb, addr) == 0);
153 offset += CR_BASE_OFFSET;
156 offset += E2P_BASE_OFFSET;
159 offset += usb->fw_base_offset;
166 /* USB device initialization */
168 static int request_fw_file(
169 const struct firmware **fw, const char *name, struct device *device)
173 dev_dbg_f(device, "fw name %s\n", name);
175 r = request_firmware(fw, name, device);
178 "Could not load firmware file %s. Error number %d\n",
183 static inline u16 get_bcdDevice(const struct usb_device *udev)
185 return le16_to_cpu(udev->descriptor.bcdDevice);
188 enum upload_code_flags {
192 /* Ensures that MAX_TRANSFER_SIZE is even. */
193 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
195 static int upload_code(struct usb_device *udev,
196 const u8 *data, size_t size, u16 code_offset, int flags)
201 /* USB request blocks need "kmalloced" buffers.
203 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
205 dev_err(&udev->dev, "out of memory\n");
212 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
213 size : MAX_TRANSFER_SIZE;
215 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
217 memcpy(p, data, transfer_size);
218 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
219 USB_REQ_FIRMWARE_DOWNLOAD,
220 USB_DIR_OUT | USB_TYPE_VENDOR,
221 code_offset, 0, p, transfer_size, 1000 /* ms */);
224 "USB control request for firmware upload"
225 " failed. Error number %d\n", r);
228 transfer_size = r & ~1;
230 size -= transfer_size;
231 data += transfer_size;
232 code_offset += transfer_size/sizeof(u16);
235 if (flags & REBOOT) {
238 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
239 USB_REQ_FIRMWARE_CONFIRM,
240 USB_DIR_IN | USB_TYPE_VENDOR,
241 0, 0, &ret, sizeof(ret), 5000 /* ms */);
242 if (r != sizeof(ret)) {
244 "control request firmeware confirmation failed."
245 " Return value %d\n", r);
252 "Internal error while downloading."
253 " Firmware confirm return value %#04x\n",
258 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
268 static u16 get_word(const void *data, u16 offset)
270 const __le16 *p = data;
271 return le16_to_cpu(p[offset]);
274 static char *get_fw_name(char *buffer, size_t size, u8 device_type,
277 scnprintf(buffer, size, "%s%s",
278 device_type == DEVICE_ZD1211B ?
279 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
284 static int handle_version_mismatch(struct usb_device *udev, u8 device_type,
285 const struct firmware *ub_fw)
287 const struct firmware *ur_fw = NULL;
292 r = request_fw_file(&ur_fw,
293 get_fw_name(fw_name, sizeof(fw_name), device_type, "ur"),
298 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START_OFFSET,
303 offset = ((EEPROM_REGS_OFFSET + EEPROM_REGS_SIZE) * sizeof(u16));
304 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
305 E2P_BASE_OFFSET + EEPROM_REGS_SIZE, REBOOT);
307 /* At this point, the vendor driver downloads the whole firmware
308 * image, hacks around with version IDs, and uploads it again,
309 * completely overwriting the boot code. We do not do this here as
310 * it is not required on any tested devices, and it is suspected to
313 release_firmware(ur_fw);
317 static int upload_firmware(struct usb_device *udev, u8 device_type)
322 const struct firmware *ub_fw = NULL;
323 const struct firmware *uph_fw = NULL;
326 bcdDevice = get_bcdDevice(udev);
328 r = request_fw_file(&ub_fw,
329 get_fw_name(fw_name, sizeof(fw_name), device_type, "ub"),
334 fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET);
336 if (fw_bcdDevice != bcdDevice) {
338 "firmware version %#06x and device bootcode version "
339 "%#06x differ\n", fw_bcdDevice, bcdDevice);
340 if (bcdDevice <= 0x4313)
341 dev_warn(&udev->dev, "device has old bootcode, please "
342 "report success or failure\n");
344 r = handle_version_mismatch(udev, device_type, ub_fw);
348 dev_dbg_f(&udev->dev,
349 "firmware device id %#06x is equal to the "
350 "actual device id\n", fw_bcdDevice);
354 r = request_fw_file(&uph_fw,
355 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
360 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET,
364 "Could not upload firmware code uph. Error number %d\n",
370 release_firmware(ub_fw);
371 release_firmware(uph_fw);
375 #define urb_dev(urb) (&(urb)->dev->dev)
377 static inline void handle_regs_int(struct urb *urb)
379 struct zd_usb *usb = urb->context;
380 struct zd_usb_interrupt *intr = &usb->intr;
383 ZD_ASSERT(in_interrupt());
384 spin_lock(&intr->lock);
386 if (intr->read_regs_enabled) {
387 intr->read_regs.length = len = urb->actual_length;
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);
397 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
399 spin_unlock(&intr->lock);
402 static inline void handle_retry_failed_int(struct urb *urb)
404 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
408 static void int_urb_complete(struct urb *urb)
411 struct usb_int_header *hdr;
413 switch (urb->status) {
427 if (urb->actual_length < sizeof(hdr)) {
428 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
432 hdr = urb->transfer_buffer;
433 if (hdr->type != USB_INT_TYPE) {
434 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
439 case USB_INT_ID_REGS:
440 handle_regs_int(urb);
442 case USB_INT_ID_RETRY_FAILED:
443 handle_retry_failed_int(urb);
446 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
447 (unsigned int)hdr->id);
452 r = usb_submit_urb(urb, GFP_ATOMIC);
454 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
459 kfree(urb->transfer_buffer);
462 static inline int int_urb_interval(struct usb_device *udev)
464 switch (udev->speed) {
475 static inline int usb_int_enabled(struct zd_usb *usb)
478 struct zd_usb_interrupt *intr = &usb->intr;
481 spin_lock_irqsave(&intr->lock, flags);
483 spin_unlock_irqrestore(&intr->lock, flags);
487 int zd_usb_enable_int(struct zd_usb *usb)
490 struct usb_device *udev;
491 struct zd_usb_interrupt *intr = &usb->intr;
492 void *transfer_buffer = NULL;
495 dev_dbg_f(zd_usb_dev(usb), "\n");
497 urb = usb_alloc_urb(0, GFP_NOFS);
503 ZD_ASSERT(!irqs_disabled());
504 spin_lock_irq(&intr->lock);
506 spin_unlock_irq(&intr->lock);
511 spin_unlock_irq(&intr->lock);
513 /* TODO: make it a DMA buffer */
515 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
516 if (!transfer_buffer) {
517 dev_dbg_f(zd_usb_dev(usb),
518 "couldn't allocate transfer_buffer\n");
519 goto error_set_urb_null;
522 udev = zd_usb_to_usbdev(usb);
523 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
524 transfer_buffer, USB_MAX_EP_INT_BUFFER,
525 int_urb_complete, usb,
528 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
529 r = usb_submit_urb(urb, GFP_NOFS);
531 dev_dbg_f(zd_usb_dev(usb),
532 "Couldn't submit urb. Error number %d\n", r);
538 kfree(transfer_buffer);
540 spin_lock_irq(&intr->lock);
542 spin_unlock_irq(&intr->lock);
549 void zd_usb_disable_int(struct zd_usb *usb)
552 struct zd_usb_interrupt *intr = &usb->intr;
555 spin_lock_irqsave(&intr->lock, flags);
558 spin_unlock_irqrestore(&intr->lock, flags);
562 spin_unlock_irqrestore(&intr->lock, flags);
565 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
569 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
573 struct zd_mac *mac = zd_usb_to_mac(usb);
574 const struct rx_length_info *length_info;
576 if (length < sizeof(struct rx_length_info)) {
577 /* It's not a complete packet anyhow. */
580 length_info = (struct rx_length_info *)
581 (buffer + length - sizeof(struct rx_length_info));
583 /* It might be that three frames are merged into a single URB
584 * transaction. We have to check for the length info tag.
586 * While testing we discovered that length_info might be unaligned,
587 * because if USB transactions are merged, the last packet will not
588 * be padded. Unaligned access might also happen if the length_info
589 * structure is not present.
591 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
593 unsigned int l, k, n;
594 for (i = 0, l = 0;; i++) {
595 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
601 zd_mac_rx_irq(mac, buffer+l, k);
607 zd_mac_rx_irq(mac, buffer, length);
611 static void rx_urb_complete(struct urb *urb)
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)
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 struct workqueue_struct *zd_workqueue;
1117 static int __init usb_init(void)
1121 pr_debug("%s usb_init()\n", driver.name);
1123 zd_workqueue = create_singlethread_workqueue(driver.name);
1124 if (zd_workqueue == NULL) {
1125 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1129 r = usb_register(&driver);
1131 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1136 pr_debug("%s initialized\n", driver.name);
1140 static void __exit usb_exit(void)
1142 pr_debug("%s usb_exit()\n", driver.name);
1143 usb_deregister(&driver);
1144 destroy_workqueue(zd_workqueue);
1147 module_init(usb_init);
1148 module_exit(usb_exit);
1150 static int usb_int_regs_length(unsigned int count)
1152 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1155 static void prepare_read_regs_int(struct zd_usb *usb)
1157 struct zd_usb_interrupt *intr = &usb->intr;
1159 spin_lock_irq(&intr->lock);
1160 intr->read_regs_enabled = 1;
1161 INIT_COMPLETION(intr->read_regs.completion);
1162 spin_unlock_irq(&intr->lock);
1165 static void disable_read_regs_int(struct zd_usb *usb)
1167 struct zd_usb_interrupt *intr = &usb->intr;
1169 spin_lock_irq(&intr->lock);
1170 intr->read_regs_enabled = 0;
1171 spin_unlock_irq(&intr->lock);
1174 static int get_results(struct zd_usb *usb, u16 *values,
1175 struct usb_req_read_regs *req, unsigned int count)
1179 struct zd_usb_interrupt *intr = &usb->intr;
1180 struct read_regs_int *rr = &intr->read_regs;
1181 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1183 spin_lock_irq(&intr->lock);
1186 /* The created block size seems to be larger than expected.
1187 * However results appear to be correct.
1189 if (rr->length < usb_int_regs_length(count)) {
1190 dev_dbg_f(zd_usb_dev(usb),
1191 "error: actual length %d less than expected %d\n",
1192 rr->length, usb_int_regs_length(count));
1195 if (rr->length > sizeof(rr->buffer)) {
1196 dev_dbg_f(zd_usb_dev(usb),
1197 "error: actual length %d exceeds buffer size %zu\n",
1198 rr->length, sizeof(rr->buffer));
1202 for (i = 0; i < count; i++) {
1203 struct reg_data *rd = ®s->regs[i];
1204 if (rd->addr != req->addr[i]) {
1205 dev_dbg_f(zd_usb_dev(usb),
1206 "rd[%d] addr %#06hx expected %#06hx\n", i,
1207 le16_to_cpu(rd->addr),
1208 le16_to_cpu(req->addr[i]));
1211 values[i] = le16_to_cpu(rd->value);
1216 spin_unlock_irq(&intr->lock);
1220 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1221 const zd_addr_t *addresses, unsigned int count)
1224 int i, req_len, actual_req_len;
1225 struct usb_device *udev;
1226 struct usb_req_read_regs *req = NULL;
1227 unsigned long timeout;
1230 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1233 if (count > USB_MAX_IOREAD16_COUNT) {
1234 dev_dbg_f(zd_usb_dev(usb),
1235 "error: count %u exceeds possible max %u\n",
1236 count, USB_MAX_IOREAD16_COUNT);
1240 dev_dbg_f(zd_usb_dev(usb),
1241 "error: io in atomic context not supported\n");
1242 return -EWOULDBLOCK;
1244 if (!usb_int_enabled(usb)) {
1245 dev_dbg_f(zd_usb_dev(usb),
1246 "error: usb interrupt not enabled\n");
1247 return -EWOULDBLOCK;
1250 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1251 req = kmalloc(req_len, GFP_NOFS);
1254 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1255 for (i = 0; i < count; i++)
1256 req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i]));
1258 udev = zd_usb_to_usbdev(usb);
1259 prepare_read_regs_int(usb);
1260 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1261 req, req_len, &actual_req_len, 1000 /* ms */);
1263 dev_dbg_f(zd_usb_dev(usb),
1264 "error in usb_bulk_msg(). Error number %d\n", r);
1267 if (req_len != actual_req_len) {
1268 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1269 " req_len %d != actual_req_len %d\n",
1270 req_len, actual_req_len);
1275 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1276 msecs_to_jiffies(1000));
1278 disable_read_regs_int(usb);
1279 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1284 r = get_results(usb, values, req, count);
1290 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1294 struct usb_device *udev;
1295 struct usb_req_write_regs *req = NULL;
1296 int i, req_len, actual_req_len;
1300 if (count > USB_MAX_IOWRITE16_COUNT) {
1301 dev_dbg_f(zd_usb_dev(usb),
1302 "error: count %u exceeds possible max %u\n",
1303 count, USB_MAX_IOWRITE16_COUNT);
1307 dev_dbg_f(zd_usb_dev(usb),
1308 "error: io in atomic context not supported\n");
1309 return -EWOULDBLOCK;
1312 req_len = sizeof(struct usb_req_write_regs) +
1313 count * sizeof(struct reg_data);
1314 req = kmalloc(req_len, GFP_NOFS);
1318 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1319 for (i = 0; i < count; i++) {
1320 struct reg_data *rw = &req->reg_writes[i];
1321 rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr));
1322 rw->value = cpu_to_le16(ioreqs[i].value);
1325 udev = zd_usb_to_usbdev(usb);
1326 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1327 req, req_len, &actual_req_len, 1000 /* ms */);
1329 dev_dbg_f(zd_usb_dev(usb),
1330 "error in usb_bulk_msg(). Error number %d\n", r);
1333 if (req_len != actual_req_len) {
1334 dev_dbg_f(zd_usb_dev(usb),
1335 "error in usb_bulk_msg()"
1336 " req_len %d != actual_req_len %d\n",
1337 req_len, actual_req_len);
1342 /* FALL-THROUGH with r == 0 */
1348 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1351 struct usb_device *udev;
1352 struct usb_req_rfwrite *req = NULL;
1353 int i, req_len, actual_req_len;
1354 u16 bit_value_template;
1357 dev_dbg_f(zd_usb_dev(usb),
1358 "error: io in atomic context not supported\n");
1359 return -EWOULDBLOCK;
1361 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1362 dev_dbg_f(zd_usb_dev(usb),
1363 "error: bits %d are smaller than"
1364 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1365 bits, USB_MIN_RFWRITE_BIT_COUNT);
1368 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1369 dev_dbg_f(zd_usb_dev(usb),
1370 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1371 bits, USB_MAX_RFWRITE_BIT_COUNT);
1375 if (value & (~0UL << bits)) {
1376 dev_dbg_f(zd_usb_dev(usb),
1377 "error: value %#09x has bits >= %d set\n",
1383 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1385 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1387 dev_dbg_f(zd_usb_dev(usb),
1388 "error %d: Couldn't read CR203\n", r);
1391 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1393 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1394 req = kmalloc(req_len, GFP_NOFS);
1398 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1399 /* 1: 3683a, but not used in ZYDAS driver */
1400 req->value = cpu_to_le16(2);
1401 req->bits = cpu_to_le16(bits);
1403 for (i = 0; i < bits; i++) {
1404 u16 bv = bit_value_template;
1405 if (value & (1 << (bits-1-i)))
1407 req->bit_values[i] = cpu_to_le16(bv);
1410 udev = zd_usb_to_usbdev(usb);
1411 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1412 req, req_len, &actual_req_len, 1000 /* ms */);
1414 dev_dbg_f(zd_usb_dev(usb),
1415 "error in usb_bulk_msg(). Error number %d\n", r);
1418 if (req_len != actual_req_len) {
1419 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1420 " req_len %d != actual_req_len %d\n",
1421 req_len, actual_req_len);
1426 /* FALL-THROUGH with r == 0 */