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