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