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 <linux/kernel.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 <linux/workqueue.h>
27 #include <net/ieee80211.h>
28 #include <asm/unaligned.h>
31 #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(0x0df6, 0x9075), .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 },
55 { USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },
56 { USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },
57 { USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },
59 { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
60 { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
61 { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
62 { USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },
63 { USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },
64 { USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },
65 { USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },
66 { USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },
67 { USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },
68 { USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },
69 { USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },
70 { USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },
71 { USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },
72 { USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },
73 { USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },
74 { USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },
75 { USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },
76 { USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },
77 { USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },
78 { USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },
79 { USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },
80 /* "Driverless" devices that need ejecting */
81 { USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },
82 { USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },
86 MODULE_LICENSE("GPL");
87 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
88 MODULE_AUTHOR("Ulrich Kunitz");
89 MODULE_AUTHOR("Daniel Drake");
90 MODULE_VERSION("1.0");
91 MODULE_DEVICE_TABLE(usb, usb_ids);
93 #define FW_ZD1211_PREFIX "zd1211/zd1211_"
94 #define FW_ZD1211B_PREFIX "zd1211/zd1211b_"
96 /* USB device initialization */
98 static int request_fw_file(
99 const struct firmware **fw, const char *name, struct device *device)
103 dev_dbg_f(device, "fw name %s\n", name);
105 r = request_firmware(fw, name, device);
108 "Could not load firmware file %s. Error number %d\n",
113 static inline u16 get_bcdDevice(const struct usb_device *udev)
115 return le16_to_cpu(udev->descriptor.bcdDevice);
118 enum upload_code_flags {
122 /* Ensures that MAX_TRANSFER_SIZE is even. */
123 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
125 static int upload_code(struct usb_device *udev,
126 const u8 *data, size_t size, u16 code_offset, int flags)
131 /* USB request blocks need "kmalloced" buffers.
133 p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
135 dev_err(&udev->dev, "out of memory\n");
142 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
143 size : MAX_TRANSFER_SIZE;
145 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
147 memcpy(p, data, transfer_size);
148 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
149 USB_REQ_FIRMWARE_DOWNLOAD,
150 USB_DIR_OUT | USB_TYPE_VENDOR,
151 code_offset, 0, p, transfer_size, 1000 /* ms */);
154 "USB control request for firmware upload"
155 " failed. Error number %d\n", r);
158 transfer_size = r & ~1;
160 size -= transfer_size;
161 data += transfer_size;
162 code_offset += transfer_size/sizeof(u16);
165 if (flags & REBOOT) {
168 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
169 USB_REQ_FIRMWARE_CONFIRM,
170 USB_DIR_IN | USB_TYPE_VENDOR,
171 0, 0, &ret, sizeof(ret), 5000 /* ms */);
172 if (r != sizeof(ret)) {
174 "control request firmeware confirmation failed."
175 " Return value %d\n", r);
182 "Internal error while downloading."
183 " Firmware confirm return value %#04x\n",
188 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
198 static u16 get_word(const void *data, u16 offset)
200 const __le16 *p = data;
201 return le16_to_cpu(p[offset]);
204 static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,
207 scnprintf(buffer, size, "%s%s",
209 FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
214 static int handle_version_mismatch(struct zd_usb *usb,
215 const struct firmware *ub_fw)
217 struct usb_device *udev = zd_usb_to_usbdev(usb);
218 const struct firmware *ur_fw = NULL;
223 r = request_fw_file(&ur_fw,
224 get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),
229 r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);
233 offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));
234 r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,
235 E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);
237 /* At this point, the vendor driver downloads the whole firmware
238 * image, hacks around with version IDs, and uploads it again,
239 * completely overwriting the boot code. We do not do this here as
240 * it is not required on any tested devices, and it is suspected to
243 release_firmware(ur_fw);
247 static int upload_firmware(struct zd_usb *usb)
252 struct usb_device *udev = zd_usb_to_usbdev(usb);
253 const struct firmware *ub_fw = NULL;
254 const struct firmware *uph_fw = NULL;
257 bcdDevice = get_bcdDevice(udev);
259 r = request_fw_file(&ub_fw,
260 get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),
265 fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);
267 if (fw_bcdDevice != bcdDevice) {
269 "firmware version %#06x and device bootcode version "
270 "%#06x differ\n", fw_bcdDevice, bcdDevice);
271 if (bcdDevice <= 0x4313)
272 dev_warn(&udev->dev, "device has old bootcode, please "
273 "report success or failure\n");
275 r = handle_version_mismatch(usb, ub_fw);
279 dev_dbg_f(&udev->dev,
280 "firmware device id %#06x is equal to the "
281 "actual device id\n", fw_bcdDevice);
285 r = request_fw_file(&uph_fw,
286 get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),
291 r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);
294 "Could not upload firmware code uph. Error number %d\n",
300 release_firmware(ub_fw);
301 release_firmware(uph_fw);
305 /* Read data from device address space using "firmware interface" which does
306 * not require firmware to be loaded. */
307 int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len)
310 struct usb_device *udev = zd_usb_to_usbdev(usb);
312 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
313 USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,
317 "read over firmware interface failed: %d\n", r);
319 } else if (r != len) {
321 "incomplete read over firmware interface: %d/%d\n",
329 #define urb_dev(urb) (&(urb)->dev->dev)
331 static inline void handle_regs_int(struct urb *urb)
333 struct zd_usb *usb = urb->context;
334 struct zd_usb_interrupt *intr = &usb->intr;
337 ZD_ASSERT(in_interrupt());
338 spin_lock(&intr->lock);
340 if (intr->read_regs_enabled) {
341 intr->read_regs.length = len = urb->actual_length;
343 if (len > sizeof(intr->read_regs.buffer))
344 len = sizeof(intr->read_regs.buffer);
345 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
346 intr->read_regs_enabled = 0;
347 complete(&intr->read_regs.completion);
351 dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
353 spin_unlock(&intr->lock);
356 static inline void handle_retry_failed_int(struct urb *urb)
358 struct zd_usb *usb = urb->context;
359 struct zd_mac *mac = zd_usb_to_mac(usb);
360 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
362 ieee->stats.tx_errors++;
363 ieee->ieee_stats.tx_retry_limit_exceeded++;
364 dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
368 static void int_urb_complete(struct urb *urb)
371 struct usb_int_header *hdr;
373 switch (urb->status) {
387 if (urb->actual_length < sizeof(hdr)) {
388 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
392 hdr = urb->transfer_buffer;
393 if (hdr->type != USB_INT_TYPE) {
394 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
399 case USB_INT_ID_REGS:
400 handle_regs_int(urb);
402 case USB_INT_ID_RETRY_FAILED:
403 handle_retry_failed_int(urb);
406 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
407 (unsigned int)hdr->id);
412 r = usb_submit_urb(urb, GFP_ATOMIC);
414 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
419 kfree(urb->transfer_buffer);
422 static inline int int_urb_interval(struct usb_device *udev)
424 switch (udev->speed) {
435 static inline int usb_int_enabled(struct zd_usb *usb)
438 struct zd_usb_interrupt *intr = &usb->intr;
441 spin_lock_irqsave(&intr->lock, flags);
443 spin_unlock_irqrestore(&intr->lock, flags);
447 int zd_usb_enable_int(struct zd_usb *usb)
450 struct usb_device *udev;
451 struct zd_usb_interrupt *intr = &usb->intr;
452 void *transfer_buffer = NULL;
455 dev_dbg_f(zd_usb_dev(usb), "\n");
457 urb = usb_alloc_urb(0, GFP_KERNEL);
463 ZD_ASSERT(!irqs_disabled());
464 spin_lock_irq(&intr->lock);
466 spin_unlock_irq(&intr->lock);
471 spin_unlock_irq(&intr->lock);
473 /* TODO: make it a DMA buffer */
475 transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_KERNEL);
476 if (!transfer_buffer) {
477 dev_dbg_f(zd_usb_dev(usb),
478 "couldn't allocate transfer_buffer\n");
479 goto error_set_urb_null;
482 udev = zd_usb_to_usbdev(usb);
483 usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
484 transfer_buffer, USB_MAX_EP_INT_BUFFER,
485 int_urb_complete, usb,
488 dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
489 r = usb_submit_urb(urb, GFP_KERNEL);
491 dev_dbg_f(zd_usb_dev(usb),
492 "Couldn't submit urb. Error number %d\n", r);
498 kfree(transfer_buffer);
500 spin_lock_irq(&intr->lock);
502 spin_unlock_irq(&intr->lock);
509 void zd_usb_disable_int(struct zd_usb *usb)
512 struct zd_usb_interrupt *intr = &usb->intr;
515 spin_lock_irqsave(&intr->lock, flags);
518 spin_unlock_irqrestore(&intr->lock, flags);
522 spin_unlock_irqrestore(&intr->lock, flags);
525 dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
529 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
533 struct zd_mac *mac = zd_usb_to_mac(usb);
534 const struct rx_length_info *length_info;
536 if (length < sizeof(struct rx_length_info)) {
537 /* It's not a complete packet anyhow. */
538 struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
539 ieee->stats.rx_errors++;
540 ieee->stats.rx_length_errors++;
543 length_info = (struct rx_length_info *)
544 (buffer + length - sizeof(struct rx_length_info));
546 /* It might be that three frames are merged into a single URB
547 * transaction. We have to check for the length info tag.
549 * While testing we discovered that length_info might be unaligned,
550 * because if USB transactions are merged, the last packet will not
551 * be padded. Unaligned access might also happen if the length_info
552 * structure is not present.
554 if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
556 unsigned int l, k, n;
557 for (i = 0, l = 0;; i++) {
558 k = le16_to_cpu(get_unaligned(&length_info->length[i]));
564 zd_mac_rx_irq(mac, buffer+l, k);
570 zd_mac_rx_irq(mac, buffer, length);
574 static void rx_urb_complete(struct urb *urb)
577 struct zd_usb_rx *rx;
581 switch (urb->status) {
592 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
596 buffer = urb->transfer_buffer;
597 length = urb->actual_length;
601 if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
602 /* If there is an old first fragment, we don't care. */
603 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
604 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
605 spin_lock(&rx->lock);
606 memcpy(rx->fragment, buffer, length);
607 rx->fragment_length = length;
608 spin_unlock(&rx->lock);
612 spin_lock(&rx->lock);
613 if (rx->fragment_length > 0) {
614 /* We are on a second fragment, we believe */
615 ZD_ASSERT(length + rx->fragment_length <=
616 ARRAY_SIZE(rx->fragment));
617 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
618 memcpy(rx->fragment+rx->fragment_length, buffer, length);
619 handle_rx_packet(usb, rx->fragment,
620 rx->fragment_length + length);
621 rx->fragment_length = 0;
622 spin_unlock(&rx->lock);
624 spin_unlock(&rx->lock);
625 handle_rx_packet(usb, buffer, length);
629 usb_submit_urb(urb, GFP_ATOMIC);
632 static struct urb *alloc_urb(struct zd_usb *usb)
634 struct usb_device *udev = zd_usb_to_usbdev(usb);
638 urb = usb_alloc_urb(0, GFP_KERNEL);
641 buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_KERNEL,
648 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
649 buffer, USB_MAX_RX_SIZE,
650 rx_urb_complete, usb);
651 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
656 static void free_urb(struct urb *urb)
660 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
661 urb->transfer_buffer, urb->transfer_dma);
665 int zd_usb_enable_rx(struct zd_usb *usb)
668 struct zd_usb_rx *rx = &usb->rx;
671 dev_dbg_f(zd_usb_dev(usb), "\n");
674 urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_KERNEL);
677 for (i = 0; i < URBS_COUNT; i++) {
678 urbs[i] = alloc_urb(usb);
683 ZD_ASSERT(!irqs_disabled());
684 spin_lock_irq(&rx->lock);
686 spin_unlock_irq(&rx->lock);
691 rx->urbs_count = URBS_COUNT;
692 spin_unlock_irq(&rx->lock);
694 for (i = 0; i < URBS_COUNT; i++) {
695 r = usb_submit_urb(urbs[i], GFP_KERNEL);
702 for (i = 0; i < URBS_COUNT; i++) {
703 usb_kill_urb(urbs[i]);
705 spin_lock_irq(&rx->lock);
708 spin_unlock_irq(&rx->lock);
711 for (i = 0; i < URBS_COUNT; i++)
717 void zd_usb_disable_rx(struct zd_usb *usb)
723 struct zd_usb_rx *rx = &usb->rx;
725 spin_lock_irqsave(&rx->lock, flags);
727 count = rx->urbs_count;
728 spin_unlock_irqrestore(&rx->lock, flags);
732 for (i = 0; i < count; i++) {
733 usb_kill_urb(urbs[i]);
738 spin_lock_irqsave(&rx->lock, flags);
741 spin_unlock_irqrestore(&rx->lock, flags);
744 static void tx_urb_complete(struct urb *urb)
748 switch (urb->status) {
757 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
760 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
764 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
765 urb->transfer_buffer, urb->transfer_dma);
769 r = usb_submit_urb(urb, GFP_ATOMIC);
771 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
776 /* Puts the frame on the USB endpoint. It doesn't wait for
777 * completion. The frame must contain the control set.
779 int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
782 struct usb_device *udev = zd_usb_to_usbdev(usb);
786 urb = usb_alloc_urb(0, GFP_ATOMIC);
792 buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
798 memcpy(buffer, frame, length);
800 usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
801 buffer, length, tx_urb_complete, NULL);
802 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
804 r = usb_submit_urb(urb, GFP_ATOMIC);
809 usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
817 static inline void init_usb_interrupt(struct zd_usb *usb)
819 struct zd_usb_interrupt *intr = &usb->intr;
821 spin_lock_init(&intr->lock);
822 intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
823 init_completion(&intr->read_regs.completion);
824 intr->read_regs.cr_int_addr = cpu_to_le16((u16)CR_INTERRUPT);
827 static inline void init_usb_rx(struct zd_usb *usb)
829 struct zd_usb_rx *rx = &usb->rx;
830 spin_lock_init(&rx->lock);
831 if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
832 rx->usb_packet_size = 512;
834 rx->usb_packet_size = 64;
836 ZD_ASSERT(rx->fragment_length == 0);
839 static inline void init_usb_tx(struct zd_usb *usb)
841 /* FIXME: at this point we will allocate a fixed number of urb's for
842 * use in a cyclic scheme */
845 void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
846 struct usb_interface *intf)
848 memset(usb, 0, sizeof(*usb));
849 usb->intf = usb_get_intf(intf);
850 usb_set_intfdata(usb->intf, netdev);
851 init_usb_interrupt(usb);
856 void zd_usb_clear(struct zd_usb *usb)
858 usb_set_intfdata(usb->intf, NULL);
859 usb_put_intf(usb->intf);
860 ZD_MEMCLEAR(usb, sizeof(*usb));
861 /* FIXME: usb_interrupt, usb_tx, usb_rx? */
864 static const char *speed(enum usb_device_speed speed)
874 return "unknown speed";
878 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
880 return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
881 le16_to_cpu(udev->descriptor.idVendor),
882 le16_to_cpu(udev->descriptor.idProduct),
887 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
889 struct usb_device *udev = interface_to_usbdev(usb->intf);
890 return scnprint_id(udev, buffer, size);
894 static void print_id(struct usb_device *udev)
898 scnprint_id(udev, buffer, sizeof(buffer));
899 buffer[sizeof(buffer)-1] = 0;
900 dev_dbg_f(&udev->dev, "%s\n", buffer);
903 #define print_id(udev) do { } while (0)
906 static int eject_installer(struct usb_interface *intf)
908 struct usb_device *udev = interface_to_usbdev(intf);
909 struct usb_host_interface *iface_desc = &intf->altsetting[0];
910 struct usb_endpoint_descriptor *endpoint;
915 /* Find bulk out endpoint */
916 endpoint = &iface_desc->endpoint[1].desc;
917 if ((endpoint->bEndpointAddress & USB_TYPE_MASK) == USB_DIR_OUT &&
918 (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
919 USB_ENDPOINT_XFER_BULK) {
920 bulk_out_ep = endpoint->bEndpointAddress;
923 "zd1211rw: Could not find bulk out endpoint\n");
927 cmd = kzalloc(31, GFP_KERNEL);
931 /* USB bulk command block */
932 cmd[0] = 0x55; /* bulk command signature */
933 cmd[1] = 0x53; /* bulk command signature */
934 cmd[2] = 0x42; /* bulk command signature */
935 cmd[3] = 0x43; /* bulk command signature */
936 cmd[14] = 6; /* command length */
938 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
939 cmd[19] = 0x2; /* eject disc */
941 dev_info(&udev->dev, "Ejecting virtual installer media...\n");
942 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
943 cmd, 31, NULL, 2000);
948 /* At this point, the device disconnects and reconnects with the real
951 usb_set_intfdata(intf, NULL);
955 int zd_usb_init_hw(struct zd_usb *usb)
958 struct zd_mac *mac = zd_usb_to_mac(usb);
960 dev_dbg_f(zd_usb_dev(usb), "\n");
962 r = upload_firmware(usb);
964 dev_err(zd_usb_dev(usb),
965 "couldn't load firmware. Error number %d\n", r);
969 r = usb_reset_configuration(zd_usb_to_usbdev(usb));
971 dev_dbg_f(zd_usb_dev(usb),
972 "couldn't reset configuration. Error number %d\n", r);
976 r = zd_mac_init_hw(mac);
978 dev_dbg_f(zd_usb_dev(usb),
979 "couldn't initialize mac. Error number %d\n", r);
983 usb->initialized = 1;
987 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
991 struct usb_device *udev = interface_to_usbdev(intf);
992 struct net_device *netdev = NULL;
996 if (id->driver_info & DEVICE_INSTALLER)
997 return eject_installer(intf);
999 switch (udev->speed) {
1001 case USB_SPEED_FULL:
1002 case USB_SPEED_HIGH:
1005 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
1010 usb_reset_device(interface_to_usbdev(intf));
1012 netdev = zd_netdev_alloc(intf);
1013 if (netdev == NULL) {
1018 usb = &zd_netdev_mac(netdev)->chip.usb;
1019 usb->is_zd1211b = (id->driver_info == DEVICE_ZD1211B) != 0;
1021 r = zd_mac_preinit_hw(zd_netdev_mac(netdev));
1023 dev_dbg_f(&intf->dev,
1024 "couldn't initialize mac. Error number %d\n", r);
1028 r = register_netdev(netdev);
1030 dev_dbg_f(&intf->dev,
1031 "couldn't register netdev. Error number %d\n", r);
1035 dev_dbg_f(&intf->dev, "successful\n");
1036 dev_info(&intf->dev,"%s\n", netdev->name);
1039 usb_reset_device(interface_to_usbdev(intf));
1040 zd_netdev_free(netdev);
1044 static void disconnect(struct usb_interface *intf)
1046 struct net_device *netdev = zd_intf_to_netdev(intf);
1050 /* Either something really bad happened, or we're just dealing with
1051 * a DEVICE_INSTALLER. */
1055 mac = zd_netdev_mac(netdev);
1056 usb = &mac->chip.usb;
1058 dev_dbg_f(zd_usb_dev(usb), "\n");
1060 zd_netdev_disconnect(netdev);
1062 /* Just in case something has gone wrong! */
1063 zd_usb_disable_rx(usb);
1064 zd_usb_disable_int(usb);
1066 /* If the disconnect has been caused by a removal of the
1067 * driver module, the reset allows reloading of the driver. If the
1068 * reset will not be executed here, the upload of the firmware in the
1069 * probe function caused by the reloading of the driver will fail.
1071 usb_reset_device(interface_to_usbdev(intf));
1073 zd_netdev_free(netdev);
1074 dev_dbg(&intf->dev, "disconnected\n");
1077 static struct usb_driver driver = {
1079 .id_table = usb_ids,
1081 .disconnect = disconnect,
1084 struct workqueue_struct *zd_workqueue;
1086 static int __init usb_init(void)
1090 pr_debug("%s usb_init()\n", driver.name);
1092 zd_workqueue = create_singlethread_workqueue(driver.name);
1093 if (zd_workqueue == NULL) {
1094 printk(KERN_ERR "%s couldn't create workqueue\n", driver.name);
1098 r = usb_register(&driver);
1100 destroy_workqueue(zd_workqueue);
1101 printk(KERN_ERR "%s usb_register() failed. Error number %d\n",
1106 pr_debug("%s initialized\n", driver.name);
1110 static void __exit usb_exit(void)
1112 pr_debug("%s usb_exit()\n", driver.name);
1113 usb_deregister(&driver);
1114 destroy_workqueue(zd_workqueue);
1117 module_init(usb_init);
1118 module_exit(usb_exit);
1120 static int usb_int_regs_length(unsigned int count)
1122 return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1125 static void prepare_read_regs_int(struct zd_usb *usb)
1127 struct zd_usb_interrupt *intr = &usb->intr;
1129 spin_lock_irq(&intr->lock);
1130 intr->read_regs_enabled = 1;
1131 INIT_COMPLETION(intr->read_regs.completion);
1132 spin_unlock_irq(&intr->lock);
1135 static void disable_read_regs_int(struct zd_usb *usb)
1137 struct zd_usb_interrupt *intr = &usb->intr;
1139 spin_lock_irq(&intr->lock);
1140 intr->read_regs_enabled = 0;
1141 spin_unlock_irq(&intr->lock);
1144 static int get_results(struct zd_usb *usb, u16 *values,
1145 struct usb_req_read_regs *req, unsigned int count)
1149 struct zd_usb_interrupt *intr = &usb->intr;
1150 struct read_regs_int *rr = &intr->read_regs;
1151 struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1153 spin_lock_irq(&intr->lock);
1156 /* The created block size seems to be larger than expected.
1157 * However results appear to be correct.
1159 if (rr->length < usb_int_regs_length(count)) {
1160 dev_dbg_f(zd_usb_dev(usb),
1161 "error: actual length %d less than expected %d\n",
1162 rr->length, usb_int_regs_length(count));
1165 if (rr->length > sizeof(rr->buffer)) {
1166 dev_dbg_f(zd_usb_dev(usb),
1167 "error: actual length %d exceeds buffer size %zu\n",
1168 rr->length, sizeof(rr->buffer));
1172 for (i = 0; i < count; i++) {
1173 struct reg_data *rd = ®s->regs[i];
1174 if (rd->addr != req->addr[i]) {
1175 dev_dbg_f(zd_usb_dev(usb),
1176 "rd[%d] addr %#06hx expected %#06hx\n", i,
1177 le16_to_cpu(rd->addr),
1178 le16_to_cpu(req->addr[i]));
1181 values[i] = le16_to_cpu(rd->value);
1186 spin_unlock_irq(&intr->lock);
1190 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1191 const zd_addr_t *addresses, unsigned int count)
1194 int i, req_len, actual_req_len;
1195 struct usb_device *udev;
1196 struct usb_req_read_regs *req = NULL;
1197 unsigned long timeout;
1200 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1203 if (count > USB_MAX_IOREAD16_COUNT) {
1204 dev_dbg_f(zd_usb_dev(usb),
1205 "error: count %u exceeds possible max %u\n",
1206 count, USB_MAX_IOREAD16_COUNT);
1210 dev_dbg_f(zd_usb_dev(usb),
1211 "error: io in atomic context not supported\n");
1212 return -EWOULDBLOCK;
1214 if (!usb_int_enabled(usb)) {
1215 dev_dbg_f(zd_usb_dev(usb),
1216 "error: usb interrupt not enabled\n");
1217 return -EWOULDBLOCK;
1220 req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1221 req = kmalloc(req_len, GFP_KERNEL);
1224 req->id = cpu_to_le16(USB_REQ_READ_REGS);
1225 for (i = 0; i < count; i++)
1226 req->addr[i] = cpu_to_le16((u16)addresses[i]);
1228 udev = zd_usb_to_usbdev(usb);
1229 prepare_read_regs_int(usb);
1230 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1231 req, req_len, &actual_req_len, 1000 /* ms */);
1233 dev_dbg_f(zd_usb_dev(usb),
1234 "error in usb_bulk_msg(). Error number %d\n", r);
1237 if (req_len != actual_req_len) {
1238 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1239 " req_len %d != actual_req_len %d\n",
1240 req_len, actual_req_len);
1245 timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1246 msecs_to_jiffies(1000));
1248 disable_read_regs_int(usb);
1249 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1254 r = get_results(usb, values, req, count);
1260 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1264 struct usb_device *udev;
1265 struct usb_req_write_regs *req = NULL;
1266 int i, req_len, actual_req_len;
1270 if (count > USB_MAX_IOWRITE16_COUNT) {
1271 dev_dbg_f(zd_usb_dev(usb),
1272 "error: count %u exceeds possible max %u\n",
1273 count, USB_MAX_IOWRITE16_COUNT);
1277 dev_dbg_f(zd_usb_dev(usb),
1278 "error: io in atomic context not supported\n");
1279 return -EWOULDBLOCK;
1282 req_len = sizeof(struct usb_req_write_regs) +
1283 count * sizeof(struct reg_data);
1284 req = kmalloc(req_len, GFP_KERNEL);
1288 req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1289 for (i = 0; i < count; i++) {
1290 struct reg_data *rw = &req->reg_writes[i];
1291 rw->addr = cpu_to_le16((u16)ioreqs[i].addr);
1292 rw->value = cpu_to_le16(ioreqs[i].value);
1295 udev = zd_usb_to_usbdev(usb);
1296 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1297 req, req_len, &actual_req_len, 1000 /* ms */);
1299 dev_dbg_f(zd_usb_dev(usb),
1300 "error in usb_bulk_msg(). Error number %d\n", r);
1303 if (req_len != actual_req_len) {
1304 dev_dbg_f(zd_usb_dev(usb),
1305 "error in usb_bulk_msg()"
1306 " req_len %d != actual_req_len %d\n",
1307 req_len, actual_req_len);
1312 /* FALL-THROUGH with r == 0 */
1318 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1321 struct usb_device *udev;
1322 struct usb_req_rfwrite *req = NULL;
1323 int i, req_len, actual_req_len;
1324 u16 bit_value_template;
1327 dev_dbg_f(zd_usb_dev(usb),
1328 "error: io in atomic context not supported\n");
1329 return -EWOULDBLOCK;
1331 if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1332 dev_dbg_f(zd_usb_dev(usb),
1333 "error: bits %d are smaller than"
1334 " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1335 bits, USB_MIN_RFWRITE_BIT_COUNT);
1338 if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1339 dev_dbg_f(zd_usb_dev(usb),
1340 "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1341 bits, USB_MAX_RFWRITE_BIT_COUNT);
1345 if (value & (~0UL << bits)) {
1346 dev_dbg_f(zd_usb_dev(usb),
1347 "error: value %#09x has bits >= %d set\n",
1353 dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1355 r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1357 dev_dbg_f(zd_usb_dev(usb),
1358 "error %d: Couldn't read CR203\n", r);
1361 bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1363 req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1364 req = kmalloc(req_len, GFP_KERNEL);
1368 req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1369 /* 1: 3683a, but not used in ZYDAS driver */
1370 req->value = cpu_to_le16(2);
1371 req->bits = cpu_to_le16(bits);
1373 for (i = 0; i < bits; i++) {
1374 u16 bv = bit_value_template;
1375 if (value & (1 << (bits-1-i)))
1377 req->bit_values[i] = cpu_to_le16(bv);
1380 udev = zd_usb_to_usbdev(usb);
1381 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1382 req, req_len, &actual_req_len, 1000 /* ms */);
1384 dev_dbg_f(zd_usb_dev(usb),
1385 "error in usb_bulk_msg(). Error number %d\n", r);
1388 if (req_len != actual_req_len) {
1389 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1390 " req_len %d != actual_req_len %d\n",
1391 req_len, actual_req_len);
1396 /* FALL-THROUGH with r == 0 */