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