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